dolibarr  13.0.2
ecmfiles.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2014-2016 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
5  * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
6  * Copyright (C) 2018 Francis Appels <francis.appels@yahoo.com>
7  * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  */
22 
29 // Put here all includes required by your class file
30 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
31 
35 class EcmFiles extends CommonObject
36 {
40  public $element = 'ecmfiles';
41 
45  public $table_element = 'ecm_files';
46 
50  public $picto = 'generic';
51 
55  public $ref;
56 
61  public $label;
62 
66  public $share;
67 
71  public $entity;
72 
76  public $filename;
77 
81  public $filepath;
82 
86  public $fullpath_orig;
87 
91  public $description;
92 
96  public $keywords;
97 
101  public $cover;
102 
106  public $position;
107 
111  public $gen_or_uploaded;
112 
116  public $extraparams;
117 
121  public $date_c = '';
122 
126  public $date_m = '';
127 
131  public $fk_user_c;
132 
136  public $fk_user_m;
137 
141  public $acl;
142 
146  public $src_object_type;
147 
151  public $src_object_id;
152 
153 
159  public function __construct(DoliDB $db)
160  {
161  $this->db = $db;
162  }
163 
171  public function create(User $user, $notrigger = false)
172  {
173  global $conf;
174 
175  dol_syslog(__METHOD__, LOG_DEBUG);
176 
177  $error = 0;
178 
179  // Clean parameters
180  if (isset($this->ref)) {
181  $this->ref = trim($this->ref);
182  }
183  if (isset($this->label)) {
184  $this->label = trim($this->label);
185  }
186  if (isset($this->share)) {
187  $this->share = trim($this->share);
188  }
189  if (isset($this->entity)) {
190  $this->entity = (int) $this->entity;
191  }
192  if (isset($this->filename)) {
193  $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
194  }
195  if (isset($this->filepath)) {
196  $this->filepath = trim($this->filepath);
197  $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
198  }
199  if (isset($this->fullpath_orig)) {
200  $this->fullpath_orig = trim($this->fullpath_orig);
201  }
202  if (isset($this->description)) {
203  $this->description = trim($this->description);
204  }
205  if (isset($this->keywords)) {
206  $this->keywords = trim($this->keywords);
207  }
208  if (isset($this->cover)) {
209  $this->cover = trim($this->cover);
210  }
211  if (isset($this->gen_or_uploaded)) {
212  $this->gen_or_uploaded = trim($this->gen_or_uploaded);
213  }
214  if (isset($this->extraparams)) {
215  $this->extraparams = trim($this->extraparams);
216  }
217  if (isset($this->fk_user_c)) {
218  $this->fk_user_c = (int) $this->fk_user_c;
219  }
220  if (isset($this->fk_user_m)) {
221  $this->fk_user_m = (int) $this->fk_user_m;
222  }
223  if (isset($this->acl)) {
224  $this->acl = trim($this->acl);
225  }
226  if (isset($this->src_object_type)) {
227  $this->src_object_type = trim($this->src_object_type);
228  }
229  if (empty($this->date_c)) {
230  $this->date_c = dol_now();
231  }
232  if (empty($this->date_m)) {
233  $this->date_m = dol_now();
234  }
235 
236  // If ref not defined
237  $ref = '';
238  if (!empty($this->ref)) {
239  $ref = $this->ref;
240  } else {
241  include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
242  $ref = dol_hash($this->filepath.'/'.$this->filename, 3);
243  }
244 
245  $maxposition = 0;
246  if (empty($this->position)) {
247  // Get max used
248  $sql = "SELECT MAX(position) as maxposition FROM ".MAIN_DB_PREFIX.$this->table_element;
249  $sql .= " WHERE filepath ='".$this->db->escape($this->filepath)."'";
250 
251  $resql = $this->db->query($sql);
252  if ($resql) {
253  $obj = $this->db->fetch_object($resql);
254  $maxposition = (int) $obj->maxposition;
255  } else {
256  $this->errors[] = 'Error '.$this->db->lasterror();
257  return --$error;
258  }
259  $maxposition = $maxposition + 1;
260  } else {
261  $maxposition = $this->position;
262  }
263 
264  // Check parameters
265  if (empty($this->filename) || empty($this->filepath)) {
266  $this->errors[] = 'Bad property filename or filepath';
267  return --$error;
268  }
269  if (!isset($this->entity)) {
270  $this->entity = $conf->entity;
271  }
272  // Put here code to add control on parameters values
273 
274  // Insert request
275  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
276  $sql .= 'ref,';
277  $sql .= 'label,';
278  $sql .= 'share,';
279  $sql .= 'entity,';
280  $sql .= 'filename,';
281  $sql .= 'filepath,';
282  $sql .= 'fullpath_orig,';
283  $sql .= 'description,';
284  $sql .= 'keywords,';
285  $sql .= 'cover,';
286  $sql .= 'position,';
287  $sql .= 'gen_or_uploaded,';
288  $sql .= 'extraparams,';
289  $sql .= 'date_c,';
290  $sql .= 'tms,';
291  $sql .= 'fk_user_c,';
292  $sql .= 'fk_user_m,';
293  $sql .= 'acl,';
294  $sql .= 'src_object_type,';
295  $sql .= 'src_object_id';
296  $sql .= ') VALUES (';
297  $sql .= " '".$this->db->escape($ref)."', ";
298  $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
299  $sql .= ' '.(!isset($this->share) ? 'NULL' : "'".$this->db->escape($this->share)."'").',';
300  $sql .= ' '.$this->entity.',';
301  $sql .= ' '.(!isset($this->filename) ? 'NULL' : "'".$this->db->escape($this->filename)."'").',';
302  $sql .= ' '.(!isset($this->filepath) ? 'NULL' : "'".$this->db->escape($this->filepath)."'").',';
303  $sql .= ' '.(!isset($this->fullpath_orig) ? 'NULL' : "'".$this->db->escape($this->fullpath_orig)."'").',';
304  $sql .= ' '.(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").',';
305  $sql .= ' '.(!isset($this->keywords) ? 'NULL' : "'".$this->db->escape($this->keywords)."'").',';
306  $sql .= ' '.(!isset($this->cover) ? 'NULL' : "'".$this->db->escape($this->cover)."'").',';
307  $sql .= ' '.$maxposition.',';
308  $sql .= ' '.(!isset($this->gen_or_uploaded) ? 'NULL' : "'".$this->db->escape($this->gen_or_uploaded)."'").',';
309  $sql .= ' '.(!isset($this->extraparams) ? 'NULL' : "'".$this->db->escape($this->extraparams)."'").',';
310  $sql .= ' '."'".$this->db->idate($this->date_c)."'".',';
311  $sql .= ' '.(!isset($this->date_m) || dol_strlen($this->date_m) == 0 ? 'NULL' : "'".$this->db->idate($this->date_m)."'").',';
312  $sql .= ' '.(!isset($this->fk_user_c) ? $user->id : $this->fk_user_c).',';
313  $sql .= ' '.(!isset($this->fk_user_m) ? 'NULL' : $this->fk_user_m).',';
314  $sql .= ' '.(!isset($this->acl) ? 'NULL' : "'".$this->db->escape($this->acl)."'").',';
315  $sql .= ' '.(!isset($this->src_object_type) ? 'NULL' : "'".$this->db->escape($this->src_object_type)."'").',';
316  $sql .= ' '.(!isset($this->src_object_id) ? 'NULL' : $this->src_object_id);
317  $sql .= ')';
318 
319  $this->db->begin();
320 
321  $resql = $this->db->query($sql);
322  if (!$resql) {
323  $error++;
324  $this->errors[] = 'Error '.$this->db->lasterror();
325  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
326  }
327 
328  if (!$error) {
329  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
330  $this->position = $maxposition;
331 
332  // Triggers
333  if (!$notrigger) {
334  // Call triggers
335  $result = $this->call_trigger(strtoupper(get_class($this)).'_CREATE', $user);
336  if ($result < 0) {
337  $error++;
338  }
339  // End call triggers
340  }
341  }
342 
343  // Commit or rollback
344  if ($error) {
345  $this->db->rollback();
346 
347  return -1 * $error;
348  } else {
349  $this->db->commit();
350 
351  return $this->id;
352  }
353  }
354 
367  public function fetch($id, $ref = '', $relativepath = '', $hashoffile = '', $hashforshare = '', $src_object_type = '', $src_object_id = 0)
368  {
369  global $conf;
370 
371  dol_syslog(__METHOD__, LOG_DEBUG);
372 
373  $sql = 'SELECT';
374  $sql .= ' t.rowid,';
375  $sql .= " t.ref,";
376  $sql .= " t.label,";
377  $sql .= " t.share,";
378  $sql .= " t.entity,";
379  $sql .= " t.filename,";
380  $sql .= " t.filepath,";
381  $sql .= " t.fullpath_orig,";
382  $sql .= " t.description,";
383  $sql .= " t.keywords,";
384  $sql .= " t.cover,";
385  $sql .= " t.position,";
386  $sql .= " t.gen_or_uploaded,";
387  $sql .= " t.extraparams,";
388  $sql .= " t.date_c,";
389  $sql .= " t.tms as date_m,";
390  $sql .= " t.fk_user_c,";
391  $sql .= " t.fk_user_m,";
392  $sql .= " t.acl,";
393  $sql .= " t.src_object_type,";
394  $sql .= " t.src_object_id";
395  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
396  $sql .= ' WHERE 1 = 1';
397  /* Fetching this table depends on filepath+filename, it must not depends on entity because filesystem on disk does not know what is Dolibarr entities
398  if (! empty($conf->multicompany->enabled)) {
399  $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
400  }*/
401  if ($relativepath) {
402  $relativepathwithnoexe = preg_replace('/\.noexe$/', '', $relativepath); // We must never have the .noexe into the database
403  $sql .= " AND t.filepath = '".$this->db->escape(dirname($relativepath))."'";
404  $filename = basename($relativepathwithnoexe);
405  if ($filename != '*') {
406  $sql .= " AND t.filename = '".$this->db->escape($filename)."'";
407  }
408  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
409  } elseif (!empty($ref)) { // hash of file path
410  $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
411  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
412  } elseif (!empty($hashoffile)) { // hash of content
413  $sql .= " AND t.label = '".$this->db->escape($hashoffile)."'";
414  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
415  } elseif (!empty($hashforshare)) {
416  $sql .= " AND t.share = '".$this->db->escape($hashforshare)."'";
417  //$sql .= " AND t.entity = ".$conf->entity; // hashforshare already unique
418  } elseif ($src_object_type && $src_object_id) {
419  // Warning: May return several record, and only first one is returned !
420  $sql .= " AND t.src_object_type ='".$this->db->escape($src_object_type)."' AND t.src_object_id = ".$this->db->escape($src_object_id);
421  $sql .= " AND t.entity = ".$conf->entity;
422  } else {
423  $sql .= ' AND t.rowid = '.$this->db->escape($id); // rowid already unique
424  }
425 
426  $this->db->plimit(1); // When we search on src or on hash of content (hashforfile) to solve hash conflict when several files has same content, we take first one only
427  $this->db->order('t.rowid', 'ASC');
428 
429  $resql = $this->db->query($sql);
430  if ($resql) {
431  $numrows = $this->db->num_rows($resql);
432  if ($numrows) {
433  $obj = $this->db->fetch_object($resql);
434 
435  $this->id = $obj->rowid;
436  $this->ref = $obj->ref;
437  $this->label = $obj->label;
438  $this->share = $obj->share;
439  $this->entity = $obj->entity;
440  $this->filename = $obj->filename;
441  $this->filepath = $obj->filepath;
442  $this->fullpath_orig = $obj->fullpath_orig;
443  $this->description = $obj->description;
444  $this->keywords = $obj->keywords;
445  $this->cover = $obj->cover;
446  $this->position = $obj->position;
447  $this->gen_or_uploaded = $obj->gen_or_uploaded;
448  $this->extraparams = $obj->extraparams;
449  $this->date_c = $this->db->jdate($obj->date_c);
450  $this->date_m = $this->db->jdate($obj->date_m);
451  $this->fk_user_c = $obj->fk_user_c;
452  $this->fk_user_m = $obj->fk_user_m;
453  $this->acl = $obj->acl;
454  $this->src_object_type = $obj->src_object_type;
455  $this->src_object_id = $obj->src_object_id;
456  }
457 
458  // Retrieve all extrafields for ecm_files
459  // fetch optionals attributes and labels
460  $this->fetch_optionals();
461 
462  // $this->fetch_lines();
463 
464  $this->db->free($resql);
465 
466  if ($numrows) {
467  return 1;
468  } else {
469  return 0;
470  }
471  } else {
472  $this->errors[] = 'Error '.$this->db->lasterror();
473  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
474 
475  return -1;
476  }
477  }
478 
491  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
492  {
493  dol_syslog(__METHOD__, LOG_DEBUG);
494 
495  $sql = 'SELECT';
496  $sql .= ' t.rowid,';
497  $sql .= " t.label,";
498  $sql .= " t.share,";
499  $sql .= " t.entity,";
500  $sql .= " t.filename,";
501  $sql .= " t.filepath,";
502  $sql .= " t.fullpath_orig,";
503  $sql .= " t.description,";
504  $sql .= " t.keywords,";
505  $sql .= " t.cover,";
506  $sql .= " t.position,";
507  $sql .= " t.gen_or_uploaded,";
508  $sql .= " t.extraparams,";
509  $sql .= " t.date_c,";
510  $sql .= " t.tms as date_m,";
511  $sql .= " t.fk_user_c,";
512  $sql .= " t.fk_user_m,";
513  $sql .= " t.acl,";
514  $sql .= " t.src_object_type,";
515  $sql .= " t.src_object_id";
516  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
517 
518  // Manage filter
519  $sqlwhere = array();
520  if (count($filter) > 0) {
521  foreach ($filter as $key => $value) {
522  $sqlwhere [] = $key.' LIKE \'%'.$this->db->escape($value).'%\'';
523  }
524  }
525  $sql .= ' WHERE 1 = 1';
526  /* Fetching this table depends on filepath+filename, it must not depends on entity
527  if (! empty($conf->multicompany->enabled)) {
528  $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
529  }*/
530  if (count($sqlwhere) > 0) {
531  $sql .= ' AND '.implode(' '.$filtermode.' ', $sqlwhere);
532  }
533  if (!empty($sortfield)) {
534  $sql .= $this->db->order($sortfield, $sortorder);
535  }
536  if (!empty($limit)) {
537  $sql .= ' '.$this->db->plimit($limit, $offset);
538  }
539 
540  $this->lines = array();
541 
542  $resql = $this->db->query($sql);
543  if ($resql) {
544  $num = $this->db->num_rows($resql);
545 
546  while ($obj = $this->db->fetch_object($resql)) {
547  $line = new EcmfilesLine();
548 
549  $line->id = $obj->rowid;
550  $line->ref = $obj->ref;
551  $line->label = $obj->label;
552  $line->share = $obj->share;
553  $line->entity = $obj->entity;
554  $line->filename = $obj->filename;
555  $line->filepath = $obj->filepath;
556  $line->fullpath_orig = $obj->fullpath_orig;
557  $line->description = $obj->description;
558  $line->keywords = $obj->keywords;
559  $line->cover = $obj->cover;
560  $line->position = $obj->position;
561  $line->gen_or_uploaded = $obj->gen_or_uploaded;
562  $line->extraparams = $obj->extraparams;
563  $line->date_c = $this->db->jdate($obj->date_c);
564  $line->date_m = $this->db->jdate($obj->date_m);
565  $line->fk_user_c = $obj->fk_user_c;
566  $line->fk_user_m = $obj->fk_user_m;
567  $line->acl = $obj->acl;
568  $line->src_object_type = $obj->src_object_type;
569  $line->src_object_id = $obj->src_object_id;
570  $this->lines[] = $line;
571  }
572  $this->db->free($resql);
573 
574  return $num;
575  } else {
576  $this->errors[] = 'Error '.$this->db->lasterror();
577  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
578 
579  return -1;
580  }
581  }
582 
591  public function update(User $user, $notrigger = false)
592  {
593  global $conf;
594 
595  $error = 0;
596 
597  dol_syslog(__METHOD__, LOG_DEBUG);
598 
599  // Clean parameters
600 
601  if (isset($this->ref)) {
602  $this->ref = trim($this->ref);
603  }
604  if (isset($this->label)) {
605  $this->label = trim($this->label);
606  }
607  if (isset($this->share)) {
608  $this->share = trim($this->share);
609  }
610  if (isset($this->entity)) {
611  $this->entity = trim($this->entity);
612  }
613  if (isset($this->filename)) {
614  $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
615  }
616  if (isset($this->filepath)) {
617  $this->filepath = trim($this->filepath);
618  $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
619  }
620  if (isset($this->fullpath_orig)) {
621  $this->fullpath_orig = trim($this->fullpath_orig);
622  }
623  if (isset($this->description)) {
624  $this->description = trim($this->description);
625  }
626  if (isset($this->keywords)) {
627  $this->keywords = trim($this->keywords);
628  }
629  if (isset($this->cover)) {
630  $this->cover = trim($this->cover);
631  }
632  if (isset($this->gen_or_uploaded)) {
633  $this->gen_or_uploaded = trim($this->gen_or_uploaded);
634  }
635  if (isset($this->extraparams)) {
636  $this->extraparams = trim($this->extraparams);
637  }
638  if (isset($this->fk_user_m)) {
639  $this->fk_user_m = trim($this->fk_user_m);
640  }
641  if (isset($this->acl)) {
642  $this->acl = trim($this->acl);
643  }
644  if (isset($this->src_object_type)) {
645  $this->src_object_type = trim($this->src_object_type);
646  }
647 
648  // Check parameters
649  // Put here code to add a control on parameters values
650 
651  // Update request
652  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
653  $sql .= " ref = '".dol_hash($this->filepath.'/'.$this->filename, 3)."',";
654  $sql .= ' label = '.(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
655  $sql .= ' share = '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null").',';
656  $sql .= ' entity = '.(isset($this->entity) ? $this->entity : $conf->entity).',';
657  $sql .= ' filename = '.(isset($this->filename) ? "'".$this->db->escape($this->filename)."'" : "null").',';
658  $sql .= ' filepath = '.(isset($this->filepath) ? "'".$this->db->escape($this->filepath)."'" : "null").',';
659  $sql .= ' fullpath_orig = '.(isset($this->fullpath_orig) ? "'".$this->db->escape($this->fullpath_orig)."'" : "null").',';
660  $sql .= ' description = '.(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").',';
661  $sql .= ' keywords = '.(isset($this->keywords) ? "'".$this->db->escape($this->keywords)."'" : "null").',';
662  $sql .= ' cover = '.(isset($this->cover) ? "'".$this->db->escape($this->cover)."'" : "null").',';
663  $sql .= ' position = '.(isset($this->position) ? $this->db->escape($this->position) : "0").',';
664  $sql .= ' gen_or_uploaded = '.(isset($this->gen_or_uploaded) ? "'".$this->db->escape($this->gen_or_uploaded)."'" : "null").',';
665  $sql .= ' extraparams = '.(isset($this->extraparams) ? "'".$this->db->escape($this->extraparams)."'" : "null").',';
666  $sql .= ' date_c = '.(!isset($this->date_c) || dol_strlen($this->date_c) != 0 ? "'".$this->db->idate($this->date_c)."'" : 'null').',';
667  //$sql .= ' tms = '.(! isset($this->date_m) || dol_strlen($this->date_m) != 0 ? "'".$this->db->idate($this->date_m)."'" : 'null').','; // Field automatically updated
668  $sql .= ' fk_user_m = '.($this->fk_user_m > 0 ? $this->fk_user_m : $user->id).',';
669  $sql .= ' acl = '.(isset($this->acl) ? "'".$this->db->escape($this->acl)."'" : "null").',';
670  $sql .= ' src_object_id = '.($this->src_object_id > 0 ? $this->src_object_id : "null").',';
671  $sql .= ' src_object_type = '.(isset($this->src_object_type) ? "'".$this->db->escape($this->src_object_type)."'" : "null");
672  $sql .= ' WHERE rowid='.$this->id;
673 
674  $this->db->begin();
675 
676  $resql = $this->db->query($sql);
677  if (!$resql) {
678  $error++;
679  $this->errors[] = 'Error '.$this->db->lasterror();
680  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
681  }
682 
683  // Triggers
684  if (!$error && !$notrigger) {
685  // Call triggers
686  $result = $this->call_trigger(strtoupper(get_class($this)).'_MODIFY', $user);
687  if ($result < 0) {
688  $error++;
689  } //Do also here what you must do to rollback action if trigger fail
690  // End call triggers
691  }
692 
693  // Commit or rollback
694  if ($error) {
695  $this->db->rollback();
696 
697  return -1 * $error;
698  } else {
699  $this->db->commit();
700 
701  return 1;
702  }
703  }
704 
713  public function delete(User $user, $notrigger = false)
714  {
715  dol_syslog(__METHOD__, LOG_DEBUG);
716 
717  $error = 0;
718 
719  $this->db->begin();
720 
721  // Triggers
722  if (!$notrigger) {
723  // Call triggers
724  $result = $this->call_trigger(strtoupper(get_class($this)).'_DELETE', $user);
725  if ($result < 0) {
726  $error++;
727  } //Do also here what you must do to rollback action if trigger fail
728  // End call triggers
729  }
730 
731  // If you need to delete child tables to, you can insert them here
732 
733  if (!$error) {
734  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
735  $sql .= ' WHERE rowid='.$this->id;
736 
737  $resql = $this->db->query($sql);
738  if (!$resql) {
739  $error++;
740  $this->errors[] = 'Error '.$this->db->lasterror();
741  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
742  }
743  }
744 
745  // Commit or rollback
746  if ($error) {
747  $this->db->rollback();
748 
749  return -1 * $error;
750  } else {
751  $this->db->commit();
752 
753  return 1;
754  }
755  }
756 
764  public function createFromClone(User $user, $fromid)
765  {
766  dol_syslog(__METHOD__, LOG_DEBUG);
767 
768  $error = 0;
769  $object = new Ecmfiles($this->db);
770 
771  $this->db->begin();
772 
773  // Load source object
774  $object->fetch($fromid);
775  // Reset object
776  $object->id = 0;
777 
778  // Clear fields
779  // ...
780 
781  // Create clone
782  $object->context['createfromclone'] = 'createfromclone';
783  $result = $object->create($user);
784 
785  // Other options
786  if ($result < 0) {
787  $error++;
788  $this->errors = $object->errors;
789  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
790  }
791 
792  unset($object->context['createfromclone']);
793 
794  // End
795  if (!$error) {
796  $this->db->commit();
797 
798  return $object->id;
799  } else {
800  $this->db->rollback();
801 
802  return -1;
803  }
804  }
805 
816  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
817  {
818  global $db, $conf, $langs;
819  global $dolibarr_main_authentication, $dolibarr_main_demo;
820  global $menumanager;
821 
822  if (!empty($conf->dol_no_mouse_hover)) {
823  $notooltip = 1; // Force disable tooltips
824  }
825 
826  $result = '';
827 
828  $label = '<u>'.$langs->trans("MyModule").'</u>';
829  $label .= '<br>';
830  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
831 
832  $url = DOL_URL_ROOT.'/ecm/'.$this->table_name.'_card.php?id='.$this->id;
833 
834  $linkclose = '';
835  if (empty($notooltip)) {
836  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
837  $label = $langs->trans("ShowProject");
838  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
839  }
840  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
841  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
842  } else {
843  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
844  }
845 
846  $linkstart = '<a href="'.$url.'"';
847  $linkstart .= $linkclose.'>';
848  $linkend = '</a>';
849 
850  if ($withpicto) {
851  $result .= ($linkstart.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
852  if ($withpicto != 2) {
853  $result .= ' ';
854  }
855  }
856  $result .= $linkstart.$this->ref.$linkend;
857  return $result;
858  }
859 
866  public function getLibStatut($mode = 0)
867  {
868  return $this->LibStatut($this->status, $mode);
869  }
870 
871  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
879  public static function LibStatut($status, $mode = 0)
880  {
881  // phpcs:enable
882  global $langs;
883  return '';
884  }
885 
886 
893  public function initAsSpecimen()
894  {
895  global $conf, $user;
896 
897  $this->id = 0;
898  $this->specimen = 1;
899  $this->label = '0a1b2c3e4f59999999';
900  $this->entity = 1;
901  $this->filename = 'myspecimenfilefile.pdf';
902  $this->filepath = '/aaa/bbb';
903  $this->fullpath_orig = 'c:/file on my disk.pdf';
904  $this->description = 'This is a long description of file';
905  $this->keywords = 'key1,key2';
906  $this->cover = '1';
907  $this->position = 5;
908  $this->gen_or_uploaded = 'uploaded';
909  $this->extraparams = '';
910  $this->date_c = (dol_now() - 3600 * 24 * 10);
911  $this->date_m = '';
912  $this->fk_user_c = $user->id;
913  $this->fk_user_m = '';
914  $this->acl = '';
915  $this->src_object_type = 'product';
916  $this->src_object_id = 1;
917  }
918 }
919 
920 
925 {
929  public $label;
930 
934  public $entity;
935 
936  public $filename;
937  public $filepath;
938  public $fullpath_orig;
939 
943  public $description;
944 
945  public $keywords;
946  public $cover;
947  public $position;
948  public $gen_or_uploaded; // can be 'generated', 'uploaded', 'unknown'
949  public $extraparams;
950  public $date_c = '';
951  public $date_m = '';
952 
956  public $fk_user_c;
957 
961  public $fk_user_m;
962 
963  public $acl;
964  public $src_object_type;
965  public $src_object_id;
966 }
dol_hash($chain, $type= '0')
Returns a hash of a string.
getLibStatut($mode=0)
Retourne le libelle du status d&#39;un user (actif, inactif)
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:750
static LibStatut($status, $mode=0)
Return the status.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
fetchAll($sortorder= '', $sortfield= '', $limit=0, $offset=0, array $filter=array(), $filtermode= 'AND')
Load object in memory from the database.
$conf db
API class for accounts.
Definition: inc.php:54
Class of an index line of a document.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
create(User $user, $notrigger=false)
Create object into database.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this-&gt;array_options This method is in most cases call...
getNomUrl($withpicto=0, $option= '', $notooltip=0, $maxlen=24, $morecss= '')
Return a link to the object card (with optionaly the picto)
update(User $user, $notrigger=false)
Update object into database.
call_trigger($triggerName, $user)
Call trigger based on this instance.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Class to manage ECM files.
fetch($id, $ref= '', $relativepath= '', $hashoffile= '', $hashforshare= '', $src_object_type= '', $src_object_id=0)
Load object in memory from the database.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
__construct(DoliDB $db)
Constructor.