33 public $element =
'ecm_directories';
38 public $table_element =
'ecm_directories';
43 public $picto =
'folder-open';
68 public $cachenbofdoc = -1;
98 public $cats = array();
103 public $motherof = array();
108 public $forbiddenchars = array(
'<',
'>',
':',
'/',
'\\',
'?',
'*',
'|',
'"');
113 public $forbiddencharsdir = array(
'<',
'>',
':',
'?',
'*',
'|',
'"');
118 public $full_arbo_loaded;
140 global $conf, $langs;
148 $this->date_c = $now;
149 $this->fk_user_c = $user->id;
150 if ($this->fk_parent <= 0) {
151 $this->fk_parent = 0;
156 $relativepath = $this->label;
157 if ($this->fk_parent > 0) {
159 $parent->fetch($this->fk_parent);
160 $relativepath = $parent->getRelativePath().$relativepath;
162 $relativepath = preg_replace(
'/([\/])+/i',
'/', $relativepath);
166 $cate_arbo = $cat->get_full_arbo(1);
168 foreach ($cate_arbo as $key => $categ) {
169 $path = str_replace($this->forbiddencharsdir,
'_', $categ[
'fullrelativename']);
171 if ($path == $relativepath) {
178 $this->error =
"ErrorDirAlreadyExists";
179 dol_syslog(get_class($this).
"::create ".$this->error, LOG_WARNING);
185 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"ecm_directories(";
188 $sql .=
"fk_parent,";
189 $sql .=
"description,";
190 $sql .=
"cachenbofdoc,";
193 $sql .=
") VALUES (";
194 $sql .=
" '".$this->db->escape($this->label).
"',";
195 $sql .=
" '".$this->db->escape($conf->entity).
"',";
196 $sql .=
" ".($this->fk_parent > 0 ? ((int) $this->fk_parent) :
"null").
",";
198 $sql .=
" ".((int) $this->cachenbofdoc).
",";
199 $sql .=
" '".$this->db->idate($this->date_c).
"',";
200 $sql .=
" ".($this->fk_user_c > 0 ? ((int) $this->fk_user_c) :
"null");
203 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
204 $resql = $this->db->query(
$sql);
206 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"ecm_directories");
211 $error++; $this->error =
"ErrorFailedToCreateDir";
215 $result = $this->
call_trigger(
'MYECMDIR_CREATE', $user);
225 $this->db->rollback();
229 $this->error =
"Error ".$this->db->lasterror();
230 $this->db->rollback();
243 public function update($user =
null, $notrigger = 0)
245 global $conf, $langs;
250 $this->label = trim($this->label);
252 if ($this->fk_parent <= 0) {
253 $this->fk_parent = 0;
259 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"ecm_directories SET";
260 $sql .=
" label = '".$this->db->escape($this->label).
"',";
261 $sql .=
" fk_parent = ".($this->fk_parent > 0 ? ((int) $this->fk_parent) :
"null").
",";
263 $sql .=
" WHERE rowid = ".((int) $this->
id);
265 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
266 $resql = $this->db->query(
$sql);
269 $this->error =
"Error ".$this->db->lasterror();
272 if (!$error && !$notrigger) {
274 $result = $this->
call_trigger(
'MYECMDIR_MODIFY', $user);
285 $this->db->rollback();
300 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"ecm_directories SET";
301 if (preg_match(
'/[0-9]+/', $value)) {
302 $sql .=
" cachenbofdoc = ".(int) $value;
304 $sql .=
" cachenbofdoc = cachenbofdoc ".$value.
" 1";
306 $sql .=
" WHERE rowid = ".((int) $this->
id);
308 dol_syslog(get_class($this).
"::changeNbOfFiles", LOG_DEBUG);
309 $resql = $this->db->query(
$sql);
311 $this->error =
"Error ".$this->db->lasterror();
314 if (preg_match(
'/[0-9]+/', $value)) {
315 $this->cachenbofdoc = (int) $value;
316 } elseif ($value ==
'+') {
317 $this->cachenbofdoc++;
318 } elseif ($value ==
'-') {
319 $this->cachenbofdoc--;
338 $sql .=
" t.fk_parent,";
339 $sql .=
" t.description,";
340 $sql .=
" t.cachenbofdoc,";
341 $sql .=
" t.fk_user_c,";
342 $sql .=
" t.fk_user_m,";
343 $sql .=
" t.date_c as date_c,";
344 $sql .=
" t.tms as date_m";
345 $sql .=
" FROM ".MAIN_DB_PREFIX.
"ecm_directories as t";
346 $sql .=
" WHERE t.rowid = ".((int) $id);
348 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
349 $resql = $this->db->query(
$sql);
351 $obj = $this->db->fetch_object($resql);
353 $this->
id = $obj->rowid;
354 $this->
ref = $obj->rowid;
356 $this->label = $obj->label;
357 $this->fk_parent = $obj->fk_parent;
359 $this->cachenbofdoc = $obj->cachenbofdoc;
360 $this->fk_user_m = $obj->fk_user_m;
361 $this->fk_user_c = $obj->fk_user_c;
362 $this->date_c = $this->db->jdate($obj->date_c);
363 $this->date_m = $this->db->jdate($obj->date_m);
370 $this->db->free($resql);
374 $this->error =
"Error ".$this->db->lasterror();
388 public function delete($user, $mode =
'all', $deletedirrecursive = 0)
390 global $conf, $langs;
391 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
395 if ($mode !=
'databaseonly') {
399 dol_syslog(get_class($this).
"::delete remove directory id=".$this->
id.
" mode=".$mode.(($mode ==
'databaseonly') ?
'' :
' relativepath='.$relativepath));
403 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"ecm_directories";
404 $sql .=
" WHERE rowid=".((int) $this->
id);
406 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
407 $resql = $this->db->query(
$sql);
409 $this->db->rollback();
410 $this->error =
"Error ".$this->db->lasterror();
414 $result = $this->
call_trigger(
'MYECMDIR_DELETE', $user);
416 $this->db->rollback();
422 if ($mode !=
'databaseonly') {
423 $file = $conf->ecm->dir_output.
"/".$relativepath;
424 if ($deletedirrecursive) {
434 $this->error =
'ErrorFailToDeleteDir';
435 dol_syslog(get_class($this).
"::delete ".$this->error, LOG_ERR);
436 $this->db->rollback();
459 $this->label =
'MyDirectory';
460 $this->fk_parent =
'0';
475 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $more =
'', $notooltip = 0)
477 global $langs, $hookmanager;
481 $newref = $this->ref;
482 $label = $langs->trans(
"ShowECMSection").
': '.$newref;
483 $linkclose =
'"'.($more ?
' '.$more :
'').
' title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
485 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/ecm/dir_card.php?section='.$this->
id.$linkclose;
486 if ($option ==
'index') {
487 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/ecm/index.php?section='.$this->
id.
'&sectionexpand=true'.$linkclose;
489 if ($option ==
'indexexpanded') {
490 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/ecm/index.php?section='.$this->
id.
'&sectionexpand=false'.$linkclose;
492 if ($option ==
'indexnotexpanded') {
493 $linkstart =
'<a href="'.DOL_URL_ROOT.
'/ecm/index.php?section='.$this->
id.
'&sectionexpand=true'.$linkclose;
500 $result .= $linkstart;
502 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
504 if ($withpicto != 2) {
505 $result .= ($max ?
dol_trunc($newref, $max,
'middle') : $newref);
510 $hookmanager->initHooks(array($this->element .
'dao'));
511 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
512 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
514 $result = $hookmanager->resPrint;
516 $result .= $hookmanager->resPrint;
532 $idtosearch = $this->id;
537 foreach ($this->cats as $key => $val) {
538 if ($this->cats[$key][
'id'] == $idtosearch) {
545 if ($cursorindex >= 0) {
549 $idtosearch = $this->cats[$cursorindex][
'id_mere'];
552 }
while ($cursorindex >= 0 && !empty($idtosearch) && $i < 100);
568 $this->motherof = array();
571 $sql =
"SELECT fk_parent as id_parent, rowid as id_son";
572 $sql .=
" FROM ".MAIN_DB_PREFIX.
"ecm_directories";
573 $sql .=
" WHERE fk_parent != 0";
574 $sql .=
" AND entity = ".$conf->entity;
576 dol_syslog(get_class($this).
"::load_motherof", LOG_DEBUG);
577 $resql = $this->db->query(
$sql);
580 while ($obj = $this->db->fetch_object($resql)) {
581 $this->motherof[$obj->id_son] = $obj->id_parent;
599 return $this->
LibStatut($this->status, $mode);
643 if (empty($force) && !empty($this->full_arbo_loaded)) {
651 $sql =
"SELECT c.rowid as rowid, c.label as label,";
652 $sql .=
" c.description as description, c.cachenbofdoc,";
653 $sql .=
" c.fk_user_c,";
654 $sql .=
" c.date_c,";
655 $sql .=
" u.login as login_c,";
656 $sql .=
" u.statut as statut_c,";
657 $sql .=
" ca.rowid as rowid_fille";
658 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user as u, ".MAIN_DB_PREFIX.
"ecm_directories as c";
659 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"ecm_directories as ca";
660 $sql .=
" ON c.rowid = ca.fk_parent";
661 $sql .=
" WHERE c.fk_user_c = u.rowid";
662 $sql .=
" AND c.entity = ".$conf->entity;
663 $sql .=
" ORDER BY c.label, c.rowid";
665 dol_syslog(get_class($this).
"::get_full_arbo", LOG_DEBUG);
666 $resql = $this->db->query(
$sql);
668 $this->cats = array();
671 while ($obj = $this->db->fetch_object($resql)) {
672 $this->cats[$obj->rowid][
'id'] = $obj->rowid;
673 $this->cats[$obj->rowid][
'id_mere'] = (isset($this->motherof[$obj->rowid]) ? $this->motherof[$obj->rowid] :
'');
674 $this->cats[$obj->rowid][
'label'] = $obj->label;
675 $this->cats[$obj->rowid][
'description'] = $obj->description;
676 $this->cats[$obj->rowid][
'cachenbofdoc'] = $obj->cachenbofdoc;
677 $this->cats[$obj->rowid][
'date_c'] = $this->db->jdate($obj->date_c);
678 $this->cats[$obj->rowid][
'fk_user_c'] = (int) $obj->fk_user_c;
679 $this->cats[$obj->rowid][
'statut_c'] = (
int) $obj->statut_c;
680 $this->cats[$obj->rowid][
'login_c'] = $obj->login_c;
682 if (!empty($obj->rowid_fille)) {
683 if (isset($this->cats[$obj->rowid][
'id_children']) && is_array($this->cats[$obj->rowid][
'id_children'])) {
684 $newelempos = count($this->cats[$obj->rowid][
'id_children']);
686 $this->cats[$obj->rowid][
'id_children'][$newelempos] = $obj->rowid_fille;
689 $this->cats[$obj->rowid][
'id_children'] = array($obj->rowid_fille);
700 foreach ($this->cats as $key => $val) {
701 if (isset($this->motherof[$key])) {
707 $this->cats =
dol_sort_array($this->cats,
'fulllabel',
'asc',
true,
false);
708 $this->full_arbo_loaded = 1;
724 if (!empty($this->cats[$id_categ][
'id_mere'])) {
725 $this->cats[$id_categ][
'fullpath'] = $this->cats[$this->cats[$id_categ][
'id_mere']][
'fullpath'];
726 $this->cats[$id_categ][
'fullpath'] .=
'_'.$id_categ;
727 $this->cats[$id_categ][
'fullrelativename'] = $this->cats[$this->cats[$id_categ][
'id_mere']][
'fullrelativename'];
728 $this->cats[$id_categ][
'fullrelativename'] .=
'/'.$this->cats[$id_categ][
'label'];
729 $this->cats[$id_categ][
'fulllabel'] = $this->cats[$this->cats[$id_categ][
'id_mere']][
'fulllabel'];
730 $this->cats[$id_categ][
'fulllabel'] .=
' >> '.$this->cats[$id_categ][
'label'];
732 $this->cats[$id_categ][
'fullpath'] =
'_'.$id_categ;
733 $this->cats[$id_categ][
'fullrelativename'] = $this->cats[$id_categ][
'label'];
734 $this->cats[$id_categ][
'fulllabel'] = $this->cats[$id_categ][
'label'];
737 $this->cats[$id_categ][
'level'] = strlen(preg_replace(
'/([^_])/i',
'', $this->cats[$id_categ][
'fullpath']));
741 if ($protection > 20) {
744 if (isset($this->cats[$id_categ][
'id_children']) && is_array($this->cats[$id_categ][
'id_children'])) {
745 foreach ($this->cats[$id_categ][
'id_children'] as $key => $val) {
760 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
763 $filelist =
dol_dir_list($dir,
'files', 0,
'',
'(\.meta|_preview.*\.png)$');
769 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"ecm_directories SET";
770 $sql .=
" cachenbofdoc = '".count($filelist).
"'";
772 $sql .=
" WHERE rowid = ".((int) $this->
id);
774 $sql .=
" WHERE entity = ".$conf->entity;
777 dol_syslog(get_class($this).
"::refreshcachenboffile", LOG_DEBUG);
778 $resql = $this->db->query(
$sql);
780 $this->cachenbofdoc = count($filelist);
781 return $this->cachenbofdoc;
783 $this->error =
"Error ".$this->db->lasterror();
804 global $langs, $conf;
806 include_once DOL_DOCUMENT_ROOT.
'/core/class/interfaces.class.php';
808 $result = $interface->run_triggers($triggerName, $this, $user, $langs, $conf);
810 if (!empty($this->errors)) {
811 $this->errors = array_merge($this->errors, $interface->errors);
813 $this->errors = $interface->errors;