dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
27require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
28
29
34{
38 public $element = 'propal_merge_pdf_product';
39
43 public $table_element = 'propal_merge_pdf_product';
44
48 public $fk_product;
49
53 public $file_name;
54
58 public $fk_user_author;
59
63 public $fk_user_mod;
64 public $datec = '';
65
69 public $lang;
70
74 public $lines = array();
75
76
82 public function __construct($db)
83 {
84 $this->db = $db;
85 }
86
87
95 public function create($user, $notrigger = 0)
96 {
97 global $conf, $langs;
98 $error = 0;
99
100 // Clean parameters
101
102 if (isset($this->fk_product)) {
103 $this->fk_product = (int) $this->fk_product;
104 }
105 if (isset($this->file_name)) {
106 $this->file_name = trim($this->file_name);
107 }
108 if (isset($this->fk_user_author)) {
109 $this->fk_user_author = (int) $this->fk_user_author;
110 }
111 if (isset($this->fk_user_mod)) {
112 $this->fk_user_mod = (int) $this->fk_user_mod;
113 }
114 if (isset($this->lang)) {
115 $this->lang = trim($this->lang);
116 }
117 if (isset($this->import_key)) {
118 $this->import_key = trim($this->import_key);
119 }
120
121
122 // Check parameters
123 // Put here code to add control on parameters values
124
125 // Insert request
126 $sql = "INSERT INTO ".$this->db->prefix()."propal_merge_pdf_product(";
127 $sql .= "fk_product,";
128 $sql .= "file_name,";
129 if (getDolGlobalInt('MAIN_MULTILANGS')) {
130 $sql .= "lang,";
131 }
132 $sql .= "fk_user_author,";
133 $sql .= "fk_user_mod,";
134 $sql .= "datec";
135 $sql .= ") VALUES (";
136 $sql .= " ".(!isset($this->fk_product) ? 'NULL' : ((int) $this->fk_product)).",";
137 $sql .= " ".(!isset($this->file_name) ? 'NULL' : "'".$this->db->escape($this->file_name)."'").",";
138 if (getDolGlobalInt('MAIN_MULTILANGS')) {
139 $sql .= " ".(!isset($this->lang) ? 'NULL' : "'".$this->db->escape($this->lang)."'").",";
140 }
141 $sql .= " ".((int) $user->id).",";
142 $sql .= " ".((int) $user->id).",";
143 $sql .= " '".$this->db->idate(dol_now())."'";
144 $sql .= ")";
145
146 $this->db->begin();
147
148 $resql = $this->db->query($sql);
149 if (!$resql) {
150 $error++;
151 $this->errors[] = "Error ".$this->db->lasterror();
152 }
153
154 if (!$error) {
155 $this->id = $this->db->last_insert_id($this->db->prefix()."propal_merge_pdf_product");
156 }
157
158 // Commit or rollback
159 if ($error) {
160 foreach ($this->errors as $errmsg) {
161 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
162 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
163 }
164 $this->db->rollback();
165 return -1 * $error;
166 } else {
167 $this->db->commit();
168 return $this->id;
169 }
170 }
171
172
179 public function fetch($id)
180 {
181 global $langs, $conf;
182
183 $sql = "SELECT";
184 $sql .= " t.rowid,";
185
186 $sql .= " t.fk_product,";
187 $sql .= " t.file_name,";
188 $sql .= " t.lang,";
189 $sql .= " t.fk_user_author,";
190 $sql .= " t.fk_user_mod,";
191 $sql .= " t.datec,";
192 $sql .= " t.tms,";
193 $sql .= " t.import_key";
194
195
196 $sql .= " FROM ".$this->db->prefix()."propal_merge_pdf_product as t";
197 $sql .= " WHERE t.rowid = ".((int) $id);
198
199 dol_syslog(__METHOD__, LOG_DEBUG);
200 $resql = $this->db->query($sql);
201 if ($resql) {
202 if ($this->db->num_rows($resql)) {
203 $obj = $this->db->fetch_object($resql);
204
205 $this->id = $obj->rowid;
206
207 $this->fk_product = $obj->fk_product;
208 $this->file_name = $obj->file_name;
209 if (getDolGlobalInt('MAIN_MULTILANGS')) {
210 $this->lang = $obj->lang;
211 }
212 $this->fk_user_author = $obj->fk_user_author;
213 $this->fk_user_mod = $obj->fk_user_mod;
214 $this->datec = $this->db->jdate($obj->datec);
215 $this->tms = $this->db->jdate($obj->tms);
216 $this->import_key = $obj->import_key;
217 }
218 $this->db->free($resql);
219
220 return 1;
221 } else {
222 $this->error = "Error ".$this->db->lasterror();
223 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
224 return -1;
225 }
226 }
227
228 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
236 public function fetch_by_product($product_id, $lang = '')
237 {
238 // phpcs:enable
239 global $langs, $conf;
240
241 $sql = "SELECT";
242 $sql .= " t.rowid,";
243
244 $sql .= " t.fk_product,";
245 $sql .= " t.file_name,";
246 $sql .= " t.lang,";
247 $sql .= " t.fk_user_author,";
248 $sql .= " t.fk_user_mod,";
249 $sql .= " t.datec,";
250 $sql .= " t.tms,";
251 $sql .= " t.import_key";
252
253
254 $sql .= " FROM ".$this->db->prefix()."propal_merge_pdf_product as t";
255 $sql .= " WHERE t.fk_product = ".((int) $product_id);
256 if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($lang)) {
257 $sql .= " AND t.lang = '".$this->db->escape($lang)."'";
258 }
259
260 dol_syslog(__METHOD__, LOG_DEBUG);
261 $resql = $this->db->query($sql);
262 if ($resql) {
263 if ($this->db->num_rows($resql)) {
264 while ($obj = $this->db->fetch_object($resql)) {
265 $line = new PropalmergepdfproductLine($this->db);
266
267 $line->id = $obj->rowid;
268
269 $line->fk_product = $obj->fk_product;
270 $line->file_name = $obj->file_name;
271 if (getDolGlobalInt('MAIN_MULTILANGS')) {
272 $line->lang = $obj->lang;
273 }
274 $line->fk_user_author = $obj->fk_user_author;
275 $line->fk_user_mod = $obj->fk_user_mod;
276 $line->datec = $this->db->jdate($obj->datec);
277 $line->tms = $this->db->jdate($obj->tms);
278 $line->import_key = $obj->import_key;
279
280
281 if (getDolGlobalInt('MAIN_MULTILANGS')) {
282 $this->lines[$obj->file_name.'_'.$obj->lang] = $line;
283 } else {
284 $this->lines[$obj->file_name] = $line;
285 }
286 }
287 }
288 $this->db->free($resql);
289
290 return 1;
291 } else {
292 $this->error = "Error ".$this->db->lasterror();
293 dol_syslog(get_class($this)."::fetch_by_product ".$this->error, LOG_ERR);
294 return -1;
295 }
296 }
297
298
306 public function update(User $user, $notrigger = 0)
307 {
308 global $conf, $langs;
309 $error = 0;
310
311 // Clean parameters
312
313 if (isset($this->fk_product)) {
314 $this->fk_product = (int) $this->fk_product;
315 }
316 if (isset($this->file_name)) {
317 $this->file_name = trim($this->file_name);
318 }
319 if (isset($this->fk_user_mod)) {
320 $this->fk_user_mod = (int) $this->fk_user_mod;
321 }
322 if (isset($this->lang)) {
323 $this->lang = trim($this->lang);
324 }
325
326 // Check parameters
327 // Put here code to add a control on parameters values
328
329 // Update request
330 $sql = "UPDATE ".$this->db->prefix()."propal_merge_pdf_product SET";
331
332 $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
333 $sql .= " file_name=".(isset($this->file_name) ? "'".$this->db->escape($this->file_name)."'" : "null").",";
334 if (getDolGlobalInt('MAIN_MULTILANGS')) {
335 $sql .= " lang=".(isset($this->lang) ? "'".$this->db->escape($this->lang)."'" : "null").",";
336 }
337 $sql .= " fk_user_mod=".$user->id;
338
339
340 $sql .= " WHERE rowid=".((int) $this->id);
341
342 $this->db->begin();
343
344 dol_syslog(__METHOD__, LOG_DEBUG);
345 $resql = $this->db->query($sql);
346 if (!$resql) {
347 $error++;
348 $this->errors[] = "Error ".$this->db->lasterror();
349 }
350
351 // Commit or rollback
352 if ($error) {
353 foreach ($this->errors as $errmsg) {
354 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
355 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
356 }
357 $this->db->rollback();
358 return -1 * $error;
359 } else {
360 $this->db->commit();
361 return 1;
362 }
363 }
364
365
373 public function delete($user, $notrigger = 0)
374 {
375 global $conf, $langs;
376 $error = 0;
377
378 $this->db->begin();
379
380 if (!$error) {
381 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
382 $sql .= " WHERE rowid=".((int) $this->id);
383
384 dol_syslog(__METHOD__, LOG_DEBUG);
385 $resql = $this->db->query($sql);
386 if (!$resql) {
387 $error++;
388 $this->errors[] = "Error ".$this->db->lasterror();
389 }
390 }
391
392 // Commit or rollback
393 if ($error) {
394 foreach ($this->errors as $errmsg) {
395 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
396 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
397 }
398 $this->db->rollback();
399 return -1 * $error;
400 } else {
401 $this->db->commit();
402 return 1;
403 }
404 }
405
406 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
416 public function delete_by_product($user, $product_id, $lang_id = '', $notrigger = 0)
417 {
418 // phpcs:enable
419 global $conf, $langs;
420 $error = 0;
421
422 $this->db->begin();
423
424 if (!$error) {
425 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
426 $sql .= " WHERE fk_product = ".((int) $product_id);
427
428 if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($lang_id)) {
429 $sql .= " AND lang = '".$this->db->escape($lang_id)."'";
430 }
431
432 dol_syslog(__METHOD__, LOG_DEBUG);
433 $resql = $this->db->query($sql);
434 if (!$resql) {
435 $error++;
436 $this->errors[] = "Error ".$this->db->lasterror();
437 }
438 }
439
440 // Commit or rollback
441 if ($error) {
442 foreach ($this->errors as $errmsg) {
443 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
444 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
445 }
446 $this->db->rollback();
447 return -1 * $error;
448 } else {
449 $this->db->commit();
450 return 1;
451 }
452 }
453
454 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
461 public function delete_by_file($user)
462 {
463 // phpcs:enable
464 global $conf, $langs;
465 $error = 0;
466
467 $this->db->begin();
468
469 if (!$error) {
470 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
471 $sql .= " WHERE fk_product = ".((int) $this->fk_product)." AND file_name = '".$this->db->escape($this->file_name)."'";
472
473 dol_syslog(__METHOD__, LOG_DEBUG);
474 $resql = $this->db->query($sql);
475 if (!$resql) {
476 $error++;
477 $this->errors[] = "Error ".$this->db->lasterror();
478 }
479 }
480
481 // Commit or rollback
482 if ($error) {
483 foreach ($this->errors as $errmsg) {
484 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
485 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
486 }
487 $this->db->rollback();
488 return -1 * $error;
489 } else {
490 $this->db->commit();
491 return 1;
492 }
493 }
494
495
496
504 public function createFromClone(User $user, $fromid)
505 {
506 $error = 0;
507
508 $object = new Propalmergepdfproduct($this->db);
509
510 $this->db->begin();
511
512 // Load source object
513 $object->fetch($fromid);
514 $object->id = 0;
515 $object->statut = 0;
516
517 // Clear fields
518 // ...
519
520 // Create clone
521 $object->context['createfromclone'] = 'createfromclone';
522 $result = $object->create($user);
523
524 // Other options
525 if ($result < 0) {
526 $this->error = $object->error;
527 $this->errors = array_merge($this->errors, $object->errors);
528 $error++;
529 }
530
531 if (!$error) {
532 }
533
534 unset($object->context['createfromclone']);
535
536 // End
537 if (!$error) {
538 $this->db->commit();
539 return $object->id;
540 } else {
541 $this->db->rollback();
542 return -1;
543 }
544 }
545
546
553 public function initAsSpecimen()
554 {
555 $this->id = 0;
556
557 $this->fk_product = 0;
558 $this->file_name = '';
559 $this->fk_user_author = 0;
560 $this->fk_user_mod = 0;
561 $this->datec = '';
562 $this->tms = dol_now();
563 $this->import_key = '';
564
565 return 1;
566 }
567}
568
573{
577 public $id;
578
582 public $fk_product;
583
587 public $file_name;
588
592 public $lang;
593
597 public $fk_user_author;
598
602 public $fk_user_mod;
603
604 public $datec = '';
605
606 public $import_key;
607}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
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.
update(User $user, $notrigger=0)
Update object into database.
delete_by_file($user)
Delete object in 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 a Dolibarr global constant int value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.