30 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
40 public $element =
'ecmfiles';
45 public $table_element =
'ecm_files';
50 public $picto =
'folder-open';
86 public $fullpath_orig;
111 public $gen_or_uploaded;
146 public $src_object_type;
151 public $src_object_id;
186 if (isset($this->
ref)) {
187 $this->
ref = trim($this->
ref);
189 if (isset($this->label)) {
190 $this->label = trim($this->label);
192 if (isset($this->share)) {
193 $this->share = trim($this->share);
195 if (isset($this->entity)) {
196 $this->entity = (int) $this->entity;
198 if (isset($this->filename)) {
199 $this->filename = preg_replace(
'/\.noexe$/',
'', trim($this->filename));
201 if (isset($this->filepath)) {
202 $this->filepath = trim($this->filepath);
203 $this->filepath = preg_replace(
'/[\\/]+$/',
'', $this->filepath);
205 if (isset($this->fullpath_orig)) {
206 $this->fullpath_orig = trim($this->fullpath_orig);
211 if (isset($this->keywords)) {
212 $this->keywords = trim($this->keywords);
214 if (isset($this->cover)) {
215 $this->cover = trim($this->cover);
217 if (isset($this->gen_or_uploaded)) {
218 $this->gen_or_uploaded = trim($this->gen_or_uploaded);
220 if (isset($this->extraparams)) {
221 $this->extraparams = trim($this->extraparams);
223 if (isset($this->fk_user_c)) {
224 $this->fk_user_c = (int) $this->fk_user_c;
226 if (isset($this->fk_user_m)) {
227 $this->fk_user_m = (int) $this->fk_user_m;
229 if (isset($this->acl)) {
230 $this->acl = trim($this->acl);
232 if (isset($this->src_object_type)) {
233 $this->src_object_type = trim($this->src_object_type);
235 if (empty($this->date_c)) {
238 if (empty($this->date_m)) {
243 if (empty($this->
ref)) {
244 include_once DOL_DOCUMENT_ROOT.
'/core/lib/security.lib.php';
245 $this->
ref =
dol_hash($this->filepath.
'/'.$this->filename, 3);
249 if (empty($this->position)) {
251 $sql =
"SELECT MAX(position) as maxposition FROM ".MAIN_DB_PREFIX.$this->table_element;
252 $sql .=
" WHERE filepath ='".$this->db->escape($this->filepath).
"'";
256 $obj = $this->
db->fetch_object(
$resql);
257 $maxposition = (int) $obj->maxposition;
259 $this->errors[] =
'Error '.$this->db->lasterror();
262 $maxposition = $maxposition + 1;
264 $maxposition = $this->position;
268 if (empty($this->filename) || empty($this->filepath)) {
269 $this->errors[] =
'Bad property filename or filepath';
272 if (!isset($this->entity)) {
273 $this->entity = $conf->entity;
278 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.
'(';
285 $sql .=
'fullpath_orig,';
286 $sql .=
'description,';
290 $sql .=
'gen_or_uploaded,';
291 $sql .=
'extraparams,';
294 $sql .=
'fk_user_c,';
295 $sql .=
'fk_user_m,';
297 $sql .=
'src_object_type,';
298 $sql .=
'src_object_id';
299 $sql .=
') VALUES (';
300 $sql .=
" '".$this->db->escape($this->
ref).
"', ";
301 $sql .=
' '.(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
',';
302 $sql .=
' '.(!isset($this->share) ?
'NULL' :
"'".$this->db->escape($this->share).
"'").
',';
303 $sql .=
' '.((int) $this->entity).
',';
304 $sql .=
' '.(!isset($this->filename) ?
'NULL' :
"'".$this->db->escape($this->filename).
"'").
',';
305 $sql .=
' '.(!isset($this->filepath) ?
'NULL' :
"'".$this->db->escape($this->filepath).
"'").
',';
306 $sql .=
' '.(!isset($this->fullpath_orig) ?
'NULL' :
"'".$this->db->escape($this->fullpath_orig).
"'").
',';
308 $sql .=
' '.(!isset($this->keywords) ?
'NULL' :
"'".$this->db->escape($this->keywords).
"'").
',';
309 $sql .=
' '.(!isset($this->cover) ?
'NULL' :
"'".$this->db->escape($this->cover).
"'").
',';
310 $sql .=
' '.((int) $maxposition).
',';
311 $sql .=
' '.(!isset($this->gen_or_uploaded) ?
'NULL' :
"'".$this->db->escape($this->gen_or_uploaded).
"'").
',';
312 $sql .=
' '.(!isset($this->extraparams) ?
'NULL' :
"'".$this->db->escape($this->extraparams).
"'").
',';
313 $sql .=
" '".$this->db->idate($this->date_c).
"',";
314 $sql .=
' '.(!isset($this->date_m) ||
dol_strlen($this->date_m) == 0 ?
'NULL' :
"'".$this->db->idate($this->date_m).
"'").
',';
315 $sql .=
' '.(!isset($this->fk_user_c) ? $user->id : $this->fk_user_c).
',';
316 $sql .=
' '.(!isset($this->fk_user_m) ?
'NULL' : $this->fk_user_m).
',';
317 $sql .=
' '.(!isset($this->acl) ?
'NULL' :
"'".$this->db->escape($this->acl).
"'").
',';
318 $sql .=
' '.(!isset($this->src_object_type) ?
'NULL' :
"'".$this->db->escape($this->src_object_type).
"'").
',';
319 $sql .=
' '.(!isset($this->src_object_id) ?
'NULL' : $this->src_object_id);
327 if ($this->
db->lasterrno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
328 $this->errors[] =
'Error DB_ERROR_RECORD_ALREADY_EXISTS : '.$this->db->lasterror();
330 $this->errors[] =
'Error '.$this->db->lasterror();
332 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
336 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
337 $this->position = $maxposition;
342 $result = $this->
call_trigger(strtoupper(get_class($this)).
'_CREATE', $user);
352 $this->
db->rollback();
374 public function fetch($id, $ref =
'', $relativepath =
'', $hashoffile =
'', $hashforshare =
'', $src_object_type =
'', $src_object_id = 0)
385 $sql .=
" t.entity,";
386 $sql .=
" t.filename,";
387 $sql .=
" t.filepath,";
388 $sql .=
" t.fullpath_orig,";
389 $sql .=
" t.description,";
390 $sql .=
" t.keywords,";
392 $sql .=
" t.position,";
393 $sql .=
" t.gen_or_uploaded,";
394 $sql .=
" t.extraparams,";
395 $sql .=
" t.date_c,";
396 $sql .=
" t.tms as date_m,";
397 $sql .=
" t.fk_user_c,";
398 $sql .=
" t.fk_user_m,";
399 $sql .=
' t.note_private,';
400 $sql .=
' t.note_public,';
402 $sql .=
" t.src_object_type,";
403 $sql .=
" t.src_object_id";
404 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
405 $sql .=
' WHERE 1 = 1';
411 $relativepathwithnoexe = preg_replace(
'/\.noexe$/',
'', $relativepath);
412 $sql .=
" AND t.filepath = '".$this->db->escape(dirname($relativepath)).
"'";
413 $filename = basename($relativepathwithnoexe);
414 if ($filename !=
'*') {
415 $sql .=
" AND t.filename = '".$this->db->escape($filename).
"'";
417 $sql .=
" AND t.entity = ".$conf->entity;
418 } elseif (!empty($ref)) {
419 $sql .=
" AND t.ref = '".$this->db->escape($ref).
"'";
420 $sql .=
" AND t.entity = ".$conf->entity;
421 } elseif (!empty($hashoffile)) {
422 $sql .=
" AND t.label = '".$this->db->escape($hashoffile).
"'";
423 $sql .=
" AND t.entity = ".$conf->entity;
424 } elseif (!empty($hashforshare)) {
425 if ($hashforshare !=
'shared') {
426 $sql .=
" AND t.share = '".$this->db->escape($hashforshare).
"'";
428 $sql .=
" AND t.share IS NOT NULL AND t.share <> ''";
431 } elseif ($src_object_type && $src_object_id) {
433 $sql .=
" AND t.src_object_type = '".$this->db->escape($src_object_type).
"' AND t.src_object_id = ".((int) $src_object_id);
434 $sql .=
" AND t.entity = ".((int) $conf->entity);
436 $sql .=
' AND t.rowid = '.((int) $id);
439 $this->
db->plimit(1);
440 $this->
db->order(
't.rowid',
'ASC');
444 $numrows = $this->
db->num_rows(
$resql);
446 $obj = $this->
db->fetch_object(
$resql);
448 $this->
id = $obj->rowid;
449 $this->
ref = $obj->ref;
450 $this->label = $obj->label;
451 $this->share = $obj->share;
452 $this->entity = $obj->entity;
453 $this->filename = $obj->filename;
454 $this->filepath = $obj->filepath;
455 $this->fullpath_orig = $obj->fullpath_orig;
457 $this->keywords = $obj->keywords;
458 $this->cover = $obj->cover;
459 $this->position = $obj->position;
460 $this->gen_or_uploaded = $obj->gen_or_uploaded;
461 $this->extraparams = $obj->extraparams;
462 $this->date_c = $this->
db->jdate($obj->date_c);
463 $this->date_m = $this->
db->jdate($obj->date_m);
464 $this->fk_user_c = $obj->fk_user_c;
465 $this->fk_user_m = $obj->fk_user_m;
466 $this->note_private = $obj->note_private;
467 $this->note_public = $obj->note_public;
468 $this->acl = $obj->acl;
469 $this->src_object_type = $obj->src_object_type;
470 $this->src_object_id = $obj->src_object_id;
487 $this->errors[] =
'Error '.$this->db->lasterror();
488 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
506 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
514 $sql .=
" t.entity,";
515 $sql .=
" t.filename,";
516 $sql .=
" t.filepath,";
517 $sql .=
" t.fullpath_orig,";
518 $sql .=
" t.description,";
519 $sql .=
" t.keywords,";
521 $sql .=
" t.position,";
522 $sql .=
" t.gen_or_uploaded,";
523 $sql .=
" t.extraparams,";
524 $sql .=
" t.date_c,";
525 $sql .=
" t.tms as date_m,";
526 $sql .=
" t.fk_user_c,";
527 $sql .=
" t.fk_user_m,";
529 $sql .=
" t.src_object_type,";
530 $sql .=
" t.src_object_id";
531 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
535 if (count($filter) > 0) {
536 foreach ($filter as $key => $value) {
537 if ($key ==
't.src_object_id') {
538 $sqlwhere[] = $key.
" = ".((int) $value);
540 $sqlwhere[] = $key.
" LIKE '%".$this->
db->escape($value).
"%'";
544 $sql .=
' WHERE 1 = 1';
549 if (count($sqlwhere) > 0) {
550 $sql .=
' AND '.implode(
' '.$this->
db->escape($filtermode).
' ', $sqlwhere);
552 if (!empty($sortfield)) {
553 $sql .= $this->
db->order($sortfield, $sortorder);
555 if (!empty($limit)) {
556 $sql .= $this->
db->plimit($limit, $offset);
559 $this->lines = array();
565 while ($obj = $this->
db->fetch_object(
$resql)) {
568 $line->id = $obj->rowid;
569 $line->ref = $obj->rowid;
570 $line->label = $obj->label;
571 $line->share = $obj->share;
572 $line->entity = $obj->entity;
573 $line->filename = $obj->filename;
574 $line->filepath = $obj->filepath;
575 $line->fullpath_orig = $obj->fullpath_orig;
576 $line->description = $obj->description;
577 $line->keywords = $obj->keywords;
578 $line->cover = $obj->cover;
579 $line->position = $obj->position;
580 $line->gen_or_uploaded = $obj->gen_or_uploaded;
581 $line->extraparams = $obj->extraparams;
582 $line->date_c = $this->
db->jdate($obj->date_c);
583 $line->date_m = $this->
db->jdate($obj->date_m);
584 $line->fk_user_c = $obj->fk_user_c;
585 $line->fk_user_m = $obj->fk_user_m;
586 $line->acl = $obj->acl;
587 $line->src_object_type = $obj->src_object_type;
588 $line->src_object_id = $obj->src_object_id;
589 $this->lines[] = $line;
595 $this->errors[] =
'Error '.$this->db->lasterror();
596 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
620 if (isset($this->
ref)) {
621 $this->
ref = trim($this->
ref);
623 if (isset($this->label)) {
624 $this->label = trim($this->label);
626 if (isset($this->share)) {
627 $this->share = trim($this->share);
629 if (isset($this->entity)) {
630 $this->entity = trim($this->entity);
632 if (isset($this->filename)) {
633 $this->filename = preg_replace(
'/\.noexe$/',
'', trim($this->filename));
635 if (isset($this->filepath)) {
636 $this->filepath = trim($this->filepath);
637 $this->filepath = preg_replace(
'/[\\/]+$/',
'', $this->filepath);
639 if (isset($this->fullpath_orig)) {
640 $this->fullpath_orig = trim($this->fullpath_orig);
645 if (isset($this->keywords)) {
646 $this->keywords = trim($this->keywords);
648 if (isset($this->cover)) {
649 $this->cover = trim($this->cover);
651 if (isset($this->gen_or_uploaded)) {
652 $this->gen_or_uploaded = trim($this->gen_or_uploaded);
654 if (isset($this->extraparams)) {
655 $this->extraparams = trim($this->extraparams);
657 if (isset($this->fk_user_m)) {
658 $this->fk_user_m = trim($this->fk_user_m);
660 if (isset($this->acl)) {
661 $this->acl = trim($this->acl);
663 if (isset($this->src_object_type)) {
664 $this->src_object_type = trim($this->src_object_type);
671 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.
' SET';
672 $sql .=
" ref = '".$this->db->escape(
dol_hash($this->filepath.
"/".$this->filename, 3)).
"',";
673 $sql .=
' label = '.(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
',';
674 $sql .=
' share = '.(!empty($this->share) ?
"'".$this->db->escape($this->share).
"'" :
"null").
',';
675 $sql .=
' entity = '.(isset($this->entity) ? $this->entity : $conf->entity).
',';
676 $sql .=
' filename = '.(isset($this->filename) ?
"'".$this->db->escape($this->filename).
"'" :
"null").
',';
677 $sql .=
' filepath = '.(isset($this->filepath) ?
"'".$this->db->escape($this->filepath).
"'" :
"null").
',';
678 $sql .=
' fullpath_orig = '.(isset($this->fullpath_orig) ?
"'".$this->db->escape($this->fullpath_orig).
"'" :
"null").
',';
679 $sql .=
' description = '.(isset($this->
description) ?
"'".$this->db->escape($this->
description).
"'" :
"null").
',';
680 $sql .=
' keywords = '.(isset($this->keywords) ?
"'".$this->db->escape($this->keywords).
"'" :
"null").
',';
681 $sql .=
' cover = '.(isset($this->cover) ?
"'".$this->db->escape($this->cover).
"'" :
"null").
',';
682 $sql .=
' position = '.(isset($this->position) ? $this->
db->escape($this->position) :
"0").
',';
683 $sql .=
' gen_or_uploaded = '.(isset($this->gen_or_uploaded) ?
"'".$this->db->escape($this->gen_or_uploaded).
"'" :
"null").
',';
684 $sql .=
' extraparams = '.(isset($this->extraparams) ?
"'".$this->db->escape($this->extraparams).
"'" :
"null").
',';
685 $sql .=
' date_c = '.(!isset($this->date_c) ||
dol_strlen($this->date_c) != 0 ?
"'".$this->db->idate($this->date_c).
"'" :
'null').
',';
687 $sql .=
' fk_user_m = '.($this->fk_user_m > 0 ? $this->fk_user_m : $user->id).
',';
688 $sql .=
' acl = '.(isset($this->acl) ?
"'".$this->db->escape($this->acl).
"'" :
"null").
',';
689 $sql .=
' src_object_id = '.($this->src_object_id > 0 ? $this->src_object_id :
"null").
',';
690 $sql .=
' src_object_type = '.(isset($this->src_object_type) ?
"'".$this->db->escape($this->src_object_type).
"'" :
"null");
691 $sql .=
' WHERE rowid='.((int) $this->
id);
698 $this->errors[] =
'Error '.$this->db->lasterror();
699 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
703 if (!$error && !$notrigger) {
705 $result = $this->
call_trigger(strtoupper(get_class($this)).
'_MODIFY', $user);
714 $this->
db->rollback();
732 public function delete(
User $user, $notrigger =
false)
743 $result = $this->
call_trigger(strtoupper(get_class($this)).
'_DELETE', $user);
754 dol_syslog(get_class($this).
"::delete error ".$this->error, LOG_ERR);
759 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
760 $sql .=
' WHERE rowid='.((int) $this->
id);
765 $this->errors[] =
'Error '.$this->db->lasterror();
766 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
772 $this->
db->rollback();
794 $object =
new Ecmfiles($this->
db);
799 $object->fetch($fromid);
807 $object->context[
'createfromclone'] =
'createfromclone';
808 $result = $object->create($user);
813 $this->errors = $object->errors;
814 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
817 unset($object->context[
'createfromclone']);
825 $this->
db->rollback();
841 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $maxlen = 24, $morecss =
'')
843 global $db, $conf, $langs;
844 global $dolibarr_main_authentication, $dolibarr_main_demo;
845 global $menumanager, $hookmanager;
847 if (!empty($conf->dol_no_mouse_hover)) {
853 $label =
'<u>'.$langs->trans(
"MyModule").
'</u>';
855 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
857 $url = DOL_URL_ROOT.
'/ecm/'.$this->table_name.
'_card.php?id='.$this->id;
860 if (empty($notooltip)) {
861 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
862 $label = $langs->trans(
"ShowProject");
863 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
865 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
866 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
868 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
871 $linkstart =
'<a href="'.$url.
'"';
872 $linkstart .= $linkclose.
'>';
876 $result .= ($linkstart.img_object(($notooltip ?
'' : $label),
'label', ($notooltip ?
'' :
'class="classfortooltip"')).$linkend);
877 if ($withpicto != 2) {
881 $result .= $linkstart.$this->ref.$linkend;
884 $hookmanager->initHooks(array($this->element .
'dao'));
885 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
886 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
888 $result = $hookmanager->resPrint;
890 $result .= $hookmanager->resPrint;
903 return $this->
LibStatut($this->status, $mode);
934 $this->label =
'0a1b2c3e4f59999999';
936 $this->filename =
'myspecimenfilefile.pdf';
937 $this->filepath =
'/aaa/bbb';
938 $this->fullpath_orig =
'c:/file on my disk.pdf';
939 $this->
description =
'This is a long description of file';
940 $this->keywords =
'key1,key2';
943 $this->gen_or_uploaded =
'uploaded';
944 $this->extraparams =
'';
945 $this->date_c = (
dol_now() - 3600 * 24 * 10);
947 $this->fk_user_c = $user->id;
948 $this->fk_user_m =
'';
950 $this->src_object_type =
'product';
951 $this->src_object_id = 1;
973 public $fullpath_orig;
983 public $gen_or_uploaded;
999 public $src_object_type;
1000 public $src_object_id;