You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

phcptype.class.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) ---Put here your own copyright and developer email---
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file dev/skeletons/phcptype.class.php
  20. * \ingroup mymodule othermodule1 othermodule2
  21. * \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
  22. * Initialy built by build_class_from_table on 2013-09-12 16:46
  23. */
  24. // Put here all includes required by your class file
  25. require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
  26. //require_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php");
  27. require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php");
  28. /**
  29. * Put here description of your class
  30. */
  31. class PhcpType extends CommonObject
  32. {
  33. var $db; //!< To store db handler
  34. var $error; //!< To return error code (or message)
  35. var $errors=array(); //!< To return several error codes (or messages)
  36. var $element='phcptype'; //!< Id that identify managed objects
  37. var $table_element='phcptype'; //!< Name of table without prefix where object is stored
  38. var $id;
  39. var $nom;
  40. var $ref;
  41. var $products;
  42. /**
  43. * Constructor
  44. *
  45. * @param DoliDb $db Database handler
  46. */
  47. function __construct($db)
  48. {
  49. $this->db = $db;
  50. return 1;
  51. }
  52. function all(){
  53. global $conf, $langs;
  54. $sql = "SELECT";
  55. $sql.= " t.rowid";
  56. $sql.= " FROM ".MAIN_DB_PREFIX."phcp_type as t";
  57. $list = array();
  58. dol_syslog(get_class($this)."::all sql=".$sql, LOG_DEBUG);
  59. $resql=$this->db->query($sql);
  60. if ($resql){
  61. if ($num = $this->db->num_rows($resql)){
  62. $i = 0;
  63. while ($i < $num){
  64. $obj = $this->db->fetch_object($resql);
  65. if ($obj){
  66. $phcptype = new PhcpType($this->db);
  67. $phcptype->fetch($obj->rowid);
  68. $list[]= $phcptype;
  69. }
  70. $i++;
  71. }
  72. }
  73. }
  74. return $list;
  75. }
  76. function add_product($fk_product){
  77. global $conf, $langs;
  78. $error =0;
  79. $present = false;
  80. foreach($this->products as $product){
  81. $present |= $product->id == $fk_product;
  82. }
  83. if(!$present){
  84. $product = new Product($this->db);
  85. $product->fetch($fk_product);
  86. $sql = "INSERT INTO ".MAIN_DB_PREFIX."phcp_type_product(";
  87. $sql.= "fk_phcp_type, fk_product) ";
  88. $sql.= "VALUES (";
  89. $sql.= $this->id.",";
  90. $sql.= $fk_product.")";
  91. $this->db->begin();
  92. dol_syslog(get_class($this)."::add_product sql=".$sql, LOG_DEBUG);
  93. $resql=$this->db->query($sql);
  94. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  95. // Commit or rollback
  96. if ($error)
  97. {
  98. foreach($this->errors as $errmsg)
  99. {
  100. dol_syslog(get_class($this)."::add_product ".$errmsg, LOG_ERR);
  101. $this->error.=($this->error?', '.$errmsg:$errmsg);
  102. }
  103. $this->db->rollback();
  104. return -1*$error;
  105. }
  106. else
  107. {
  108. $this->db->commit();
  109. $this->products[] = $product;
  110. return 1;
  111. }
  112. }
  113. }
  114. function remove_product($fk_product){
  115. global $conf, $langs;
  116. $error=0;
  117. foreach($this->products as $key => $product){
  118. if(intval($product->id) == intval($fk_product))
  119. unset($this->products[$key]);
  120. }
  121. $sql = "DELETE FROM ".MAIN_DB_PREFIX."phcp_type_product ";
  122. $sql.= "WHERE fk_product = ".$fk_product." ";
  123. $sql.= "AND fk_phcp_type = ".$this->id;
  124. $this->db->begin();
  125. dol_syslog(get_class($this)."::remove_product sql=".$sql, LOG_DEBUG);
  126. $resql=$this->db->query($sql);
  127. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  128. // Commit or rollback
  129. if ($error)
  130. {
  131. foreach($this->errors as $errmsg)
  132. {
  133. dol_syslog(get_class($this)."::remove_product ".$errmsg, LOG_ERR);
  134. $this->error.=($this->error?', '.$errmsg:$errmsg);
  135. }
  136. $this->db->rollback();
  137. return -1*$error;
  138. }
  139. else
  140. {
  141. $this->db->commit();
  142. return 1;
  143. }
  144. }
  145. /**
  146. * Create object into database
  147. *
  148. * @param User $user User that creates
  149. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  150. * @return int <0 if KO, Id of created object if OK
  151. */
  152. function create($user, $notrigger=0)
  153. {
  154. global $conf, $langs;
  155. $error=0;
  156. // Clean parameters
  157. if (isset($this->nom)) $this->nom=trim($this->nom);
  158. if (isset($this->ref)) $this->ref=trim($this->ref);
  159. // Check parameters
  160. // Put here code to add control on parameters values
  161. if ($this->nom == ''){
  162. $error++; $this->errors[]="Vous devez spécifier un label pour le type de photocopieur";
  163. }
  164. if(!$error){
  165. // Insert request
  166. $sql = "INSERT INTO ".MAIN_DB_PREFIX."phcp_type(";
  167. $sql.= "nom,";
  168. $sql.= "ref";
  169. $sql.= ") VALUES (";
  170. $sql.= " ".(! isset($this->nom)?'NULL':"'".$this->db->escape($this->nom)."'").",";
  171. $sql.= " ".(! isset($this->ref)?'NULL':"'".$this->db->escape($this->ref)."'")."";
  172. $sql.= ")";
  173. $this->db->begin();
  174. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  175. $resql=$this->db->query($sql);
  176. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  177. }
  178. if (! $error)
  179. {
  180. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."phcp_type");
  181. if (! $notrigger)
  182. {
  183. // Uncomment this and change MYOBJECT to your own tag if you
  184. // want this action calls a trigger.
  185. //// Call triggers
  186. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  187. //$interface=new Interfaces($this->db);
  188. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  189. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  190. //// End call triggers
  191. }
  192. }
  193. // Commit or rollback
  194. if ($error)
  195. {
  196. foreach($this->errors as $errmsg)
  197. {
  198. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  199. $this->error.=($this->error?', '.$errmsg:$errmsg);
  200. }
  201. $this->db->rollback();
  202. return -1*$error;
  203. }
  204. else
  205. {
  206. $this->db->commit();
  207. return $this->id;
  208. }
  209. }
  210. /**
  211. * Load object in memory from the database
  212. *
  213. * @param int $id Id object
  214. * @return int <0 if KO, >0 if OK
  215. */
  216. function fetch($id)
  217. {
  218. global $langs;
  219. $sql = "SELECT";
  220. $sql.= " t.rowid,";
  221. $sql.= " t.nom,";
  222. $sql.= " t.ref";
  223. $sql.= " FROM ".MAIN_DB_PREFIX."phcp_type as t";
  224. $sql.= " WHERE t.rowid = ".$id;
  225. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  226. $resql=$this->db->query($sql);
  227. if ($resql)
  228. {
  229. if ($this->db->num_rows($resql))
  230. {
  231. $obj = $this->db->fetch_object($resql);
  232. $this->id = $obj->rowid;
  233. $this->nom = $obj->nom;
  234. $this->ref = $obj->ref;
  235. $this->products = array();
  236. $sql2 = "SELECT";
  237. $sql2.= " ptpr.fk_product";
  238. $sql2.= " FROM ".MAIN_DB_PREFIX."phcp_type_product as ptpr";
  239. $sql2.= " WHERE ptpr.fk_phcp_type = ".$this->id;
  240. $resql2=$this->db->query($sql2);
  241. if ($resql2){
  242. $num2 = $this->db->num_rows($resql2);
  243. $i2 = 0;
  244. if ($num2){
  245. while ($i2 < $num2){
  246. $obj2 = $this->db->fetch_object($resql2);
  247. if ($obj2){
  248. $product = new Product($this->db);
  249. $product->fetch($obj2->fk_product);
  250. $this->products[] = $product;
  251. }
  252. $i2++;
  253. }
  254. }
  255. }
  256. }
  257. $this->db->free($resql);
  258. return 1;
  259. }
  260. else
  261. {
  262. $this->error="Error ".$this->db->lasterror();
  263. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  264. return -1;
  265. }
  266. }
  267. /**
  268. * Update object into database
  269. *
  270. * @param User $user User that modifies
  271. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  272. * @return int <0 if KO, >0 if OK
  273. */
  274. function update($user=0, $notrigger=0)
  275. {
  276. global $conf, $langs;
  277. $error=0;
  278. // Clean parameters
  279. if (isset($this->nom)) $this->nom=trim($this->nom);
  280. if (isset($this->ref)) $this->ref=trim($this->ref);
  281. // Check parameters
  282. // Put here code to add a control on parameters values
  283. // Update request
  284. $sql = "UPDATE ".MAIN_DB_PREFIX."phcp_type SET";
  285. $sql.= " nom=".(isset($this->nom)?"'".$this->db->escape($this->nom)."'":"null").",";
  286. $sql.= " ref=".(isset($this->ref)?"'".$this->db->escape($this->ref)."'":"null")."";
  287. $sql.= " WHERE rowid=".$this->id;
  288. $this->db->begin();
  289. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  290. $resql = $this->db->query($sql);
  291. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  292. if (! $error)
  293. {
  294. if (! $notrigger)
  295. {
  296. // Uncomment this and change MYOBJECT to your own tag if you
  297. // want this action calls a trigger.
  298. //// Call triggers
  299. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  300. //$interface=new Interfaces($this->db);
  301. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  302. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  303. //// End call triggers
  304. }
  305. }
  306. // Commit or rollback
  307. if ($error)
  308. {
  309. foreach($this->errors as $errmsg)
  310. {
  311. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  312. $this->error.=($this->error?', '.$errmsg:$errmsg);
  313. }
  314. $this->db->rollback();
  315. return -1*$error;
  316. }
  317. else
  318. {
  319. $this->db->commit();
  320. return 1;
  321. }
  322. }
  323. /**
  324. * Delete object in database
  325. *
  326. * @param User $user User that deletes
  327. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  328. * @return int <0 if KO, >0 if OK
  329. */
  330. function delete($user, $notrigger=0)
  331. {
  332. global $conf, $langs;
  333. $error=0;
  334. $this->db->begin();
  335. if (! $error)
  336. {
  337. if (! $notrigger)
  338. {
  339. // Uncomment this and change MYOBJECT to your own tag if you
  340. // want this action calls a trigger.
  341. //// Call triggers
  342. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  343. //$interface=new Interfaces($this->db);
  344. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  345. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  346. //// End call triggers
  347. }
  348. }
  349. if (! $error)
  350. {
  351. $sql = "UPDATE ".MAIN_DB_PREFIX."phcp";
  352. $sql.= " SET fk_phcp_type=NULL";
  353. $sql.= " WHERE fk_phcp_type=".$this->id;
  354. dol_syslog(get_class($this)."::delete sql=".$sql);
  355. $resql = $this->db->query($sql);
  356. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  357. }
  358. if (! $error)
  359. {
  360. $sql = "DELETE FROM ".MAIN_DB_PREFIX."phcp_type_product";
  361. $sql.= " WHERE fk_phcp_type=".$this->id;
  362. dol_syslog(get_class($this)."::delete sql=".$sql);
  363. $resql = $this->db->query($sql);
  364. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  365. }
  366. if (! $error)
  367. {
  368. $sql = "DELETE FROM ".MAIN_DB_PREFIX."phcp_type";
  369. $sql.= " WHERE rowid=".$this->id;
  370. dol_syslog(get_class($this)."::delete sql=".$sql);
  371. $resql = $this->db->query($sql);
  372. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  373. }
  374. // Commit or rollback
  375. if ($error)
  376. {
  377. foreach($this->errors as $errmsg)
  378. {
  379. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  380. $this->error.=($this->error?', '.$errmsg:$errmsg);
  381. }
  382. $this->db->rollback();
  383. return -1*$error;
  384. }
  385. else
  386. {
  387. $this->db->commit();
  388. return 1;
  389. }
  390. }
  391. /**
  392. * Load an object from its id and create a new one in database
  393. *
  394. * @param int $fromid Id of object to clone
  395. * @return int New id of clone
  396. */
  397. function createFromClone($fromid)
  398. {
  399. global $user,$langs;
  400. $error=0;
  401. $object=new Phcptype($this->db);
  402. $this->db->begin();
  403. // Load source object
  404. $object->fetch($fromid);
  405. $object->id=0;
  406. $object->statut=0;
  407. // Clear fields
  408. // ...
  409. // Create clone
  410. $result=$object->create($user);
  411. // Other options
  412. if ($result < 0)
  413. {
  414. $this->error=$object->error;
  415. $error++;
  416. }
  417. if (! $error)
  418. {
  419. }
  420. // End
  421. if (! $error)
  422. {
  423. $this->db->commit();
  424. return $object->id;
  425. }
  426. else
  427. {
  428. $this->db->rollback();
  429. return -1;
  430. }
  431. }
  432. /**
  433. * Initialise object with example values
  434. * Id must be 0 if object instance is a specimen
  435. *
  436. * @return void
  437. */
  438. function initAsSpecimen()
  439. {
  440. $this->id=0;
  441. $this->nom='';
  442. $this->ref='';
  443. $this->products=array();
  444. }
  445. }
  446. ?>