dolibarr 19.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
30require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
31
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 $filterfound = 0;
436 if ($relativepath) {
437 $relativepathwithnoexe = preg_replace('/\.noexe$/', '', $relativepath); // We must never have the .noexe into the database
438 $sql .= " AND t.filepath = '".$this->db->escape(dirname($relativepath))."'";
439 $filename = basename($relativepathwithnoexe);
440 if ($filename != '*') {
441 $sql .= " AND t.filename = '".$this->db->escape($filename)."'";
442 }
443 $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
444 $filterfound++;
445 }
446 if (!empty($ref)) { // hash of file path
447 $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
448 $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
449 $filterfound++;
450 }
451 if (!empty($hashoffile)) { // hash of content
452 $sql .= " AND t.label = '".$this->db->escape($hashoffile)."'";
453 $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
454 $filterfound++;
455 }
456 if (!empty($hashforshare)) {
457 if ($hashforshare != 'shared') {
458 $sql .= " AND t.share = '".$this->db->escape($hashforshare)."'";
459 } else {
460 $sql .= " AND t.share IS NOT NULL AND t.share <> ''";
461 }
462 //$sql .= " AND t.entity = ".$conf->entity; // hashforshare already unique
463 $filterfound++;
464 }
465 if ($src_object_type && $src_object_id) {
466 $sql .= " AND t.src_object_type = '".$this->db->escape($src_object_type)."' AND t.src_object_id = ".((int) $src_object_id);
467 $sql .= " AND t.entity = ".((int) $conf->entity);
468 $filterfound++;
469 }
470 if ($id > 0 || empty($filterfound)) {
471 $sql .= ' AND t.rowid = '.((int) $id); // rowid already unique
472 }
473
474 // Warning: May return several record, and only first one is returned !
475 $this->db->plimit(1); // When we search on src, or on hash of content (hashforfile), we take first one only
476 $this->db->order('t.rowid', 'ASC');
477
478 $resql = $this->db->query($sql);
479 if ($resql) {
480 $numrows = $this->db->num_rows($resql);
481 if ($numrows) {
482 $obj = $this->db->fetch_object($resql);
483
484 $this->id = $obj->rowid;
485 $this->ref = $obj->ref;
486 $this->label = $obj->label;
487 $this->share = $obj->share;
488 $this->entity = $obj->entity;
489 $this->filename = $obj->filename;
490 $this->filepath = $obj->filepath;
491 $this->fullpath_orig = $obj->fullpath_orig;
492 $this->description = $obj->description;
493 $this->keywords = $obj->keywords;
494 $this->cover = $obj->cover;
495 $this->position = $obj->position;
496 $this->gen_or_uploaded = $obj->gen_or_uploaded;
497 $this->extraparams = $obj->extraparams;
498 $this->date_c = $this->db->jdate($obj->date_c);
499 $this->date_m = $this->db->jdate($obj->date_m);
500 $this->fk_user_c = $obj->fk_user_c;
501 $this->fk_user_m = $obj->fk_user_m;
502 $this->note_private = $obj->note_private;
503 $this->note_public = $obj->note_public;
504 $this->acl = $obj->acl;
505 $this->src_object_type = $obj->src_object_type;
506 $this->src_object_id = $obj->src_object_id;
507 }
508
509 // Retrieve all extrafields for ecm_files
510 // fetch optionals attributes and labels
511 $this->fetch_optionals();
512
513 // $this->fetch_lines();
514
515 $this->db->free($resql);
516
517 if ($numrows) {
518 return 1;
519 } else {
520 return 0;
521 }
522 } else {
523 $this->errors[] = 'Error '.$this->db->lasterror();
524 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
525
526 return -1;
527 }
528 }
529
542 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
543 {
544 dol_syslog(__METHOD__, LOG_DEBUG);
545
546 $sql = 'SELECT';
547 $sql .= ' t.rowid,';
548 $sql .= " t.label,";
549 $sql .= " t.share,";
550 $sql .= " t.entity,";
551 $sql .= " t.filename,";
552 $sql .= " t.filepath,";
553 $sql .= " t.fullpath_orig,";
554 $sql .= " t.description,";
555 $sql .= " t.keywords,";
556 $sql .= " t.cover,";
557 $sql .= " t.position,";
558 $sql .= " t.gen_or_uploaded,";
559 $sql .= " t.extraparams,";
560 $sql .= " t.date_c,";
561 $sql .= " t.tms as date_m,";
562 $sql .= " t.fk_user_c,";
563 $sql .= " t.fk_user_m,";
564 $sql .= " t.acl,";
565 $sql .= " t.src_object_type,";
566 $sql .= " t.src_object_id";
567 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
568
569 // Manage filter
570 $sqlwhere = array();
571 if (count($filter) > 0) {
572 foreach ($filter as $key => $value) {
573 if ($key == 't.src_object_id') {
574 $sqlwhere[] = $key." = ".((int) $value);
575 } else {
576 $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
577 }
578 }
579 }
580 $sql .= ' WHERE 1 = 1';
581 /* Fetching this table depends on filepath+filename, it must not depends on entity
582 if (isModEnabled('multicompany')) {
583 $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
584 }*/
585 if (count($sqlwhere) > 0) {
586 $sql .= ' AND '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
587 }
588 if (!empty($sortfield)) {
589 $sql .= $this->db->order($sortfield, $sortorder);
590 }
591 if (!empty($limit)) {
592 $sql .= $this->db->plimit($limit, $offset);
593 }
594
595 $this->lines = array();
596
597 $resql = $this->db->query($sql);
598 if ($resql) {
599 $num = $this->db->num_rows($resql);
600
601 while ($obj = $this->db->fetch_object($resql)) {
602 $line = new EcmFilesLine();
603
604 $line->id = $obj->rowid;
605 $line->ref = $obj->rowid;
606 $line->label = $obj->label;
607 $line->share = $obj->share;
608 $line->entity = $obj->entity;
609 $line->filename = $obj->filename;
610 $line->filepath = $obj->filepath;
611 $line->fullpath_orig = $obj->fullpath_orig;
612 $line->description = $obj->description;
613 $line->keywords = $obj->keywords;
614 $line->cover = $obj->cover;
615 $line->position = $obj->position;
616 $line->gen_or_uploaded = $obj->gen_or_uploaded;
617 $line->extraparams = $obj->extraparams;
618 $line->date_c = $this->db->jdate($obj->date_c);
619 $line->date_m = $this->db->jdate($obj->date_m);
620 $line->fk_user_c = $obj->fk_user_c;
621 $line->fk_user_m = $obj->fk_user_m;
622 $line->acl = $obj->acl;
623 $line->src_object_type = $obj->src_object_type;
624 $line->src_object_id = $obj->src_object_id;
625 $this->lines[] = $line;
626 }
627 $this->db->free($resql);
628
629 return $num;
630 } else {
631 $this->errors[] = 'Error '.$this->db->lasterror();
632 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
633
634 return -1;
635 }
636 }
637
646 public function update(User $user, $notrigger = false)
647 {
648 global $conf;
649
650 $error = 0;
651
652 dol_syslog(__METHOD__, LOG_DEBUG);
653
654 // Clean parameters
655
656 if (isset($this->ref)) {
657 $this->ref = trim($this->ref);
658 }
659 if (isset($this->label)) {
660 $this->label = trim($this->label);
661 }
662 if (isset($this->share)) {
663 $this->share = trim($this->share);
664 }
665 if (isset($this->entity)) {
666 $this->entity = trim($this->entity);
667 }
668 if (isset($this->filename)) {
669 $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
670 }
671 if (isset($this->filepath)) {
672 $this->filepath = trim($this->filepath);
673 $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
674 }
675 if (isset($this->fullpath_orig)) {
676 $this->fullpath_orig = trim($this->fullpath_orig);
677 }
678 if (isset($this->description)) {
679 $this->description = trim($this->description);
680 }
681 if (isset($this->keywords)) {
682 $this->keywords = trim($this->keywords);
683 }
684 if (isset($this->cover)) {
685 $this->cover = trim($this->cover);
686 }
687 if (isset($this->gen_or_uploaded)) {
688 $this->gen_or_uploaded = trim($this->gen_or_uploaded);
689 }
690 if (isset($this->extraparams)) {
691 $this->extraparams = trim($this->extraparams);
692 }
693 if (isset($this->fk_user_m)) {
694 $this->fk_user_m = trim($this->fk_user_m);
695 }
696 if (isset($this->acl)) {
697 $this->acl = trim($this->acl);
698 }
699 if (isset($this->src_object_type)) {
700 $this->src_object_type = trim($this->src_object_type);
701 }
702
703 // Check parameters
704 // Put here code to add a control on parameters values
705
706 // Update request
707 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
708 $sql .= " ref = '".$this->db->escape(dol_hash($this->filepath."/".$this->filename, 3))."',";
709 $sql .= ' label = '.(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
710 $sql .= ' share = '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null").',';
711 $sql .= ' entity = '.(isset($this->entity) ? $this->entity : $conf->entity).',';
712 $sql .= ' filename = '.(isset($this->filename) ? "'".$this->db->escape($this->filename)."'" : "null").',';
713 $sql .= ' filepath = '.(isset($this->filepath) ? "'".$this->db->escape($this->filepath)."'" : "null").',';
714 $sql .= ' fullpath_orig = '.(isset($this->fullpath_orig) ? "'".$this->db->escape($this->fullpath_orig)."'" : "null").',';
715 $sql .= ' description = '.(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").',';
716 $sql .= ' keywords = '.(isset($this->keywords) ? "'".$this->db->escape($this->keywords)."'" : "null").',';
717 $sql .= ' cover = '.(isset($this->cover) ? "'".$this->db->escape($this->cover)."'" : "null").',';
718 $sql .= ' position = '.(isset($this->position) ? $this->db->escape($this->position) : "0").',';
719 $sql .= ' gen_or_uploaded = '.(isset($this->gen_or_uploaded) ? "'".$this->db->escape($this->gen_or_uploaded)."'" : "null").',';
720 $sql .= ' extraparams = '.(isset($this->extraparams) ? "'".$this->db->escape($this->extraparams)."'" : "null").',';
721 $sql .= ' date_c = '.(!isset($this->date_c) || dol_strlen($this->date_c) != 0 ? "'".$this->db->idate($this->date_c)."'" : 'null').',';
722 //$sql .= ' tms = '.(! isset($this->date_m) || dol_strlen($this->date_m) != 0 ? "'".$this->db->idate($this->date_m)."'" : 'null').','; // Field automatically updated
723 $sql .= ' fk_user_m = '.($this->fk_user_m > 0 ? $this->fk_user_m : $user->id).',';
724 $sql .= ' acl = '.(isset($this->acl) ? "'".$this->db->escape($this->acl)."'" : "null").',';
725 $sql .= ' src_object_id = '.($this->src_object_id > 0 ? $this->src_object_id : "null").',';
726 $sql .= ' src_object_type = '.(isset($this->src_object_type) ? "'".$this->db->escape($this->src_object_type)."'" : "null");
727 $sql .= ' WHERE rowid='.((int) $this->id);
728
729 $this->db->begin();
730
731 $resql = $this->db->query($sql);
732 if (!$resql) {
733 $error++;
734 $this->errors[] = 'Error '.$this->db->lasterror();
735 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
736 }
737
738 // Triggers
739 if (!$error && !$notrigger) {
740 // Call triggers
741 $result = $this->call_trigger(strtoupper(get_class($this)).'_MODIFY', $user);
742 if ($result < 0) {
743 $error++;
744 } //Do also here what you must do to rollback action if trigger fail
745 // End call triggers
746 }
747
748 // Commit or rollback
749 if ($error) {
750 $this->db->rollback();
751
752 return -1 * $error;
753 } else {
754 $this->db->commit();
755
756 return 1;
757 }
758 }
759
768 public function delete(User $user, $notrigger = false)
769 {
770 dol_syslog(__METHOD__, LOG_DEBUG);
771
772 $error = 0;
773
774 $this->db->begin();
775
776 // Triggers
777 if (!$notrigger) {
778 // Call triggers
779 $result = $this->call_trigger(strtoupper(get_class($this)).'_DELETE', $user);
780 if ($result < 0) {
781 $error++;
782 } //Do also here what you must do to rollback action if trigger fail
783 // End call triggers
784 }
785
786 // If you need to delete child tables to, you can insert them here
787 if (!$error) {
788 $result = $this->deleteExtraFields();
789 if (!$result) {
790 dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
791 $error++;
792 }
793 }
794 if (!$error) {
795 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
796 $sql .= ' WHERE rowid='.((int) $this->id);
797
798 $resql = $this->db->query($sql);
799 if (!$resql) {
800 $error++;
801 $this->errors[] = 'Error '.$this->db->lasterror();
802 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
803 }
804 }
805
806 // Commit or rollback
807 if ($error) {
808 $this->db->rollback();
809
810 return -1 * $error;
811 } else {
812 $this->db->commit();
813
814 return 1;
815 }
816 }
817
825 public function createFromClone(User $user, $fromid)
826 {
827 dol_syslog(__METHOD__, LOG_DEBUG);
828
829 $error = 0;
830 $object = new EcmFiles($this->db);
831
832 $this->db->begin();
833
834 // Load source object
835 $object->fetch($fromid);
836 // Reset object
837 $object->id = 0;
838
839 // Clear fields
840 // ...
841
842 // Create clone
843 $object->context['createfromclone'] = 'createfromclone';
844 $result = $object->create($user);
845
846 // Other options
847 if ($result < 0) {
848 $error++;
849 $this->errors = $object->errors;
850 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
851 }
852
853 unset($object->context['createfromclone']);
854
855 // End
856 if (!$error) {
857 $this->db->commit();
858
859 return $object->id;
860 } else {
861 $this->db->rollback();
862
863 return -1;
864 }
865 }
866
877 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
878 {
879 global $db, $conf, $langs;
880 global $dolibarr_main_authentication, $dolibarr_main_demo;
881 global $menumanager, $hookmanager;
882
883 if (!empty($conf->dol_no_mouse_hover)) {
884 $notooltip = 1; // Force disable tooltips
885 }
886
887 $result = '';
888
889 $label = '<u>'.$langs->trans("MyModule").'</u>';
890 $label .= '<br>';
891 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
892
893 $url = DOL_URL_ROOT.'/ecm/file_card.php?id='.$this->id;
894
895 $linkclose = '';
896 if (empty($notooltip)) {
897 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
898 $label = $langs->trans("ShowProject");
899 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
900 }
901 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
902 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
903 } else {
904 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
905 }
906
907 $linkstart = '<a href="'.$url.'"';
908 $linkstart .= $linkclose.'>';
909 $linkend = '</a>';
910
911 if ($withpicto) {
912 $result .= ($linkstart.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
913 if ($withpicto != 2) {
914 $result .= ' ';
915 }
916 }
917 $result .= $linkstart.$this->ref.$linkend;
918
919 global $action;
920 $hookmanager->initHooks(array($this->element . 'dao'));
921 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
922 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
923 if ($reshook > 0) {
924 $result = $hookmanager->resPrint;
925 } else {
926 $result .= $hookmanager->resPrint;
927 }
928 return $result;
929 }
930
937 public function getLibStatut($mode = 0)
938 {
939 return $this->LibStatut($this->status, $mode);
940 }
941
942 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
950 public static function LibStatut($status, $mode = 0)
951 {
952 // phpcs:enable
953 global $langs;
954 return '';
955 }
956
957
964 public function initAsSpecimen()
965 {
966 global $conf, $user;
967
968 $this->id = 0;
969 $this->specimen = 1;
970 $this->label = '0a1b2c3e4f59999999';
971 $this->entity = 1;
972 $this->filename = 'myspecimenfilefile.pdf';
973 $this->filepath = '/aaa/bbb';
974 $this->fullpath_orig = 'c:/file on my disk.pdf';
975 $this->description = 'This is a long description of file';
976 $this->keywords = 'key1,key2';
977 $this->cover = '1';
978 $this->position = 5;
979 $this->gen_or_uploaded = 'uploaded';
980 $this->extraparams = '';
981 $this->date_c = (dol_now() - 3600 * 24 * 10);
982 $this->date_m = '';
983 $this->fk_user_c = $user->id;
984 $this->fk_user_m = '';
985 $this->acl = '';
986 $this->src_object_type = 'product';
987 $this->src_object_id = 1;
988 }
989}
990
991
996{
1000 public $label;
1001
1005 public $entity;
1006
1007 public $filename;
1008 public $filepath;
1009 public $fullpath_orig;
1010
1014 public $description;
1015
1016 public $keywords;
1017 public $cover;
1018 public $position;
1019 public $gen_or_uploaded; // can be 'generated', 'uploaded', 'unknown'
1020 public $extraparams;
1021 public $date_c = '';
1022 public $date_m = '';
1023
1027 public $fk_user_c;
1028
1032 public $fk_user_m;
1033
1034 public $acl;
1035 public $src_object_type;
1036 public $src_object_id;
1037}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Definition security.php:604
$object ref
Definition info.php:79
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.
create(User $user, $notrigger=false)
Create 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.
static LibStatut($status, $mode=0)
Return the status.
Class of an index line of a document.
Class to manage Dolibarr users.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after 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.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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', $nosalt=0)
Returns a hash (non reversible encryption) of a string.