db_object.class.inc.php.orig 18 KB

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