dolibarr 19.0.4
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++;
132 $this->errors[] = "Error ".$this->db->lasterror();
133 }
134
135 if (!$error) {
136 $this->id = $this->db->last_insert_id($this->db->prefix()."propal_merge_pdf_product");
137 }
138
139 // Commit or rollback
140 if ($error) {
141 foreach ($this->errors as $errmsg) {
142 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
143 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
144 }
145 $this->db->rollback();
146 return -1 * $error;
147 } else {
148 $this->db->commit();
149 return $this->id;
150 }
151 }
152
153
160 public function fetch($id)
161 {
162 global $langs, $conf;
163
164 $sql = "SELECT";
165 $sql .= " t.rowid,";
166
167 $sql .= " t.fk_product,";
168 $sql .= " t.file_name,";
169 $sql .= " t.lang,";
170 $sql .= " t.fk_user_author,";
171 $sql .= " t.fk_user_mod,";
172 $sql .= " t.datec,";
173 $sql .= " t.tms,";
174 $sql .= " t.import_key";
175
176
177 $sql .= " FROM ".$this->db->prefix()."propal_merge_pdf_product as t";
178 $sql .= " WHERE t.rowid = ".((int) $id);
179
180 dol_syslog(__METHOD__, LOG_DEBUG);
181 $resql = $this->db->query($sql);
182 if ($resql) {
183 if ($this->db->num_rows($resql)) {
184 $obj = $this->db->fetch_object($resql);
185
186 $this->id = $obj->rowid;
187
188 $this->fk_product = $obj->fk_product;
189 $this->file_name = $obj->file_name;
190 if (getDolGlobalInt('MAIN_MULTILANGS')) {
191 $this->lang = $obj->lang;
192 }
193 $this->fk_user_author = $obj->fk_user_author;
194 $this->fk_user_mod = $obj->fk_user_mod;
195 $this->datec = $this->db->jdate($obj->datec);
196 $this->tms = $this->db->jdate($obj->tms);
197 $this->import_key = $obj->import_key;
198 }
199 $this->db->free($resql);
200
201 return 1;
202 } else {
203 $this->error = "Error ".$this->db->lasterror();
204 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
205 return -1;
206 }
207 }
208
209 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
217 public function fetch_by_product($product_id, $lang = '')
218 {
219 // phpcs:enable
220 global $langs, $conf;
221
222 $sql = "SELECT";
223 $sql .= " t.rowid,";
224
225 $sql .= " t.fk_product,";
226 $sql .= " t.file_name,";
227 $sql .= " t.lang,";
228 $sql .= " t.fk_user_author,";
229 $sql .= " t.fk_user_mod,";
230 $sql .= " t.datec,";
231 $sql .= " t.tms,";
232 $sql .= " t.import_key";
233
234
235 $sql .= " FROM ".$this->db->prefix()."propal_merge_pdf_product as t";
236 $sql .= " WHERE t.fk_product = ".((int) $product_id);
237 if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($lang)) {
238 $sql .= " AND t.lang = '".$this->db->escape($lang)."'";
239 }
240
241 dol_syslog(__METHOD__, LOG_DEBUG);
242 $resql = $this->db->query($sql);
243 if ($resql) {
244 if ($this->db->num_rows($resql)) {
245 while ($obj = $this->db->fetch_object($resql)) {
246 $line = new PropalmergepdfproductLine();
247
248 $line->id = $obj->rowid;
249
250 $line->fk_product = $obj->fk_product;
251 $line->file_name = $obj->file_name;
252 if (getDolGlobalInt('MAIN_MULTILANGS')) {
253 $line->lang = $obj->lang;
254 }
255 $line->fk_user_author = $obj->fk_user_author;
256 $line->fk_user_mod = $obj->fk_user_mod;
257 $line->datec = $this->db->jdate($obj->datec);
258 $line->tms = $this->db->jdate($obj->tms);
259 $line->import_key = $obj->import_key;
260
261
262 if (getDolGlobalInt('MAIN_MULTILANGS')) {
263 $this->lines[$obj->file_name.'_'.$obj->lang] = $line;
264 } else {
265 $this->lines[$obj->file_name] = $line;
266 }
267 }
268 }
269 $this->db->free($resql);
270
271 return 1;
272 } else {
273 $this->error = "Error ".$this->db->lasterror();
274 dol_syslog(get_class($this)."::fetch_by_product ".$this->error, LOG_ERR);
275 return -1;
276 }
277 }
278
279
287 public function update($user = 0, $notrigger = 0)
288 {
289 global $conf, $langs;
290 $error = 0;
291
292 // Clean parameters
293
294 if (isset($this->fk_product)) {
295 $this->fk_product = trim($this->fk_product);
296 }
297 if (isset($this->file_name)) {
298 $this->file_name = trim($this->file_name);
299 }
300 if (isset($this->fk_user_mod)) {
301 $this->fk_user_mod = trim($this->fk_user_mod);
302 }
303 if (isset($this->lang)) {
304 $this->lang = trim($this->lang);
305 }
306
307 // Check parameters
308 // Put here code to add a control on parameters values
309
310 // Update request
311 $sql = "UPDATE ".$this->db->prefix()."propal_merge_pdf_product SET";
312
313 $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
314 $sql .= " file_name=".(isset($this->file_name) ? "'".$this->db->escape($this->file_name)."'" : "null").",";
315 if (getDolGlobalInt('MAIN_MULTILANGS')) {
316 $sql .= " lang=".(isset($this->lang) ? "'".$this->db->escape($this->lang)."'" : "null").",";
317 }
318 $sql .= " fk_user_mod=".$user->id;
319
320
321 $sql .= " WHERE rowid=".((int) $this->id);
322
323 $this->db->begin();
324
325 dol_syslog(__METHOD__, LOG_DEBUG);
326 $resql = $this->db->query($sql);
327 if (!$resql) {
328 $error++;
329 $this->errors[] = "Error ".$this->db->lasterror();
330 }
331
332 // Commit or rollback
333 if ($error) {
334 foreach ($this->errors as $errmsg) {
335 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
336 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
337 }
338 $this->db->rollback();
339 return -1 * $error;
340 } else {
341 $this->db->commit();
342 return 1;
343 }
344 }
345
346
354 public function delete($user, $notrigger = 0)
355 {
356 global $conf, $langs;
357 $error = 0;
358
359 $this->db->begin();
360
361 if (!$error) {
362 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
363 $sql .= " WHERE rowid=".((int) $this->id);
364
365 dol_syslog(__METHOD__, LOG_DEBUG);
366 $resql = $this->db->query($sql);
367 if (!$resql) {
368 $error++;
369 $this->errors[] = "Error ".$this->db->lasterror();
370 }
371 }
372
373 // Commit or rollback
374 if ($error) {
375 foreach ($this->errors as $errmsg) {
376 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
377 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
378 }
379 $this->db->rollback();
380 return -1 * $error;
381 } else {
382 $this->db->commit();
383 return 1;
384 }
385 }
386
387 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
397 public function delete_by_product($user, $product_id, $lang_id = '', $notrigger = 0)
398 {
399 // phpcs:enable
400 global $conf, $langs;
401 $error = 0;
402
403 $this->db->begin();
404
405 if (!$error) {
406 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
407 $sql .= " WHERE fk_product = ".((int) $product_id);
408
409 if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($lang_id)) {
410 $sql .= " AND lang = '".$this->db->escape($lang_id)."'";
411 }
412
413 dol_syslog(__METHOD__, LOG_DEBUG);
414 $resql = $this->db->query($sql);
415 if (!$resql) {
416 $error++;
417 $this->errors[] = "Error ".$this->db->lasterror();
418 }
419 }
420
421 // Commit or rollback
422 if ($error) {
423 foreach ($this->errors as $errmsg) {
424 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
425 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
426 }
427 $this->db->rollback();
428 return -1 * $error;
429 } else {
430 $this->db->commit();
431 return 1;
432 }
433 }
434
435 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
442 public function delete_by_file($user)
443 {
444 // phpcs:enable
445 global $conf, $langs;
446 $error = 0;
447
448 $this->db->begin();
449
450 if (!$error) {
451 $sql = "DELETE FROM ".$this->db->prefix()."propal_merge_pdf_product";
452 $sql .= " WHERE fk_product = ".((int) $this->fk_product)." AND file_name = '".$this->db->escape($this->file_name)."'";
453
454 dol_syslog(__METHOD__, LOG_DEBUG);
455 $resql = $this->db->query($sql);
456 if (!$resql) {
457 $error++;
458 $this->errors[] = "Error ".$this->db->lasterror();
459 }
460 }
461
462 // Commit or rollback
463 if ($error) {
464 foreach ($this->errors as $errmsg) {
465 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
466 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
467 }
468 $this->db->rollback();
469 return -1 * $error;
470 } else {
471 $this->db->commit();
472 return 1;
473 }
474 }
475
476
477
485 public function createFromClone(User $user, $fromid)
486 {
487 $error = 0;
488
489 $object = new Propalmergepdfproduct($this->db);
490
491 $this->db->begin();
492
493 // Load source object
494 $object->fetch($fromid);
495 $object->id = 0;
496 $object->statut = 0;
497
498 // Clear fields
499 // ...
500
501 // Create clone
502 $object->context['createfromclone'] = 'createfromclone';
503 $result = $object->create($user);
504
505 // Other options
506 if ($result < 0) {
507 $this->error = $object->error;
508 $this->errors = array_merge($this->errors, $object->errors);
509 $error++;
510 }
511
512 if (!$error) {
513 }
514
515 unset($object->context['createfromclone']);
516
517 // End
518 if (!$error) {
519 $this->db->commit();
520 return $object->id;
521 } else {
522 $this->db->rollback();
523 return -1;
524 }
525 }
526
527
534 public function initAsSpecimen()
535 {
536 $this->id = 0;
537
538 $this->fk_product = '';
539 $this->file_name = '';
540 $this->fk_user_author = '';
541 $this->fk_user_mod = '';
542 $this->datec = '';
543 $this->tms = '';
544 $this->import_key = '';
545 }
546}
547
552{
556 public $id;
557
561 public $fk_product;
562
563 public $file_name;
564 public $lang;
565
569 public $fk_user_author;
570
574 public $fk_user_mod;
575
576 public $datec = '';
577 public $tms = '';
578 public $import_key;
579
583 public function __construct()
584 {
585 return;
586 }
587}
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 a Dolibarr global constant int value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.