dolibarr  17.0.4
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 = 'folder-open';
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 
156  public $section_id;
157 
158 
159 
165  public function __construct(DoliDB $db)
166  {
167  $this->db = $db;
168  }
169 
177  public function create(User $user, $notrigger = false)
178  {
179  global $conf;
180 
181  dol_syslog(__METHOD__, LOG_DEBUG);
182 
183  $error = 0;
184 
185  // Clean parameters
186  if (isset($this->ref)) {
187  $this->ref = trim($this->ref);
188  }
189  if (isset($this->label)) {
190  $this->label = trim($this->label);
191  }
192  if (isset($this->share)) {
193  $this->share = trim($this->share);
194  }
195  if (isset($this->entity)) {
196  $this->entity = (int) $this->entity;
197  }
198  if (isset($this->filename)) {
199  $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
200  }
201  if (isset($this->filepath)) {
202  $this->filepath = trim($this->filepath);
203  $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
204  }
205  if (isset($this->fullpath_orig)) {
206  $this->fullpath_orig = trim($this->fullpath_orig);
207  }
208  if (isset($this->description)) {
209  $this->description = trim($this->description);
210  }
211  if (isset($this->keywords)) {
212  $this->keywords = trim($this->keywords);
213  }
214  if (isset($this->cover)) {
215  $this->cover = trim($this->cover);
216  }
217  if (isset($this->gen_or_uploaded)) {
218  $this->gen_or_uploaded = trim($this->gen_or_uploaded);
219  }
220  if (isset($this->extraparams)) {
221  $this->extraparams = trim($this->extraparams);
222  }
223  if (isset($this->fk_user_c)) {
224  $this->fk_user_c = (int) $this->fk_user_c;
225  }
226  if (isset($this->fk_user_m)) {
227  $this->fk_user_m = (int) $this->fk_user_m;
228  }
229  if (isset($this->acl)) {
230  $this->acl = trim($this->acl);
231  }
232  if (isset($this->src_object_type)) {
233  $this->src_object_type = trim($this->src_object_type);
234  }
235  if (empty($this->date_c)) {
236  $this->date_c = dol_now();
237  }
238  if (empty($this->date_m)) {
239  $this->date_m = dol_now();
240  }
241 
242  // If ref not defined
243  if (empty($this->ref)) {
244  include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
245  $this->ref = dol_hash($this->filepath.'/'.$this->filename, 3);
246  }
247 
248  $maxposition = 0;
249  if (empty($this->position)) {
250  // Get max used
251  $sql = "SELECT MAX(position) as maxposition FROM ".MAIN_DB_PREFIX.$this->table_element;
252  $sql .= " WHERE filepath ='".$this->db->escape($this->filepath)."'";
253 
254  $resql = $this->db->query($sql);
255  if ($resql) {
256  $obj = $this->db->fetch_object($resql);
257  $maxposition = (int) $obj->maxposition;
258  } else {
259  $this->errors[] = 'Error '.$this->db->lasterror();
260  return --$error;
261  }
262  $maxposition = $maxposition + 1;
263  } else {
264  $maxposition = $this->position;
265  }
266 
267  // Check parameters
268  if (empty($this->filename) || empty($this->filepath)) {
269  $this->errors[] = 'Bad property filename or filepath';
270  return --$error;
271  }
272  if (!isset($this->entity)) {
273  $this->entity = $conf->entity;
274  }
275  // Put here code to add control on parameters values
276 
277  // Insert request
278  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
279  $sql .= 'ref,';
280  $sql .= 'label,';
281  $sql .= 'share,';
282  $sql .= 'entity,';
283  $sql .= 'filename,';
284  $sql .= 'filepath,';
285  $sql .= 'fullpath_orig,';
286  $sql .= 'description,';
287  $sql .= 'keywords,';
288  $sql .= 'cover,';
289  $sql .= 'position,';
290  $sql .= 'gen_or_uploaded,';
291  $sql .= 'extraparams,';
292  $sql .= 'date_c,';
293  $sql .= 'tms,';
294  $sql .= 'fk_user_c,';
295  $sql .= 'fk_user_m,';
296  $sql .= 'acl,';
297  $sql .= 'src_object_type,';
298  $sql .= 'src_object_id';
299  $sql .= ') VALUES (';
300  $sql .= " '".$this->db->escape($this->ref)."', ";
301  $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
302  $sql .= ' '.(!isset($this->share) ? 'NULL' : "'".$this->db->escape($this->share)."'").',';
303  $sql .= ' '.((int) $this->entity).',';
304  $sql .= ' '.(!isset($this->filename) ? 'NULL' : "'".$this->db->escape($this->filename)."'").',';
305  $sql .= ' '.(!isset($this->filepath) ? 'NULL' : "'".$this->db->escape($this->filepath)."'").',';
306  $sql .= ' '.(!isset($this->fullpath_orig) ? 'NULL' : "'".$this->db->escape($this->fullpath_orig)."'").',';
307  $sql .= ' '.(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").',';
308  $sql .= ' '.(!isset($this->keywords) ? 'NULL' : "'".$this->db->escape($this->keywords)."'").',';
309  $sql .= ' '.(!isset($this->cover) ? 'NULL' : "'".$this->db->escape($this->cover)."'").',';
310  $sql .= ' '.((int) $maxposition).',';
311  $sql .= ' '.(!isset($this->gen_or_uploaded) ? 'NULL' : "'".$this->db->escape($this->gen_or_uploaded)."'").',';
312  $sql .= ' '.(!isset($this->extraparams) ? 'NULL' : "'".$this->db->escape($this->extraparams)."'").',';
313  $sql .= " '".$this->db->idate($this->date_c)."',";
314  $sql .= ' '.(!isset($this->date_m) || dol_strlen($this->date_m) == 0 ? 'NULL' : "'".$this->db->idate($this->date_m)."'").',';
315  $sql .= ' '.(!isset($this->fk_user_c) ? $user->id : $this->fk_user_c).',';
316  $sql .= ' '.(!isset($this->fk_user_m) ? 'NULL' : $this->fk_user_m).',';
317  $sql .= ' '.(!isset($this->acl) ? 'NULL' : "'".$this->db->escape($this->acl)."'").',';
318  $sql .= ' '.(!isset($this->src_object_type) ? 'NULL' : "'".$this->db->escape($this->src_object_type)."'").',';
319  $sql .= ' '.(!isset($this->src_object_id) ? 'NULL' : $this->src_object_id);
320  $sql .= ')';
321 
322  $this->db->begin();
323 
324  $resql = $this->db->query($sql);
325  if (!$resql) {
326  $error++;
327  if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
328  $this->errors[] = 'Error DB_ERROR_RECORD_ALREADY_EXISTS : '.$this->db->lasterror();
329  } else {
330  $this->errors[] = 'Error '.$this->db->lasterror();
331  }
332  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
333  }
334 
335  if (!$error) {
336  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
337  $this->position = $maxposition;
338 
339  // Triggers
340  if (!$notrigger) {
341  // Call triggers
342  $result = $this->call_trigger(strtoupper(get_class($this)).'_CREATE', $user);
343  if ($result < 0) {
344  $error++;
345  }
346  // End call triggers
347  }
348  }
349 
350  // Commit or rollback
351  if ($error) {
352  $this->db->rollback();
353 
354  return -1 * $error;
355  } else {
356  $this->db->commit();
357 
358  return $this->id;
359  }
360  }
361 
374  public function fetch($id, $ref = '', $relativepath = '', $hashoffile = '', $hashforshare = '', $src_object_type = '', $src_object_id = 0)
375  {
376  global $conf;
377 
378  dol_syslog(__METHOD__, LOG_DEBUG);
379 
380  $sql = 'SELECT';
381  $sql .= ' t.rowid,';
382  $sql .= " t.ref,";
383  $sql .= " t.label,";
384  $sql .= " t.share,";
385  $sql .= " t.entity,";
386  $sql .= " t.filename,";
387  $sql .= " t.filepath,";
388  $sql .= " t.fullpath_orig,";
389  $sql .= " t.description,";
390  $sql .= " t.keywords,";
391  $sql .= " t.cover,";
392  $sql .= " t.position,";
393  $sql .= " t.gen_or_uploaded,";
394  $sql .= " t.extraparams,";
395  $sql .= " t.date_c,";
396  $sql .= " t.tms as date_m,";
397  $sql .= " t.fk_user_c,";
398  $sql .= " t.fk_user_m,";
399  $sql .= ' t.note_private,';
400  $sql .= ' t.note_public,';
401  $sql .= " t.acl,";
402  $sql .= " t.src_object_type,";
403  $sql .= " t.src_object_id";
404  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
405  $sql .= ' WHERE 1 = 1';
406  /* Fetching this table depends on filepath+filename, it must not depends on entity because filesystem on disk does not know what is Dolibarr entities
407  if (isModEnabled('multicompany')) {
408  $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
409  }*/
410  if ($relativepath) {
411  $relativepathwithnoexe = preg_replace('/\.noexe$/', '', $relativepath); // We must never have the .noexe into the database
412  $sql .= " AND t.filepath = '".$this->db->escape(dirname($relativepath))."'";
413  $filename = basename($relativepathwithnoexe);
414  if ($filename != '*') {
415  $sql .= " AND t.filename = '".$this->db->escape($filename)."'";
416  }
417  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
418  } elseif (!empty($ref)) { // hash of file path
419  $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
420  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
421  } elseif (!empty($hashoffile)) { // hash of content
422  $sql .= " AND t.label = '".$this->db->escape($hashoffile)."'";
423  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
424  } elseif (!empty($hashforshare)) {
425  if ($hashforshare != 'shared') {
426  $sql .= " AND t.share = '".$this->db->escape($hashforshare)."'";
427  } else {
428  $sql .= " AND t.share IS NOT NULL AND t.share <> ''";
429  }
430  //$sql .= " AND t.entity = ".$conf->entity; // hashforshare already unique
431  } elseif ($src_object_type && $src_object_id) {
432  // Warning: May return several record, and only first one is returned !
433  $sql .= " AND t.src_object_type = '".$this->db->escape($src_object_type)."' AND t.src_object_id = ".((int) $src_object_id);
434  $sql .= " AND t.entity = ".((int) $conf->entity);
435  } else {
436  $sql .= ' AND t.rowid = '.((int) $id); // rowid already unique
437  }
438 
439  $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
440  $this->db->order('t.rowid', 'ASC');
441 
442  $resql = $this->db->query($sql);
443  if ($resql) {
444  $numrows = $this->db->num_rows($resql);
445  if ($numrows) {
446  $obj = $this->db->fetch_object($resql);
447 
448  $this->id = $obj->rowid;
449  $this->ref = $obj->ref;
450  $this->label = $obj->label;
451  $this->share = $obj->share;
452  $this->entity = $obj->entity;
453  $this->filename = $obj->filename;
454  $this->filepath = $obj->filepath;
455  $this->fullpath_orig = $obj->fullpath_orig;
456  $this->description = $obj->description;
457  $this->keywords = $obj->keywords;
458  $this->cover = $obj->cover;
459  $this->position = $obj->position;
460  $this->gen_or_uploaded = $obj->gen_or_uploaded;
461  $this->extraparams = $obj->extraparams;
462  $this->date_c = $this->db->jdate($obj->date_c);
463  $this->date_m = $this->db->jdate($obj->date_m);
464  $this->fk_user_c = $obj->fk_user_c;
465  $this->fk_user_m = $obj->fk_user_m;
466  $this->note_private = $obj->note_private;
467  $this->note_public = $obj->note_public;
468  $this->acl = $obj->acl;
469  $this->src_object_type = $obj->src_object_type;
470  $this->src_object_id = $obj->src_object_id;
471  }
472 
473  // Retrieve all extrafields for ecm_files
474  // fetch optionals attributes and labels
475  $this->fetch_optionals();
476 
477  // $this->fetch_lines();
478 
479  $this->db->free($resql);
480 
481  if ($numrows) {
482  return 1;
483  } else {
484  return 0;
485  }
486  } else {
487  $this->errors[] = 'Error '.$this->db->lasterror();
488  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
489 
490  return -1;
491  }
492  }
493 
506  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
507  {
508  dol_syslog(__METHOD__, LOG_DEBUG);
509 
510  $sql = 'SELECT';
511  $sql .= ' t.rowid,';
512  $sql .= " t.label,";
513  $sql .= " t.share,";
514  $sql .= " t.entity,";
515  $sql .= " t.filename,";
516  $sql .= " t.filepath,";
517  $sql .= " t.fullpath_orig,";
518  $sql .= " t.description,";
519  $sql .= " t.keywords,";
520  $sql .= " t.cover,";
521  $sql .= " t.position,";
522  $sql .= " t.gen_or_uploaded,";
523  $sql .= " t.extraparams,";
524  $sql .= " t.date_c,";
525  $sql .= " t.tms as date_m,";
526  $sql .= " t.fk_user_c,";
527  $sql .= " t.fk_user_m,";
528  $sql .= " t.acl,";
529  $sql .= " t.src_object_type,";
530  $sql .= " t.src_object_id";
531  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
532 
533  // Manage filter
534  $sqlwhere = array();
535  if (count($filter) > 0) {
536  foreach ($filter as $key => $value) {
537  if ($key == 't.src_object_id') {
538  $sqlwhere[] = $key." = ".((int) $value);
539  } else {
540  $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
541  }
542  }
543  }
544  $sql .= ' WHERE 1 = 1';
545  /* Fetching this table depends on filepath+filename, it must not depends on entity
546  if (isModEnabled('multicompany')) {
547  $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
548  }*/
549  if (count($sqlwhere) > 0) {
550  $sql .= ' AND '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
551  }
552  if (!empty($sortfield)) {
553  $sql .= $this->db->order($sortfield, $sortorder);
554  }
555  if (!empty($limit)) {
556  $sql .= $this->db->plimit($limit, $offset);
557  }
558 
559  $this->lines = array();
560 
561  $resql = $this->db->query($sql);
562  if ($resql) {
563  $num = $this->db->num_rows($resql);
564 
565  while ($obj = $this->db->fetch_object($resql)) {
566  $line = new EcmfilesLine();
567 
568  $line->id = $obj->rowid;
569  $line->ref = $obj->rowid;
570  $line->label = $obj->label;
571  $line->share = $obj->share;
572  $line->entity = $obj->entity;
573  $line->filename = $obj->filename;
574  $line->filepath = $obj->filepath;
575  $line->fullpath_orig = $obj->fullpath_orig;
576  $line->description = $obj->description;
577  $line->keywords = $obj->keywords;
578  $line->cover = $obj->cover;
579  $line->position = $obj->position;
580  $line->gen_or_uploaded = $obj->gen_or_uploaded;
581  $line->extraparams = $obj->extraparams;
582  $line->date_c = $this->db->jdate($obj->date_c);
583  $line->date_m = $this->db->jdate($obj->date_m);
584  $line->fk_user_c = $obj->fk_user_c;
585  $line->fk_user_m = $obj->fk_user_m;
586  $line->acl = $obj->acl;
587  $line->src_object_type = $obj->src_object_type;
588  $line->src_object_id = $obj->src_object_id;
589  $this->lines[] = $line;
590  }
591  $this->db->free($resql);
592 
593  return $num;
594  } else {
595  $this->errors[] = 'Error '.$this->db->lasterror();
596  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
597 
598  return -1;
599  }
600  }
601 
610  public function update(User $user, $notrigger = false)
611  {
612  global $conf;
613 
614  $error = 0;
615 
616  dol_syslog(__METHOD__, LOG_DEBUG);
617 
618  // Clean parameters
619 
620  if (isset($this->ref)) {
621  $this->ref = trim($this->ref);
622  }
623  if (isset($this->label)) {
624  $this->label = trim($this->label);
625  }
626  if (isset($this->share)) {
627  $this->share = trim($this->share);
628  }
629  if (isset($this->entity)) {
630  $this->entity = trim($this->entity);
631  }
632  if (isset($this->filename)) {
633  $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
634  }
635  if (isset($this->filepath)) {
636  $this->filepath = trim($this->filepath);
637  $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
638  }
639  if (isset($this->fullpath_orig)) {
640  $this->fullpath_orig = trim($this->fullpath_orig);
641  }
642  if (isset($this->description)) {
643  $this->description = trim($this->description);
644  }
645  if (isset($this->keywords)) {
646  $this->keywords = trim($this->keywords);
647  }
648  if (isset($this->cover)) {
649  $this->cover = trim($this->cover);
650  }
651  if (isset($this->gen_or_uploaded)) {
652  $this->gen_or_uploaded = trim($this->gen_or_uploaded);
653  }
654  if (isset($this->extraparams)) {
655  $this->extraparams = trim($this->extraparams);
656  }
657  if (isset($this->fk_user_m)) {
658  $this->fk_user_m = trim($this->fk_user_m);
659  }
660  if (isset($this->acl)) {
661  $this->acl = trim($this->acl);
662  }
663  if (isset($this->src_object_type)) {
664  $this->src_object_type = trim($this->src_object_type);
665  }
666 
667  // Check parameters
668  // Put here code to add a control on parameters values
669 
670  // Update request
671  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
672  $sql .= " ref = '".$this->db->escape(dol_hash($this->filepath."/".$this->filename, 3))."',";
673  $sql .= ' label = '.(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
674  $sql .= ' share = '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null").',';
675  $sql .= ' entity = '.(isset($this->entity) ? $this->entity : $conf->entity).',';
676  $sql .= ' filename = '.(isset($this->filename) ? "'".$this->db->escape($this->filename)."'" : "null").',';
677  $sql .= ' filepath = '.(isset($this->filepath) ? "'".$this->db->escape($this->filepath)."'" : "null").',';
678  $sql .= ' fullpath_orig = '.(isset($this->fullpath_orig) ? "'".$this->db->escape($this->fullpath_orig)."'" : "null").',';
679  $sql .= ' description = '.(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").',';
680  $sql .= ' keywords = '.(isset($this->keywords) ? "'".$this->db->escape($this->keywords)."'" : "null").',';
681  $sql .= ' cover = '.(isset($this->cover) ? "'".$this->db->escape($this->cover)."'" : "null").',';
682  $sql .= ' position = '.(isset($this->position) ? $this->db->escape($this->position) : "0").',';
683  $sql .= ' gen_or_uploaded = '.(isset($this->gen_or_uploaded) ? "'".$this->db->escape($this->gen_or_uploaded)."'" : "null").',';
684  $sql .= ' extraparams = '.(isset($this->extraparams) ? "'".$this->db->escape($this->extraparams)."'" : "null").',';
685  $sql .= ' date_c = '.(!isset($this->date_c) || dol_strlen($this->date_c) != 0 ? "'".$this->db->idate($this->date_c)."'" : 'null').',';
686  //$sql .= ' tms = '.(! isset($this->date_m) || dol_strlen($this->date_m) != 0 ? "'".$this->db->idate($this->date_m)."'" : 'null').','; // Field automatically updated
687  $sql .= ' fk_user_m = '.($this->fk_user_m > 0 ? $this->fk_user_m : $user->id).',';
688  $sql .= ' acl = '.(isset($this->acl) ? "'".$this->db->escape($this->acl)."'" : "null").',';
689  $sql .= ' src_object_id = '.($this->src_object_id > 0 ? $this->src_object_id : "null").',';
690  $sql .= ' src_object_type = '.(isset($this->src_object_type) ? "'".$this->db->escape($this->src_object_type)."'" : "null");
691  $sql .= ' WHERE rowid='.((int) $this->id);
692 
693  $this->db->begin();
694 
695  $resql = $this->db->query($sql);
696  if (!$resql) {
697  $error++;
698  $this->errors[] = 'Error '.$this->db->lasterror();
699  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
700  }
701 
702  // Triggers
703  if (!$error && !$notrigger) {
704  // Call triggers
705  $result = $this->call_trigger(strtoupper(get_class($this)).'_MODIFY', $user);
706  if ($result < 0) {
707  $error++;
708  } //Do also here what you must do to rollback action if trigger fail
709  // End call triggers
710  }
711 
712  // Commit or rollback
713  if ($error) {
714  $this->db->rollback();
715 
716  return -1 * $error;
717  } else {
718  $this->db->commit();
719 
720  return 1;
721  }
722  }
723 
732  public function delete(User $user, $notrigger = false)
733  {
734  dol_syslog(__METHOD__, LOG_DEBUG);
735 
736  $error = 0;
737 
738  $this->db->begin();
739 
740  // Triggers
741  if (!$notrigger) {
742  // Call triggers
743  $result = $this->call_trigger(strtoupper(get_class($this)).'_DELETE', $user);
744  if ($result < 0) {
745  $error++;
746  } //Do also here what you must do to rollback action if trigger fail
747  // End call triggers
748  }
749 
750  // If you need to delete child tables to, you can insert them here
751  if (!$error) {
752  $result = $this->deleteExtraFields();
753  if (!$result) {
754  dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
755  $error++;
756  }
757  }
758  if (!$error) {
759  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
760  $sql .= ' WHERE rowid='.((int) $this->id);
761 
762  $resql = $this->db->query($sql);
763  if (!$resql) {
764  $error++;
765  $this->errors[] = 'Error '.$this->db->lasterror();
766  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
767  }
768  }
769 
770  // Commit or rollback
771  if ($error) {
772  $this->db->rollback();
773 
774  return -1 * $error;
775  } else {
776  $this->db->commit();
777 
778  return 1;
779  }
780  }
781 
789  public function createFromClone(User $user, $fromid)
790  {
791  dol_syslog(__METHOD__, LOG_DEBUG);
792 
793  $error = 0;
794  $object = new Ecmfiles($this->db);
795 
796  $this->db->begin();
797 
798  // Load source object
799  $object->fetch($fromid);
800  // Reset object
801  $object->id = 0;
802 
803  // Clear fields
804  // ...
805 
806  // Create clone
807  $object->context['createfromclone'] = 'createfromclone';
808  $result = $object->create($user);
809 
810  // Other options
811  if ($result < 0) {
812  $error++;
813  $this->errors = $object->errors;
814  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
815  }
816 
817  unset($object->context['createfromclone']);
818 
819  // End
820  if (!$error) {
821  $this->db->commit();
822 
823  return $object->id;
824  } else {
825  $this->db->rollback();
826 
827  return -1;
828  }
829  }
830 
841  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
842  {
843  global $db, $conf, $langs;
844  global $dolibarr_main_authentication, $dolibarr_main_demo;
845  global $menumanager, $hookmanager;
846 
847  if (!empty($conf->dol_no_mouse_hover)) {
848  $notooltip = 1; // Force disable tooltips
849  }
850 
851  $result = '';
852 
853  $label = '<u>'.$langs->trans("MyModule").'</u>';
854  $label .= '<br>';
855  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
856 
857  $url = DOL_URL_ROOT.'/ecm/'.$this->table_name.'_card.php?id='.$this->id;
858 
859  $linkclose = '';
860  if (empty($notooltip)) {
861  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
862  $label = $langs->trans("ShowProject");
863  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
864  }
865  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
866  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
867  } else {
868  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
869  }
870 
871  $linkstart = '<a href="'.$url.'"';
872  $linkstart .= $linkclose.'>';
873  $linkend = '</a>';
874 
875  if ($withpicto) {
876  $result .= ($linkstart.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
877  if ($withpicto != 2) {
878  $result .= ' ';
879  }
880  }
881  $result .= $linkstart.$this->ref.$linkend;
882 
883  global $action;
884  $hookmanager->initHooks(array($this->element . 'dao'));
885  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
886  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
887  if ($reshook > 0) {
888  $result = $hookmanager->resPrint;
889  } else {
890  $result .= $hookmanager->resPrint;
891  }
892  return $result;
893  }
894 
901  public function getLibStatut($mode = 0)
902  {
903  return $this->LibStatut($this->status, $mode);
904  }
905 
906  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
914  public static function LibStatut($status, $mode = 0)
915  {
916  // phpcs:enable
917  global $langs;
918  return '';
919  }
920 
921 
928  public function initAsSpecimen()
929  {
930  global $conf, $user;
931 
932  $this->id = 0;
933  $this->specimen = 1;
934  $this->label = '0a1b2c3e4f59999999';
935  $this->entity = 1;
936  $this->filename = 'myspecimenfilefile.pdf';
937  $this->filepath = '/aaa/bbb';
938  $this->fullpath_orig = 'c:/file on my disk.pdf';
939  $this->description = 'This is a long description of file';
940  $this->keywords = 'key1,key2';
941  $this->cover = '1';
942  $this->position = 5;
943  $this->gen_or_uploaded = 'uploaded';
944  $this->extraparams = '';
945  $this->date_c = (dol_now() - 3600 * 24 * 10);
946  $this->date_m = '';
947  $this->fk_user_c = $user->id;
948  $this->fk_user_m = '';
949  $this->acl = '';
950  $this->src_object_type = 'product';
951  $this->src_object_id = 1;
952  }
953 }
954 
955 
960 {
964  public $label;
965 
969  public $entity;
970 
971  public $filename;
972  public $filepath;
973  public $fullpath_orig;
974 
978  public $description;
979 
980  public $keywords;
981  public $cover;
982  public $position;
983  public $gen_or_uploaded; // can be 'generated', 'uploaded', 'unknown'
984  public $extraparams;
985  public $date_c = '';
986  public $date_m = '';
987 
991  public $fk_user_c;
992 
996  public $fk_user_m;
997 
998  public $acl;
999  public $src_object_type;
1000  public $src_object_id;
1001 }
$object ref
Definition: info.php:78
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
deleteExtraFields()
Delete all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage ECM files.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
getLibStatut($mode=0)
Retourne le libelle du status d'un user (actif, inactif)
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
update(User $user, $notrigger=false)
Update object into database.
getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
Return a link to the object card (with optionaly the picto)
fetch($id, $ref='', $relativepath='', $hashoffile='', $hashforshare='', $src_object_type='', $src_object_id=0)
Load object in memory from the database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object in memory from the database.
__construct(DoliDB $db)
Constructor.
create(User $user, $notrigger=false)
Create object into database.
static LibStatut($status, $mode=0)
Return the status.
Class of an index line of a document.
Class to manage Dolibarr users.
Definition: user.class.php:47
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_hash($chain, $type='0')
Returns a hash (non reversible encryption) of a string.
$conf db
API class for accounts.
Definition: inc.php:41