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