ecmdirectory.class.php 23 KB

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