dolibarr 25.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-2025 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
28require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
29
30
35{
39 public $element = 'propal_merge_pdf_product';
40
44 public $table_element = 'propal_merge_pdf_product';
45
49 public $fk_product;
50
54 public $file_name;
55
59 public $fk_user_author;
60
64 public $fk_user_mod;
68 public $datec = '';
69
73 public $lang;
74
78 public $lines = array();
79
80
86 public function __construct($db)
87 {
88 $this->db = $db;
89 }
90
91
99 public function create($user, $notrigger = 0)
100 {
101 global $conf, $langs;
102 $error = 0;
103
104 // Clean parameters
105
106 if (isset($this->fk_product)) {
107 $this->fk_product = (int) $this->fk_product;
108 }
109 if (isset($this->file_name)) {
110 $this->file_name = trim($this->file_name);
111 }
112 if (isset($this->fk_user_author)) {
113 $this->fk_user_author = (int) $this->fk_user_author;
114 }
115 if (isset($this->fk_user_mod)) {
116 $this->fk_user_mod = (int) $this->fk_user_mod;
117 }
118 if (isset($this->lang)) {
119 $this->lang = trim($this->lang);
120 }
121 if (isset($this->import_key)) {
122 $this->import_key = trim($this->import_key);
123 }
124
125
126 // Check parameters
127 // Put here code to add control on parameters values
128
129 // Insert request
130 $sql = "INSERT INTO ".$this->db->prefix()."propal_merge_pdf_product(";
131 $sql .= "fk_product,";
132 $sql .= "file_name,";
133 if (getDolGlobalInt('MAIN_MULTILANGS')) {
134 $sql .= "lang,";
135 }
136 $sql .= "fk_user_author,";
137 $sql .= "fk_user_mod,";
138 $sql .= "datec";
139 $sql .= ") VALUES (";
140 $sql .= " ".(!isset($this->fk_product) ? 'NULL' : ((int) $this->fk_product)).",";
141 $sql .= " ".(!isset($this->file_name) ? 'NULL' : "'".$this->db->escape($this->file_name)."'").",";
142 if (getDolGlobalInt('MAIN_MULTILANGS')) {
143 $sql .= " ".(!isset($this->lang) ? 'NULL' : "'".$this->db->escape($this->lang)."'").",";
144 }
145 $sql .= " ".((int) $user->id).",";
146 $sql .= " ".((int) $user->id).",";
147 $sql .= " '".$this->db->idate(dol_now())."'";
148 $sql .= ")";
149
150 $this->db->begin();
151
152 $resql = $this->db->query($sql);
153 if (!$resql) {
154 $error++;
155 $this->errors[] = "Error ".$this->db->lasterror();
156 }
157
158 if (!$error) {
159 $this->id = $this->db->last_insert_id($this->db->prefix()."propal_merge_pdf_product");
160 }
161
162 // Commit or rollback
163 if ($error) {
164 foreach ($this->errors as $errmsg) {
165 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
166 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
167 }
168 $this->db->rollback();
169 return -1 * $error;
170 } else {
171 $this->db->commit();
172 return $this->id;
173 }
174 }
175
176
183 public function fetch($id)
184 {
185 global $langs, $conf;
186
187 $sql = "SELECT";
188 $sql .= " t.rowid,";
189
190 $sql .= " t.fk_product,";
191 $sql .= " t.file_name,";
192 $sql .= " t.lang,";
193 $sql .= " t.fk_user_author,";
194 $sql .= " t.fk_user_mod,";
195 $sql .= " t.datec,";
196 $sql .= " t.tms,";
197 $sql .= " t.import_key";
198
199
200 $sql .= " FROM ".$this->db->prefix()."propal_merge_pdf_product as t";
201 $sql .= " WHERE t.rowid = ".((int) $id);
202
203 dol_syslog(__METHOD__, LOG_DEBUG);
204 $resql = $this->db->query($sql);
205 if ($resql) {
206 if ($this->db->num_rows($resql)) {
207 $obj = $this->db->fetch_object($resql);
208
209 $this->id = $obj->rowid;
210
211 $this->fk_product = $obj->fk_product;
212 $this->file_name = $obj->file_name;
213 if (getDolGlobalInt('MAIN_MULTILANGS')) {
214 $this->lang = $obj->lang;
215 }
216 $this->fk_user_author = $obj->fk_user_author;
217 $this->fk_user_mod = $obj->fk_user_mod;
218 $this->datec = $this->db->jdate($obj->datec);
219 $this->tms = $this->db->jdate($obj->tms);
220 $this->import_key = $obj->import_key;
221 }
222 $this->db->free($resql);
223
224 return 1;
225 } else {
226 $this->error = "Error ".$this->db->lasterror();
227 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
228 return -1;
229 }
230 }
231
232 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
240 public function fetch_by_product($product_id, $lang = '')
241 {
242 // phpcs:enable
243 global $langs, $conf;
244
245 $sql = "SELECT";
246 $sql .= " t.rowid,";
247
248 $sql .= " t.fk_product,";
249 $sql .= " t.file_name,";
250 $sql .= " t.lang,";
251 $sql .= " t.fk_user_author,";
252 $sql .= " t.fk_user_mod,";
253 $sql .= " t.datec,";
254 $sql .= " t.tms,";
255 $sql .= " t.import_key";
256
257
258 $sql .= " FROM ".$this->db->prefix()."propal_merge_pdf_product as t";
259 $sql .= " WHERE t.fk_product = ".((int) $product_id);
260 if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($lang)) {
261 $sql .= " AND t.lang = '".$this->db->escape($lang)."'";
262 }
263
264 dol_syslog(__METHOD__, LOG_DEBUG);
265 $resql = $this->db->query($sql);
266 if ($resql) {
267 if ($this->db->num_rows($resql)) {
268 while ($obj = $this->db->fetch_object($resql)) {
269 $line = new PropalmergepdfproductLine($this->db);
270
271 $line->id = $obj->rowid;
272
273 $line->fk_product = $obj->fk_product;
274 $line->file_name = $obj->file_name;
275 if (getDolGlobalInt('MAIN_MULTILANGS')) {
276 $line->lang = $obj->lang;
277 }
278 $line->fk_user_author = $obj->fk_user_author;
279 $line->fk_user_mod = $obj->fk_user_mod;
280 $line->datec = $this->db->jdate($obj->datec);
281 $line->tms = $this->db->jdate($obj->tms);
282 $line->import_key = $obj->import_key;
283
284
285 if (getDolGlobalInt('MAIN_MULTILANGS')) {
286 $this->lines[$obj->file_name.'_'.$obj->lang] = $line;
287 } else {
288 $this->lines[$obj->file_name] = $line;
289 }
290 }
291 }
292 $this->db->free($resql);
293
294 return 1;
295 } else {
296 $this->error = "Error ".$this->db->lasterror();
297 dol_syslog(get_class($this)."::fetch_by_product ".$this->error, LOG_ERR);
298 return -1;
299 }
300 }
301
302
310 public function update(User $user, $notrigger = 0)
311 {
312 global $conf, $langs;
313 $error = 0;
314
315 // Clean parameters
316
317 if (isset($this->fk_product)) {
318 $this->fk_product = (int) $this->fk_product;
319 }
320 if (isset($this->file_name)) {
321 $this->file_name = trim($this->file_name);
322 }
323 if (isset($this->fk_user_mod)) {
324 $this->fk_user_mod = (int) $this->fk_user_mod;
325 }
326 if (isset($this->lang)) {
327 $this->lang = trim($this->lang);
328 }
329
330 // Check parameters
331 // Put here code to add a control on parameters values
332
333 // Update request
334 $sql = "UPDATE ".$this->db->prefix()."propal_merge_pdf_product SET";
335
336 $sql .= " fk_product = ".(isset($this->fk_product) ? ((int) $this->fk_product) : "null").",";
337 $sql .= " file_name = ".(isset($this->file_name) ? "'".$this->db->escape($this->file_name)."'" : "null").",";
338 if (getDolGlobalInt('MAIN_MULTILANGS')) {
339 $sql .= " lang = ".(isset($this->lang) ? "'".$this->db->escape($this->lang)."'" : "null").",";
340 }
341 $sql .= " fk_user_mod = ".((int) $user->id);
342
343
344 $sql .= " WHERE rowid = ".((int) $this->id);
345
346 $this->db->begin();
347
348 dol_syslog(__METHOD__, LOG_DEBUG);
349 $resql = $this->db->query($sql);
350 if (!$resql) {
351 $error++;
352 $this->errors[] = "Error ".$this->db->lasterror();
353 }
354
355 // Commit or rollback
356 if ($error) {
357 foreach ($this->errors as $errmsg) {
358 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
359 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
360 }
361 $this->db->rollback();
362 return -1 * $error;
363 } else {
364 $this->db->commit();
365 return 1;
366 }
367 }
368
369
377 public function delete($user, $notrigger = 0)
378 {
379 global $conf, $langs;
380 $error = 0;
381
382 $this->db->begin();
383
384 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
385 $sql .= " WHERE rowid=".((int) $this->id);
386
387 dol_syslog(__METHOD__, LOG_DEBUG);
388 $resql = $this->db->query($sql);
389 if (!$resql) {
390 $error++;
391 $this->errors[] = "Error ".$this->db->lasterror();
392 }
393
394 // Commit or rollback
395 if ($error) {
396 foreach ($this->errors as $errmsg) {
397 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
398 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
399 }
400 $this->db->rollback();
401 return -1 * $error;
402 } else {
403 $this->db->commit();
404 return 1;
405 }
406 }
407
408 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
418 public function delete_by_product($user, $product_id, $lang_id = '', $notrigger = 0)
419 {
420 // phpcs:enable
421 global $conf, $langs;
422 $error = 0;
423
424 $this->db->begin();
425
426 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
427 $sql .= " WHERE fk_product = ".((int) $product_id);
428
429 if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($lang_id)) {
430 $sql .= " AND lang = '".$this->db->escape($lang_id)."'";
431 }
432
433 dol_syslog(__METHOD__, LOG_DEBUG);
434 $resql = $this->db->query($sql);
435 if (!$resql) {
436 $error++;
437 $this->errors[] = "Error ".$this->db->lasterror();
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 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
470 $sql .= " WHERE fk_product = ".((int) $this->fk_product)." AND file_name = '".$this->db->escape($this->file_name)."'";
471
472 dol_syslog(__METHOD__, LOG_DEBUG);
473 $resql = $this->db->query($sql);
474 if (!$resql) {
475 $error++;
476 $this->errors[] = "Error ".$this->db->lasterror();
477 }
478
479 // Commit or rollback
480 if ($error) {
481 foreach ($this->errors as $errmsg) {
482 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
483 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
484 }
485 $this->db->rollback();
486 return -1 * $error;
487 } else {
488 $this->db->commit();
489 return 1;
490 }
491 }
492
493
494
502 public function createFromClone(User $user, $fromid)
503 {
504 $error = 0;
505
506 $object = new Propalmergepdfproduct($this->db);
507
508 $this->db->begin();
509
510 // Load source object
511 $object->fetch($fromid);
512 $object->id = 0;
513 $object->statut = 0;
514
515 // Clear fields
516 // ...
517
518 // Create clone
519 $object->context['createfromclone'] = 'createfromclone';
520 $result = $object->create($user);
521
522 // Other options
523 if ($result < 0) {
524 $this->error = $object->error;
525 $this->errors = array_merge($this->errors, $object->errors);
526 $error++;
527 }
528
529 if (!$error) {
530 }
531
532 unset($object->context['createfromclone']);
533
534 // End
535 if (!$error) {
536 $this->db->commit();
537 return $object->id;
538 } else {
539 $this->db->rollback();
540 return -1;
541 }
542 }
543
544
551 public function initAsSpecimen()
552 {
553 $this->id = 0;
554
555 $this->fk_product = 0;
556 $this->file_name = '';
557 $this->fk_user_author = 0;
558 $this->fk_user_mod = 0;
559 $this->datec = '';
560 $this->tms = dol_now();
561 $this->import_key = '';
562
563 return 1;
564 }
565}
566
571{
575 public $id;
576
580 public $fk_product;
581
585 public $file_name;
586
590 public $lang;
591
595 public $fk_user_author;
596
600 public $fk_user_mod;
601
605 public $datec = '';
606
610 public $import_key;
611}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_now($mode='gmt')
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.