db_object.class.inc.php 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. <?php
  2. /**
  3. * Classe de gestion ORM
  4. * @author mathieu
  5. * @package db_object
  6. *
  7. */
  8. class db_object_manager
  9. {
  10. /**
  11. * Liste des types de champ "simples" (dans la table principale en base de donnée)
  12. * @var []
  13. */
  14. public $field_simple = array("int", "float", "string", "password", "text", "richtext", "object", "timestamp", "bool", 'date', "select", "select_multiple", "file", "img");
  15. /**
  16. * Debug database operations
  17. * @var bool
  18. */
  19. public $DB_DEBUG = false;
  20. /**
  21. * Debug file operations
  22. * @var bool
  23. */
  24. public $FILE_DEBUG = false;
  25. /**
  26. * Class name
  27. * @var string
  28. */
  29. public $_name = "";
  30. public $_label = "";
  31. /**
  32. * Database name
  33. * @var string
  34. */
  35. public $_db_table = "";
  36. /**
  37. * ID field name in database
  38. * @var string
  39. */
  40. public $_db_id = "id";
  41. /**
  42. * Fields specifications
  43. * @var []
  44. */
  45. public $_fields = array();
  46. /**
  47. * More fields specifications
  48. * @var []
  49. */
  50. public $_fields_more = array();
  51. public $_db_fields_more_table = '';
  52. /**
  53. * Fields to display in lists
  54. * @var []
  55. */
  56. public $_field_disp_list = array();
  57. /**
  58. * Object list cache
  59. * @var []
  60. */
  61. private $list = array();
  62. /**
  63. * Object list cache
  64. * @var []
  65. */
  66. private $list_ref = array();
  67. /**
  68. * Constructeur
  69. */
  70. public function __construct()
  71. {
  72. if (!$this->_name)
  73. $this->_name = substr(get_class($this), 0, -8);
  74. if (!$this->_db_table)
  75. $this->_db_table = $this->_name;
  76. }
  77. /**
  78. * Returns if an object exists
  79. * @param int|string $param
  80. * @return bool
  81. */
  82. public function exists($param)
  83. {
  84. if (is_numeric($param) || is_string($param)) {
  85. if (is_numeric($param) && isset($this->list[$param])) {
  86. return true;
  87. }
  88. elseif (is_string($param) && isset($this->list_ref[$param])) {
  89. return true;
  90. }
  91. elseif ($this->get($param)) {
  92. return true;
  93. }
  94. else {
  95. return false;
  96. }
  97. }
  98. else {
  99. return false;
  100. }
  101. }
  102. /**
  103. * Renvoie un object de la base
  104. * @param int|string $param
  105. * @return db_object
  106. */
  107. public function get($param)
  108. {
  109. if (is_numeric($param)) {
  110. if (isset($this->list[$param])) {
  111. return $this->list[$param];
  112. }
  113. elseif(count($objects=$this->select('id='.$param))==1) {
  114. $object = array_pop($objects);
  115. if ($object->ref) {
  116. $this->list_ref[$object->ref] = $object->id;
  117. }
  118. return $object;
  119. }
  120. else {
  121. return false;
  122. }
  123. }
  124. elseif (isset($this->_fields['ref']) && is_string($param)) {
  125. if (isset($this->list_ref[$param])) {
  126. return $this->list[$this->list_ref[$param]];
  127. }
  128. elseif (count($objects=$this->select("ref='".mysql_real_escape_string($param)."'"))==1) {
  129. $object = array_pop($objects);
  130. if ($object->ref) {
  131. $this->list_ref[$object->ref] = $object->id;
  132. }
  133. return $object;
  134. }
  135. else {
  136. return false;
  137. }
  138. }
  139. else {
  140. return false;
  141. }
  142. }
  143. /**
  144. * Select en base de donnée
  145. * @param [] $params
  146. * @param int $limit
  147. * @param string $order
  148. * @return []
  149. */
  150. public function select($params=null, $limit=null, $order=null)
  151. {
  152. $classname = $this->_name;
  153. $list = array();
  154. foreach ($this->db_retrieve($params, $limit, $order) as $row)
  155. {
  156. if (!isset($this->list[$row["id"]])){
  157. $this->list[$row["id"]] = $object = new $classname(null, $row);
  158. if (isset($row['ref']))
  159. $this->list_ref[$row["ref"]] = $row["id"];
  160. }
  161. else
  162. $object = $this->list[$row["id"]];
  163. $list[] = $object;
  164. }
  165. return $list;
  166. }
  167. /**
  168. * Select en base de donnée
  169. * @param [] $params
  170. * @param int $limit
  171. * @param string $order
  172. * @return []
  173. */
  174. public function db_retrieve($params=null, $limit=null, $order=null)
  175. {
  176. if ($params)
  177. $q_where = "WHERE ".$params;
  178. else
  179. $q_where = "";
  180. if ($limit)
  181. $q_limit = "LIMIT ".$limit;
  182. else
  183. $q_limit = "";
  184. if ($order){
  185. $q_o = array();
  186. foreach(explode(', ', $order) as $o)
  187. if (count($o2=explode(' ', trim($o)))>1)
  188. $q_o[] = "`".$o2[0]."` ".$o2[1];
  189. else
  190. $q_o[] = "`".$o2[0]."`";
  191. $q_order = "ORDER BY ".implode(', ', $q_o);
  192. }
  193. else
  194. $q_order = "";
  195. $select_more = array();
  196. $type_select_multiple = array();
  197. $q_select = array("`".$this->_db_id."` as `id`");
  198. foreach($this->_fields as $name=>$field)
  199. {
  200. if (in_array($field["type"], $this->field_simple))
  201. {
  202. if ($field["type"] == "select_multiple")
  203. $type_select_multiple[] = $name;
  204. if (isset($field["db_fieldname"]))
  205. $q_select[] = "`".$field["db_fieldname"]."` as `".$name."`";
  206. else
  207. $q_select[] = "`".$name."`";
  208. }
  209. elseif ($field["type"] == "object_list")
  210. {
  211. $select_more[] = $name;
  212. }
  213. }
  214. $q_s = "SELECT ".implode(", ", $q_select)." FROM `".$this->_db_table."` ".$q_where." ".$q_order." ".$q_limit;
  215. $q_r = mysql_query($q_s);
  216. if ($this->DB_DEBUG == true || ($error=mysql_error()))
  217. {
  218. echo "$q_s : ".$error;
  219. }
  220. $list = array();
  221. $list_id = array();
  222. while ($row=mysql_fetch_assoc($q_r))
  223. {
  224. // @todo : tout pourris...
  225. foreach($row as $i=>$j)
  226. $row[$i] = stripslashes($j);
  227. foreach($type_select_multiple as $name)
  228. {
  229. if ($row[$name] !== null)
  230. $row[$name] = explode(",", $row[$name]);
  231. else
  232. $row[$name] = array();
  233. }
  234. $list[$row["id"]] = $row;
  235. $list_id[] = $row["id"];
  236. }
  237. // Fields of type : object_list
  238. foreach($select_more as $name)
  239. {
  240. $field = $this->_fields[$name];
  241. $classname = $field["object_type"];
  242. $q_s = "SELECT `".$field["db_field_id"]."` as id, `".$field["db_field_ref_id"]."` as ref_id FROM `".$field["db_table"]."` WHERE `".$field["db_field_ref_id"]."` IN (".implode(", ", $list_id).")";
  243. $q_r = mysql_query($q_s);
  244. while ($row=mysql_fetch_assoc($q_r))
  245. {
  246. $list[$row["ref_id"]][$name][] = $row["id"];
  247. }
  248. }
  249. // More fields
  250. foreach($this->db_retrieve_more($list_id) as $id=>$row)
  251. $list[$id] = array_merge($list[$id], $row);
  252. return $list;
  253. }
  254. /**
  255. * retrieve more info
  256. * @param []int $list_id
  257. */
  258. protected function db_retrieve_more($list_id=null)
  259. {
  260. return array();
  261. }
  262. /**
  263. * Count en base de donnée
  264. * @param [] $params
  265. * @return int
  266. */
  267. public function count($params=null)
  268. {
  269. $classname = $this->_name;
  270. if ($params)
  271. $q_where = "WHERE ".$params;
  272. else
  273. $q_where = "";
  274. $q_s = "SELECT COUNT(*) FROM `".$this->_db_table."` ".$q_where;
  275. $q_r = mysql_query($q_s);
  276. if ($this->DB_DEBUG == true) {
  277. echo "$q_s : ".mysql_error();
  278. }
  279. $row = mysql_fetch_row($q_r);
  280. return array_pop($row);
  281. }
  282. public function insert($info=array())
  283. {
  284. $classname = $this->_name;
  285. $object = new $classname();
  286. $object->insert($info);
  287. if ($object->id) {
  288. $this->list[$object->id] = $object;
  289. if ($object->ref) {
  290. $this->list_ref[$object->ref] = $object->id;
  291. }
  292. return $object;
  293. }
  294. }
  295. }
  296. /**
  297. * Classe objet ORM
  298. * @author mathieu
  299. * @package db_object
  300. *
  301. */
  302. class db_object
  303. {
  304. /**
  305. * Class name
  306. * @var string
  307. */
  308. public $_name = "";
  309. /**
  310. * Manager object
  311. * @access public
  312. * @var db_object_manager
  313. */
  314. public $_manager = null;
  315. /**
  316. * Object ID
  317. * @var int
  318. */
  319. public $id = null;
  320. /* CONSTRUCT */
  321. /**
  322. * Constructeur
  323. * @param int $id
  324. * @param [] $info
  325. */
  326. public function __construct($id=null, $info=null)
  327. {
  328. if (!$this->_name)
  329. $this->_name = get_class($this);
  330. $classname = $this->_name;
  331. $this->_manager = $classname();
  332. foreach ($this->_manager->_fields as $name=>$value)
  333. $this->$name = null;
  334. if (is_numeric($id))
  335. {
  336. if ($info=$this->db_retrieve($id))
  337. $this->field_update($info);
  338. }
  339. elseif (is_array($info))
  340. {
  341. $this->field_update($info);
  342. }
  343. }
  344. /* DISPLAY */
  345. /**
  346. * Default display
  347. * @return string
  348. */
  349. public function __tostring()
  350. {
  351. if ($this->id)
  352. return $this->_manager->_name." #".$this->id;
  353. else
  354. return $this->_manager->_name." # en cours...";
  355. }
  356. /**
  357. * Return image url
  358. * @param string $name
  359. * @return string
  360. */
  361. public function file($name)
  362. {
  363. if (isset($this->_manager->_fields[$name]) && in_array($this->_manager->_fields[$name]["type"], array("img", "file")) && $this->$name)
  364. {
  365. return $this->_manager->_fields[$name]["folder"]."/".$this->$name;
  366. }
  367. }
  368. /**
  369. * Return image url
  370. * @param string $name
  371. * @return string
  372. */
  373. public function img($name, $width=null)
  374. {
  375. if (isset($this->_manager->_fields[$name]) && in_array($this->_manager->_fields[$name]["type"], array("img", "file")) && $this->$name)
  376. {
  377. if (is_numeric($width))
  378. return str_replace('img/', 'img2/'.$width.'/', $this->_manager->_fields[$name]["folder"])."/".$this->$name;
  379. else
  380. return $this->_manager->_fields[$name]["folder"]."/".$this->$name;
  381. }
  382. }
  383. /**
  384. * Return object associated to a field
  385. * @param string $fieldname
  386. * @return db_object
  387. */
  388. function object($fieldname)
  389. {
  390. if (!is_string($fieldname) || !isset($this->_manager->_fields[$fieldname]))
  391. return;
  392. $field = $this->_manager->_fields[$fieldname];
  393. if (!isset($field["type"]) || $field["type"] != "object" || !isset($field["object_type"]))
  394. return;
  395. if (!is_string($classname=$field["object_type"]) || !class_exists($classname))
  396. return;
  397. if (!is_numeric($this->$fieldname))
  398. return;
  399. return $classname()->get($this->$fieldname);
  400. }
  401. /* INSERT UPDATE VERIF */
  402. public function field_calculated(&$info)
  403. {
  404. foreach($this->_manager->_fields as $name=>&$field) if (isset($field['calculate'])){
  405. list($type, $var) = explode(':', $field['calculate']);
  406. if ($type=='url'){
  407. if (!isset($info[$var]))
  408. continue;
  409. $url = strtolower(stripAccents($info[$var]));
  410. $patterns = $replacements = array();
  411. $patterns[0] = '/(&amp;|&)/i';
  412. $replacements[0] = '-and-';
  413. $patterns[1] = '/[^a-zA-Z01-9]/i';
  414. $replacements[1] = '-';
  415. $patterns[2] = '/(-+)/i';
  416. $replacements[2] = '-';
  417. $patterns[3] = '/(-$|^-)/i';
  418. $replacements[3] = '';
  419. $url = preg_replace($patterns, $replacements, $url);
  420. if (strlen($url)>100){
  421. $urle = explode('-', $url);
  422. while (strlen($url)>100){
  423. array_pop($urle);
  424. $url = implode('-', $urle);
  425. }
  426. }
  427. $info[$name] = $url;
  428. }
  429. if ($type=='ref'){
  430. if (!isset($info[$var]))
  431. continue;
  432. $url = strtolower(stripAccents($info[$var]));
  433. $patterns = $replacements = array();
  434. $patterns[0] = '/(&amp;|&)/i';
  435. $replacements[0] = '';
  436. $patterns[1] = '/[^a-zA-Z01-9]/i';
  437. $replacements[1] = '_';
  438. $patterns[2] = '/(_+)/i';
  439. $replacements[2] = '_';
  440. $patterns[3] = '/(_$|^_)/i';
  441. $replacements[3] = '';
  442. $url = preg_replace($patterns, $replacements, $url);
  443. $urle = explode('_', $url);
  444. $s = 0;
  445. while (strlen($url)>20) {
  446. $s++;
  447. foreach($urle as $i=>&$j) {
  448. if (strlen($j)<=$s) {
  449. unset($urle[$i]);
  450. $url = implode('_', $urle);
  451. if (strlen($url)<=20)
  452. break;
  453. }
  454. }
  455. }
  456. while (strlen($url)>50){
  457. array_pop($urle);
  458. $url = implode('_', $urle);
  459. }
  460. $info[$name] = $url;
  461. }
  462. }
  463. //var_dump($info); die();
  464. }
  465. /**
  466. * Contrôle des champs
  467. * @param [] $info
  468. */
  469. public function field_verif(&$info)
  470. {
  471. if (!is_array($info))
  472. $info = array();
  473. foreach($info as $name=>&$value)
  474. {
  475. if (!isset($this->_manager->_fields[$name]))
  476. {
  477. unset($info[$name]);
  478. }
  479. else
  480. {
  481. $field = $this->_manager->_fields[$name];
  482. if (isset($field["type"]))
  483. {
  484. $type = $field["type"];
  485. if (in_array($type, array("object", "int", "float", "numeric", "decimal")) && !is_numeric($value))
  486. {
  487. $value = NULL;
  488. }
  489. elseif ($type == "boolean")
  490. {
  491. $value = ($value) ?1 :0;
  492. }
  493. elseif ($type == "object" && $value)
  494. {
  495. $object_classname = $field["object_type"];
  496. if (!$object_classname()->exists($value))
  497. $value = NULL;
  498. }
  499. elseif (in_array($type, array("string", "text", "richtext")) && !is_string($value))
  500. {
  501. $value = NULL;
  502. }
  503. elseif ($type == "select" && ((!is_string($value) && !is_numeric($value)) || !isset($field["list"][$value])))
  504. {
  505. $value = NULL;
  506. }
  507. elseif ($type == "select_multiple")
  508. {
  509. if (!is_array($value))
  510. {
  511. $value = NULL;
  512. }
  513. else
  514. {
  515. foreach($value as $i=>$v)
  516. if (!isset($field["list"][$v]))
  517. unset($value[$i]);
  518. }
  519. }
  520. elseif ($type == "date" && (!is_string($value)))
  521. {
  522. $value = NULL;
  523. }
  524. elseif (in_array($type, array("datetime", "timestamp")) && (!is_string($value)))
  525. {
  526. $value = NULL;
  527. }
  528. elseif ($type == "img" || $type == "file")
  529. {
  530. //var_dump($_FILES);
  531. // Sans upload ou upload foireux
  532. if (isset($_FILES[$name]) && (!$_FILES[$name]["tmp_name"] || $_FILES[$name]["error"] != UPLOAD_ERR_OK)) {
  533. unset($_FILES[$name]);
  534. }
  535. // Fichier sans nom ou nom foireux
  536. if (isset($_FILES[$name]))
  537. {
  538. if (isset($field["filename"]))
  539. {
  540. $_FILES[$name]["name"] = $this->field_map_replace($field["filename"]);
  541. }
  542. else
  543. {
  544. $nb = 0;
  545. $name = $field["filename"];
  546. while (file_exists(PATH_ROOT."/".$field["folder"]."/".$_FILES[$name]["name"]))
  547. {
  548. $nb++;
  549. $_FILES[$name]["name"] = $nb."-".$name;
  550. }
  551. }
  552. $value = $_FILES[$name]["name"];
  553. }
  554. // Renommage sans fichier
  555. elseif ($value && (isset($field["filename"]) || !$this->$name || !file_exists(PATH_ROOT."/".$field["folder"]."/".$this->$name)))
  556. {
  557. //echo PATH_ROOT."/".$field["folder"]."/".$this->$name;
  558. unset($info[$name]);
  559. }
  560. }
  561. }
  562. }
  563. }
  564. //var_dump($_FILES); var_dump($info);
  565. }
  566. function field_map_replace($string)
  567. {
  568. $replace_from = $replace_to = array();
  569. foreach($this->_manager->_fields as $name=>$field)
  570. {
  571. $replace_from[] = "{".$name."}";
  572. $replace_to[] = $this->$name;
  573. }
  574. return str_replace($replace_from, $replace_to, $string);
  575. }
  576. /**
  577. * Contrôle supplémentaire à l'insertion
  578. * @param [] $info
  579. */
  580. public function field_verif_insert(&$info)
  581. {
  582. }
  583. /**
  584. * Contrôle supplémentaire à la mise à jour
  585. * @param [] $info
  586. */
  587. public function field_verif_update(&$info)
  588. {
  589. }
  590. /**
  591. * Update object with data
  592. * @param [] $info
  593. */
  594. private function field_update(&$info)
  595. {
  596. if (!is_array($info))
  597. return;
  598. foreach($info as $name=>$value)
  599. if ($name=="id" || isset($this->_manager->_fields[$name]))
  600. $this->$name = $value;
  601. }
  602. public function duplicate()
  603. {
  604. }
  605. /* OPERATIONS */
  606. /**
  607. * Insertion objet
  608. * @param [] $info
  609. * @return bool
  610. */
  611. public function insert($info)
  612. {
  613. if (is_numeric($this->id))
  614. return;
  615. $this->field_verif($info);
  616. $this->field_calculated($info);
  617. $this->field_verif_insert($info);
  618. if (is_numeric($id=$this->db_insert($info)))
  619. {
  620. $this->id = $id;
  621. $this->field_update($info);
  622. return true;
  623. }
  624. else
  625. return false;
  626. }
  627. /**
  628. * Mise à jour objet
  629. * @param [] $info
  630. * @return bool
  631. */
  632. public function update($info)
  633. {
  634. if (!is_numeric($this->id))
  635. return;
  636. $this->field_verif($info);
  637. foreach($info as $name=>$value)
  638. if ($this->$name === $value && (!in_array($this->_manager->_fields[$name]["type"], array("img", "file")) || !isset($_FILES[$name])))
  639. unset($info[$name]);
  640. $this->field_verif_update($info);
  641. if ($this->db_update($info))
  642. {
  643. $this->field_update($info);
  644. return true;
  645. }
  646. else
  647. return false;
  648. }
  649. /**
  650. * Supression objet
  651. * @return bool
  652. */
  653. public function delete()
  654. {
  655. if (!is_numeric($this->id))
  656. return;
  657. return $this->db_delete($this->id);
  658. }
  659. /* DATABASE */
  660. /*
  661. * Comprend autant les opérations en base de donnée que sur le système de fichiers,
  662. * bref sur tout support de stockage de donnée (ce qui se tient logiquement)
  663. */
  664. /**
  665. * Retrieve object info from database
  666. * @param int $id
  667. * @return []|bool
  668. */
  669. protected function db_retrieve($id)
  670. {
  671. if (!is_numeric($id) || !count($list=$this->_manager->db_retrieve("`".$this->_manager->_db_id."`='".$id."'")))
  672. return false;
  673. return array_pop($list);
  674. }
  675. /**
  676. * Update object in database
  677. * @param [] $info
  678. * @return int
  679. */
  680. protected function db_update($info)
  681. {
  682. if (!is_numeric($this->id))
  683. return false;
  684. $q_list = array();
  685. $file_move_list = $file_rename_list = $file_delete_list = array();
  686. //var_dump($_FILES); var_dump($info);
  687. foreach($info as $name=>$value)
  688. {
  689. $field = $this->_manager->_fields[$name];
  690. if (isset($field["type"]))
  691. {
  692. $type = $field["type"];
  693. if (in_array($field["type"], $this->_manager->field_simple))
  694. {
  695. if ($field["type"] == "select_multiple")
  696. {
  697. if (is_array($value))
  698. foreach($value as $i=>$v)
  699. $value[$i] = mysql_real_escape_string($v);
  700. $q_list[] = "`".(isset($field["db_fieldname"]) ?$field["db_fieldname"] :$name)."` = ".(is_array($value)&&count($value) ?"'".implode(",",$value)."'" :"NULL");
  701. }
  702. else
  703. $q_list[] = "`".(isset($field["db_fieldname"]) ?$field["db_fieldname"] :$name)."` = ".($value===NULL?"NULL":"'".mysql_real_escape_string($value)."'");
  704. if ($type == "img")
  705. {
  706. if (isset($_FILES[$name]) && $this->$name) {
  707. $file_update = true;
  708. $file_delete_list[] = PATH_ROOT."/".$field["folder"]."/".$this->$name;
  709. }
  710. if (!isset($_FILES[$name]) && $this->$name) {
  711. $file_update = true;
  712. $file_rename_list[PATH_ROOT."/".$field["folder"]."/".$this->$name] = PATH_ROOT."/".$field["folder"]."/".$value;
  713. }
  714. if (isset($_FILES[$name])) {
  715. $file_update = true;
  716. $file_move_list[$_FILES[$name]["tmp_name"]] = PATH_ROOT."/".$field["folder"]."/".$value;
  717. }
  718. }
  719. }
  720. }
  721. }
  722. if (count($q_list)){
  723. $q_s = "UPDATE `".$this->_manager->_db_table."` SET ".implode(", ", $q_list)." WHERE `".$this->_manager->_db_id."`='".$this->id."'";
  724. $q_r = mysql_query($q_s);
  725. }
  726. if ($this->_manager->DB_DEBUG == true || (isset($q_s) && ($error=mysql_error())))
  727. {
  728. echo "$q_s : ".$error;
  729. }
  730. $return = (isset($q_s) && mysql_affected_rows()>0 ?true :false);
  731. foreach($file_delete_list as $name)
  732. {
  733. if ($this->_manager->FILE_DEBUG)
  734. echo "<p>Delete $name</p>\n";
  735. $return = (unlink($name) || $return);
  736. }
  737. foreach($file_rename_list as $from=>$to)
  738. {
  739. if ($this->_manager->FILE_DEBUG)
  740. echo "<p>Rename $from $to</p>\n";
  741. $return = (rename($from, $to) || $return);
  742. }
  743. foreach($file_move_list as $from=>$to)
  744. {
  745. if ($this->_manager->FILE_DEBUG)
  746. echo "<p>Move uplodaed $from $to</p>\n";
  747. //var_dump(move_uploaded_file($from, $to));
  748. $return = (move_uploaded_file($from, $to) || $return);
  749. }
  750. // More fields
  751. $return = ($this->db_update_more($info) || $return);
  752. return $return;
  753. }
  754. /**
  755. * Modification supplémentaire
  756. * @param [] $info
  757. */
  758. protected function db_update_more($info)
  759. {
  760. return false;
  761. }
  762. /**
  763. * Insert in database
  764. * @param [] $info
  765. * @return bool
  766. */
  767. protected function db_insert($info)
  768. {
  769. if (is_numeric($this->id))
  770. return false;
  771. $q_list_1 = $q_list_2 = array();
  772. $file_move_list = array();
  773. foreach($info as $name=>$value)
  774. {
  775. $field = $this->_manager->_fields[$name];
  776. if (isset($field["type"]))
  777. {
  778. if (in_array($field["type"], $this->_manager->field_simple))
  779. {
  780. $type = $field["type"];
  781. $q_list_1[] = "`".(isset($field["db_fieldname"]) ?$field["db_fieldname"] : $name)."`";
  782. if ($field["type"] == "select_multiple")
  783. {
  784. if (is_array($value))
  785. foreach($value as $i=>$v)
  786. $value[$i] = "'".mysql_real_escape_string($v)."'";
  787. $q_list_2[] = (is_array($value)&&count($value) ?"(".implode(",",$value).")" :"NULL");
  788. }
  789. else
  790. $q_list_2[] = ($value===NULL?"NULL":"'".mysql_real_escape_string($value)."'");
  791. if ($type == "img")
  792. {
  793. $file_move_list[$_FILES[$name]["tmp_name"]] = PATH_ROOT."/".$field["folder"]."/".$value;
  794. }
  795. }
  796. }
  797. }
  798. $q_s = "INSERT INTO `".$this->_manager->_db_table."` (".implode(", ", $q_list_1).") VALUES (".implode(", ", $q_list_2).")";
  799. $q_r = mysql_query($q_s);
  800. if ($this->_manager->DB_DEBUG == true)
  801. {
  802. echo "$q_s : ".mysql_error();
  803. }
  804. $return = mysql_insert_id();
  805. if ($return)
  806. {
  807. foreach($file_move_list as $from=>$to)
  808. {
  809. if ($this->_manager->FILE_DEBUG)
  810. echo "<p>Move uploaded $from $to</p>\n";
  811. move_uploaded_file($from, $to);
  812. }
  813. }
  814. if ($return)
  815. $this->db_insert_more($return, $info);
  816. return $return;
  817. }
  818. /**
  819. * Insertions supplémentaires
  820. * @param int $id
  821. * @param [] $info
  822. * @return bool
  823. */
  824. protected function db_insert_more($id, $info)
  825. {
  826. return false;
  827. }
  828. /**
  829. * Delete object in database
  830. * @return bool
  831. */
  832. protected function db_delete()
  833. {
  834. if (!is_numeric($this->id))
  835. return false;
  836. $q_s = "DELETE FROM `".$this->_manager->_db_table."` WHERE `".$this->_manager->_db_id."`='".$this->id."'";
  837. $q_r = mysql_query($q_s);
  838. if ($this->_manager->DB_DEBUG == true)
  839. {
  840. echo "$q_s : ".mysql_error();
  841. }
  842. $return = (mysql_affected_rows()>0 ?true :false);
  843. if ($return)
  844. $this->db_delete_more();
  845. return $return;
  846. }
  847. /**
  848. * Suppressions supplémentaires
  849. * @return bool
  850. */
  851. protected function db_delete_more()
  852. {
  853. return false;
  854. }
  855. }