dolibarr  18.0.0
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  public $fields = array(
159  'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>'1', 'position'=>1, 'notnull'=>1, 'visible'=>0, 'noteditable'=>'1', 'index'=>1, 'css'=>'left', 'comment'=>"Id"),
160  'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>'1', 'position'=>20, 'notnull'=>1, 'visible'=>-1, 'index'=>1, 'searchall'=>1, 'showoncombobox'=>'1', 'validate'=>'1', 'comment'=>"contains hash from filename+filepath"),
161  'label' => array('type'=>'varchar(128)', 'label'=>'Label', 'enabled'=>'1', 'position'=>30, 'notnull'=>0, 'visible'=>-1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1', 'comment'=>"contains hash of file content"),
162  'share' => array('type'=>'varchar(128)', 'label'=>'Share', 'enabled'=>'1', 'position'=>40, 'notnull'=>0, 'visible'=>-1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1', 'comment' => "contains hash for file sharing"),
163  'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => 1, 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 50, 'index' => 1),
164  'filepath' => array('type'=>'varchar(255)', 'label'=>'FilePath', 'enabled'=>'1', 'position'=>60, 'notnull'=>0, 'visible'=>0, 'searchall'=>0, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=> "relative to dolibarr document dir. Example module/def"),
165  'filename' => array('type'=>'varchar(255)', 'label'=>'FileName', 'enabled'=>'1', 'position'=>70, 'notnull'=>0, 'visible'=>1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=>"file name only without any directory"),
166  'src_object_type' => array('type'=>'varchar(64)', 'label'=>'SourceType', 'enabled'=>'1', 'position'=>80, 'notnull'=>0, 'visible'=>0, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=> "Source object type ('proposal', 'invoice', ...)"),
167  'src_object_id' => array('type' => 'integer', 'label' => 'SourceID', 'default' => 1, 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'position' => 90, 'index' => 1, 'comment' => "Source object id"),
168  'fullpath_orig' => array('type'=>'varchar(750)', 'label'=>'FullPathOrig', 'enabled'=>'1', 'position'=>100, 'notnull'=>0, 'visible'=>0, 'searchall'=>0, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=>"full path of original filename, when file is uploaded from a local computer"),
169  'description' => array('type' => 'text', 'label' => 'Description', 'enabled' => 1, 'visible' => 0, 'position' => 110),
170  'keywords' => array('type'=>'varchar(750)', 'label'=>'Keywords', 'enabled'=>'1', 'position'=>120, 'notnull'=>0, 'visible'=>1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=>"list of keywords, separated with comma. Must be limited to most important keywords."),
171  'cover' => array('type' => 'text', 'label' => 'Cover', 'enabled' => 1, 'visible' => 0, 'position' => 130, 'comment'=>"is this file a file to use for a cover"),
172  'position' => array('type' => 'integer', 'label' => 'Position', 'default' => 1, 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 140, 'index' => 1, 'comment' => "position of file among others"),
173  'gen_or_uploaded' => array('type'=>'varchar(12)', 'label'=>'GenOrUpload', 'enabled'=>'1', 'position'=>150, 'notnull'=>0, 'visible'=>-1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=>"'generated' or 'uploaded'"),
174  'extraparams' => array('type'=>'varchar(255)', 'label'=>'ExtraParams', 'enabled'=>'1', 'position'=>160, 'notnull'=>0, 'visible'=>1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1', 'comment' => "for stocking other parameters with json format"),
175  'date_c' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 170),
176  'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 175),
177  'fk_user_c' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>'1', 'position'=>510, 'notnull'=>1, 'visible'=>-2, 'foreignkey'=>'user.rowid',),
178  'fk_user_m' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>'1', 'position'=>511, 'notnull'=>-1, 'visible'=>-2,),
179  'note_public' => array('type' => 'text', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 155),
180  'note_private' => array('type' => 'text', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 160),
181  'acl' => array('type' => 'text', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 160, 'comment' => "for future permission 'per file'"),
182  );
183 
184 
190  public function __construct(DoliDB $db)
191  {
192  $this->db = $db;
193  }
194 
202  public function create(User $user, $notrigger = false)
203  {
204  global $conf;
205 
206  dol_syslog(__METHOD__, LOG_DEBUG);
207 
208  $error = 0;
209 
210  // Clean parameters
211  if (isset($this->ref)) {
212  $this->ref = trim($this->ref);
213  }
214  if (isset($this->label)) {
215  $this->label = trim($this->label);
216  }
217  if (isset($this->share)) {
218  $this->share = trim($this->share);
219  }
220  if (isset($this->entity)) {
221  $this->entity = (int) $this->entity;
222  }
223  if (isset($this->filename)) {
224  $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
225  }
226  if (isset($this->filepath)) {
227  $this->filepath = trim($this->filepath);
228  $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
229  }
230  if (isset($this->fullpath_orig)) {
231  $this->fullpath_orig = trim($this->fullpath_orig);
232  }
233  if (isset($this->description)) {
234  $this->description = trim($this->description);
235  }
236  if (isset($this->keywords)) {
237  $this->keywords = trim($this->keywords);
238  }
239  if (isset($this->cover)) {
240  $this->cover = trim($this->cover);
241  }
242  if (isset($this->gen_or_uploaded)) {
243  $this->gen_or_uploaded = trim($this->gen_or_uploaded);
244  }
245  if (isset($this->extraparams)) {
246  $this->extraparams = trim($this->extraparams);
247  }
248  if (isset($this->fk_user_c)) {
249  $this->fk_user_c = (int) $this->fk_user_c;
250  }
251  if (isset($this->fk_user_m)) {
252  $this->fk_user_m = (int) $this->fk_user_m;
253  }
254  if (isset($this->acl)) {
255  $this->acl = trim($this->acl);
256  }
257  if (isset($this->src_object_type)) {
258  $this->src_object_type = trim($this->src_object_type);
259  }
260  if (empty($this->date_c)) {
261  $this->date_c = dol_now();
262  }
263  if (empty($this->date_m)) {
264  $this->date_m = dol_now();
265  }
266 
267  // If ref not defined
268  if (empty($this->ref)) {
269  include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
270  $this->ref = dol_hash($this->filepath.'/'.$this->filename, 3);
271  }
272 
273  $maxposition = 0;
274  if (empty($this->position)) {
275  // Get max used
276  $sql = "SELECT MAX(position) as maxposition FROM ".MAIN_DB_PREFIX.$this->table_element;
277  $sql .= " WHERE filepath ='".$this->db->escape($this->filepath)."'";
278 
279  $resql = $this->db->query($sql);
280  if ($resql) {
281  $obj = $this->db->fetch_object($resql);
282  $maxposition = (int) $obj->maxposition;
283  } else {
284  $this->errors[] = 'Error '.$this->db->lasterror();
285  return --$error;
286  }
287  $maxposition = $maxposition + 1;
288  } else {
289  $maxposition = $this->position;
290  }
291 
292  // Check parameters
293  if (empty($this->filename) || empty($this->filepath)) {
294  $this->errors[] = 'Bad property filename or filepath';
295  return --$error;
296  }
297  if (!isset($this->entity)) {
298  $this->entity = $conf->entity;
299  }
300  // Put here code to add control on parameters values
301 
302  // Insert request
303  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
304  $sql .= 'ref,';
305  $sql .= 'label,';
306  $sql .= 'share,';
307  $sql .= 'entity,';
308  $sql .= 'filename,';
309  $sql .= 'filepath,';
310  $sql .= 'fullpath_orig,';
311  $sql .= 'description,';
312  $sql .= 'keywords,';
313  $sql .= 'cover,';
314  $sql .= 'position,';
315  $sql .= 'gen_or_uploaded,';
316  $sql .= 'extraparams,';
317  $sql .= 'date_c,';
318  $sql .= 'tms,';
319  $sql .= 'fk_user_c,';
320  $sql .= 'fk_user_m,';
321  $sql .= 'acl,';
322  $sql .= 'src_object_type,';
323  $sql .= 'src_object_id';
324  $sql .= ') VALUES (';
325  $sql .= " '".$this->db->escape($this->ref)."', ";
326  $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
327  $sql .= ' '.(!isset($this->share) ? 'NULL' : "'".$this->db->escape($this->share)."'").',';
328  $sql .= ' '.((int) $this->entity).',';
329  $sql .= ' '.(!isset($this->filename) ? 'NULL' : "'".$this->db->escape($this->filename)."'").',';
330  $sql .= ' '.(!isset($this->filepath) ? 'NULL' : "'".$this->db->escape($this->filepath)."'").',';
331  $sql .= ' '.(!isset($this->fullpath_orig) ? 'NULL' : "'".$this->db->escape($this->fullpath_orig)."'").',';
332  $sql .= ' '.(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").',';
333  $sql .= ' '.(!isset($this->keywords) ? 'NULL' : "'".$this->db->escape($this->keywords)."'").',';
334  $sql .= ' '.(!isset($this->cover) ? 'NULL' : "'".$this->db->escape($this->cover)."'").',';
335  $sql .= ' '.((int) $maxposition).',';
336  $sql .= ' '.(!isset($this->gen_or_uploaded) ? 'NULL' : "'".$this->db->escape($this->gen_or_uploaded)."'").',';
337  $sql .= ' '.(!isset($this->extraparams) ? 'NULL' : "'".$this->db->escape($this->extraparams)."'").',';
338  $sql .= " '".$this->db->idate($this->date_c)."',";
339  $sql .= ' '.(!isset($this->date_m) || dol_strlen($this->date_m) == 0 ? 'NULL' : "'".$this->db->idate($this->date_m)."'").',';
340  $sql .= ' '.(!isset($this->fk_user_c) ? $user->id : $this->fk_user_c).',';
341  $sql .= ' '.(!isset($this->fk_user_m) ? 'NULL' : $this->fk_user_m).',';
342  $sql .= ' '.(!isset($this->acl) ? 'NULL' : "'".$this->db->escape($this->acl)."'").',';
343  $sql .= ' '.(!isset($this->src_object_type) ? 'NULL' : "'".$this->db->escape($this->src_object_type)."'").',';
344  $sql .= ' '.(!isset($this->src_object_id) ? 'NULL' : $this->src_object_id);
345  $sql .= ')';
346 
347  $this->db->begin();
348 
349  $resql = $this->db->query($sql);
350  if (!$resql) {
351  $error++;
352  if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
353  $this->errors[] = 'Error DB_ERROR_RECORD_ALREADY_EXISTS : '.$this->db->lasterror();
354  } else {
355  $this->errors[] = 'Error '.$this->db->lasterror();
356  }
357  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
358  }
359 
360  if (!$error) {
361  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
362  $this->position = $maxposition;
363 
364  // Triggers
365  if (!$notrigger) {
366  // Call triggers
367  $result = $this->call_trigger(strtoupper(get_class($this)).'_CREATE', $user);
368  if ($result < 0) {
369  $error++;
370  }
371  // End call triggers
372  }
373  }
374 
375  // Commit or rollback
376  if ($error) {
377  $this->db->rollback();
378 
379  return -1 * $error;
380  } else {
381  $this->db->commit();
382 
383  return $this->id;
384  }
385  }
386 
399  public function fetch($id, $ref = '', $relativepath = '', $hashoffile = '', $hashforshare = '', $src_object_type = '', $src_object_id = 0)
400  {
401  global $conf;
402 
403  dol_syslog(__METHOD__, LOG_DEBUG);
404 
405  $sql = 'SELECT';
406  $sql .= ' t.rowid,';
407  $sql .= " t.ref,";
408  $sql .= " t.label,";
409  $sql .= " t.share,";
410  $sql .= " t.entity,";
411  $sql .= " t.filename,";
412  $sql .= " t.filepath,";
413  $sql .= " t.fullpath_orig,";
414  $sql .= " t.description,";
415  $sql .= " t.keywords,";
416  $sql .= " t.cover,";
417  $sql .= " t.position,";
418  $sql .= " t.gen_or_uploaded,";
419  $sql .= " t.extraparams,";
420  $sql .= " t.date_c,";
421  $sql .= " t.tms as date_m,";
422  $sql .= " t.fk_user_c,";
423  $sql .= " t.fk_user_m,";
424  $sql .= ' t.note_private,';
425  $sql .= ' t.note_public,';
426  $sql .= " t.acl,";
427  $sql .= " t.src_object_type,";
428  $sql .= " t.src_object_id";
429  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
430  $sql .= ' WHERE 1 = 1';
431  /* Fetching this table depends on filepath+filename, it must not depends on entity because filesystem on disk does not know what is Dolibarr entities
432  if (isModEnabled('multicompany')) {
433  $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
434  }*/
435  if ($relativepath) {
436  $relativepathwithnoexe = preg_replace('/\.noexe$/', '', $relativepath); // We must never have the .noexe into the database
437  $sql .= " AND t.filepath = '".$this->db->escape(dirname($relativepath))."'";
438  $filename = basename($relativepathwithnoexe);
439  if ($filename != '*') {
440  $sql .= " AND t.filename = '".$this->db->escape($filename)."'";
441  }
442  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
443  } elseif (!empty($ref)) { // hash of file path
444  $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
445  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
446  } elseif (!empty($hashoffile)) { // hash of content
447  $sql .= " AND t.label = '".$this->db->escape($hashoffile)."'";
448  $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
449  } elseif (!empty($hashforshare)) {
450  if ($hashforshare != 'shared') {
451  $sql .= " AND t.share = '".$this->db->escape($hashforshare)."'";
452  } else {
453  $sql .= " AND t.share IS NOT NULL AND t.share <> ''";
454  }
455  //$sql .= " AND t.entity = ".$conf->entity; // hashforshare already unique
456  } elseif ($src_object_type && $src_object_id) {
457  // Warning: May return several record, and only first one is returned !
458  $sql .= " AND t.src_object_type = '".$this->db->escape($src_object_type)."' AND t.src_object_id = ".((int) $src_object_id);
459  $sql .= " AND t.entity = ".((int) $conf->entity);
460  } else {
461  $sql .= ' AND t.rowid = '.((int) $id); // rowid already unique
462  }
463 
464  $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
465  $this->db->order('t.rowid', 'ASC');
466 
467  $resql = $this->db->query($sql);
468  if ($resql) {
469  $numrows = $this->db->num_rows($resql);
470  if ($numrows) {
471  $obj = $this->db->fetch_object($resql);
472 
473  $this->id = $obj->rowid;
474  $this->ref = $obj->ref;
475  $this->label = $obj->label;
476  $this->share = $obj->share;
477  $this->entity = $obj->entity;
478  $this->filename = $obj->filename;
479  $this->filepath = $obj->filepath;
480  $this->fullpath_orig = $obj->fullpath_orig;
481  $this->description = $obj->description;
482  $this->keywords = $obj->keywords;
483  $this->cover = $obj->cover;
484  $this->position = $obj->position;
485  $this->gen_or_uploaded = $obj->gen_or_uploaded;
486  $this->extraparams = $obj->extraparams;
487  $this->date_c = $this->db->jdate($obj->date_c);
488  $this->date_m = $this->db->jdate($obj->date_m);
489  $this->fk_user_c = $obj->fk_user_c;
490  $this->fk_user_m = $obj->fk_user_m;
491  $this->note_private = $obj->note_private;
492  $this->note_public = $obj->note_public;
493  $this->acl = $obj->acl;
494  $this->src_object_type = $obj->src_object_type;
495  $this->src_object_id = $obj->src_object_id;
496  }
497 
498  // Retrieve all extrafields for ecm_files
499  // fetch optionals attributes and labels
500  $this->fetch_optionals();
501 
502  // $this->fetch_lines();
503 
504  $this->db->free($resql);
505 
506  if ($numrows) {
507  return 1;
508  } else {
509  return 0;
510  }
511  } else {
512  $this->errors[] = 'Error '.$this->db->lasterror();
513  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
514 
515  return -1;
516  }
517  }
518 
531  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
532  {
533  dol_syslog(__METHOD__, LOG_DEBUG);
534 
535  $sql = 'SELECT';
536  $sql .= ' t.rowid,';
537  $sql .= " t.label,";
538  $sql .= " t.share,";
539  $sql .= " t.entity,";
540  $sql .= " t.filename,";
541  $sql .= " t.filepath,";
542  $sql .= " t.fullpath_orig,";
543  $sql .= " t.description,";
544  $sql .= " t.keywords,";
545  $sql .= " t.cover,";
546  $sql .= " t.position,";
547  $sql .= " t.gen_or_uploaded,";
548  $sql .= " t.extraparams,";
549  $sql .= " t.date_c,";
550  $sql .= " t.tms as date_m,";
551  $sql .= " t.fk_user_c,";
552  $sql .= " t.fk_user_m,";
553  $sql .= " t.acl,";
554  $sql .= " t.src_object_type,";
555  $sql .= " t.src_object_id";
556  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
557 
558  // Manage filter
559  $sqlwhere = array();
560  if (count($filter) > 0) {
561  foreach ($filter as $key => $value) {
562  if ($key == 't.src_object_id') {
563  $sqlwhere[] = $key." = ".((int) $value);
564  } else {
565  $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
566  }
567  }
568  }
569  $sql .= ' WHERE 1 = 1';
570  /* Fetching this table depends on filepath+filename, it must not depends on entity
571  if (isModEnabled('multicompany')) {
572  $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
573  }*/
574  if (count($sqlwhere) > 0) {
575  $sql .= ' AND '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
576  }
577  if (!empty($sortfield)) {
578  $sql .= $this->db->order($sortfield, $sortorder);
579  }
580  if (!empty($limit)) {
581  $sql .= $this->db->plimit($limit, $offset);
582  }
583 
584  $this->lines = array();
585 
586  $resql = $this->db->query($sql);
587  if ($resql) {
588  $num = $this->db->num_rows($resql);
589 
590  while ($obj = $this->db->fetch_object($resql)) {
591  $line = new EcmFilesLine();
592 
593  $line->id = $obj->rowid;
594  $line->ref = $obj->rowid;
595  $line->label = $obj->label;
596  $line->share = $obj->share;
597  $line->entity = $obj->entity;
598  $line->filename = $obj->filename;
599  $line->filepath = $obj->filepath;
600  $line->fullpath_orig = $obj->fullpath_orig;
601  $line->description = $obj->description;
602  $line->keywords = $obj->keywords;
603  $line->cover = $obj->cover;
604  $line->position = $obj->position;
605  $line->gen_or_uploaded = $obj->gen_or_uploaded;
606  $line->extraparams = $obj->extraparams;
607  $line->date_c = $this->db->jdate($obj->date_c);
608  $line->date_m = $this->db->jdate($obj->date_m);
609  $line->fk_user_c = $obj->fk_user_c;
610  $line->fk_user_m = $obj->fk_user_m;
611  $line->acl = $obj->acl;
612  $line->src_object_type = $obj->src_object_type;
613  $line->src_object_id = $obj->src_object_id;
614  $this->lines[] = $line;
615  }
616  $this->db->free($resql);
617 
618  return $num;
619  } else {
620  $this->errors[] = 'Error '.$this->db->lasterror();
621  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
622 
623  return -1;
624  }
625  }
626 
635  public function update(User $user, $notrigger = false)
636  {
637  global $conf;
638 
639  $error = 0;
640 
641  dol_syslog(__METHOD__, LOG_DEBUG);
642 
643  // Clean parameters
644 
645  if (isset($this->ref)) {
646  $this->ref = trim($this->ref);
647  }
648  if (isset($this->label)) {
649  $this->label = trim($this->label);
650  }
651  if (isset($this->share)) {
652  $this->share = trim($this->share);
653  }
654  if (isset($this->entity)) {
655  $this->entity = trim($this->entity);
656  }
657  if (isset($this->filename)) {
658  $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
659  }
660  if (isset($this->filepath)) {
661  $this->filepath = trim($this->filepath);
662  $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
663  }
664  if (isset($this->fullpath_orig)) {
665  $this->fullpath_orig = trim($this->fullpath_orig);
666  }
667  if (isset($this->description)) {
668  $this->description = trim($this->description);
669  }
670  if (isset($this->keywords)) {
671  $this->keywords = trim($this->keywords);
672  }
673  if (isset($this->cover)) {
674  $this->cover = trim($this->cover);
675  }
676  if (isset($this->gen_or_uploaded)) {
677  $this->gen_or_uploaded = trim($this->gen_or_uploaded);
678  }
679  if (isset($this->extraparams)) {
680  $this->extraparams = trim($this->extraparams);
681  }
682  if (isset($this->fk_user_m)) {
683  $this->fk_user_m = trim($this->fk_user_m);
684  }
685  if (isset($this->acl)) {
686  $this->acl = trim($this->acl);
687  }
688  if (isset($this->src_object_type)) {
689  $this->src_object_type = trim($this->src_object_type);
690  }
691 
692  // Check parameters
693  // Put here code to add a control on parameters values
694 
695  // Update request
696  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
697  $sql .= " ref = '".$this->db->escape(dol_hash($this->filepath."/".$this->filename, 3))."',";
698  $sql .= ' label = '.(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
699  $sql .= ' share = '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null").',';
700  $sql .= ' entity = '.(isset($this->entity) ? $this->entity : $conf->entity).',';
701  $sql .= ' filename = '.(isset($this->filename) ? "'".$this->db->escape($this->filename)."'" : "null").',';
702  $sql .= ' filepath = '.(isset($this->filepath) ? "'".$this->db->escape($this->filepath)."'" : "null").',';
703  $sql .= ' fullpath_orig = '.(isset($this->fullpath_orig) ? "'".$this->db->escape($this->fullpath_orig)."'" : "null").',';
704  $sql .= ' description = '.(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").',';
705  $sql .= ' keywords = '.(isset($this->keywords) ? "'".$this->db->escape($this->keywords)."'" : "null").',';
706  $sql .= ' cover = '.(isset($this->cover) ? "'".$this->db->escape($this->cover)."'" : "null").',';
707  $sql .= ' position = '.(isset($this->position) ? $this->db->escape($this->position) : "0").',';
708  $sql .= ' gen_or_uploaded = '.(isset($this->gen_or_uploaded) ? "'".$this->db->escape($this->gen_or_uploaded)."'" : "null").',';
709  $sql .= ' extraparams = '.(isset($this->extraparams) ? "'".$this->db->escape($this->extraparams)."'" : "null").',';
710  $sql .= ' date_c = '.(!isset($this->date_c) || dol_strlen($this->date_c) != 0 ? "'".$this->db->idate($this->date_c)."'" : 'null').',';
711  //$sql .= ' tms = '.(! isset($this->date_m) || dol_strlen($this->date_m) != 0 ? "'".$this->db->idate($this->date_m)."'" : 'null').','; // Field automatically updated
712  $sql .= ' fk_user_m = '.($this->fk_user_m > 0 ? $this->fk_user_m : $user->id).',';
713  $sql .= ' acl = '.(isset($this->acl) ? "'".$this->db->escape($this->acl)."'" : "null").',';
714  $sql .= ' src_object_id = '.($this->src_object_id > 0 ? $this->src_object_id : "null").',';
715  $sql .= ' src_object_type = '.(isset($this->src_object_type) ? "'".$this->db->escape($this->src_object_type)."'" : "null");
716  $sql .= ' WHERE rowid='.((int) $this->id);
717 
718  $this->db->begin();
719 
720  $resql = $this->db->query($sql);
721  if (!$resql) {
722  $error++;
723  $this->errors[] = 'Error '.$this->db->lasterror();
724  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
725  }
726 
727  // Triggers
728  if (!$error && !$notrigger) {
729  // Call triggers
730  $result = $this->call_trigger(strtoupper(get_class($this)).'_MODIFY', $user);
731  if ($result < 0) {
732  $error++;
733  } //Do also here what you must do to rollback action if trigger fail
734  // End call triggers
735  }
736 
737  // Commit or rollback
738  if ($error) {
739  $this->db->rollback();
740 
741  return -1 * $error;
742  } else {
743  $this->db->commit();
744 
745  return 1;
746  }
747  }
748 
757  public function delete(User $user, $notrigger = false)
758  {
759  dol_syslog(__METHOD__, LOG_DEBUG);
760 
761  $error = 0;
762 
763  $this->db->begin();
764 
765  // Triggers
766  if (!$notrigger) {
767  // Call triggers
768  $result = $this->call_trigger(strtoupper(get_class($this)).'_DELETE', $user);
769  if ($result < 0) {
770  $error++;
771  } //Do also here what you must do to rollback action if trigger fail
772  // End call triggers
773  }
774 
775  // If you need to delete child tables to, you can insert them here
776  if (!$error) {
777  $result = $this->deleteExtraFields();
778  if (!$result) {
779  dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
780  $error++;
781  }
782  }
783  if (!$error) {
784  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
785  $sql .= ' WHERE rowid='.((int) $this->id);
786 
787  $resql = $this->db->query($sql);
788  if (!$resql) {
789  $error++;
790  $this->errors[] = 'Error '.$this->db->lasterror();
791  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
792  }
793  }
794 
795  // Commit or rollback
796  if ($error) {
797  $this->db->rollback();
798 
799  return -1 * $error;
800  } else {
801  $this->db->commit();
802 
803  return 1;
804  }
805  }
806 
814  public function createFromClone(User $user, $fromid)
815  {
816  dol_syslog(__METHOD__, LOG_DEBUG);
817 
818  $error = 0;
819  $object = new EcmFiles($this->db);
820 
821  $this->db->begin();
822 
823  // Load source object
824  $object->fetch($fromid);
825  // Reset object
826  $object->id = 0;
827 
828  // Clear fields
829  // ...
830 
831  // Create clone
832  $object->context['createfromclone'] = 'createfromclone';
833  $result = $object->create($user);
834 
835  // Other options
836  if ($result < 0) {
837  $error++;
838  $this->errors = $object->errors;
839  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
840  }
841 
842  unset($object->context['createfromclone']);
843 
844  // End
845  if (!$error) {
846  $this->db->commit();
847 
848  return $object->id;
849  } else {
850  $this->db->rollback();
851 
852  return -1;
853  }
854  }
855 
866  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
867  {
868  global $db, $conf, $langs;
869  global $dolibarr_main_authentication, $dolibarr_main_demo;
870  global $menumanager, $hookmanager;
871 
872  if (!empty($conf->dol_no_mouse_hover)) {
873  $notooltip = 1; // Force disable tooltips
874  }
875 
876  $result = '';
877 
878  $label = '<u>'.$langs->trans("MyModule").'</u>';
879  $label .= '<br>';
880  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
881 
882  $url = DOL_URL_ROOT.'/ecm/'.$this->table_name.'_card.php?id='.$this->id;
883 
884  $linkclose = '';
885  if (empty($notooltip)) {
886  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
887  $label = $langs->trans("ShowProject");
888  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
889  }
890  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
891  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
892  } else {
893  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
894  }
895 
896  $linkstart = '<a href="'.$url.'"';
897  $linkstart .= $linkclose.'>';
898  $linkend = '</a>';
899 
900  if ($withpicto) {
901  $result .= ($linkstart.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
902  if ($withpicto != 2) {
903  $result .= ' ';
904  }
905  }
906  $result .= $linkstart.$this->ref.$linkend;
907 
908  global $action;
909  $hookmanager->initHooks(array($this->element . 'dao'));
910  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
911  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
912  if ($reshook > 0) {
913  $result = $hookmanager->resPrint;
914  } else {
915  $result .= $hookmanager->resPrint;
916  }
917  return $result;
918  }
919 
926  public function getLibStatut($mode = 0)
927  {
928  return $this->LibStatut($this->status, $mode);
929  }
930 
931  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
939  public static function LibStatut($status, $mode = 0)
940  {
941  // phpcs:enable
942  global $langs;
943  return '';
944  }
945 
946 
953  public function initAsSpecimen()
954  {
955  global $conf, $user;
956 
957  $this->id = 0;
958  $this->specimen = 1;
959  $this->label = '0a1b2c3e4f59999999';
960  $this->entity = 1;
961  $this->filename = 'myspecimenfilefile.pdf';
962  $this->filepath = '/aaa/bbb';
963  $this->fullpath_orig = 'c:/file on my disk.pdf';
964  $this->description = 'This is a long description of file';
965  $this->keywords = 'key1,key2';
966  $this->cover = '1';
967  $this->position = 5;
968  $this->gen_or_uploaded = 'uploaded';
969  $this->extraparams = '';
970  $this->date_c = (dol_now() - 3600 * 24 * 10);
971  $this->date_m = '';
972  $this->fk_user_c = $user->id;
973  $this->fk_user_m = '';
974  $this->acl = '';
975  $this->src_object_type = 'product';
976  $this->src_object_id = 1;
977  }
978 }
979 
980 
985 {
989  public $label;
990 
994  public $entity;
995 
996  public $filename;
997  public $filepath;
998  public $fullpath_orig;
999 
1003  public $description;
1004 
1005  public $keywords;
1006  public $cover;
1007  public $position;
1008  public $gen_or_uploaded; // can be 'generated', 'uploaded', 'unknown'
1009  public $extraparams;
1010  public $date_c = '';
1011  public $date_m = '';
1012 
1016  public $fk_user_c;
1017 
1021  public $fk_user_m;
1022 
1023  public $acl;
1024  public $src_object_type;
1025  public $src_object_id;
1026 }
$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)
Return the label of the status.
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:48
if(isModEnabled('facture') && $user->hasRight('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') && $user->hasRight('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)) $sql
Social contributions to pay.
Definition: index.php:746
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.
rtl background position
dol_hash($chain, $type='0')
Returns a hash (non reversible encryption) of a string.