dolibarr 20.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-2024 Frédéric France <frederic.france@free.fr>
8 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30// Put here all includes required by your class file
31require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
32
37{
41 public $element = 'ecmfiles';
42
46 public $table_element = 'ecm_files';
47
51 public $picto = 'folder-open';
52
56 public $ref;
57
62 public $label;
63
67 public $share;
68
72 public $entity;
73
77 public $filename;
78
82 public $filepath;
83
87 public $fullpath_orig;
88
92 public $description;
93
97 public $keywords;
98
102 public $cover;
103
107 public $position;
108
112 public $gen_or_uploaded;
113
117 public $extraparams;
118
122 public $date_c = '';
123
127 public $date_m = '';
128
132 public $fk_user_c;
133
137 public $fk_user_m;
138
142 public $acl;
143
147 public $src_object_type;
148
152 public $src_object_id;
153
157 public $section_id;
158
159 public $fields = array(
160 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'visible' => 0, 'noteditable' => 1, 'index' => 1, 'css' => 'left', 'comment' => "Id"),
161 '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"),
162 '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"),
163 '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"),
164 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 50, 'index' => 1),
165 '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"),
166 '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"),
167 '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', ...)"),
168 'src_object_id' => array('type' => 'integer', 'label' => 'SourceID', 'default' => '1', 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'position' => 90, 'index' => 1, 'comment' => "Source object id"),
169 '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"),
170 'description' => array('type' => 'text', 'label' => 'Description', 'enabled' => 1, 'visible' => 0, 'position' => 110),
171 '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."),
172 'cover' => array('type' => 'text', 'label' => 'Cover', 'enabled' => 1, 'visible' => 0, 'position' => 130, 'comment' => "is this file a file to use for a cover"),
173 'position' => array('type' => 'integer', 'label' => 'Position', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 140, 'index' => 1, 'comment' => "position of file among others"),
174 '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'"),
175 '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"),
176 'date_c' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 170),
177 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 175),
178 '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',),
179 'fk_user_m' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'position' => 511, 'notnull' => -1, 'visible' => -2,),
180 'note_public' => array('type' => 'text', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 155),
181 'note_private' => array('type' => 'text', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 160),
182 'acl' => array('type' => 'text', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 160, 'comment' => "for future permission 'per file'"),
183 );
184
185
191 public function __construct(DoliDB $db)
192 {
193 $this->db = $db;
194 }
195
203 public function create(User $user, $notrigger = 0)
204 {
205 global $conf;
206
207 dol_syslog(__METHOD__, LOG_DEBUG);
208
209 $error = 0;
210
211 // Clean parameters
212 if (isset($this->ref)) {
213 $this->ref = trim($this->ref);
214 }
215 if (isset($this->label)) {
216 $this->label = trim($this->label);
217 }
218 if (isset($this->share)) {
219 $this->share = trim($this->share);
220 }
221 if (isset($this->entity)) {
222 $this->entity = (int) $this->entity;
223 }
224 if (isset($this->filename)) {
225 $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
226 }
227 if (isset($this->filepath)) {
228 $this->filepath = trim($this->filepath);
229 $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
230 }
231 if (isset($this->fullpath_orig)) {
232 $this->fullpath_orig = trim($this->fullpath_orig);
233 }
234 if (isset($this->description)) {
235 $this->description = trim($this->description);
236 }
237 if (isset($this->keywords)) {
238 $this->keywords = trim($this->keywords);
239 }
240 if (isset($this->cover)) {
241 $this->cover = trim($this->cover);
242 }
243 if (isset($this->gen_or_uploaded)) {
244 $this->gen_or_uploaded = trim($this->gen_or_uploaded);
245 }
246 if (isset($this->extraparams)) {
247 $this->extraparams = trim($this->extraparams);
248 }
249 if (isset($this->fk_user_c)) {
250 $this->fk_user_c = (int) $this->fk_user_c;
251 }
252 if (isset($this->fk_user_m)) {
253 $this->fk_user_m = (int) $this->fk_user_m;
254 }
255 if (isset($this->acl)) {
256 $this->acl = trim($this->acl);
257 }
258 if (isset($this->src_object_type)) {
259 $this->src_object_type = trim($this->src_object_type);
260 }
261 if (empty($this->date_c)) {
262 $this->date_c = dol_now();
263 }
264 if (empty($this->date_m)) {
265 $this->date_m = dol_now();
266 }
267
268 // If ref not defined
269 if (empty($this->ref)) {
270 include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
271 $this->ref = dol_hash($this->filepath.'/'.$this->filename, 3);
272 }
273
274 $maxposition = 0;
275 if (empty($this->position)) {
276 // Get max used
277 $sql = "SELECT MAX(position) as maxposition FROM ".MAIN_DB_PREFIX.$this->table_element;
278 $sql .= " WHERE filepath ='".$this->db->escape($this->filepath)."'";
279
280 $resql = $this->db->query($sql);
281 if ($resql) {
282 $obj = $this->db->fetch_object($resql);
283 $maxposition = (int) $obj->maxposition;
284 } else {
285 $this->errors[] = 'Error '.$this->db->lasterror();
286 return --$error;
287 }
288 $maxposition = $maxposition + 1;
289 } else {
290 $maxposition = $this->position;
291 }
292
293 // Check parameters
294 if (empty($this->filename) || empty($this->filepath)) {
295 $this->errors[] = 'Bad property filename or filepath';
296 return --$error;
297 }
298 if (!isset($this->entity)) {
299 $this->entity = $conf->entity;
300 }
301 // Put here code to add control on parameters values
302
303 // Insert request
304 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
305 $sql .= 'ref,';
306 $sql .= 'label,';
307 $sql .= 'share,';
308 $sql .= 'entity,';
309 $sql .= 'filename,';
310 $sql .= 'filepath,';
311 $sql .= 'fullpath_orig,';
312 $sql .= 'description,';
313 $sql .= 'keywords,';
314 $sql .= 'cover,';
315 $sql .= 'position,';
316 $sql .= 'gen_or_uploaded,';
317 $sql .= 'extraparams,';
318 $sql .= 'date_c,';
319 $sql .= 'tms,';
320 $sql .= 'fk_user_c,';
321 $sql .= 'fk_user_m,';
322 $sql .= 'acl,';
323 $sql .= 'src_object_type,';
324 $sql .= 'src_object_id';
325 $sql .= ') VALUES (';
326 $sql .= " '".$this->db->escape($this->ref)."', ";
327 $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
328 $sql .= ' '.(!isset($this->share) ? 'NULL' : "'".$this->db->escape($this->share)."'").',';
329 $sql .= ' '.((int) $this->entity).',';
330 $sql .= ' '.(!isset($this->filename) ? 'NULL' : "'".$this->db->escape($this->filename)."'").',';
331 $sql .= ' '.(!isset($this->filepath) ? 'NULL' : "'".$this->db->escape($this->filepath)."'").',';
332 $sql .= ' '.(!isset($this->fullpath_orig) ? 'NULL' : "'".$this->db->escape($this->fullpath_orig)."'").',';
333 $sql .= ' '.(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").',';
334 $sql .= ' '.(!isset($this->keywords) ? 'NULL' : "'".$this->db->escape($this->keywords)."'").',';
335 $sql .= ' '.(!isset($this->cover) ? 'NULL' : "'".$this->db->escape($this->cover)."'").',';
336 $sql .= ' '.((int) $maxposition).',';
337 $sql .= ' '.(!isset($this->gen_or_uploaded) ? 'NULL' : "'".$this->db->escape($this->gen_or_uploaded)."'").',';
338 $sql .= ' '.(!isset($this->extraparams) ? 'NULL' : "'".$this->db->escape($this->extraparams)."'").',';
339 $sql .= " '".$this->db->idate($this->date_c)."',";
340 $sql .= ' '.(!isset($this->date_m) || dol_strlen($this->date_m) == 0 ? 'NULL' : "'".$this->db->idate($this->date_m)."'").',';
341 $sql .= ' '.(!isset($this->fk_user_c) ? $user->id : $this->fk_user_c).',';
342 $sql .= ' '.(!isset($this->fk_user_m) ? 'NULL' : $this->fk_user_m).',';
343 $sql .= ' '.(!isset($this->acl) ? 'NULL' : "'".$this->db->escape($this->acl)."'").',';
344 $sql .= ' '.(!isset($this->src_object_type) ? 'NULL' : "'".$this->db->escape($this->src_object_type)."'").',';
345 $sql .= ' '.(!isset($this->src_object_id) ? 'NULL' : $this->src_object_id);
346 $sql .= ')';
347
348 $this->db->begin();
349
350 $resql = $this->db->query($sql);
351 if (!$resql) {
352 $error++;
353 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
354 $this->errors[] = 'Error DB_ERROR_RECORD_ALREADY_EXISTS : '.$this->db->lasterror();
355 } else {
356 $this->errors[] = 'Error '.$this->db->lasterror();
357 }
358 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
359 }
360
361 if (!$error) {
362 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
363 $this->position = $maxposition;
364
365 // Triggers
366 if (!$notrigger) {
367 // Call triggers
368 $result = $this->call_trigger(strtoupper(get_class($this)).'_CREATE', $user);
369 if ($result < 0) {
370 $error++;
371 }
372 // End call triggers
373 }
374 }
375
376 // Commit or rollback
377 if ($error) {
378 $this->db->rollback();
379
380 return -1 * $error;
381 } else {
382 $this->db->commit();
383
384 return $this->id;
385 }
386 }
387
400 public function fetch($id, $ref = '', $relativepath = '', $hashoffile = '', $hashforshare = '', $src_object_type = '', $src_object_id = 0)
401 {
402 global $conf;
403
404 dol_syslog(__METHOD__, LOG_DEBUG);
405
406 $sql = 'SELECT';
407 $sql .= ' t.rowid,';
408 $sql .= " t.ref,";
409 $sql .= " t.label,";
410 $sql .= " t.share,";
411 $sql .= " t.entity,";
412 $sql .= " t.filename,";
413 $sql .= " t.filepath,";
414 $sql .= " t.fullpath_orig,";
415 $sql .= " t.description,";
416 $sql .= " t.keywords,";
417 $sql .= " t.cover,";
418 $sql .= " t.position,";
419 $sql .= " t.gen_or_uploaded,";
420 $sql .= " t.extraparams,";
421 $sql .= " t.date_c,";
422 $sql .= " t.tms as date_m,";
423 $sql .= " t.fk_user_c,";
424 $sql .= " t.fk_user_m,";
425 $sql .= ' t.note_private,';
426 $sql .= ' t.note_public,';
427 $sql .= " t.acl,";
428 $sql .= " t.src_object_type,";
429 $sql .= " t.src_object_id";
430 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
431 $sql .= ' WHERE 1 = 1';
432 /* Fetching this table depends on filepath+filename, it must not depends on entity because filesystem on disk does not know what is Dolibarr entities
433 if (isModEnabled('multicompany')) {
434 $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
435 }*/
436 $filterfound = 0;
437 if ($relativepath) {
438 $relativepathwithnoexe = preg_replace('/\.noexe$/', '', $relativepath); // We must never have the .noexe into the database
439 $sql .= " AND t.filepath = '".$this->db->escape(dirname($relativepath))."'";
440 $filename = basename($relativepathwithnoexe);
441 if ($filename != '*') {
442 $sql .= " AND t.filename = '".$this->db->escape($filename)."'";
443 }
444 $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
445 $filterfound++;
446 }
447 if (!empty($ref)) { // hash of file path
448 $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
449 $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
450 $filterfound++;
451 }
452 if (!empty($hashoffile)) { // hash of content
453 $sql .= " AND t.label = '".$this->db->escape($hashoffile)."'";
454 $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
455 $filterfound++;
456 }
457 if (!empty($hashforshare)) {
458 if ($hashforshare != 'shared') {
459 $sql .= " AND t.share = '".$this->db->escape($hashforshare)."'";
460 } else {
461 $sql .= " AND t.share IS NOT NULL AND t.share <> ''";
462 }
463 //$sql .= " AND t.entity = ".$conf->entity; // hashforshare already unique
464 $filterfound++;
465 }
466 if ($src_object_type && $src_object_id) {
467 $sql .= " AND t.src_object_type = '".$this->db->escape($src_object_type)."' AND t.src_object_id = ".((int) $src_object_id);
468 $sql .= " AND t.entity = ".((int) $conf->entity);
469 $filterfound++;
470 }
471 if ($id > 0 || empty($filterfound)) {
472 $sql .= ' AND t.rowid = '.((int) $id); // rowid already unique
473 }
474
475 // Warning: May return several record, and only first one is returned !
476 $this->db->plimit(1); // When we search on src, or on hash of content (hashforfile), we take first one only
477 $this->db->order('t.rowid', 'ASC');
478
479 $resql = $this->db->query($sql);
480 if ($resql) {
481 $numrows = $this->db->num_rows($resql);
482 if ($numrows) {
483 $obj = $this->db->fetch_object($resql);
484
485 $this->id = $obj->rowid;
486 $this->ref = $obj->ref;
487 $this->label = $obj->label;
488 $this->share = $obj->share;
489 $this->entity = $obj->entity;
490 $this->filename = $obj->filename;
491 $this->filepath = $obj->filepath;
492 $this->fullpath_orig = $obj->fullpath_orig;
493 $this->description = $obj->description;
494 $this->keywords = $obj->keywords;
495 $this->cover = $obj->cover;
496 $this->position = $obj->position;
497 $this->gen_or_uploaded = $obj->gen_or_uploaded;
498 $this->extraparams = $obj->extraparams;
499 $this->date_c = $this->db->jdate($obj->date_c);
500 $this->date_m = $this->db->jdate($obj->date_m);
501 $this->fk_user_c = $obj->fk_user_c;
502 $this->fk_user_m = $obj->fk_user_m;
503 $this->note_private = $obj->note_private;
504 $this->note_public = $obj->note_public;
505 $this->acl = $obj->acl;
506 $this->src_object_type = $obj->src_object_type;
507 $this->src_object_id = $obj->src_object_id;
508 }
509
510 // Retrieve all extrafields for ecm_files
511 // fetch optionals attributes and labels
512 $this->fetch_optionals();
513
514 // $this->fetch_lines();
515
516 $this->db->free($resql);
517
518 if ($numrows) {
519 return 1;
520 } else {
521 return 0;
522 }
523 } else {
524 $this->errors[] = 'Error '.$this->db->lasterror();
525 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
526
527 return -1;
528 }
529 }
530
542 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $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 $sql .= ' WHERE 1 = 1';
569
570 // Manage filter
571 if (is_array($filter)) {
572 $sqlwhere = array();
573 if (count($filter) > 0) {
574 foreach ($filter as $key => $value) {
575 if ($key == 't.src_object_id') {
576 $sqlwhere[] = $this->db->sanitize($key)." = ".((int) $value);
577 } else {
578 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
579 }
580 }
581 }
582 if (count($sqlwhere) > 0) {
583 $sql .= ' AND '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
584 }
585
586 $filter = '';
587 }
588
589 // Manage filter
590 $errormessage = '';
591 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
592 if ($errormessage) {
593 $this->errors[] = $errormessage;
594 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
595 return -1;
596 }
597
598 /* Fetching this table depends on filepath+filename, it must not depends on entity
599 if (isModEnabled('multicompany')) {
600 $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
601 }*/
602 if (!empty($sortfield)) {
603 $sql .= $this->db->order($sortfield, $sortorder);
604 }
605 if (!empty($limit)) {
606 $sql .= $this->db->plimit($limit, $offset);
607 }
608
609 $this->lines = array();
610
611 $resql = $this->db->query($sql);
612 if ($resql) {
613 $num = $this->db->num_rows($resql);
614
615 while ($obj = $this->db->fetch_object($resql)) {
616 $line = new EcmFilesLine();
617
618 $line->id = $obj->rowid;
619 $line->ref = $obj->rowid;
620 $line->label = $obj->label;
621 $line->share = $obj->share;
622 $line->entity = $obj->entity;
623 $line->filename = $obj->filename;
624 $line->filepath = $obj->filepath;
625 $line->fullpath_orig = $obj->fullpath_orig;
626 $line->description = $obj->description;
627 $line->keywords = $obj->keywords;
628 $line->cover = $obj->cover;
629 $line->position = $obj->position;
630 $line->gen_or_uploaded = $obj->gen_or_uploaded;
631 $line->extraparams = $obj->extraparams;
632 $line->date_c = $this->db->jdate($obj->date_c);
633 $line->date_m = $this->db->jdate($obj->date_m);
634 $line->fk_user_c = $obj->fk_user_c;
635 $line->fk_user_m = $obj->fk_user_m;
636 $line->acl = $obj->acl;
637 $line->src_object_type = $obj->src_object_type;
638 $line->src_object_id = $obj->src_object_id;
639 $this->lines[] = $line;
640 }
641 $this->db->free($resql);
642
643 return $num;
644 } else {
645 $this->errors[] = 'Error '.$this->db->lasterror();
646 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
647
648 return -1;
649 }
650 }
651
659 public function update(User $user, $notrigger = 0)
660 {
661 global $conf;
662
663 $error = 0;
664
665 dol_syslog(__METHOD__, LOG_DEBUG);
666
667 // Clean parameters
668
669 if (isset($this->ref)) {
670 $this->ref = trim($this->ref);
671 }
672 if (isset($this->label)) {
673 $this->label = trim($this->label);
674 }
675 if (isset($this->share)) {
676 $this->share = trim($this->share);
677 }
678 if (isset($this->entity)) {
679 $this->entity = (int) $this->entity;
680 }
681 if (isset($this->filename)) {
682 $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
683 }
684 if (isset($this->filepath)) {
685 $this->filepath = trim($this->filepath);
686 $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
687 }
688 if (isset($this->fullpath_orig)) {
689 $this->fullpath_orig = trim($this->fullpath_orig);
690 }
691 if (isset($this->description)) {
692 $this->description = trim($this->description);
693 }
694 if (isset($this->keywords)) {
695 $this->keywords = trim($this->keywords);
696 }
697 if (isset($this->cover)) {
698 $this->cover = trim($this->cover);
699 }
700 if (isset($this->gen_or_uploaded)) {
701 $this->gen_or_uploaded = trim($this->gen_or_uploaded);
702 }
703 if (isset($this->extraparams)) {
704 $this->extraparams = trim($this->extraparams);
705 }
706 if (isset($this->fk_user_m)) {
707 $this->fk_user_m = (int) $this->fk_user_m;
708 }
709 if (isset($this->acl)) {
710 $this->acl = trim($this->acl);
711 }
712 if (isset($this->src_object_type)) {
713 $this->src_object_type = trim($this->src_object_type);
714 }
715
716 // Check parameters
717 // Put here code to add a control on parameters values
718
719 // Update request
720 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
721 $sql .= " ref = '".$this->db->escape(dol_hash($this->filepath."/".$this->filename, 3))."',";
722 $sql .= ' label = '.(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
723 $sql .= ' share = '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null").',';
724 $sql .= ' entity = '.(isset($this->entity) ? $this->entity : $conf->entity).',';
725 $sql .= ' filename = '.(isset($this->filename) ? "'".$this->db->escape($this->filename)."'" : "null").',';
726 $sql .= ' filepath = '.(isset($this->filepath) ? "'".$this->db->escape($this->filepath)."'" : "null").',';
727 $sql .= ' fullpath_orig = '.(isset($this->fullpath_orig) ? "'".$this->db->escape($this->fullpath_orig)."'" : "null").',';
728 $sql .= ' description = '.(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").',';
729 $sql .= ' keywords = '.(isset($this->keywords) ? "'".$this->db->escape($this->keywords)."'" : "null").',';
730 $sql .= ' cover = '.(isset($this->cover) ? "'".$this->db->escape($this->cover)."'" : "null").',';
731 $sql .= ' position = '.(isset($this->position) ? $this->db->escape($this->position) : "0").',';
732 $sql .= ' gen_or_uploaded = '.(isset($this->gen_or_uploaded) ? "'".$this->db->escape($this->gen_or_uploaded)."'" : "null").',';
733 $sql .= ' extraparams = '.(isset($this->extraparams) ? "'".$this->db->escape($this->extraparams)."'" : "null").',';
734 $sql .= ' date_c = '.(!isset($this->date_c) || dol_strlen($this->date_c) != 0 ? "'".$this->db->idate($this->date_c)."'" : 'null').',';
735 //$sql .= ' tms = '.(! isset($this->date_m) || dol_strlen($this->date_m) != 0 ? "'".$this->db->idate($this->date_m)."'" : 'null').','; // Field automatically updated
736 $sql .= ' fk_user_m = '.($this->fk_user_m > 0 ? $this->fk_user_m : $user->id).',';
737 $sql .= ' acl = '.(isset($this->acl) ? "'".$this->db->escape($this->acl)."'" : "null").',';
738 $sql .= ' src_object_id = '.($this->src_object_id > 0 ? $this->src_object_id : "null").',';
739 $sql .= ' src_object_type = '.(isset($this->src_object_type) ? "'".$this->db->escape($this->src_object_type)."'" : "null");
740 $sql .= ' WHERE rowid='.((int) $this->id);
741
742 $this->db->begin();
743
744 $resql = $this->db->query($sql);
745 if (!$resql) {
746 $error++;
747 $this->errors[] = 'Error '.$this->db->lasterror();
748 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
749 }
750
751 if (!$error) {
752 // Update extrafields
753 $result = $this->insertExtraFields();
754 if ($result < 0) {
755 $error++;
756 }
757 }
758
759 // Triggers
760 if (!$error && !$notrigger) {
761 // Call triggers
762 $result = $this->call_trigger(strtoupper(get_class($this)).'_MODIFY', $user);
763 if ($result < 0) {
764 $error++;
765 } //Do also here what you must do to rollback action if trigger fail
766 // End call triggers
767 }
768
769 // Commit or rollback
770 if ($error) {
771 $this->db->rollback();
772
773 return -1 * $error;
774 } else {
775 $this->db->commit();
776
777 return 1;
778 }
779 }
780
788 public function delete(User $user, $notrigger = 0)
789 {
790 dol_syslog(__METHOD__, LOG_DEBUG);
791
792 $error = 0;
793
794 $this->db->begin();
795
796 // Triggers
797 if (!$notrigger) {
798 // Call triggers
799 $result = $this->call_trigger(strtoupper(get_class($this)).'_DELETE', $user);
800 if ($result < 0) {
801 $error++;
802 } //Do also here what you must do to rollback action if trigger fail
803 // End call triggers
804 }
805
806 // If you need to delete child tables to, you can insert them here
807 if (!$error) {
808 $result = $this->deleteExtraFields();
809 if (!$result) {
810 dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
811 $error++;
812 }
813 }
814 if (!$error) {
815 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
816 $sql .= ' WHERE rowid='.((int) $this->id);
817
818 $resql = $this->db->query($sql);
819 if (!$resql) {
820 $error++;
821 $this->errors[] = 'Error '.$this->db->lasterror();
822 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
823 }
824 }
825
826 // Commit or rollback
827 if ($error) {
828 $this->db->rollback();
829
830 return -1 * $error;
831 } else {
832 $this->db->commit();
833
834 return 1;
835 }
836 }
837
845 public function createFromClone(User $user, $fromid)
846 {
847 dol_syslog(__METHOD__, LOG_DEBUG);
848
849 $error = 0;
850 $object = new EcmFiles($this->db);
851
852 $this->db->begin();
853
854 // Load source object
855 $object->fetch($fromid);
856 // Reset object
857 $object->id = 0;
858
859 // Clear fields
860 // ...
861
862 // Create clone
863 $object->context['createfromclone'] = 'createfromclone';
864 $result = $object->create($user);
865
866 // Other options
867 if ($result < 0) {
868 $error++;
869 $this->errors = $object->errors;
870 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
871 }
872
873 unset($object->context['createfromclone']);
874
875 // End
876 if (!$error) {
877 $this->db->commit();
878
879 return $object->id;
880 } else {
881 $this->db->rollback();
882
883 return -1;
884 }
885 }
886
897 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
898 {
899 global $db, $conf, $langs;
900 global $dolibarr_main_authentication, $dolibarr_main_demo;
901 global $menumanager, $hookmanager;
902
903 if (!empty($conf->dol_no_mouse_hover)) {
904 $notooltip = 1; // Force disable tooltips
905 }
906
907 $result = '';
908
909 $label = '<u>'.$langs->trans("MyModule").'</u>';
910 $label .= '<br>';
911 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
912
913 $url = DOL_URL_ROOT.'/ecm/file_card.php?id='.$this->id;
914
915 $linkclose = '';
916 if (empty($notooltip)) {
917 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
918 $label = $langs->trans("ShowProject");
919 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
920 }
921 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
922 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
923 } else {
924 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
925 }
926
927 $linkstart = '<a href="'.$url.'"';
928 $linkstart .= $linkclose.'>';
929 $linkend = '</a>';
930
931 if ($withpicto) {
932 $result .= ($linkstart.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
933 if ($withpicto != 2) {
934 $result .= ' ';
935 }
936 }
937 $result .= $linkstart.$this->ref.$linkend;
938
939 global $action;
940 $hookmanager->initHooks(array($this->element . 'dao'));
941 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
942 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
943 if ($reshook > 0) {
944 $result = $hookmanager->resPrint;
945 } else {
946 $result .= $hookmanager->resPrint;
947 }
948 return $result;
949 }
950
957 public function getLibStatut($mode = 0)
958 {
959 return $this->LibStatut($this->status, $mode);
960 }
961
962 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
970 public static function LibStatut($status, $mode = 0)
971 {
972 // phpcs:enable
973 global $langs;
974 return '';
975 }
976
977
984 public function initAsSpecimen()
985 {
986 global $conf, $user;
987
988 $this->id = 0;
989 $this->specimen = 1;
990 $this->label = '0a1b2c3e4f59999999';
991 $this->entity = 1;
992 $this->filename = 'myspecimenfilefile.pdf';
993 $this->filepath = '/aaa/bbb';
994 $this->fullpath_orig = 'c:/file on my disk.pdf';
995 $this->description = 'This is a long description of file';
996 $this->keywords = 'key1,key2';
997 $this->cover = '1';
998 $this->position = 5;
999 $this->gen_or_uploaded = 'uploaded';
1000 $this->extraparams = '';
1001 $this->date_c = (dol_now() - 3600 * 24 * 10);
1002 $this->date_m = '';
1003 $this->fk_user_c = $user->id;
1004 $this->fk_user_m = $user->id;
1005 $this->acl = '';
1006 $this->src_object_type = 'product';
1007 $this->src_object_id = 1;
1008
1009 return 1;
1010 }
1011}
1012
1013
1018{
1022 public $label;
1023
1027 public $entity;
1028
1029 public $filename;
1030 public $filepath;
1031 public $fullpath_orig;
1032
1036 public $description;
1037
1038 public $keywords;
1039 public $cover;
1040 public $position;
1041 public $gen_or_uploaded; // can be 'generated', 'uploaded', 'unknown'
1042 public $extraparams;
1043 public $date_c = '';
1044 public $date_m = '';
1045
1049 public $fk_user_c;
1050
1054 public $fk_user_m;
1055
1056 public $acl;
1057 public $src_object_type;
1058 public $src_object_id;
1059}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition security.php:637
print $object position
Definition edit.php:195
$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.
insertExtraFields($trigger='', $userused=null)
Add/Update 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=0)
Update object into database.
getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
Return a link to the object card (with optionally the picto)
fetch($id, $ref='', $relativepath='', $hashoffile='', $hashforshare='', $src_object_type='', $src_object_id=0)
Load object in memory from the database.
__construct(DoliDB $db)
Constructor.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load object in memory from the database.
static LibStatut($status, $mode=0)
Return the status.
create(User $user, $notrigger=0)
Create object into database.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.