dolibarr 21.0.0-beta
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 * Copyright (C) 2024 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) ? $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 if (!$error) {
385 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
386 $sql .= " WHERE rowid=".((int) $this->id);
387
388 dol_syslog(__METHOD__, LOG_DEBUG);
389 $resql = $this->db->query($sql);
390 if (!$resql) {
391 $error++;
392 $this->errors[] = "Error ".$this->db->lasterror();
393 }
394 }
395
396 // Commit or rollback
397 if ($error) {
398 foreach ($this->errors as $errmsg) {
399 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
400 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
401 }
402 $this->db->rollback();
403 return -1 * $error;
404 } else {
405 $this->db->commit();
406 return 1;
407 }
408 }
409
410 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
420 public function delete_by_product($user, $product_id, $lang_id = '', $notrigger = 0)
421 {
422 // phpcs:enable
423 global $conf, $langs;
424 $error = 0;
425
426 $this->db->begin();
427
428 if (!$error) {
429 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
430 $sql .= " WHERE fk_product = ".((int) $product_id);
431
432 if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($lang_id)) {
433 $sql .= " AND lang = '".$this->db->escape($lang_id)."'";
434 }
435
436 dol_syslog(__METHOD__, LOG_DEBUG);
437 $resql = $this->db->query($sql);
438 if (!$resql) {
439 $error++;
440 $this->errors[] = "Error ".$this->db->lasterror();
441 }
442 }
443
444 // Commit or rollback
445 if ($error) {
446 foreach ($this->errors as $errmsg) {
447 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
448 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
449 }
450 $this->db->rollback();
451 return -1 * $error;
452 } else {
453 $this->db->commit();
454 return 1;
455 }
456 }
457
458 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
465 public function delete_by_file($user)
466 {
467 // phpcs:enable
468 global $conf, $langs;
469 $error = 0;
470
471 $this->db->begin();
472
473 if (!$error) {
474 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
475 $sql .= " WHERE fk_product = ".((int) $this->fk_product)." AND file_name = '".$this->db->escape($this->file_name)."'";
476
477 dol_syslog(__METHOD__, LOG_DEBUG);
478 $resql = $this->db->query($sql);
479 if (!$resql) {
480 $error++;
481 $this->errors[] = "Error ".$this->db->lasterror();
482 }
483 }
484
485 // Commit or rollback
486 if ($error) {
487 foreach ($this->errors as $errmsg) {
488 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
489 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
490 }
491 $this->db->rollback();
492 return -1 * $error;
493 } else {
494 $this->db->commit();
495 return 1;
496 }
497 }
498
499
500
508 public function createFromClone(User $user, $fromid)
509 {
510 $error = 0;
511
512 $object = new Propalmergepdfproduct($this->db);
513
514 $this->db->begin();
515
516 // Load source object
517 $object->fetch($fromid);
518 $object->id = 0;
519 $object->statut = 0;
520
521 // Clear fields
522 // ...
523
524 // Create clone
525 $object->context['createfromclone'] = 'createfromclone';
526 $result = $object->create($user);
527
528 // Other options
529 if ($result < 0) {
530 $this->error = $object->error;
531 $this->errors = array_merge($this->errors, $object->errors);
532 $error++;
533 }
534
535 if (!$error) {
536 }
537
538 unset($object->context['createfromclone']);
539
540 // End
541 if (!$error) {
542 $this->db->commit();
543 return $object->id;
544 } else {
545 $this->db->rollback();
546 return -1;
547 }
548 }
549
550
557 public function initAsSpecimen()
558 {
559 $this->id = 0;
560
561 $this->fk_product = 0;
562 $this->file_name = '';
563 $this->fk_user_author = 0;
564 $this->fk_user_mod = 0;
565 $this->datec = '';
566 $this->tms = dol_now();
567 $this->import_key = '';
568
569 return 1;
570 }
571}
572
577{
581 public $id;
582
586 public $fk_product;
587
591 public $file_name;
592
596 public $lang;
597
601 public $fk_user_author;
602
606 public $fk_user_mod;
607
611 public $datec = '';
612
616 public $import_key;
617}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79