dolibarr 18.0.6
propalmergepdfproduct.class.php
Go to the documentation of this file.
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
25require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
26
27
28
33{
37 public $element = 'propal_merge_pdf_product';
38
42 public $table_element = 'propal_merge_pdf_product';
43
44 public $fk_product;
45 public $file_name;
46 public $fk_user_author;
47 public $fk_user_mod;
48 public $datec = '';
49 public $tms = '';
50 public $lang;
51
52 public $lines = array();
53
54
55
56
62 public function __construct($db)
63 {
64 $this->db = $db;
65 }
66
67
75 public function create($user, $notrigger = 0)
76 {
77 global $conf, $langs;
78 $error = 0;
79
80 // Clean parameters
81
82 if (isset($this->fk_product)) {
83 $this->fk_product = trim($this->fk_product);
84 }
85 if (isset($this->file_name)) {
86 $this->file_name = trim($this->file_name);
87 }
88 if (isset($this->fk_user_author)) {
89 $this->fk_user_author = trim($this->fk_user_author);
90 }
91 if (isset($this->fk_user_mod)) {
92 $this->fk_user_mod = trim($this->fk_user_mod);
93 }
94 if (isset($this->lang)) {
95 $this->lang = trim($this->lang);
96 }
97 if (isset($this->import_key)) {
98 $this->import_key = trim($this->import_key);
99 }
100
101
102
103 // Check parameters
104 // Put here code to add control on parameters values
105
106 // Insert request
107 $sql = "INSERT INTO ".$this->db->prefix()."propal_merge_pdf_product(";
108 $sql .= "fk_product,";
109 $sql .= "file_name,";
110 if (getDolGlobalInt('MAIN_MULTILANGS')) {
111 $sql .= "lang,";
112 }
113 $sql .= "fk_user_author,";
114 $sql .= "fk_user_mod,";
115 $sql .= "datec";
116 $sql .= ") VALUES (";
117 $sql .= " ".(!isset($this->fk_product) ? 'NULL' : ((int) $this->fk_product)).",";
118 $sql .= " ".(!isset($this->file_name) ? 'NULL' : "'".$this->db->escape($this->file_name)."'").",";
119 if (getDolGlobalInt('MAIN_MULTILANGS')) {
120 $sql .= " ".(!isset($this->lang) ? 'NULL' : "'".$this->db->escape($this->lang)."'").",";
121 }
122 $sql .= " ".((int) $user->id).",";
123 $sql .= " ".((int) $user->id).",";
124 $sql .= " '".$this->db->idate(dol_now())."'";
125 $sql .= ")";
126
127 $this->db->begin();
128
129 $resql = $this->db->query($sql);
130 if (!$resql) {
131 $error++; $this->errors[] = "Error ".$this->db->lasterror();
132 }
133
134 if (!$error) {
135 $this->id = $this->db->last_insert_id($this->db->prefix()."propal_merge_pdf_product");
136 }
137
138 // Commit or rollback
139 if ($error) {
140 foreach ($this->errors as $errmsg) {
141 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
142 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
143 }
144 $this->db->rollback();
145 return -1 * $error;
146 } else {
147 $this->db->commit();
148 return $this->id;
149 }
150 }
151
152
159 public function fetch($id)
160 {
161 global $langs, $conf;
162
163 $sql = "SELECT";
164 $sql .= " t.rowid,";
165
166 $sql .= " t.fk_product,";
167 $sql .= " t.file_name,";
168 $sql .= " t.lang,";
169 $sql .= " t.fk_user_author,";
170 $sql .= " t.fk_user_mod,";
171 $sql .= " t.datec,";
172 $sql .= " t.tms,";
173 $sql .= " t.import_key";
174
175
176 $sql .= " FROM ".$this->db->prefix()."propal_merge_pdf_product as t";
177 $sql .= " WHERE t.rowid = ".((int) $id);
178
179 dol_syslog(__METHOD__, LOG_DEBUG);
180 $resql = $this->db->query($sql);
181 if ($resql) {
182 if ($this->db->num_rows($resql)) {
183 $obj = $this->db->fetch_object($resql);
184
185 $this->id = $obj->rowid;
186
187 $this->fk_product = $obj->fk_product;
188 $this->file_name = $obj->file_name;
189 if (getDolGlobalInt('MAIN_MULTILANGS')) {
190 $this->lang = $obj->lang;
191 }
192 $this->fk_user_author = $obj->fk_user_author;
193 $this->fk_user_mod = $obj->fk_user_mod;
194 $this->datec = $this->db->jdate($obj->datec);
195 $this->tms = $this->db->jdate($obj->tms);
196 $this->import_key = $obj->import_key;
197 }
198 $this->db->free($resql);
199
200 return 1;
201 } else {
202 $this->error = "Error ".$this->db->lasterror();
203 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
204 return -1;
205 }
206 }
207
208 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
216 public function fetch_by_product($product_id, $lang = '')
217 {
218 // phpcs:enable
219 global $langs, $conf;
220
221 $sql = "SELECT";
222 $sql .= " t.rowid,";
223
224 $sql .= " t.fk_product,";
225 $sql .= " t.file_name,";
226 $sql .= " t.lang,";
227 $sql .= " t.fk_user_author,";
228 $sql .= " t.fk_user_mod,";
229 $sql .= " t.datec,";
230 $sql .= " t.tms,";
231 $sql .= " t.import_key";
232
233
234 $sql .= " FROM ".$this->db->prefix()."propal_merge_pdf_product as t";
235 $sql .= " WHERE t.fk_product = ".((int) $product_id);
236 if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($lang)) {
237 $sql .= " AND t.lang = '".$this->db->escape($lang)."'";
238 }
239
240 dol_syslog(__METHOD__, LOG_DEBUG);
241 $resql = $this->db->query($sql);
242 if ($resql) {
243 if ($this->db->num_rows($resql)) {
244 while ($obj = $this->db->fetch_object($resql)) {
245 $line = new PropalmergepdfproductLine();
246
247 $line->id = $obj->rowid;
248
249 $line->fk_product = $obj->fk_product;
250 $line->file_name = $obj->file_name;
251 if (getDolGlobalInt('MAIN_MULTILANGS')) {
252 $line->lang = $obj->lang;
253 }
254 $line->fk_user_author = $obj->fk_user_author;
255 $line->fk_user_mod = $obj->fk_user_mod;
256 $line->datec = $this->db->jdate($obj->datec);
257 $line->tms = $this->db->jdate($obj->tms);
258 $line->import_key = $obj->import_key;
259
260
261 if (getDolGlobalInt('MAIN_MULTILANGS')) {
262 $this->lines[$obj->file_name.'_'.$obj->lang] = $line;
263 } else {
264 $this->lines[$obj->file_name] = $line;
265 }
266 }
267 }
268 $this->db->free($resql);
269
270 return 1;
271 } else {
272 $this->error = "Error ".$this->db->lasterror();
273 dol_syslog(get_class($this)."::fetch_by_product ".$this->error, LOG_ERR);
274 return -1;
275 }
276 }
277
278
286 public function update($user = 0, $notrigger = 0)
287 {
288 global $conf, $langs;
289 $error = 0;
290
291 // Clean parameters
292
293 if (isset($this->fk_product)) {
294 $this->fk_product = trim($this->fk_product);
295 }
296 if (isset($this->file_name)) {
297 $this->file_name = trim($this->file_name);
298 }
299 if (isset($this->fk_user_mod)) {
300 $this->fk_user_mod = trim($this->fk_user_mod);
301 }
302 if (isset($this->lang)) {
303 $this->lang = trim($this->lang);
304 }
305
306 // Check parameters
307 // Put here code to add a control on parameters values
308
309 // Update request
310 $sql = "UPDATE ".$this->db->prefix()."propal_merge_pdf_product SET";
311
312 $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
313 $sql .= " file_name=".(isset($this->file_name) ? "'".$this->db->escape($this->file_name)."'" : "null").",";
314 if (getDolGlobalInt('MAIN_MULTILANGS')) {
315 $sql .= " lang=".(isset($this->lang) ? "'".$this->db->escape($this->lang)."'" : "null").",";
316 }
317 $sql .= " fk_user_mod=".$user->id;
318
319
320 $sql .= " WHERE rowid=".((int) $this->id);
321
322 $this->db->begin();
323
324 dol_syslog(__METHOD__, LOG_DEBUG);
325 $resql = $this->db->query($sql);
326 if (!$resql) {
327 $error++; $this->errors[] = "Error ".$this->db->lasterror();
328 }
329
330 // Commit or rollback
331 if ($error) {
332 foreach ($this->errors as $errmsg) {
333 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
334 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
335 }
336 $this->db->rollback();
337 return -1 * $error;
338 } else {
339 $this->db->commit();
340 return 1;
341 }
342 }
343
344
352 public function delete($user, $notrigger = 0)
353 {
354 global $conf, $langs;
355 $error = 0;
356
357 $this->db->begin();
358
359 if (!$error) {
360 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
361 $sql .= " WHERE rowid=".((int) $this->id);
362
363 dol_syslog(__METHOD__, LOG_DEBUG);
364 $resql = $this->db->query($sql);
365 if (!$resql) {
366 $error++; $this->errors[] = "Error ".$this->db->lasterror();
367 }
368 }
369
370 // Commit or rollback
371 if ($error) {
372 foreach ($this->errors as $errmsg) {
373 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
374 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
375 }
376 $this->db->rollback();
377 return -1 * $error;
378 } else {
379 $this->db->commit();
380 return 1;
381 }
382 }
383
384 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
394 public function delete_by_product($user, $product_id, $lang_id = '', $notrigger = 0)
395 {
396 // phpcs:enable
397 global $conf, $langs;
398 $error = 0;
399
400 $this->db->begin();
401
402 if (!$error) {
403 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
404 $sql .= " WHERE fk_product = ".((int) $product_id);
405
406 if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($lang_id)) {
407 $sql .= " AND lang = '".$this->db->escape($lang_id)."'";
408 }
409
410 dol_syslog(__METHOD__, LOG_DEBUG);
411 $resql = $this->db->query($sql);
412 if (!$resql) {
413 $error++; $this->errors[] = "Error ".$this->db->lasterror();
414 }
415 }
416
417 // Commit or rollback
418 if ($error) {
419 foreach ($this->errors as $errmsg) {
420 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
421 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
422 }
423 $this->db->rollback();
424 return -1 * $error;
425 } else {
426 $this->db->commit();
427 return 1;
428 }
429 }
430
431 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
438 public function delete_by_file($user)
439 {
440 // phpcs:enable
441 global $conf, $langs;
442 $error = 0;
443
444 $this->db->begin();
445
446 if (!$error) {
447 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
448 $sql .= " WHERE fk_product = ".((int) $this->fk_product)." AND file_name = '".$this->db->escape($this->file_name)."'";
449
450 dol_syslog(__METHOD__, LOG_DEBUG);
451 $resql = $this->db->query($sql);
452 if (!$resql) {
453 $error++; $this->errors[] = "Error ".$this->db->lasterror();
454 }
455 }
456
457 // Commit or rollback
458 if ($error) {
459 foreach ($this->errors as $errmsg) {
460 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
461 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
462 }
463 $this->db->rollback();
464 return -1 * $error;
465 } else {
466 $this->db->commit();
467 return 1;
468 }
469 }
470
471
472
480 public function createFromClone(User $user, $fromid)
481 {
482 $error = 0;
483
484 $object = new Propalmergepdfproduct($this->db);
485
486 $this->db->begin();
487
488 // Load source object
489 $object->fetch($fromid);
490 $object->id = 0;
491 $object->statut = 0;
492
493 // Clear fields
494 // ...
495
496 // Create clone
497 $object->context['createfromclone'] = 'createfromclone';
498 $result = $object->create($user);
499
500 // Other options
501 if ($result < 0) {
502 $this->error = $object->error;
503 $this->errors = array_merge($this->errors, $object->errors);
504 $error++;
505 }
506
507 if (!$error) {
508 }
509
510 unset($object->context['createfromclone']);
511
512 // End
513 if (!$error) {
514 $this->db->commit();
515 return $object->id;
516 } else {
517 $this->db->rollback();
518 return -1;
519 }
520 }
521
522
529 public function initAsSpecimen()
530 {
531 $this->id = 0;
532
533 $this->fk_product = '';
534 $this->file_name = '';
535 $this->fk_user_author = '';
536 $this->fk_user_mod = '';
537 $this->datec = '';
538 $this->tms = '';
539 $this->import_key = '';
540 }
541}
542
547{
551 public $id;
552
556 public $fk_product;
557
558 public $file_name;
559 public $lang;
560
564 public $fk_user_author;
565
569 public $fk_user_mod;
570
571 public $datec = '';
572 public $tms = '';
573 public $import_key;
574
578 public function __construct()
579 {
580 return 1;
581 }
582}
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Put here description of your class.
create($user, $notrigger=0)
Create object into database.
fetch_by_product($product_id, $lang='')
Load object in memory from the database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetch($id)
Load object in memory from the database.
delete_by_file($user)
Delete object in database.
update($user=0, $notrigger=0)
Update object into database.
delete_by_product($user, $product_id, $lang_id='', $notrigger=0)
Delete object in database.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Class to manage propal merge of product line.
Class to manage Dolibarr users.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.