ecmdirectory.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2008-2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/ecm/class/ecmdirectory.class.php
  20. * \ingroup ecm
  21. * \brief This file is an example for a class file
  22. */
  23. /**
  24. * Class to manage ECM directories
  25. */
  26. class EcmDirectory // extends CommonObject
  27. {
  28. /**
  29. * @var string ID to identify managed object
  30. */
  31. public $element='ecm_directories';
  32. /**
  33. * @var string Name of table without prefix where object is stored
  34. */
  35. //public $table_element='ecm_directories';
  36. var $picto = 'dir';
  37. /**
  38. * @var int ID
  39. */
  40. public $id;
  41. /**
  42. * @var string proper name for given parameter
  43. */
  44. public $label;
  45. var $fk_parent;
  46. var $description;
  47. var $cachenbofdoc=-1; // By default cache initialized with value 'not calculated'
  48. var $date_c;
  49. var $date_m;
  50. public $fk_user_m;
  51. public $fk_user_c;
  52. /**
  53. * @var string Ref
  54. */
  55. public $ref;
  56. var $cats=array();
  57. var $motherof=array();
  58. var $forbiddenchars = array('<','>',':','/','\\','?','*','|','"');
  59. var $forbiddencharsdir = array('<','>',':','?','*','|','"');
  60. public $full_arbo_loaded;
  61. /**
  62. * @var string Error code (or message)
  63. */
  64. public $error;
  65. /**
  66. * @var string[] Error codes (or messages)
  67. */
  68. public $errors = array();
  69. /**
  70. * Constructor
  71. *
  72. * @param DoliDB $db Database handler
  73. */
  74. function __construct($db)
  75. {
  76. $this->db = $db;
  77. return 1;
  78. }
  79. /**
  80. * Create record into database
  81. *
  82. * @param User $user User that create
  83. * @return int <0 if KO, >0 if OK
  84. */
  85. function create($user)
  86. {
  87. global $conf, $langs;
  88. $error=0;
  89. $now=dol_now();
  90. // Clean parameters
  91. $this->label=dol_sanitizeFileName(trim($this->label));
  92. $this->fk_parent=trim($this->fk_parent);
  93. $this->description=trim($this->description);
  94. $this->date_c=$now;
  95. $this->fk_user_c=$user->id;
  96. if ($this->fk_parent <= 0) $this->fk_parent=0;
  97. // Check if same directory does not exists with this name
  98. $relativepath=$this->label;
  99. if ($this->fk_parent)
  100. {
  101. $parent = new EcmDirectory($this->db);
  102. $parent->fetch($this->fk_parent);
  103. $relativepath=$parent->getRelativePath().$relativepath;
  104. }
  105. $relativepath=preg_replace('/([\/])+/i','/',$relativepath); // Avoid duplicate / or \
  106. //print $relativepath.'<br>';
  107. $cat = new EcmDirectory($this->db);
  108. $cate_arbo = $cat->get_full_arbo(1);
  109. $pathfound=0;
  110. foreach ($cate_arbo as $key => $categ)
  111. {
  112. $path=str_replace($this->forbiddencharsdir, '_', $categ['fullrelativename']);
  113. //print $relativepath.' - '.$path.'<br>';
  114. if ($path == $relativepath)
  115. {
  116. $pathfound=1;
  117. break;
  118. }
  119. }
  120. if ($pathfound)
  121. {
  122. $this->error="ErrorDirAlreadyExists";
  123. dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING);
  124. return -1;
  125. }
  126. else
  127. {
  128. $this->db->begin();
  129. // Insert request
  130. $sql = "INSERT INTO ".MAIN_DB_PREFIX."ecm_directories(";
  131. $sql.= "label,";
  132. $sql.= "entity,";
  133. $sql.= "fk_parent,";
  134. $sql.= "description,";
  135. $sql.= "cachenbofdoc,";
  136. $sql.= "date_c,";
  137. $sql.= "fk_user_c";
  138. $sql.= ") VALUES (";
  139. $sql.= " '".$this->db->escape($this->label)."',";
  140. $sql.= " '".$this->db->escape($conf->entity)."',";
  141. $sql.= " '".$this->db->escape($this->fk_parent)."',";
  142. $sql.= " '".$this->db->escape($this->description)."',";
  143. $sql.= " ".$this->cachenbofdoc.",";
  144. $sql.= " '".$this->db->idate($this->date_c)."',";
  145. $sql.= " '".$this->db->escape($this->fk_user_c)."'";
  146. $sql.= ")";
  147. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  148. $resql=$this->db->query($sql);
  149. if ($resql)
  150. {
  151. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."ecm_directories");
  152. $dir=$conf->ecm->dir_output.'/'.$this->getRelativePath();
  153. $result=dol_mkdir($dir);
  154. if ($result < 0) { $error++; $this->error="ErrorFailedToCreateDir"; }
  155. // Call trigger
  156. $result=$this->call_trigger('MYECMDIR_CREATE',$user);
  157. if ($result < 0) { $error++; }
  158. // End call triggers
  159. if (! $error)
  160. {
  161. $this->db->commit();
  162. return $this->id;
  163. }
  164. else
  165. {
  166. $this->db->rollback();
  167. return -1;
  168. }
  169. }
  170. else
  171. {
  172. $this->error="Error ".$this->db->lasterror();
  173. $this->db->rollback();
  174. return -1;
  175. }
  176. }
  177. }
  178. /**
  179. * Update database
  180. *
  181. * @param User $user User that modify
  182. * @param int $notrigger 0=no, 1=yes (no update trigger)
  183. * @return int <0 if KO, >0 if OK
  184. */
  185. function update($user=null, $notrigger=0)
  186. {
  187. global $conf, $langs;
  188. $error=0;
  189. // Clean parameters
  190. $this->label=trim($this->label);
  191. $this->fk_parent=trim($this->fk_parent);
  192. $this->description=trim($this->description);
  193. // Check parameters
  194. // Put here code to add control on parameters values
  195. $this->db->begin();
  196. // Update request
  197. $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories SET";
  198. $sql.= " label='".$this->db->escape($this->label)."',";
  199. $sql.= " fk_parent='".$this->db->escape($this->fk_parent)."',";
  200. $sql.= " description='".$this->db->escape($this->description)."'";
  201. $sql.= " WHERE rowid=".$this->id;
  202. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  203. $resql = $this->db->query($sql);
  204. if (! $resql)
  205. {
  206. $error++;
  207. $this->error="Error ".$this->db->lasterror();
  208. }
  209. if (! $error && ! $notrigger)
  210. {
  211. // Call trigger
  212. $result=$this->call_trigger('MYECMDIR_MODIFY',$user);
  213. if ($result < 0) { $error++; }
  214. // End call triggers
  215. }
  216. if (! $error)
  217. {
  218. $this->db->commit();
  219. return 1;
  220. }
  221. else
  222. {
  223. $this->db->rollback();
  224. return -1;
  225. }
  226. }
  227. /**
  228. * Update cache of nb of documents into database
  229. *
  230. * @param string $value '+' or '-' or new number
  231. * @return int <0 if KO, >0 if OK
  232. */
  233. function changeNbOfFiles($value)
  234. {
  235. // Update request
  236. $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories SET";
  237. if (preg_match('/[0-9]+/', $value)) $sql.= " cachenbofdoc = ".(int) $value;
  238. else $sql.= " cachenbofdoc = cachenbofdoc ".$value." 1";
  239. $sql.= " WHERE rowid = ".$this->id;
  240. dol_syslog(get_class($this)."::changeNbOfFiles", LOG_DEBUG);
  241. $resql = $this->db->query($sql);
  242. if (! $resql)
  243. {
  244. $this->error="Error ".$this->db->lasterror();
  245. return -1;
  246. }
  247. else
  248. {
  249. if (preg_match('/[0-9]+/', $value)) $this->cachenbofdoc = (int) $value;
  250. else if ($value == '+') $this->cachenbofdoc++;
  251. else if ($value == '-') $this->cachenbofdoc--;
  252. }
  253. return 1;
  254. }
  255. /**
  256. * Load object in memory from database
  257. *
  258. * @param int $id Id of object
  259. * @return int <0 if KO, 0 if not found, >0 if OK
  260. */
  261. function fetch($id)
  262. {
  263. $sql = "SELECT";
  264. $sql.= " t.rowid,";
  265. $sql.= " t.label,";
  266. $sql.= " t.fk_parent,";
  267. $sql.= " t.description,";
  268. $sql.= " t.cachenbofdoc,";
  269. $sql.= " t.fk_user_c,";
  270. $sql.= " t.fk_user_m,";
  271. $sql.= " t.date_c as date_c,";
  272. $sql.= " t.date_m as date_m";
  273. $sql.= " FROM ".MAIN_DB_PREFIX."ecm_directories as t";
  274. $sql.= " WHERE t.rowid = ".$id;
  275. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  276. $resql=$this->db->query($sql);
  277. if ($resql)
  278. {
  279. $obj = $this->db->fetch_object($resql);
  280. if ($obj)
  281. {
  282. $this->id = $obj->rowid;
  283. $this->ref = $obj->rowid;
  284. $this->label = $obj->label;
  285. $this->fk_parent = $obj->fk_parent;
  286. $this->description = $obj->description;
  287. $this->cachenbofdoc = $obj->cachenbofdoc;
  288. $this->fk_user_m = $obj->fk_user_m;
  289. $this->fk_user_c = $obj->fk_user_c;
  290. $this->date_c = $this->db->jdate($obj->date_c);
  291. $this->date_m = $this->db->jdate($obj->date_m);
  292. }
  293. $this->db->free($resql);
  294. return $obj?1:0;
  295. }
  296. else
  297. {
  298. $this->error="Error ".$this->db->lasterror();
  299. return -1;
  300. }
  301. }
  302. /**
  303. * Delete object on database and/or on disk
  304. *
  305. * @param User $user User that delete
  306. * @param string $mode 'all'=delete all, 'databaseonly'=only database entry, 'fileonly' (not implemented)
  307. * @param int $deletedirrecursive 1=Agree to delete content recursiveley (otherwise an error will be returned when trying to delete)
  308. * @return int <0 if KO, >0 if OK
  309. */
  310. function delete($user, $mode='all', $deletedirrecursive=0)
  311. {
  312. global $conf, $langs;
  313. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  314. $error=0;
  315. if ($mode != 'databaseonly') $relativepath=$this->getRelativePath(1); // Ex: dir1/dir2/dir3
  316. dol_syslog(get_class($this)."::delete remove directory id=".$this->id." mode=".$mode.(($mode == 'databaseonly')?'':' relativepath='.$relativepath));
  317. $this->db->begin();
  318. $sql = "DELETE FROM ".MAIN_DB_PREFIX."ecm_directories";
  319. $sql.= " WHERE rowid=".$this->id;
  320. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  321. $resql = $this->db->query($sql);
  322. if (! $resql)
  323. {
  324. $this->db->rollback();
  325. $this->error="Error ".$this->db->lasterror();
  326. return -2;
  327. }
  328. else
  329. {
  330. // Call trigger
  331. $result=$this->call_trigger('MYECMDIR_DELETE',$user);
  332. if ($result < 0)
  333. {
  334. $this->db->rollback();
  335. return -2;
  336. }
  337. // End call triggers
  338. }
  339. if ($mode != 'databaseonly')
  340. {
  341. $file = $conf->ecm->dir_output . "/" . $relativepath;
  342. if ($deletedirrecursive)
  343. {
  344. $result=@dol_delete_dir_recursive($file, 0, 0);
  345. }
  346. else
  347. {
  348. $result=@dol_delete_dir($file, 0);
  349. }
  350. }
  351. if ($result || ! @is_dir(dol_osencode($file)))
  352. {
  353. $this->db->commit();
  354. }
  355. else
  356. {
  357. $this->error='ErrorFailToDeleteDir';
  358. dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
  359. $this->db->rollback();
  360. $error++;
  361. }
  362. if (! $error) return 1;
  363. else return -1;
  364. }
  365. /**
  366. * Initialise an instance with random values.
  367. * Used to build previews or test instances.
  368. * id must be 0 if object instance is a specimen.
  369. *
  370. * @return void
  371. */
  372. function initAsSpecimen()
  373. {
  374. $this->id=0;
  375. $this->label='MyDirectory';
  376. $this->fk_parent='0';
  377. $this->description='This is a directory';
  378. }
  379. /**
  380. * Return directory name you can click (and picto)
  381. *
  382. * @param int $withpicto 0=Pas de picto, 1=Include picto into link, 2=Only picto
  383. * @param string $option Sur quoi pointe le lien
  384. * @param int $max Max length
  385. * @param string $more Add more param on a link
  386. * @param int $notooltip 1=Disable tooltip
  387. * @return string Chaine avec URL
  388. */
  389. function getNomUrl($withpicto=0, $option='', $max=0, $more='', $notooltip=0)
  390. {
  391. global $langs;
  392. $result='';
  393. //$newref=str_replace('_',' ',$this->ref);
  394. $newref=$this->ref;
  395. $label=$langs->trans("ShowECMSection").': '.$newref;
  396. $linkclose='"'.($more?' '.$more:'').' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  397. $linkstart = '<a href="'.DOL_URL_ROOT.'/ecm/dir_card.php?section='.$this->id.$linkclose;
  398. if ($option == 'index') $linkstart = '<a href="'.DOL_URL_ROOT.'/ecm/index.php?section='.$this->id.'&amp;sectionexpand=true'.$linkclose;
  399. if ($option == 'indexexpanded') $linkstart = '<a href="'.DOL_URL_ROOT.'/ecm/index.php?section='.$this->id.'&amp;sectionexpand=false'.$linkclose;
  400. if ($option == 'indexnotexpanded') $linkstart = '<a href="'.DOL_URL_ROOT.'/ecm/index.php?section='.$this->id.'&amp;sectionexpand=true'.$linkclose;
  401. $linkend='</a>';
  402. //$picto=DOL_URL_ROOT.'/theme/common/treemenu/folder.gif';
  403. $picto='dir';
  404. $result .= $linkstart;
  405. if ($withpicto) $result.=img_object(($notooltip?'':$label), $this->picto, ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
  406. if ($withpicto != 2) $result.= ($max?dol_trunc($newref,$max,'middle'):$newref);
  407. $result .= $linkend;
  408. return $result;
  409. }
  410. /**
  411. * Return relative path of a directory on disk
  412. *
  413. * @param int $force Force reload of full arbo even if already loaded
  414. * @return string Relative physical path
  415. */
  416. function getRelativePath($force=0)
  417. {
  418. $this->get_full_arbo($force);
  419. $ret='';
  420. $idtosearch=$this->id;
  421. $i=0;
  422. do {
  423. // Get index cursor in this->cats for id_mere
  424. $cursorindex=-1;
  425. foreach ($this->cats as $key => $val)
  426. {
  427. if ($this->cats[$key]['id'] == $idtosearch)
  428. {
  429. $cursorindex=$key;
  430. break;
  431. }
  432. }
  433. //print "c=".$idtosearch."-".$cursorindex;
  434. if ($cursorindex >= 0)
  435. {
  436. // Path is label sanitized (no space and no special char) and concatenated
  437. $ret=dol_sanitizeFileName($this->cats[$cursorindex]['label']).'/'.$ret;
  438. $idtosearch=$this->cats[$cursorindex]['id_mere'];
  439. $i++;
  440. }
  441. }
  442. while ($cursorindex >= 0 && ! empty($idtosearch) && $i < 100); // i avoid infinite loop
  443. return $ret;
  444. }
  445. /**
  446. * Load this->motherof that is array(id_son=>id_parent, ...)
  447. *
  448. * @return int <0 if KO, >0 if OK
  449. */
  450. function load_motherof()
  451. {
  452. global $conf;
  453. $this->motherof=array();
  454. // Load array[child]=parent
  455. $sql = "SELECT fk_parent as id_parent, rowid as id_son";
  456. $sql.= " FROM ".MAIN_DB_PREFIX."ecm_directories";
  457. $sql.= " WHERE fk_parent != 0";
  458. $sql.= " AND entity = ".$conf->entity;
  459. dol_syslog(get_class($this)."::load_motherof", LOG_DEBUG);
  460. $resql = $this->db->query($sql);
  461. if ($resql)
  462. {
  463. // This assignment in condition is not a bug. It allows walking the results.
  464. while ($obj=$this->db->fetch_object($resql))
  465. {
  466. $this->motherof[$obj->id_son]=$obj->id_parent;
  467. }
  468. return 1;
  469. }
  470. else
  471. {
  472. dol_print_error($this->db);
  473. return -1;
  474. }
  475. }
  476. /**
  477. * Retourne le libelle du status d'un user (actif, inactif)
  478. *
  479. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  480. * @return string Label of status
  481. */
  482. function getLibStatut($mode=0)
  483. {
  484. return $this->LibStatut($this->status,$mode);
  485. }
  486. /**
  487. * Return the status
  488. *
  489. * @param int $status Id status
  490. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 5=Long label + Picto
  491. * @return string Label of status
  492. */
  493. static function LibStatut($status,$mode=0)
  494. {
  495. global $langs;
  496. return '';
  497. }
  498. /**
  499. * Reconstruit l'arborescence des categories sous la forme d'un tableau à partir de la base de donnée
  500. * Renvoi un tableau de tableau('id','id_mere',...) trie selon arbre et avec:
  501. * id Id de la categorie
  502. * id_mere Id de la categorie mere
  503. * id_children Tableau des id enfant
  504. * label Name of directory
  505. * cachenbofdoc Nb of documents
  506. * date_c Date creation
  507. * fk_user_c User creation
  508. * login_c Login creation
  509. * fullpath Full path of id (Added by build_path_from_id_categ call)
  510. * fullrelativename Full path name (Added by build_path_from_id_categ call)
  511. * fulllabel Full label (Added by build_path_from_id_categ call)
  512. * level Level of line (Added by build_path_from_id_categ call)
  513. *
  514. * @param int $force Force reload of full arbo even if already loaded in cache $this->cats
  515. * @return array Tableau de array
  516. */
  517. function get_full_arbo($force=0)
  518. {
  519. global $conf;
  520. if (empty($force) && ! empty($this->full_arbo_loaded))
  521. {
  522. return $this->cats;
  523. }
  524. // Init this->motherof that is array(id_son=>id_parent, ...)
  525. $this->load_motherof();
  526. // Charge tableau des categories
  527. $sql = "SELECT c.rowid as rowid, c.label as label,";
  528. $sql.= " c.description as description, c.cachenbofdoc,";
  529. $sql.= " c.fk_user_c,";
  530. $sql.= " c.date_c,";
  531. $sql.= " u.login as login_c,";
  532. $sql.= " ca.rowid as rowid_fille";
  533. $sql.= " FROM ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."ecm_directories as c";
  534. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."ecm_directories as ca";
  535. $sql.= " ON c.rowid = ca.fk_parent";
  536. $sql.= " WHERE c.fk_user_c = u.rowid";
  537. $sql.= " AND c.entity = ".$conf->entity;
  538. $sql.= " ORDER BY c.label, c.rowid";
  539. dol_syslog(get_class($this)."::get_full_arbo", LOG_DEBUG);
  540. $resql = $this->db->query($sql);
  541. if ($resql)
  542. {
  543. $this->cats = array();
  544. $i=0;
  545. // This assignment in condition is not a bug. It allows walking the results.
  546. while ($obj = $this->db->fetch_object($resql))
  547. {
  548. $this->cats[$obj->rowid]['id'] = $obj->rowid;
  549. $this->cats[$obj->rowid]['id_mere'] = (isset($this->motherof[$obj->rowid])?$this->motherof[$obj->rowid]:'');
  550. $this->cats[$obj->rowid]['label'] = $obj->label;
  551. $this->cats[$obj->rowid]['description'] = $obj->description;
  552. $this->cats[$obj->rowid]['cachenbofdoc'] = $obj->cachenbofdoc;
  553. $this->cats[$obj->rowid]['date_c'] = $this->db->jdate($obj->date_c);
  554. $this->cats[$obj->rowid]['fk_user_c'] = $obj->fk_user_c;
  555. $this->cats[$obj->rowid]['login_c'] = $obj->login_c;
  556. if (! empty($obj->rowid_fille))
  557. {
  558. if (isset($this->cats[$obj->rowid]['id_children']) && is_array($this->cats[$obj->rowid]['id_children']))
  559. {
  560. $newelempos=count($this->cats[$obj->rowid]['id_children']);
  561. //print "this->cats[$i]['id_children'] est deja un tableau de $newelem elements<br>";
  562. $this->cats[$obj->rowid]['id_children'][$newelempos]=$obj->rowid_fille;
  563. }
  564. else
  565. {
  566. //print "this->cats[".$obj->rowid."]['id_children'] n'est pas encore un tableau<br>";
  567. $this->cats[$obj->rowid]['id_children']=array($obj->rowid_fille);
  568. }
  569. }
  570. $i++;
  571. }
  572. }
  573. else
  574. {
  575. dol_print_error($this->db);
  576. return -1;
  577. }
  578. // We add properties fullxxx to all elements
  579. foreach($this->cats as $key => $val)
  580. {
  581. if (isset($motherof[$key])) continue;
  582. $this->build_path_from_id_categ($key,0);
  583. }
  584. $this->cats=dol_sort_array($this->cats, 'fulllabel', 'asc', true, false);
  585. $this->full_arbo_loaded=1;
  586. return $this->cats;
  587. }
  588. /**
  589. * Define properties fullpath, fullrelativename, fulllabel of a directory of array this->cats and all its childs.
  590. * Separator between directories is always '/', whatever is OS.
  591. *
  592. * @param int $id_categ id_categ entry to update
  593. * @param int $protection Deep counter to avoid infinite loop
  594. * @return void
  595. */
  596. function build_path_from_id_categ($id_categ,$protection=0)
  597. {
  598. // Define fullpath
  599. if (! empty($this->cats[$id_categ]['id_mere']))
  600. {
  601. $this->cats[$id_categ]['fullpath'] =$this->cats[$this->cats[$id_categ]['id_mere']]['fullpath'];
  602. $this->cats[$id_categ]['fullpath'].='_'.$id_categ;
  603. $this->cats[$id_categ]['fullrelativename'] =$this->cats[$this->cats[$id_categ]['id_mere']]['fullrelativename'];
  604. $this->cats[$id_categ]['fullrelativename'].='/'.$this->cats[$id_categ]['label'];
  605. $this->cats[$id_categ]['fulllabel'] =$this->cats[$this->cats[$id_categ]['id_mere']]['fulllabel'];
  606. $this->cats[$id_categ]['fulllabel'].=' >> '.$this->cats[$id_categ]['label'];
  607. }
  608. else
  609. {
  610. $this->cats[$id_categ]['fullpath']='_'.$id_categ;
  611. $this->cats[$id_categ]['fullrelativename']=$this->cats[$id_categ]['label'];
  612. $this->cats[$id_categ]['fulllabel']=$this->cats[$id_categ]['label'];
  613. }
  614. // We count number of _ to have level (we use strlen that is faster than dol_strlen)
  615. $this->cats[$id_categ]['level']=strlen(preg_replace('/([^_])/i','',$this->cats[$id_categ]['fullpath']));
  616. // Traite ces enfants
  617. $protection++;
  618. if ($protection > 20) return; // On ne traite pas plus de 20 niveaux
  619. if (isset($this->cats[$id_categ]['id_children']) && is_array($this->cats[$id_categ]['id_children']))
  620. {
  621. foreach($this->cats[$id_categ]['id_children'] as $key => $val)
  622. {
  623. $this->build_path_from_id_categ($val,$protection);
  624. }
  625. }
  626. }
  627. /**
  628. * Refresh value for cachenboffile. This scan and count files into directory.
  629. *
  630. * @param int $all 0=refresh record using this->id , 1=refresh record using this->entity
  631. * @return int -1 if KO, Nb of files in directory if OK
  632. */
  633. function refreshcachenboffile($all=0)
  634. {
  635. global $conf;
  636. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  637. $dir=$conf->ecm->dir_output.'/'.$this->getRelativePath();
  638. $filelist=dol_dir_list($dir,'files',0,'','(\.meta|_preview.*\.png)$');
  639. // Test if filelist is in database
  640. // Update request
  641. $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories SET";
  642. $sql.= " cachenbofdoc = '".count($filelist)."'";
  643. if (empty($all)) // By default
  644. {
  645. $sql.= " WHERE rowid = ".$this->id;
  646. }
  647. else
  648. {
  649. $sql.= " WHERE entity = ".$conf->entity;
  650. }
  651. dol_syslog(get_class($this)."::refreshcachenboffile", LOG_DEBUG);
  652. $resql = $this->db->query($sql);
  653. if ($resql)
  654. {
  655. $this->cachenbofdoc=count($filelist);
  656. return $this->cachenbofdoc;
  657. }
  658. else
  659. {
  660. $this->error="Error ".$this->db->lasterror();
  661. return -1;
  662. }
  663. }
  664. /**
  665. * Call trigger based on this instance
  666. *
  667. * NB: Error from trigger are stacked in errors
  668. * NB2: if trigger fail, action should be canceled.
  669. * NB3: Should be deleted if EcmDirectory extend CommonObject
  670. *
  671. * @param string $trigger_name trigger's name to execute
  672. * @param User $user Object user
  673. * @return int Result of run_triggers
  674. */
  675. function call_trigger($trigger_name, $user)
  676. {
  677. global $langs,$conf;
  678. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  679. $interface=new Interfaces($this->db);
  680. $result=$interface->run_triggers($trigger_name,$this,$user,$langs,$conf);
  681. if ($result < 0) {
  682. if (!empty($this->errors))
  683. {
  684. $this->errors=array_merge($this->errors,$interface->errors);
  685. }
  686. else
  687. {
  688. $this->errors=$interface->errors;
  689. }
  690. }
  691. return $result;
  692. }
  693. }