dolibarr  x.y.z
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  $ref = '';
244  if (!empty($this->ref)) {
245  $ref = $this->ref;
246  } else {
247  include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
248  $ref = dol_hash($this->filepath.'/'.$this->filename, 3);
249  }
250 
251  $maxposition = 0;
252  if (empty($this->position)) {
253  // Get max used
254  $sql = "SELECT MAX(position) as maxposition FROM ".MAIN_DB_PREFIX.$this->table_element;
255  $sql .= " WHERE filepath ='".$this->db->escape($this->filepath)."'";
256 
257  $resql = $this->db->query($sql);
258  if ($resql) {
259  $obj = $this->db->fetch_object($resql);
260  $maxposition = (int) $obj->maxposition;
261  } else {
262  $this->errors[] = 'Error '.$this->db->lasterror();
263  return --$error;
264  }
265  $maxposition = $maxposition + 1;
266  } else {
267  $maxposition = $this->position;
268  }
269 
270  // Check parameters
271  if (empty($this->filename) || empty($this->filepath)) {
272  $this->errors[] = 'Bad property filename or filepath';
273  return --$error;
274  }
275  if (!isset($this->entity)) {
276  $this->entity = $conf->entity;
277  }
278  // Put here code to add control on parameters values
279 
280  // Insert request
281  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
282  $sql .= 'ref,';
283  $sql .= 'label,';
284  $sql .= 'share,';
285  $sql .= 'entity,';
286  $sql .= 'filename,';
287  $sql .= 'filepath,';
288  $sql .= 'fullpath_orig,';
289  $sql .= 'description,';
290  $sql .= 'keywords,';
291  $sql .= 'cover,';
292  $sql .= 'position,';
293  $sql .= 'gen_or_uploaded,';
294  $sql .= 'extraparams,';
295  $sql .= 'date_c,';
296  $sql .= 'tms,';
297  $sql .= 'fk_user_c,';
298  $sql .= 'fk_user_m,';
299  $sql .= 'acl,';
300  $sql .= 'src_object_type,';
301  $sql .= 'src_object_id';
302  $sql .= ') VALUES (';
303  $sql .= " '".$this->db->escape($ref)."', ";
304  $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
305  $sql .= ' '.(!isset($this->share) ? 'NULL' : "'".$this->db->escape($this->share)."'").',';
306  $sql .= ' '.((int) $this->entity).',';
307  $sql .= ' '.(!isset($this->filename) ? 'NULL' : "'".$this->db->escape($this->filename)."'").',';
308  $sql .= ' '.(!isset($this->filepath) ? 'NULL' : "'".$this->db->escape($this->filepath)."'").',';
309  $sql .= ' '.(!isset($this->fullpath_orig) ? 'NULL' : "'".$this->db->escape($this->fullpath_orig)."'").',';
310  $sql .= ' '.(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").',';
311  $sql .= ' '.(!isset($this->keywords) ? 'NULL' : "'".$this->db->escape($this->keywords)."'").',';
312  $sql .= ' '.(!isset($this->cover) ? 'NULL' : "'".$this->db->escape($this->cover)."'").',';
313  $sql .= ' '.((int) $maxposition).',';
314  $sql .= ' '.(!isset($this->gen_or_uploaded) ? 'NULL' : "'".$this->db->escape($this->gen_or_uploaded)."'").',';
315  $sql .= ' '.(!isset($this->extraparams) ? 'NULL' : "'".$this->db->escape($this->extraparams)."'").',';
316  $sql .= " '".$this->db->idate($this->date_c)."',";
317  $sql .= ' '.(!isset($this->date_m) || dol_strlen($this->date_m) == 0 ? 'NULL' : "'".$this->db->idate($this->date_m)."'").',';
318  $sql .= ' '.(!isset($this->fk_user_c) ? $user->id : $this->fk_user_c).',';
319  $sql .= ' '.(!isset($this->fk_user_m) ? 'NULL' : $this->fk_user_m).',';
320  $sql .= ' '.(!isset($this->acl) ? 'NULL' : "'".$this->db->escape($this->acl)."'").',';
321  $sql .= ' '.(!isset($this->src_object_type) ? 'NULL' : "'".$this->db->escape($this->src_object_type)."'").',';
322  $sql .= ' '.(!isset($this->src_object_id) ? 'NULL' : $this->src_object_id);
323  $sql .= ')';
324 
325  $this->db->begin();
326 
327  $resql = $this->db->query($sql);
328  if (!$resql) {
329  $error++;
330  if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
331  $this->errors[] = 'Error DB_ERROR_RECORD_ALREADY_EXISTS : '.$this->db->lasterror();
332  } else {
333  $this->errors[] = 'Error '.$this->db->lasterror();
334  }
335  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
336  }
337 
338  if (!$error) {
339  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
340  $this->position = $maxposition;
341 
342  // Triggers
343  if (!$notrigger) {
344  // Call triggers
345  $result = $this->call_trigger(strtoupper(get_class($this)).'_CREATE', $user);
346  if ($result < 0) {
347  $error++;
348  }
349  // End call triggers
350  }
351  }
352 
353  // Commit or rollback
354  if ($error) {
355  $this->db->rollback();
356 
357  return -1 * $error;
358  } else {
359  $this->db->commit();
360 
361  return $this->id;
362  }
363  }
364 
377  public function fetch($id, $ref = '', $relativepath = '', $hashoffile = '', $hashforshare = '', $src_object_type = '', $src_object_id = 0)
378  {
379  global $conf;
380 
381  dol_syslog(__METHOD__, LOG_DEBUG);
382 
383  $sql = 'SELECT';
384  $sql .= ' t.rowid,';
385  $sql .= " t.ref,";
386  $sql .= " t.label,";
387  $sql .= " t.share,";
388  $sql .= " t.entity,";
389  $sql .= " t.filename,";
390  $sql .= " t.filepath,";
391  $sql .= " t.fullpath_orig,";
392  $sql .= " t.description,";
393  $sql .= " t.keywords,";
394  $sql .= " t.cover,";
395  $sql .= " t.position,";
396  $sql .= " t.gen_or_uploaded,";
397  $sql .= " t.extraparams,";
398  $sql .= " t.date_c,";
399  $sql .= " t.tms as date_m,";
400  $sql .= " t.fk_user_c,";
401  $sql .= " t.fk_user_m,";
402  $sql .= ' t.note_private,';
403  $sql .= ' t.note_public,';
404  $sql .= " t.acl,";
405  $sql .= " t.src_object_type,";
406  $sql .= " t.src_object_id";
407  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
408  $sql .= ' WHERE 1 = 1';
409  /* Fetching this table depends on filepath+filename, it must not depends on entity because filesystem on disk does not know what is Dolibarr entities
410  if (isModEnabled('multicompany')) {
411  $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
412  }*/
413  if ($relativepath) {
414  $relativepathwithnoexe = preg_replace('/\.noexe$/', '', $relativepath); // We must never have the .noexe into the database
415  $sql .= " AND t.filepath = '".$this->db->escape(dirname($relativepath))."'";
416  $filename = basename($relativepathwithnoexe);
417  if ($filename != '*') {
418  $sql .= " AND t.filename = '".$this->db->escape($filename)."'";
419  }
420  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
421  } elseif (!empty($ref)) { // hash of file path
422  $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
423  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
424  } elseif (!empty($hashoffile)) { // hash of content
425  $sql .= " AND t.label = '".$this->db->escape($hashoffile)."'";
426  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
427  } elseif (!empty($hashforshare)) {
428  if ($hashforshare != 'shared') {
429  $sql .= " AND t.share = '".$this->db->escape($hashforshare)."'";
430  } else {
431  $sql .= " AND t.share IS NOT NULL AND t.share <> ''";
432  }
433  //$sql .= " AND t.entity = ".$conf->entity; // hashforshare already unique
434  } elseif ($src_object_type && $src_object_id) {
435  // Warning: May return several record, and only first one is returned !
436  $sql .= " AND t.src_object_type = '".$this->db->escape($src_object_type)."' AND t.src_object_id = ".((int) $src_object_id);
437  $sql .= " AND t.entity = ".((int) $conf->entity);
438  } else {
439  $sql .= ' AND t.rowid = '.((int) $id); // rowid already unique
440  }
441 
442  $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
443  $this->db->order('t.rowid', 'ASC');
444 
445  $resql = $this->db->query($sql);
446  if ($resql) {
447  $numrows = $this->db->num_rows($resql);
448  if ($numrows) {
449  $obj = $this->db->fetch_object($resql);
450 
451  $this->id = $obj->rowid;
452  $this->ref = $obj->ref;
453  $this->label = $obj->label;
454  $this->share = $obj->share;
455  $this->entity = $obj->entity;
456  $this->filename = $obj->filename;
457  $this->filepath = $obj->filepath;
458  $this->fullpath_orig = $obj->fullpath_orig;
459  $this->description = $obj->description;
460  $this->keywords = $obj->keywords;
461  $this->cover = $obj->cover;
462  $this->position = $obj->position;
463  $this->gen_or_uploaded = $obj->gen_or_uploaded;
464  $this->extraparams = $obj->extraparams;
465  $this->date_c = $this->db->jdate($obj->date_c);
466  $this->date_m = $this->db->jdate($obj->date_m);
467  $this->fk_user_c = $obj->fk_user_c;
468  $this->fk_user_m = $obj->fk_user_m;
469  $this->note_private = $obj->note_private;
470  $this->note_public = $obj->note_public;
471  $this->acl = $obj->acl;
472  $this->src_object_type = $obj->src_object_type;
473  $this->src_object_id = $obj->src_object_id;
474  }
475 
476  // Retrieve all extrafields for ecm_files
477  // fetch optionals attributes and labels
478  $this->fetch_optionals();
479 
480  // $this->fetch_lines();
481 
482  $this->db->free($resql);
483 
484  if ($numrows) {
485  return 1;
486  } else {
487  return 0;
488  }
489  } else {
490  $this->errors[] = 'Error '.$this->db->lasterror();
491  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
492 
493  return -1;
494  }
495  }
496 
509  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
510  {
511  dol_syslog(__METHOD__, LOG_DEBUG);
512 
513  $sql = 'SELECT';
514  $sql .= ' t.rowid,';
515  $sql .= " t.label,";
516  $sql .= " t.share,";
517  $sql .= " t.entity,";
518  $sql .= " t.filename,";
519  $sql .= " t.filepath,";
520  $sql .= " t.fullpath_orig,";
521  $sql .= " t.description,";
522  $sql .= " t.keywords,";
523  $sql .= " t.cover,";
524  $sql .= " t.position,";
525  $sql .= " t.gen_or_uploaded,";
526  $sql .= " t.extraparams,";
527  $sql .= " t.date_c,";
528  $sql .= " t.tms as date_m,";
529  $sql .= " t.fk_user_c,";
530  $sql .= " t.fk_user_m,";
531  $sql .= " t.acl,";
532  $sql .= " t.src_object_type,";
533  $sql .= " t.src_object_id";
534  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
535 
536  // Manage filter
537  $sqlwhere = array();
538  if (count($filter) > 0) {
539  foreach ($filter as $key => $value) {
540  if ($key == 't.src_object_id') {
541  $sqlwhere[] = $key." = ".((int) $value);
542  } else {
543  $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
544  }
545  }
546  }
547  $sql .= ' WHERE 1 = 1';
548  /* Fetching this table depends on filepath+filename, it must not depends on entity
549  if (isModEnabled('multicompany')) {
550  $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
551  }*/
552  if (count($sqlwhere) > 0) {
553  $sql .= ' AND '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
554  }
555  if (!empty($sortfield)) {
556  $sql .= $this->db->order($sortfield, $sortorder);
557  }
558  if (!empty($limit)) {
559  $sql .= $this->db->plimit($limit, $offset);
560  }
561 
562  $this->lines = array();
563 
564  $resql = $this->db->query($sql);
565  if ($resql) {
566  $num = $this->db->num_rows($resql);
567 
568  while ($obj = $this->db->fetch_object($resql)) {
569  $line = new EcmfilesLine();
570 
571  $line->id = $obj->rowid;
572  $line->ref = $obj->rowid;
573  $line->label = $obj->label;
574  $line->share = $obj->share;
575  $line->entity = $obj->entity;
576  $line->filename = $obj->filename;
577  $line->filepath = $obj->filepath;
578  $line->fullpath_orig = $obj->fullpath_orig;
579  $line->description = $obj->description;
580  $line->keywords = $obj->keywords;
581  $line->cover = $obj->cover;
582  $line->position = $obj->position;
583  $line->gen_or_uploaded = $obj->gen_or_uploaded;
584  $line->extraparams = $obj->extraparams;
585  $line->date_c = $this->db->jdate($obj->date_c);
586  $line->date_m = $this->db->jdate($obj->date_m);
587  $line->fk_user_c = $obj->fk_user_c;
588  $line->fk_user_m = $obj->fk_user_m;
589  $line->acl = $obj->acl;
590  $line->src_object_type = $obj->src_object_type;
591  $line->src_object_id = $obj->src_object_id;
592  $this->lines[] = $line;
593  }
594  $this->db->free($resql);
595 
596  return $num;
597  } else {
598  $this->errors[] = 'Error '.$this->db->lasterror();
599  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
600 
601  return -1;
602  }
603  }
604 
613  public function update(User $user, $notrigger = false)
614  {
615  global $conf;
616 
617  $error = 0;
618 
619  dol_syslog(__METHOD__, LOG_DEBUG);
620 
621  // Clean parameters
622 
623  if (isset($this->ref)) {
624  $this->ref = trim($this->ref);
625  }
626  if (isset($this->label)) {
627  $this->label = trim($this->label);
628  }
629  if (isset($this->share)) {
630  $this->share = trim($this->share);
631  }
632  if (isset($this->entity)) {
633  $this->entity = trim($this->entity);
634  }
635  if (isset($this->filename)) {
636  $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
637  }
638  if (isset($this->filepath)) {
639  $this->filepath = trim($this->filepath);
640  $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
641  }
642  if (isset($this->fullpath_orig)) {
643  $this->fullpath_orig = trim($this->fullpath_orig);
644  }
645  if (isset($this->description)) {
646  $this->description = trim($this->description);
647  }
648  if (isset($this->keywords)) {
649  $this->keywords = trim($this->keywords);
650  }
651  if (isset($this->cover)) {
652  $this->cover = trim($this->cover);
653  }
654  if (isset($this->gen_or_uploaded)) {
655  $this->gen_or_uploaded = trim($this->gen_or_uploaded);
656  }
657  if (isset($this->extraparams)) {
658  $this->extraparams = trim($this->extraparams);
659  }
660  if (isset($this->fk_user_m)) {
661  $this->fk_user_m = trim($this->fk_user_m);
662  }
663  if (isset($this->acl)) {
664  $this->acl = trim($this->acl);
665  }
666  if (isset($this->src_object_type)) {
667  $this->src_object_type = trim($this->src_object_type);
668  }
669 
670  // Check parameters
671  // Put here code to add a control on parameters values
672 
673  // Update request
674  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
675  $sql .= " ref = '".$this->db->escape(dol_hash($this->filepath."/".$this->filename, 3))."',";
676  $sql .= ' label = '.(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
677  $sql .= ' share = '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null").',';
678  $sql .= ' entity = '.(isset($this->entity) ? $this->entity : $conf->entity).',';
679  $sql .= ' filename = '.(isset($this->filename) ? "'".$this->db->escape($this->filename)."'" : "null").',';
680  $sql .= ' filepath = '.(isset($this->filepath) ? "'".$this->db->escape($this->filepath)."'" : "null").',';
681  $sql .= ' fullpath_orig = '.(isset($this->fullpath_orig) ? "'".$this->db->escape($this->fullpath_orig)."'" : "null").',';
682  $sql .= ' description = '.(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").',';
683  $sql .= ' keywords = '.(isset($this->keywords) ? "'".$this->db->escape($this->keywords)."'" : "null").',';
684  $sql .= ' cover = '.(isset($this->cover) ? "'".$this->db->escape($this->cover)."'" : "null").',';
685  $sql .= ' position = '.(isset($this->position) ? $this->db->escape($this->position) : "0").',';
686  $sql .= ' gen_or_uploaded = '.(isset($this->gen_or_uploaded) ? "'".$this->db->escape($this->gen_or_uploaded)."'" : "null").',';
687  $sql .= ' extraparams = '.(isset($this->extraparams) ? "'".$this->db->escape($this->extraparams)."'" : "null").',';
688  $sql .= ' date_c = '.(!isset($this->date_c) || dol_strlen($this->date_c) != 0 ? "'".$this->db->idate($this->date_c)."'" : 'null').',';
689  //$sql .= ' tms = '.(! isset($this->date_m) || dol_strlen($this->date_m) != 0 ? "'".$this->db->idate($this->date_m)."'" : 'null').','; // Field automatically updated
690  $sql .= ' fk_user_m = '.($this->fk_user_m > 0 ? $this->fk_user_m : $user->id).',';
691  $sql .= ' acl = '.(isset($this->acl) ? "'".$this->db->escape($this->acl)."'" : "null").',';
692  $sql .= ' src_object_id = '.($this->src_object_id > 0 ? $this->src_object_id : "null").',';
693  $sql .= ' src_object_type = '.(isset($this->src_object_type) ? "'".$this->db->escape($this->src_object_type)."'" : "null");
694  $sql .= ' WHERE rowid='.((int) $this->id);
695 
696  $this->db->begin();
697 
698  $resql = $this->db->query($sql);
699  if (!$resql) {
700  $error++;
701  $this->errors[] = 'Error '.$this->db->lasterror();
702  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
703  }
704 
705  // Triggers
706  if (!$error && !$notrigger) {
707  // Call triggers
708  $result = $this->call_trigger(strtoupper(get_class($this)).'_MODIFY', $user);
709  if ($result < 0) {
710  $error++;
711  } //Do also here what you must do to rollback action if trigger fail
712  // End call triggers
713  }
714 
715  // Commit or rollback
716  if ($error) {
717  $this->db->rollback();
718 
719  return -1 * $error;
720  } else {
721  $this->db->commit();
722 
723  return 1;
724  }
725  }
726 
735  public function delete(User $user, $notrigger = false)
736  {
737  dol_syslog(__METHOD__, LOG_DEBUG);
738 
739  $error = 0;
740 
741  $this->db->begin();
742 
743  // Triggers
744  if (!$notrigger) {
745  // Call triggers
746  $result = $this->call_trigger(strtoupper(get_class($this)).'_DELETE', $user);
747  if ($result < 0) {
748  $error++;
749  } //Do also here what you must do to rollback action if trigger fail
750  // End call triggers
751  }
752 
753  // If you need to delete child tables to, you can insert them here
754  if (!$error) {
755  $result = $this->deleteExtraFields();
756  if (!$result) {
757  dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
758  $error++;
759  }
760  }
761  if (!$error) {
762  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
763  $sql .= ' WHERE rowid='.((int) $this->id);
764 
765  $resql = $this->db->query($sql);
766  if (!$resql) {
767  $error++;
768  $this->errors[] = 'Error '.$this->db->lasterror();
769  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
770  }
771  }
772 
773  // Commit or rollback
774  if ($error) {
775  $this->db->rollback();
776 
777  return -1 * $error;
778  } else {
779  $this->db->commit();
780 
781  return 1;
782  }
783  }
784 
792  public function createFromClone(User $user, $fromid)
793  {
794  dol_syslog(__METHOD__, LOG_DEBUG);
795 
796  $error = 0;
797  $object = new Ecmfiles($this->db);
798 
799  $this->db->begin();
800 
801  // Load source object
802  $object->fetch($fromid);
803  // Reset object
804  $object->id = 0;
805 
806  // Clear fields
807  // ...
808 
809  // Create clone
810  $object->context['createfromclone'] = 'createfromclone';
811  $result = $object->create($user);
812 
813  // Other options
814  if ($result < 0) {
815  $error++;
816  $this->errors = $object->errors;
817  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
818  }
819 
820  unset($object->context['createfromclone']);
821 
822  // End
823  if (!$error) {
824  $this->db->commit();
825 
826  return $object->id;
827  } else {
828  $this->db->rollback();
829 
830  return -1;
831  }
832  }
833 
844  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
845  {
846  global $db, $conf, $langs;
847  global $dolibarr_main_authentication, $dolibarr_main_demo;
848  global $menumanager, $hookmanager;
849 
850  if (!empty($conf->dol_no_mouse_hover)) {
851  $notooltip = 1; // Force disable tooltips
852  }
853 
854  $result = '';
855 
856  $label = '<u>'.$langs->trans("MyModule").'</u>';
857  $label .= '<br>';
858  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
859 
860  $url = DOL_URL_ROOT.'/ecm/'.$this->table_name.'_card.php?id='.$this->id;
861 
862  $linkclose = '';
863  if (empty($notooltip)) {
864  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
865  $label = $langs->trans("ShowProject");
866  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
867  }
868  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
869  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
870  } else {
871  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
872  }
873 
874  $linkstart = '<a href="'.$url.'"';
875  $linkstart .= $linkclose.'>';
876  $linkend = '</a>';
877 
878  if ($withpicto) {
879  $result .= ($linkstart.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
880  if ($withpicto != 2) {
881  $result .= ' ';
882  }
883  }
884  $result .= $linkstart.$this->ref.$linkend;
885 
886  global $action;
887  $hookmanager->initHooks(array($this->element . 'dao'));
888  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
889  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
890  if ($reshook > 0) {
891  $result = $hookmanager->resPrint;
892  } else {
893  $result .= $hookmanager->resPrint;
894  }
895  return $result;
896  }
897 
904  public function getLibStatut($mode = 0)
905  {
906  return $this->LibStatut($this->status, $mode);
907  }
908 
909  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
917  public static function LibStatut($status, $mode = 0)
918  {
919  // phpcs:enable
920  global $langs;
921  return '';
922  }
923 
924 
931  public function initAsSpecimen()
932  {
933  global $conf, $user;
934 
935  $this->id = 0;
936  $this->specimen = 1;
937  $this->label = '0a1b2c3e4f59999999';
938  $this->entity = 1;
939  $this->filename = 'myspecimenfilefile.pdf';
940  $this->filepath = '/aaa/bbb';
941  $this->fullpath_orig = 'c:/file on my disk.pdf';
942  $this->description = 'This is a long description of file';
943  $this->keywords = 'key1,key2';
944  $this->cover = '1';
945  $this->position = 5;
946  $this->gen_or_uploaded = 'uploaded';
947  $this->extraparams = '';
948  $this->date_c = (dol_now() - 3600 * 24 * 10);
949  $this->date_m = '';
950  $this->fk_user_c = $user->id;
951  $this->fk_user_m = '';
952  $this->acl = '';
953  $this->src_object_type = 'product';
954  $this->src_object_id = 1;
955  }
956 }
957 
958 
963 {
967  public $label;
968 
972  public $entity;
973 
974  public $filename;
975  public $filepath;
976  public $fullpath_orig;
977 
981  public $description;
982 
983  public $keywords;
984  public $cover;
985  public $position;
986  public $gen_or_uploaded; // can be 'generated', 'uploaded', 'unknown'
987  public $extraparams;
988  public $date_c = '';
989  public $date_m = '';
990 
994  public $fk_user_c;
995 
999  public $fk_user_m;
1000 
1001  public $acl;
1002  public $src_object_type;
1003  public $src_object_id;
1004 }
$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:45
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