propalmergepdfproduct.class.php 14 KB

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