propalmergepdfproduct.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 <https://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. public $fk_product;
  38. public $file_name;
  39. public $fk_user_author;
  40. public $fk_user_mod;
  41. public $datec = '';
  42. public $tms = '';
  43. public $lang;
  44. public $lines = array();
  45. /**
  46. * Constructor
  47. *
  48. * @param DoliDb $db Database handler
  49. */
  50. public 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. public function create($user, $notrigger = 0)
  62. {
  63. global $conf, $langs;
  64. $error = 0;
  65. // Clean parameters
  66. if (isset($this->fk_product)) {
  67. $this->fk_product = trim($this->fk_product);
  68. }
  69. if (isset($this->file_name)) {
  70. $this->file_name = trim($this->file_name);
  71. }
  72. if (isset($this->fk_user_author)) {
  73. $this->fk_user_author = trim($this->fk_user_author);
  74. }
  75. if (isset($this->fk_user_mod)) {
  76. $this->fk_user_mod = trim($this->fk_user_mod);
  77. }
  78. if (isset($this->lang)) {
  79. $this->lang = trim($this->lang);
  80. }
  81. if (isset($this->import_key)) {
  82. $this->import_key = trim($this->import_key);
  83. }
  84. // Check parameters
  85. // Put here code to add control on parameters values
  86. // Insert request
  87. $sql = "INSERT INTO ".$this->db->prefix()."propal_merge_pdf_product(";
  88. $sql .= "fk_product,";
  89. $sql .= "file_name,";
  90. if ($conf->global->MAIN_MULTILANGS) {
  91. $sql .= "lang,";
  92. }
  93. $sql .= "fk_user_author,";
  94. $sql .= "fk_user_mod,";
  95. $sql .= "datec";
  96. $sql .= ") VALUES (";
  97. $sql .= " ".(!isset($this->fk_product) ? 'NULL' : ((int) $this->fk_product)).",";
  98. $sql .= " ".(!isset($this->file_name) ? 'NULL' : "'".$this->db->escape($this->file_name)."'").",";
  99. if ($conf->global->MAIN_MULTILANGS) {
  100. $sql .= " ".(!isset($this->lang) ? 'NULL' : "'".$this->db->escape($this->lang)."'").",";
  101. }
  102. $sql .= " ".((int) $user->id).",";
  103. $sql .= " ".((int) $user->id).",";
  104. $sql .= " '".$this->db->idate(dol_now())."'";
  105. $sql .= ")";
  106. $this->db->begin();
  107. $resql = $this->db->query($sql);
  108. if (!$resql) {
  109. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  110. }
  111. if (!$error) {
  112. $this->id = $this->db->last_insert_id($this->db->prefix()."propal_merge_pdf_product");
  113. }
  114. // Commit or rollback
  115. if ($error) {
  116. foreach ($this->errors as $errmsg) {
  117. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  118. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  119. }
  120. $this->db->rollback();
  121. return -1 * $error;
  122. } else {
  123. $this->db->commit();
  124. return $this->id;
  125. }
  126. }
  127. /**
  128. * Load object in memory from the database
  129. *
  130. * @param int $id Id object
  131. * @return int <0 if KO, >0 if OK
  132. */
  133. public function fetch($id)
  134. {
  135. global $langs, $conf;
  136. $sql = "SELECT";
  137. $sql .= " t.rowid,";
  138. $sql .= " t.fk_product,";
  139. $sql .= " t.file_name,";
  140. $sql .= " t.lang,";
  141. $sql .= " t.fk_user_author,";
  142. $sql .= " t.fk_user_mod,";
  143. $sql .= " t.datec,";
  144. $sql .= " t.tms,";
  145. $sql .= " t.import_key";
  146. $sql .= " FROM ".$this->db->prefix()."propal_merge_pdf_product as t";
  147. $sql .= " WHERE t.rowid = ".((int) $id);
  148. dol_syslog(__METHOD__, LOG_DEBUG);
  149. $resql = $this->db->query($sql);
  150. if ($resql) {
  151. if ($this->db->num_rows($resql)) {
  152. $obj = $this->db->fetch_object($resql);
  153. $this->id = $obj->rowid;
  154. $this->fk_product = $obj->fk_product;
  155. $this->file_name = $obj->file_name;
  156. if ($conf->global->MAIN_MULTILANGS) {
  157. $this->lang = $obj->lang;
  158. }
  159. $this->fk_user_author = $obj->fk_user_author;
  160. $this->fk_user_mod = $obj->fk_user_mod;
  161. $this->datec = $this->db->jdate($obj->datec);
  162. $this->tms = $this->db->jdate($obj->tms);
  163. $this->import_key = $obj->import_key;
  164. }
  165. $this->db->free($resql);
  166. return 1;
  167. } else {
  168. $this->error = "Error ".$this->db->lasterror();
  169. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  170. return -1;
  171. }
  172. }
  173. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  174. /**
  175. * Load object in memory from the database
  176. *
  177. * @param int $product_id Id object
  178. * @param string $lang Lang string code
  179. * @return int <0 if KO, >0 if OK
  180. */
  181. public function fetch_by_product($product_id, $lang = '')
  182. {
  183. // phpcs:enable
  184. global $langs, $conf;
  185. $sql = "SELECT";
  186. $sql .= " t.rowid,";
  187. $sql .= " t.fk_product,";
  188. $sql .= " t.file_name,";
  189. $sql .= " t.lang,";
  190. $sql .= " t.fk_user_author,";
  191. $sql .= " t.fk_user_mod,";
  192. $sql .= " t.datec,";
  193. $sql .= " t.tms,";
  194. $sql .= " t.import_key";
  195. $sql .= " FROM ".$this->db->prefix()."propal_merge_pdf_product as t";
  196. $sql .= " WHERE t.fk_product = ".((int) $product_id);
  197. if (!empty($conf->global->MAIN_MULTILANGS) && !empty($lang)) {
  198. $sql .= " AND t.lang = '".$this->db->escape($lang)."'";
  199. }
  200. dol_syslog(__METHOD__, LOG_DEBUG);
  201. $resql = $this->db->query($sql);
  202. if ($resql) {
  203. if ($this->db->num_rows($resql)) {
  204. while ($obj = $this->db->fetch_object($resql)) {
  205. $line = new PropalmergepdfproductLine();
  206. $line->id = $obj->rowid;
  207. $line->fk_product = $obj->fk_product;
  208. $line->file_name = $obj->file_name;
  209. if (!empty($conf->global->MAIN_MULTILANGS)) {
  210. $line->lang = $obj->lang;
  211. }
  212. $line->fk_user_author = $obj->fk_user_author;
  213. $line->fk_user_mod = $obj->fk_user_mod;
  214. $line->datec = $this->db->jdate($obj->datec);
  215. $line->tms = $this->db->jdate($obj->tms);
  216. $line->import_key = $obj->import_key;
  217. if (!empty($conf->global->MAIN_MULTILANGS)) {
  218. $this->lines[$obj->file_name.'_'.$obj->lang] = $line;
  219. } else {
  220. $this->lines[$obj->file_name] = $line;
  221. }
  222. }
  223. }
  224. $this->db->free($resql);
  225. return 1;
  226. } else {
  227. $this->error = "Error ".$this->db->lasterror();
  228. dol_syslog(get_class($this)."::fetch_by_product ".$this->error, LOG_ERR);
  229. return -1;
  230. }
  231. }
  232. /**
  233. * Update object into database
  234. *
  235. * @param User $user User that modifies
  236. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  237. * @return int <0 if KO, >0 if OK
  238. */
  239. public function update($user = 0, $notrigger = 0)
  240. {
  241. global $conf, $langs;
  242. $error = 0;
  243. // Clean parameters
  244. if (isset($this->fk_product)) {
  245. $this->fk_product = trim($this->fk_product);
  246. }
  247. if (isset($this->file_name)) {
  248. $this->file_name = trim($this->file_name);
  249. }
  250. if (isset($this->fk_user_mod)) {
  251. $this->fk_user_mod = trim($this->fk_user_mod);
  252. }
  253. if (isset($this->lang)) {
  254. $this->lang = trim($this->lang);
  255. }
  256. // Check parameters
  257. // Put here code to add a control on parameters values
  258. // Update request
  259. $sql = "UPDATE ".$this->db->prefix()."propal_merge_pdf_product SET";
  260. $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
  261. $sql .= " file_name=".(isset($this->file_name) ? "'".$this->db->escape($this->file_name)."'" : "null").",";
  262. if ($conf->global->MAIN_MULTILANGS) {
  263. $sql .= " lang=".(isset($this->lang) ? "'".$this->db->escape($this->lang)."'" : "null").",";
  264. }
  265. $sql .= " fk_user_mod=".$user->id;
  266. $sql .= " WHERE rowid=".((int) $this->id);
  267. $this->db->begin();
  268. dol_syslog(__METHOD__, LOG_DEBUG);
  269. $resql = $this->db->query($sql);
  270. if (!$resql) {
  271. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  272. }
  273. // Commit or rollback
  274. if ($error) {
  275. foreach ($this->errors as $errmsg) {
  276. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  277. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  278. }
  279. $this->db->rollback();
  280. return -1 * $error;
  281. } else {
  282. $this->db->commit();
  283. return 1;
  284. }
  285. }
  286. /**
  287. * Delete object in database
  288. *
  289. * @param User $user User that deletes
  290. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  291. * @return int <0 if KO, >0 if OK
  292. */
  293. public function delete($user, $notrigger = 0)
  294. {
  295. global $conf, $langs;
  296. $error = 0;
  297. $this->db->begin();
  298. if (!$error) {
  299. $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
  300. $sql .= " WHERE rowid=".((int) $this->id);
  301. dol_syslog(__METHOD__, LOG_DEBUG);
  302. $resql = $this->db->query($sql);
  303. if (!$resql) {
  304. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  305. }
  306. }
  307. // Commit or rollback
  308. if ($error) {
  309. foreach ($this->errors as $errmsg) {
  310. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  311. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  312. }
  313. $this->db->rollback();
  314. return -1 * $error;
  315. } else {
  316. $this->db->commit();
  317. return 1;
  318. }
  319. }
  320. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  321. /**
  322. * Delete object in database
  323. *
  324. * @param User $user User that deletes
  325. * @param int $product_id product_id
  326. * @param string $lang_id language
  327. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  328. * @return int <0 if KO, >0 if OK
  329. */
  330. public function delete_by_product($user, $product_id, $lang_id = '', $notrigger = 0)
  331. {
  332. // phpcs:enable
  333. global $conf, $langs;
  334. $error = 0;
  335. $this->db->begin();
  336. if (!$error) {
  337. $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
  338. $sql .= " WHERE fk_product = ".((int) $product_id);
  339. if ($conf->global->MAIN_MULTILANGS && !empty($lang_id)) {
  340. $sql .= " AND lang = '".$this->db->escape($lang_id)."'";
  341. }
  342. dol_syslog(__METHOD__, LOG_DEBUG);
  343. $resql = $this->db->query($sql);
  344. if (!$resql) {
  345. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  346. }
  347. }
  348. // Commit or rollback
  349. if ($error) {
  350. foreach ($this->errors as $errmsg) {
  351. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  352. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  353. }
  354. $this->db->rollback();
  355. return -1 * $error;
  356. } else {
  357. $this->db->commit();
  358. return 1;
  359. }
  360. }
  361. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  362. /**
  363. * Delete object in database
  364. *
  365. * @param User $user User that deletes
  366. * @return int <0 if KO, >0 if OK
  367. */
  368. public function delete_by_file($user)
  369. {
  370. // phpcs:enable
  371. global $conf, $langs;
  372. $error = 0;
  373. $this->db->begin();
  374. if (!$error) {
  375. $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
  376. $sql .= " WHERE fk_product = ".((int) $this->fk_product)." AND file_name = '".$this->db->escape($this->file_name)."'";
  377. dol_syslog(__METHOD__, LOG_DEBUG);
  378. $resql = $this->db->query($sql);
  379. if (!$resql) {
  380. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  381. }
  382. }
  383. // Commit or rollback
  384. if ($error) {
  385. foreach ($this->errors as $errmsg) {
  386. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  387. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  388. }
  389. $this->db->rollback();
  390. return -1 * $error;
  391. } else {
  392. $this->db->commit();
  393. return 1;
  394. }
  395. }
  396. /**
  397. * Load an object from its id and create a new one in database
  398. *
  399. * @param User $user User making the clone
  400. * @param int $fromid Id of object to clone
  401. * @return int New id of clone
  402. */
  403. public function createFromClone(User $user, $fromid)
  404. {
  405. $error = 0;
  406. $object = new Propalmergepdfproduct($this->db);
  407. $this->db->begin();
  408. // Load source object
  409. $object->fetch($fromid);
  410. $object->id = 0;
  411. $object->statut = 0;
  412. // Clear fields
  413. // ...
  414. // Create clone
  415. $object->context['createfromclone'] = 'createfromclone';
  416. $result = $object->create($user);
  417. // Other options
  418. if ($result < 0) {
  419. $this->error = $object->error;
  420. $this->errors = array_merge($this->errors, $object->errors);
  421. $error++;
  422. }
  423. if (!$error) {
  424. }
  425. unset($object->context['createfromclone']);
  426. // End
  427. if (!$error) {
  428. $this->db->commit();
  429. return $object->id;
  430. } else {
  431. $this->db->rollback();
  432. return -1;
  433. }
  434. }
  435. /**
  436. * Initialise object with example values
  437. * Id must be 0 if object instance is a specimen
  438. *
  439. * @return void
  440. */
  441. public function initAsSpecimen()
  442. {
  443. $this->id = 0;
  444. $this->fk_product = '';
  445. $this->file_name = '';
  446. $this->fk_user_author = '';
  447. $this->fk_user_mod = '';
  448. $this->datec = '';
  449. $this->tms = '';
  450. $this->import_key = '';
  451. }
  452. }
  453. /**
  454. * Class to manage propal merge of product line
  455. */
  456. class PropalmergepdfproductLine
  457. {
  458. /**
  459. * @var int ID
  460. */
  461. public $id;
  462. /**
  463. * @var int ID
  464. */
  465. public $fk_product;
  466. public $file_name;
  467. public $lang;
  468. /**
  469. * @var int ID
  470. */
  471. public $fk_user_author;
  472. /**
  473. * @var int ID
  474. */
  475. public $fk_user_mod;
  476. public $datec = '';
  477. public $tms = '';
  478. public $import_key;
  479. /**
  480. * Constructor
  481. */
  482. public function __construct()
  483. {
  484. return 1;
  485. }
  486. }