propalmergepdfproduct.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <?php
  2. /* Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2015 Florian HENRY <florian.henry@open-concept.pro>
  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 htdocs/product/class/propalmergepdfproduct.class.php
  20. * \ingroup product
  21. * \brief This file is an CRUD class file (Create/Read/Update/Delete)
  22. */
  23. require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
  24. /**
  25. * Put here description of your class
  26. */
  27. class Propalmergepdfproduct extends CommonObject
  28. {
  29. /**
  30. * @var string ID to identify managed object
  31. */
  32. public $element='propal_merge_pdf_product';
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element='propal_merge_pdf_product';
  37. var $fk_product;
  38. var $file_name;
  39. var $fk_user_author;
  40. var $fk_user_mod;
  41. var $datec='';
  42. var $tms='';
  43. var $lang;
  44. var $lines=array();
  45. /**
  46. * Constructor
  47. *
  48. * @param DoliDb $db Database handler
  49. */
  50. function __construct($db)
  51. {
  52. $this->db = $db;
  53. }
  54. /**
  55. * Create object into database
  56. *
  57. * @param User $user User that creates
  58. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  59. * @return int <0 if KO, Id of created object if OK
  60. */
  61. function create($user, $notrigger=0)
  62. {
  63. global $conf, $langs;
  64. $error=0;
  65. // Clean parameters
  66. if (isset($this->fk_product)) $this->fk_product=trim($this->fk_product);
  67. if (isset($this->file_name)) $this->file_name=trim($this->file_name);
  68. if (isset($this->fk_user_author)) $this->fk_user_author=trim($this->fk_user_author);
  69. if (isset($this->fk_user_mod)) $this->fk_user_mod=trim($this->fk_user_mod);
  70. if (isset($this->lang)) $this->lang=trim($this->lang);
  71. if (isset($this->import_key)) $this->import_key=trim($this->import_key);
  72. // Check parameters
  73. // Put here code to add control on parameters values
  74. // Insert request
  75. $sql = "INSERT INTO ".MAIN_DB_PREFIX."propal_merge_pdf_product(";
  76. $sql.= "fk_product,";
  77. $sql.= "file_name,";
  78. if ($conf->global->MAIN_MULTILANGS) {
  79. $sql.= "lang,";
  80. }
  81. $sql.= "fk_user_author,";
  82. $sql.= "fk_user_mod,";
  83. $sql.= "datec";
  84. $sql.= ") VALUES (";
  85. $sql.= " ".(! isset($this->fk_product)?'NULL':"'".$this->db->escape($this->fk_product)."'").",";
  86. $sql.= " ".(! isset($this->file_name)?'NULL':"'".$this->db->escape($this->file_name)."'").",";
  87. if ($conf->global->MAIN_MULTILANGS) {
  88. $sql.= " ".(! isset($this->lang)?'NULL':"'".$this->db->escape($this->lang)."'").",";
  89. }
  90. $sql.= " ".$user->id.",";
  91. $sql.= " ".$user->id.",";
  92. $sql.= " '".$this->db->idate(dol_now())."'";
  93. $sql.= ")";
  94. $this->db->begin();
  95. dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
  96. $resql=$this->db->query($sql);
  97. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  98. if (! $error)
  99. {
  100. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."propal_merge_pdf_product");
  101. if (! $notrigger)
  102. {
  103. // Uncomment this and change MYOBJECT to your own tag if you
  104. // want this action calls a trigger.
  105. //// Call triggers
  106. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  107. //$interface=new Interfaces($this->db);
  108. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  109. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  110. //// End call triggers
  111. }
  112. }
  113. // Commit or rollback
  114. if ($error)
  115. {
  116. foreach($this->errors as $errmsg)
  117. {
  118. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  119. $this->error.=($this->error?', '.$errmsg:$errmsg);
  120. }
  121. $this->db->rollback();
  122. return -1*$error;
  123. }
  124. else
  125. {
  126. $this->db->commit();
  127. return $this->id;
  128. }
  129. }
  130. /**
  131. * Load object in memory from the database
  132. *
  133. * @param int $id Id object
  134. * @return int <0 if KO, >0 if OK
  135. */
  136. function fetch($id)
  137. {
  138. global $langs,$conf;
  139. $sql = "SELECT";
  140. $sql.= " t.rowid,";
  141. $sql.= " t.fk_product,";
  142. $sql.= " t.file_name,";
  143. $sql.= " t.lang,";
  144. $sql.= " t.fk_user_author,";
  145. $sql.= " t.fk_user_mod,";
  146. $sql.= " t.datec,";
  147. $sql.= " t.tms,";
  148. $sql.= " t.import_key";
  149. $sql.= " FROM ".MAIN_DB_PREFIX."propal_merge_pdf_product as t";
  150. $sql.= " WHERE t.rowid = ".$id;
  151. dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
  152. $resql=$this->db->query($sql);
  153. if ($resql)
  154. {
  155. if ($this->db->num_rows($resql))
  156. {
  157. $obj = $this->db->fetch_object($resql);
  158. $this->id = $obj->rowid;
  159. $this->fk_product = $obj->fk_product;
  160. $this->file_name = $obj->file_name;
  161. if ($conf->global->MAIN_MULTILANGS) {
  162. $this->lang = $obj->lang;
  163. }
  164. $this->fk_user_author = $obj->fk_user_author;
  165. $this->fk_user_mod = $obj->fk_user_mod;
  166. $this->datec = $this->db->jdate($obj->datec);
  167. $this->tms = $this->db->jdate($obj->tms);
  168. $this->import_key = $obj->import_key;
  169. }
  170. $this->db->free($resql);
  171. return 1;
  172. }
  173. else
  174. {
  175. $this->error="Error ".$this->db->lasterror();
  176. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  177. return -1;
  178. }
  179. }
  180. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  181. /**
  182. * Load object in memory from the database
  183. *
  184. * @param int $product_id Id object
  185. * @param string $lang Lang string code
  186. * @return int <0 if KO, >0 if OK
  187. */
  188. function fetch_by_product($product_id, $lang='')
  189. {
  190. // phpcs:enable
  191. global $langs,$conf;
  192. $sql = "SELECT";
  193. $sql.= " t.rowid,";
  194. $sql.= " t.fk_product,";
  195. $sql.= " t.file_name,";
  196. $sql.= " t.lang,";
  197. $sql.= " t.fk_user_author,";
  198. $sql.= " t.fk_user_mod,";
  199. $sql.= " t.datec,";
  200. $sql.= " t.tms,";
  201. $sql.= " t.import_key";
  202. $sql.= " FROM ".MAIN_DB_PREFIX."propal_merge_pdf_product as t";
  203. $sql.= " WHERE t.fk_product = ".$product_id;
  204. if ($conf->global->MAIN_MULTILANGS && !empty($lang)) {
  205. $sql.= " AND t.lang = '".$lang."'";
  206. }
  207. dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
  208. $resql=$this->db->query($sql);
  209. if ($resql)
  210. {
  211. if ($this->db->num_rows($resql))
  212. {
  213. while($obj = $this->db->fetch_object($resql)) {
  214. $line = new PropalmergepdfproductLine();
  215. $line->id = $obj->rowid;
  216. $line->fk_product = $obj->fk_product;
  217. $line->file_name = $obj->file_name;
  218. if ($conf->global->MAIN_MULTILANGS) {
  219. $line->lang = $obj->lang;
  220. }
  221. $line->fk_user_author = $obj->fk_user_author;
  222. $line->fk_user_mod = $obj->fk_user_mod;
  223. $line->datec = $this->db->jdate($obj->datec);
  224. $line->tms = $this->db->jdate($obj->tms);
  225. $line->import_key = $obj->import_key;
  226. if ($conf->global->MAIN_MULTILANGS) {
  227. $this->lines[$obj->file_name.'_'.$obj->lang]=$line;
  228. }else {
  229. $this->lines[$obj->file_name]=$line;
  230. }
  231. }
  232. }
  233. $this->db->free($resql);
  234. return 1;
  235. }
  236. else
  237. {
  238. $this->error="Error ".$this->db->lasterror();
  239. dol_syslog(get_class($this)."::fetch_by_product ".$this->error, LOG_ERR);
  240. return -1;
  241. }
  242. }
  243. /**
  244. * Update object into database
  245. *
  246. * @param User $user User that modifies
  247. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  248. * @return int <0 if KO, >0 if OK
  249. */
  250. function update($user=0, $notrigger=0)
  251. {
  252. global $conf, $langs;
  253. $error=0;
  254. // Clean parameters
  255. if (isset($this->fk_product)) $this->fk_product=trim($this->fk_product);
  256. if (isset($this->file_name)) $this->file_name=trim($this->file_name);
  257. if (isset($this->fk_user_mod)) $this->fk_user_mod=trim($this->fk_user_mod);
  258. if (isset($this->lang)) $this->lang=trim($this->lang);
  259. // Check parameters
  260. // Put here code to add a control on parameters values
  261. // Update request
  262. $sql = "UPDATE ".MAIN_DB_PREFIX."propal_merge_pdf_product SET";
  263. $sql.= " fk_product=".(isset($this->fk_product)?$this->fk_product:"null").",";
  264. $sql.= " file_name=".(isset($this->file_name)?"'".$this->db->escape($this->file_name)."'":"null").",";
  265. if ($conf->global->MAIN_MULTILANGS) {
  266. $sql.= " lang=".(isset($this->lang)?"'".$this->db->escape($this->lang)."'":"null").",";
  267. }
  268. $sql.= " fk_user_mod=".$user->id;
  269. $sql.= " WHERE rowid=".$this->id;
  270. $this->db->begin();
  271. dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
  272. $resql = $this->db->query($sql);
  273. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  274. if (! $error)
  275. {
  276. if (! $notrigger)
  277. {
  278. // Uncomment this and change MYOBJECT to your own tag if you
  279. // want this action calls a trigger.
  280. //// Call triggers
  281. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  282. //$interface=new Interfaces($this->db);
  283. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  284. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  285. //// End call triggers
  286. }
  287. }
  288. // Commit or rollback
  289. if ($error)
  290. {
  291. foreach($this->errors as $errmsg)
  292. {
  293. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  294. $this->error.=($this->error?', '.$errmsg:$errmsg);
  295. }
  296. $this->db->rollback();
  297. return -1*$error;
  298. }
  299. else
  300. {
  301. $this->db->commit();
  302. return 1;
  303. }
  304. }
  305. /**
  306. * Delete object in database
  307. *
  308. * @param User $user User that deletes
  309. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  310. * @return int <0 if KO, >0 if OK
  311. */
  312. function delete($user, $notrigger=0)
  313. {
  314. global $conf, $langs;
  315. $error=0;
  316. $this->db->begin();
  317. if (! $error)
  318. {
  319. if (! $notrigger)
  320. {
  321. // Uncomment this and change MYOBJECT to your own tag if you
  322. // want this action calls a trigger.
  323. //// Call triggers
  324. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  325. //$interface=new Interfaces($this->db);
  326. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  327. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  328. //// End call triggers
  329. }
  330. }
  331. if (! $error)
  332. {
  333. $sql = "DELETE FROM ".MAIN_DB_PREFIX."propal_merge_pdf_product";
  334. $sql.= " WHERE rowid=".$this->id;
  335. dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
  336. $resql = $this->db->query($sql);
  337. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  338. }
  339. // Commit or rollback
  340. if ($error)
  341. {
  342. foreach($this->errors as $errmsg)
  343. {
  344. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  345. $this->error.=($this->error?', '.$errmsg:$errmsg);
  346. }
  347. $this->db->rollback();
  348. return -1*$error;
  349. }
  350. else
  351. {
  352. $this->db->commit();
  353. return 1;
  354. }
  355. }
  356. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  357. /**
  358. * Delete object in database
  359. *
  360. * @param User $user User that deletes
  361. * @param int $product_id product_id
  362. * @param string $lang_id language
  363. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  364. * @return int <0 if KO, >0 if OK
  365. */
  366. function delete_by_product($user, $product_id, $lang_id='', $notrigger=0)
  367. {
  368. // phpcs:enable
  369. global $conf, $langs;
  370. $error=0;
  371. $this->db->begin();
  372. if (! $error)
  373. {
  374. if (! $notrigger)
  375. {
  376. // Uncomment this and change MYOBJECT to your own tag if you
  377. // want this action calls a trigger.
  378. //// Call triggers
  379. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  380. //$interface=new Interfaces($this->db);
  381. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  382. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  383. //// End call triggers
  384. }
  385. }
  386. if (! $error)
  387. {
  388. $sql = "DELETE FROM ".MAIN_DB_PREFIX."propal_merge_pdf_product";
  389. $sql.= " WHERE fk_product=".$product_id;
  390. if ($conf->global->MAIN_MULTILANGS && !empty($lang_id)) {
  391. $sql.= " AND lang='".$lang_id."'";
  392. }
  393. dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
  394. $resql = $this->db->query($sql);
  395. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  396. }
  397. // Commit or rollback
  398. if ($error)
  399. {
  400. foreach($this->errors as $errmsg)
  401. {
  402. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  403. $this->error.=($this->error?', '.$errmsg:$errmsg);
  404. }
  405. $this->db->rollback();
  406. return -1*$error;
  407. }
  408. else
  409. {
  410. $this->db->commit();
  411. return 1;
  412. }
  413. }
  414. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  415. /**
  416. * Delete object in database
  417. *
  418. * @param User $user User that deletes
  419. * @return int <0 if KO, >0 if OK
  420. */
  421. function delete_by_file($user)
  422. {
  423. // phpcs:enable
  424. global $conf, $langs;
  425. $error=0;
  426. $this->db->begin();
  427. if (! $error)
  428. {
  429. if (! $notrigger)
  430. {
  431. // Uncomment this and change MYOBJECT to your own tag if you
  432. // want this action calls a trigger.
  433. //// Call triggers
  434. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  435. //$interface=new Interfaces($this->db);
  436. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  437. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  438. //// End call triggers
  439. }
  440. }
  441. if (! $error)
  442. {
  443. $sql = "DELETE FROM ".MAIN_DB_PREFIX."propal_merge_pdf_product";
  444. $sql.= " WHERE fk_product=".$this->fk_product." AND file_name='".$this->db->escape($this->file_name)."'";
  445. dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
  446. $resql = $this->db->query($sql);
  447. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  448. }
  449. // Commit or rollback
  450. if ($error)
  451. {
  452. foreach($this->errors as $errmsg)
  453. {
  454. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  455. $this->error.=($this->error?', '.$errmsg:$errmsg);
  456. }
  457. $this->db->rollback();
  458. return -1*$error;
  459. }
  460. else
  461. {
  462. $this->db->commit();
  463. return 1;
  464. }
  465. }
  466. /**
  467. * Load an object from its id and create a new one in database
  468. *
  469. * @param int $fromid Id of object to clone
  470. * @return int New id of clone
  471. */
  472. function createFromClone($fromid)
  473. {
  474. global $user,$langs;
  475. $error=0;
  476. $object=new Propalmergepdfproduct($this->db);
  477. $this->db->begin();
  478. // Load source object
  479. $object->fetch($fromid);
  480. $object->id=0;
  481. $object->statut=0;
  482. // Clear fields
  483. // ...
  484. // Create clone
  485. $result=$object->create($user);
  486. // Other options
  487. if ($result < 0)
  488. {
  489. $this->error=$object->error;
  490. $error++;
  491. }
  492. if (! $error)
  493. {
  494. }
  495. // End
  496. if (! $error)
  497. {
  498. $this->db->commit();
  499. return $object->id;
  500. }
  501. else
  502. {
  503. $this->db->rollback();
  504. return -1;
  505. }
  506. }
  507. /**
  508. * Initialise object with example values
  509. * Id must be 0 if object instance is a specimen
  510. *
  511. * @return void
  512. */
  513. function initAsSpecimen()
  514. {
  515. $this->id=0;
  516. $this->fk_product='';
  517. $this->file_name='';
  518. $this->fk_user_author='';
  519. $this->fk_user_mod='';
  520. $this->datec='';
  521. $this->tms='';
  522. $this->import_key='';
  523. }
  524. }
  525. /**
  526. * Class to manage propal merge of product line
  527. */
  528. class PropalmergepdfproductLine
  529. {
  530. /**
  531. * @var int ID
  532. */
  533. public $id;
  534. public $fk_product;
  535. public $file_name;
  536. public $lang;
  537. public $fk_user_author;
  538. public $fk_user_mod;
  539. public $datec='';
  540. public $tms='';
  541. public $import_key;
  542. /**
  543. * Constructor
  544. */
  545. function __construct()
  546. {
  547. return 1;
  548. }
  549. }