ecmfiles.class.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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 Frédéric France <frederic.france@netlogic.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/ecm/class/ecmfiles.class.php
  24. * \ingroup ecm
  25. * \brief Class to manage ECM Files (Create/Read/Update/Delete)
  26. */
  27. // Put here all includes required by your class file
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  29. /**
  30. * Class to manage ECM files
  31. */
  32. class EcmFiles extends CommonObject
  33. {
  34. /**
  35. * @var string Id to identify managed objects
  36. */
  37. public $element = 'ecmfiles';
  38. /**
  39. * @var string Name of table without prefix where object is stored
  40. */
  41. public $table_element = 'ecm_files';
  42. /**
  43. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  44. */
  45. public $picto = 'folder-open';
  46. /**
  47. * @var string Ref hash of file path
  48. */
  49. public $ref;
  50. /**
  51. * hash of file content (md5_file(dol_osencode($destfull))
  52. * @var string Ecm Files label
  53. */
  54. public $label;
  55. /**
  56. * @var string hash for file sharing, empty by default (example: getRandomPassword(true))
  57. */
  58. public $share;
  59. /**
  60. * @var int Entity
  61. */
  62. public $entity;
  63. /**
  64. * @var string filename, Note: Into ecm database record, the entry $filename never ends with .noexe
  65. */
  66. public $filename;
  67. /**
  68. * @var string filepath
  69. */
  70. public $filepath;
  71. /**
  72. * @var string fullpath origin
  73. */
  74. public $fullpath_orig;
  75. /**
  76. * @var string description
  77. */
  78. public $description;
  79. /**
  80. * @var string keywords
  81. */
  82. public $keywords;
  83. /**
  84. * @var string cover
  85. */
  86. public $cover;
  87. /**
  88. * @var int position
  89. */
  90. public $position;
  91. /**
  92. * @var string can be 'generated', 'uploaded', 'unknown'
  93. */
  94. public $gen_or_uploaded;
  95. /**
  96. * @var string extraparams
  97. */
  98. public $extraparams;
  99. /**
  100. * @var int|string date create
  101. */
  102. public $date_c = '';
  103. /**
  104. * @var int|string date modify
  105. */
  106. public $date_m = '';
  107. /**
  108. * @var int ID
  109. */
  110. public $fk_user_c;
  111. /**
  112. * @var int ID
  113. */
  114. public $fk_user_m;
  115. /**
  116. * @var string acl
  117. */
  118. public $acl;
  119. /**
  120. * @var string src object type
  121. */
  122. public $src_object_type;
  123. /**
  124. * @var int src object id
  125. */
  126. public $src_object_id;
  127. /**
  128. * @var int section_id ID of section = ID of EcmDirectory, directory of manual ECM (not stored into database)
  129. */
  130. public $section_id;
  131. /**
  132. * Constructor
  133. *
  134. * @param DoliDb $db Database handler
  135. */
  136. public function __construct(DoliDB $db)
  137. {
  138. $this->db = $db;
  139. }
  140. /**
  141. * Create object into database
  142. *
  143. * @param User $user User that creates
  144. * @param bool $notrigger false=launch triggers after, true=disable triggers
  145. * @return int <0 if KO, Id of created object if OK
  146. */
  147. public function create(User $user, $notrigger = false)
  148. {
  149. global $conf;
  150. dol_syslog(__METHOD__, LOG_DEBUG);
  151. $error = 0;
  152. // Clean parameters
  153. if (isset($this->ref)) {
  154. $this->ref = trim($this->ref);
  155. }
  156. if (isset($this->label)) {
  157. $this->label = trim($this->label);
  158. }
  159. if (isset($this->share)) {
  160. $this->share = trim($this->share);
  161. }
  162. if (isset($this->entity)) {
  163. $this->entity = (int) $this->entity;
  164. }
  165. if (isset($this->filename)) {
  166. $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
  167. }
  168. if (isset($this->filepath)) {
  169. $this->filepath = trim($this->filepath);
  170. $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
  171. }
  172. if (isset($this->fullpath_orig)) {
  173. $this->fullpath_orig = trim($this->fullpath_orig);
  174. }
  175. if (isset($this->description)) {
  176. $this->description = trim($this->description);
  177. }
  178. if (isset($this->keywords)) {
  179. $this->keywords = trim($this->keywords);
  180. }
  181. if (isset($this->cover)) {
  182. $this->cover = trim($this->cover);
  183. }
  184. if (isset($this->gen_or_uploaded)) {
  185. $this->gen_or_uploaded = trim($this->gen_or_uploaded);
  186. }
  187. if (isset($this->extraparams)) {
  188. $this->extraparams = trim($this->extraparams);
  189. }
  190. if (isset($this->fk_user_c)) {
  191. $this->fk_user_c = (int) $this->fk_user_c;
  192. }
  193. if (isset($this->fk_user_m)) {
  194. $this->fk_user_m = (int) $this->fk_user_m;
  195. }
  196. if (isset($this->acl)) {
  197. $this->acl = trim($this->acl);
  198. }
  199. if (isset($this->src_object_type)) {
  200. $this->src_object_type = trim($this->src_object_type);
  201. }
  202. if (empty($this->date_c)) {
  203. $this->date_c = dol_now();
  204. }
  205. if (empty($this->date_m)) {
  206. $this->date_m = dol_now();
  207. }
  208. // If ref not defined
  209. if (empty($this->ref)) {
  210. include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
  211. $this->ref = dol_hash($this->filepath.'/'.$this->filename, 3);
  212. }
  213. $maxposition = 0;
  214. if (empty($this->position)) {
  215. // Get max used
  216. $sql = "SELECT MAX(position) as maxposition FROM ".MAIN_DB_PREFIX.$this->table_element;
  217. $sql .= " WHERE filepath ='".$this->db->escape($this->filepath)."'";
  218. $resql = $this->db->query($sql);
  219. if ($resql) {
  220. $obj = $this->db->fetch_object($resql);
  221. $maxposition = (int) $obj->maxposition;
  222. } else {
  223. $this->errors[] = 'Error '.$this->db->lasterror();
  224. return --$error;
  225. }
  226. $maxposition = $maxposition + 1;
  227. } else {
  228. $maxposition = $this->position;
  229. }
  230. // Check parameters
  231. if (empty($this->filename) || empty($this->filepath)) {
  232. $this->errors[] = 'Bad property filename or filepath';
  233. return --$error;
  234. }
  235. if (!isset($this->entity)) {
  236. $this->entity = $conf->entity;
  237. }
  238. // Put here code to add control on parameters values
  239. // Insert request
  240. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
  241. $sql .= 'ref,';
  242. $sql .= 'label,';
  243. $sql .= 'share,';
  244. $sql .= 'entity,';
  245. $sql .= 'filename,';
  246. $sql .= 'filepath,';
  247. $sql .= 'fullpath_orig,';
  248. $sql .= 'description,';
  249. $sql .= 'keywords,';
  250. $sql .= 'cover,';
  251. $sql .= 'position,';
  252. $sql .= 'gen_or_uploaded,';
  253. $sql .= 'extraparams,';
  254. $sql .= 'date_c,';
  255. $sql .= 'tms,';
  256. $sql .= 'fk_user_c,';
  257. $sql .= 'fk_user_m,';
  258. $sql .= 'acl,';
  259. $sql .= 'src_object_type,';
  260. $sql .= 'src_object_id';
  261. $sql .= ') VALUES (';
  262. $sql .= " '".$this->db->escape($this->ref)."', ";
  263. $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
  264. $sql .= ' '.(!isset($this->share) ? 'NULL' : "'".$this->db->escape($this->share)."'").',';
  265. $sql .= ' '.((int) $this->entity).',';
  266. $sql .= ' '.(!isset($this->filename) ? 'NULL' : "'".$this->db->escape($this->filename)."'").',';
  267. $sql .= ' '.(!isset($this->filepath) ? 'NULL' : "'".$this->db->escape($this->filepath)."'").',';
  268. $sql .= ' '.(!isset($this->fullpath_orig) ? 'NULL' : "'".$this->db->escape($this->fullpath_orig)."'").',';
  269. $sql .= ' '.(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").',';
  270. $sql .= ' '.(!isset($this->keywords) ? 'NULL' : "'".$this->db->escape($this->keywords)."'").',';
  271. $sql .= ' '.(!isset($this->cover) ? 'NULL' : "'".$this->db->escape($this->cover)."'").',';
  272. $sql .= ' '.((int) $maxposition).',';
  273. $sql .= ' '.(!isset($this->gen_or_uploaded) ? 'NULL' : "'".$this->db->escape($this->gen_or_uploaded)."'").',';
  274. $sql .= ' '.(!isset($this->extraparams) ? 'NULL' : "'".$this->db->escape($this->extraparams)."'").',';
  275. $sql .= " '".$this->db->idate($this->date_c)."',";
  276. $sql .= ' '.(!isset($this->date_m) || dol_strlen($this->date_m) == 0 ? 'NULL' : "'".$this->db->idate($this->date_m)."'").',';
  277. $sql .= ' '.(!isset($this->fk_user_c) ? $user->id : $this->fk_user_c).',';
  278. $sql .= ' '.(!isset($this->fk_user_m) ? 'NULL' : $this->fk_user_m).',';
  279. $sql .= ' '.(!isset($this->acl) ? 'NULL' : "'".$this->db->escape($this->acl)."'").',';
  280. $sql .= ' '.(!isset($this->src_object_type) ? 'NULL' : "'".$this->db->escape($this->src_object_type)."'").',';
  281. $sql .= ' '.(!isset($this->src_object_id) ? 'NULL' : $this->src_object_id);
  282. $sql .= ')';
  283. $this->db->begin();
  284. $resql = $this->db->query($sql);
  285. if (!$resql) {
  286. $error++;
  287. if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  288. $this->errors[] = 'Error DB_ERROR_RECORD_ALREADY_EXISTS : '.$this->db->lasterror();
  289. } else {
  290. $this->errors[] = 'Error '.$this->db->lasterror();
  291. }
  292. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  293. }
  294. if (!$error) {
  295. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  296. $this->position = $maxposition;
  297. // Triggers
  298. if (!$notrigger) {
  299. // Call triggers
  300. $result = $this->call_trigger(strtoupper(get_class($this)).'_CREATE', $user);
  301. if ($result < 0) {
  302. $error++;
  303. }
  304. // End call triggers
  305. }
  306. }
  307. // Commit or rollback
  308. if ($error) {
  309. $this->db->rollback();
  310. return -1 * $error;
  311. } else {
  312. $this->db->commit();
  313. return $this->id;
  314. }
  315. }
  316. /**
  317. * Load object in memory from the database
  318. *
  319. * @param int $id Id object
  320. * @param string $ref Hash of file name (filename+filepath). Not always defined on some version.
  321. * @param string $relativepath Relative path of file from document directory. Example: 'path/path2/file' or 'path/path2/*'
  322. * @param string $hashoffile Hash of file content. Take the first one found if same file is at different places. This hash will also change if file content is changed.
  323. * @param string $hashforshare Hash of file sharing or 'shared'
  324. * @param string $src_object_type src_object_type to search (value of object->table_element)
  325. * @param string $src_object_id src_object_id to search
  326. * @return int <0 if KO, 0 if not found, >0 if OK
  327. */
  328. public function fetch($id, $ref = '', $relativepath = '', $hashoffile = '', $hashforshare = '', $src_object_type = '', $src_object_id = 0)
  329. {
  330. global $conf;
  331. dol_syslog(__METHOD__, LOG_DEBUG);
  332. $sql = 'SELECT';
  333. $sql .= ' t.rowid,';
  334. $sql .= " t.ref,";
  335. $sql .= " t.label,";
  336. $sql .= " t.share,";
  337. $sql .= " t.entity,";
  338. $sql .= " t.filename,";
  339. $sql .= " t.filepath,";
  340. $sql .= " t.fullpath_orig,";
  341. $sql .= " t.description,";
  342. $sql .= " t.keywords,";
  343. $sql .= " t.cover,";
  344. $sql .= " t.position,";
  345. $sql .= " t.gen_or_uploaded,";
  346. $sql .= " t.extraparams,";
  347. $sql .= " t.date_c,";
  348. $sql .= " t.tms as date_m,";
  349. $sql .= " t.fk_user_c,";
  350. $sql .= " t.fk_user_m,";
  351. $sql .= ' t.note_private,';
  352. $sql .= ' t.note_public,';
  353. $sql .= " t.acl,";
  354. $sql .= " t.src_object_type,";
  355. $sql .= " t.src_object_id";
  356. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  357. $sql .= ' WHERE 1 = 1';
  358. /* Fetching this table depends on filepath+filename, it must not depends on entity because filesystem on disk does not know what is Dolibarr entities
  359. if (isModEnabled('multicompany')) {
  360. $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
  361. }*/
  362. if ($relativepath) {
  363. $relativepathwithnoexe = preg_replace('/\.noexe$/', '', $relativepath); // We must never have the .noexe into the database
  364. $sql .= " AND t.filepath = '".$this->db->escape(dirname($relativepath))."'";
  365. $filename = basename($relativepathwithnoexe);
  366. if ($filename != '*') {
  367. $sql .= " AND t.filename = '".$this->db->escape($filename)."'";
  368. }
  369. $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
  370. } elseif (!empty($ref)) { // hash of file path
  371. $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
  372. $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
  373. } elseif (!empty($hashoffile)) { // hash of content
  374. $sql .= " AND t.label = '".$this->db->escape($hashoffile)."'";
  375. $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
  376. } elseif (!empty($hashforshare)) {
  377. if ($hashforshare != 'shared') {
  378. $sql .= " AND t.share = '".$this->db->escape($hashforshare)."'";
  379. } else {
  380. $sql .= " AND t.share IS NOT NULL AND t.share <> ''";
  381. }
  382. //$sql .= " AND t.entity = ".$conf->entity; // hashforshare already unique
  383. } elseif ($src_object_type && $src_object_id) {
  384. // Warning: May return several record, and only first one is returned !
  385. $sql .= " AND t.src_object_type = '".$this->db->escape($src_object_type)."' AND t.src_object_id = ".((int) $src_object_id);
  386. $sql .= " AND t.entity = ".((int) $conf->entity);
  387. } else {
  388. $sql .= ' AND t.rowid = '.((int) $id); // rowid already unique
  389. }
  390. $this->db->plimit(1); // When we search on src or on hash of content (hashforfile) to solve hash conflict when several files has same content, we take first one only
  391. $this->db->order('t.rowid', 'ASC');
  392. $resql = $this->db->query($sql);
  393. if ($resql) {
  394. $numrows = $this->db->num_rows($resql);
  395. if ($numrows) {
  396. $obj = $this->db->fetch_object($resql);
  397. $this->id = $obj->rowid;
  398. $this->ref = $obj->ref;
  399. $this->label = $obj->label;
  400. $this->share = $obj->share;
  401. $this->entity = $obj->entity;
  402. $this->filename = $obj->filename;
  403. $this->filepath = $obj->filepath;
  404. $this->fullpath_orig = $obj->fullpath_orig;
  405. $this->description = $obj->description;
  406. $this->keywords = $obj->keywords;
  407. $this->cover = $obj->cover;
  408. $this->position = $obj->position;
  409. $this->gen_or_uploaded = $obj->gen_or_uploaded;
  410. $this->extraparams = $obj->extraparams;
  411. $this->date_c = $this->db->jdate($obj->date_c);
  412. $this->date_m = $this->db->jdate($obj->date_m);
  413. $this->fk_user_c = $obj->fk_user_c;
  414. $this->fk_user_m = $obj->fk_user_m;
  415. $this->note_private = $obj->note_private;
  416. $this->note_public = $obj->note_public;
  417. $this->acl = $obj->acl;
  418. $this->src_object_type = $obj->src_object_type;
  419. $this->src_object_id = $obj->src_object_id;
  420. }
  421. // Retrieve all extrafields for ecm_files
  422. // fetch optionals attributes and labels
  423. $this->fetch_optionals();
  424. // $this->fetch_lines();
  425. $this->db->free($resql);
  426. if ($numrows) {
  427. return 1;
  428. } else {
  429. return 0;
  430. }
  431. } else {
  432. $this->errors[] = 'Error '.$this->db->lasterror();
  433. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  434. return -1;
  435. }
  436. }
  437. /**
  438. * Load object in memory from the database
  439. *
  440. * @param string $sortorder Sort Order
  441. * @param string $sortfield Sort field
  442. * @param int $limit offset limit
  443. * @param int $offset offset limit
  444. * @param array $filter filter array
  445. * @param string $filtermode filter mode (AND or OR)
  446. *
  447. * @return int <0 if KO, >0 if OK
  448. */
  449. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  450. {
  451. dol_syslog(__METHOD__, LOG_DEBUG);
  452. $sql = 'SELECT';
  453. $sql .= ' t.rowid,';
  454. $sql .= " t.label,";
  455. $sql .= " t.share,";
  456. $sql .= " t.entity,";
  457. $sql .= " t.filename,";
  458. $sql .= " t.filepath,";
  459. $sql .= " t.fullpath_orig,";
  460. $sql .= " t.description,";
  461. $sql .= " t.keywords,";
  462. $sql .= " t.cover,";
  463. $sql .= " t.position,";
  464. $sql .= " t.gen_or_uploaded,";
  465. $sql .= " t.extraparams,";
  466. $sql .= " t.date_c,";
  467. $sql .= " t.tms as date_m,";
  468. $sql .= " t.fk_user_c,";
  469. $sql .= " t.fk_user_m,";
  470. $sql .= " t.acl,";
  471. $sql .= " t.src_object_type,";
  472. $sql .= " t.src_object_id";
  473. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  474. // Manage filter
  475. $sqlwhere = array();
  476. if (count($filter) > 0) {
  477. foreach ($filter as $key => $value) {
  478. if ($key == 't.src_object_id') {
  479. $sqlwhere[] = $key." = ".((int) $value);
  480. } else {
  481. $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
  482. }
  483. }
  484. }
  485. $sql .= ' WHERE 1 = 1';
  486. /* Fetching this table depends on filepath+filename, it must not depends on entity
  487. if (isModEnabled('multicompany')) {
  488. $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
  489. }*/
  490. if (count($sqlwhere) > 0) {
  491. $sql .= ' AND '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
  492. }
  493. if (!empty($sortfield)) {
  494. $sql .= $this->db->order($sortfield, $sortorder);
  495. }
  496. if (!empty($limit)) {
  497. $sql .= $this->db->plimit($limit, $offset);
  498. }
  499. $this->lines = array();
  500. $resql = $this->db->query($sql);
  501. if ($resql) {
  502. $num = $this->db->num_rows($resql);
  503. while ($obj = $this->db->fetch_object($resql)) {
  504. $line = new EcmfilesLine();
  505. $line->id = $obj->rowid;
  506. $line->ref = $obj->rowid;
  507. $line->label = $obj->label;
  508. $line->share = $obj->share;
  509. $line->entity = $obj->entity;
  510. $line->filename = $obj->filename;
  511. $line->filepath = $obj->filepath;
  512. $line->fullpath_orig = $obj->fullpath_orig;
  513. $line->description = $obj->description;
  514. $line->keywords = $obj->keywords;
  515. $line->cover = $obj->cover;
  516. $line->position = $obj->position;
  517. $line->gen_or_uploaded = $obj->gen_or_uploaded;
  518. $line->extraparams = $obj->extraparams;
  519. $line->date_c = $this->db->jdate($obj->date_c);
  520. $line->date_m = $this->db->jdate($obj->date_m);
  521. $line->fk_user_c = $obj->fk_user_c;
  522. $line->fk_user_m = $obj->fk_user_m;
  523. $line->acl = $obj->acl;
  524. $line->src_object_type = $obj->src_object_type;
  525. $line->src_object_id = $obj->src_object_id;
  526. $this->lines[] = $line;
  527. }
  528. $this->db->free($resql);
  529. return $num;
  530. } else {
  531. $this->errors[] = 'Error '.$this->db->lasterror();
  532. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  533. return -1;
  534. }
  535. }
  536. /**
  537. * Update object into database
  538. *
  539. * @param User $user User that modifies
  540. * @param bool $notrigger false=launch triggers after, true=disable triggers
  541. *
  542. * @return int <0 if KO, >0 if OK
  543. */
  544. public function update(User $user, $notrigger = false)
  545. {
  546. global $conf;
  547. $error = 0;
  548. dol_syslog(__METHOD__, LOG_DEBUG);
  549. // Clean parameters
  550. if (isset($this->ref)) {
  551. $this->ref = trim($this->ref);
  552. }
  553. if (isset($this->label)) {
  554. $this->label = trim($this->label);
  555. }
  556. if (isset($this->share)) {
  557. $this->share = trim($this->share);
  558. }
  559. if (isset($this->entity)) {
  560. $this->entity = trim($this->entity);
  561. }
  562. if (isset($this->filename)) {
  563. $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
  564. }
  565. if (isset($this->filepath)) {
  566. $this->filepath = trim($this->filepath);
  567. $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
  568. }
  569. if (isset($this->fullpath_orig)) {
  570. $this->fullpath_orig = trim($this->fullpath_orig);
  571. }
  572. if (isset($this->description)) {
  573. $this->description = trim($this->description);
  574. }
  575. if (isset($this->keywords)) {
  576. $this->keywords = trim($this->keywords);
  577. }
  578. if (isset($this->cover)) {
  579. $this->cover = trim($this->cover);
  580. }
  581. if (isset($this->gen_or_uploaded)) {
  582. $this->gen_or_uploaded = trim($this->gen_or_uploaded);
  583. }
  584. if (isset($this->extraparams)) {
  585. $this->extraparams = trim($this->extraparams);
  586. }
  587. if (isset($this->fk_user_m)) {
  588. $this->fk_user_m = trim($this->fk_user_m);
  589. }
  590. if (isset($this->acl)) {
  591. $this->acl = trim($this->acl);
  592. }
  593. if (isset($this->src_object_type)) {
  594. $this->src_object_type = trim($this->src_object_type);
  595. }
  596. // Check parameters
  597. // Put here code to add a control on parameters values
  598. // Update request
  599. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
  600. $sql .= " ref = '".$this->db->escape(dol_hash($this->filepath."/".$this->filename, 3))."',";
  601. $sql .= ' label = '.(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
  602. $sql .= ' share = '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null").',';
  603. $sql .= ' entity = '.(isset($this->entity) ? $this->entity : $conf->entity).',';
  604. $sql .= ' filename = '.(isset($this->filename) ? "'".$this->db->escape($this->filename)."'" : "null").',';
  605. $sql .= ' filepath = '.(isset($this->filepath) ? "'".$this->db->escape($this->filepath)."'" : "null").',';
  606. $sql .= ' fullpath_orig = '.(isset($this->fullpath_orig) ? "'".$this->db->escape($this->fullpath_orig)."'" : "null").',';
  607. $sql .= ' description = '.(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").',';
  608. $sql .= ' keywords = '.(isset($this->keywords) ? "'".$this->db->escape($this->keywords)."'" : "null").',';
  609. $sql .= ' cover = '.(isset($this->cover) ? "'".$this->db->escape($this->cover)."'" : "null").',';
  610. $sql .= ' position = '.(isset($this->position) ? $this->db->escape($this->position) : "0").',';
  611. $sql .= ' gen_or_uploaded = '.(isset($this->gen_or_uploaded) ? "'".$this->db->escape($this->gen_or_uploaded)."'" : "null").',';
  612. $sql .= ' extraparams = '.(isset($this->extraparams) ? "'".$this->db->escape($this->extraparams)."'" : "null").',';
  613. $sql .= ' date_c = '.(!isset($this->date_c) || dol_strlen($this->date_c) != 0 ? "'".$this->db->idate($this->date_c)."'" : 'null').',';
  614. //$sql .= ' tms = '.(! isset($this->date_m) || dol_strlen($this->date_m) != 0 ? "'".$this->db->idate($this->date_m)."'" : 'null').','; // Field automatically updated
  615. $sql .= ' fk_user_m = '.($this->fk_user_m > 0 ? $this->fk_user_m : $user->id).',';
  616. $sql .= ' acl = '.(isset($this->acl) ? "'".$this->db->escape($this->acl)."'" : "null").',';
  617. $sql .= ' src_object_id = '.($this->src_object_id > 0 ? $this->src_object_id : "null").',';
  618. $sql .= ' src_object_type = '.(isset($this->src_object_type) ? "'".$this->db->escape($this->src_object_type)."'" : "null");
  619. $sql .= ' WHERE rowid='.((int) $this->id);
  620. $this->db->begin();
  621. $resql = $this->db->query($sql);
  622. if (!$resql) {
  623. $error++;
  624. $this->errors[] = 'Error '.$this->db->lasterror();
  625. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  626. }
  627. // Triggers
  628. if (!$error && !$notrigger) {
  629. // Call triggers
  630. $result = $this->call_trigger(strtoupper(get_class($this)).'_MODIFY', $user);
  631. if ($result < 0) {
  632. $error++;
  633. } //Do also here what you must do to rollback action if trigger fail
  634. // End call triggers
  635. }
  636. // Commit or rollback
  637. if ($error) {
  638. $this->db->rollback();
  639. return -1 * $error;
  640. } else {
  641. $this->db->commit();
  642. return 1;
  643. }
  644. }
  645. /**
  646. * Delete object in database
  647. *
  648. * @param User $user User that deletes
  649. * @param bool $notrigger false=launch triggers after, true=disable triggers
  650. *
  651. * @return int <0 if KO, >0 if OK
  652. */
  653. public function delete(User $user, $notrigger = false)
  654. {
  655. dol_syslog(__METHOD__, LOG_DEBUG);
  656. $error = 0;
  657. $this->db->begin();
  658. // Triggers
  659. if (!$notrigger) {
  660. // Call triggers
  661. $result = $this->call_trigger(strtoupper(get_class($this)).'_DELETE', $user);
  662. if ($result < 0) {
  663. $error++;
  664. } //Do also here what you must do to rollback action if trigger fail
  665. // End call triggers
  666. }
  667. // If you need to delete child tables to, you can insert them here
  668. if (!$error) {
  669. $result = $this->deleteExtraFields();
  670. if (!$result) {
  671. dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
  672. $error++;
  673. }
  674. }
  675. if (!$error) {
  676. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
  677. $sql .= ' WHERE rowid='.((int) $this->id);
  678. $resql = $this->db->query($sql);
  679. if (!$resql) {
  680. $error++;
  681. $this->errors[] = 'Error '.$this->db->lasterror();
  682. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  683. }
  684. }
  685. // Commit or rollback
  686. if ($error) {
  687. $this->db->rollback();
  688. return -1 * $error;
  689. } else {
  690. $this->db->commit();
  691. return 1;
  692. }
  693. }
  694. /**
  695. * Load an object from its id and create a new one in database
  696. *
  697. * @param User $user User making the clone
  698. * @param int $fromid Id of object to clone
  699. * @return int New id of clone
  700. */
  701. public function createFromClone(User $user, $fromid)
  702. {
  703. dol_syslog(__METHOD__, LOG_DEBUG);
  704. $error = 0;
  705. $object = new Ecmfiles($this->db);
  706. $this->db->begin();
  707. // Load source object
  708. $object->fetch($fromid);
  709. // Reset object
  710. $object->id = 0;
  711. // Clear fields
  712. // ...
  713. // Create clone
  714. $object->context['createfromclone'] = 'createfromclone';
  715. $result = $object->create($user);
  716. // Other options
  717. if ($result < 0) {
  718. $error++;
  719. $this->errors = $object->errors;
  720. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  721. }
  722. unset($object->context['createfromclone']);
  723. // End
  724. if (!$error) {
  725. $this->db->commit();
  726. return $object->id;
  727. } else {
  728. $this->db->rollback();
  729. return -1;
  730. }
  731. }
  732. /**
  733. * Return a link to the object card (with optionaly the picto)
  734. *
  735. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  736. * @param string $option On what the link point to
  737. * @param int $notooltip 1=Disable tooltip
  738. * @param int $maxlen Max length of visible user name
  739. * @param string $morecss Add more css on link
  740. * @return string String with URL
  741. */
  742. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
  743. {
  744. global $db, $conf, $langs;
  745. global $dolibarr_main_authentication, $dolibarr_main_demo;
  746. global $menumanager, $hookmanager;
  747. if (!empty($conf->dol_no_mouse_hover)) {
  748. $notooltip = 1; // Force disable tooltips
  749. }
  750. $result = '';
  751. $label = '<u>'.$langs->trans("MyModule").'</u>';
  752. $label .= '<br>';
  753. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  754. $url = DOL_URL_ROOT.'/ecm/'.$this->table_name.'_card.php?id='.$this->id;
  755. $linkclose = '';
  756. if (empty($notooltip)) {
  757. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  758. $label = $langs->trans("ShowProject");
  759. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  760. }
  761. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  762. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  763. } else {
  764. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  765. }
  766. $linkstart = '<a href="'.$url.'"';
  767. $linkstart .= $linkclose.'>';
  768. $linkend = '</a>';
  769. if ($withpicto) {
  770. $result .= ($linkstart.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
  771. if ($withpicto != 2) {
  772. $result .= ' ';
  773. }
  774. }
  775. $result .= $linkstart.$this->ref.$linkend;
  776. global $action;
  777. $hookmanager->initHooks(array($this->element . 'dao'));
  778. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  779. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  780. if ($reshook > 0) {
  781. $result = $hookmanager->resPrint;
  782. } else {
  783. $result .= $hookmanager->resPrint;
  784. }
  785. return $result;
  786. }
  787. /**
  788. * Retourne le libelle du status d'un user (actif, inactif)
  789. *
  790. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  791. * @return string Label of status
  792. */
  793. public function getLibStatut($mode = 0)
  794. {
  795. return $this->LibStatut($this->status, $mode);
  796. }
  797. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  798. /**
  799. * Return the status
  800. *
  801. * @param int $status Id status
  802. * @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
  803. * @return string Label of status
  804. */
  805. public static function LibStatut($status, $mode = 0)
  806. {
  807. // phpcs:enable
  808. global $langs;
  809. return '';
  810. }
  811. /**
  812. * Initialise object with example values
  813. * Id must be 0 if object instance is a specimen
  814. *
  815. * @return void
  816. */
  817. public function initAsSpecimen()
  818. {
  819. global $conf, $user;
  820. $this->id = 0;
  821. $this->specimen = 1;
  822. $this->label = '0a1b2c3e4f59999999';
  823. $this->entity = 1;
  824. $this->filename = 'myspecimenfilefile.pdf';
  825. $this->filepath = '/aaa/bbb';
  826. $this->fullpath_orig = 'c:/file on my disk.pdf';
  827. $this->description = 'This is a long description of file';
  828. $this->keywords = 'key1,key2';
  829. $this->cover = '1';
  830. $this->position = 5;
  831. $this->gen_or_uploaded = 'uploaded';
  832. $this->extraparams = '';
  833. $this->date_c = (dol_now() - 3600 * 24 * 10);
  834. $this->date_m = '';
  835. $this->fk_user_c = $user->id;
  836. $this->fk_user_m = '';
  837. $this->acl = '';
  838. $this->src_object_type = 'product';
  839. $this->src_object_id = 1;
  840. }
  841. }
  842. /**
  843. * Class of an index line of a document
  844. */
  845. class EcmfilesLine
  846. {
  847. /**
  848. * @var string ECM files line label
  849. */
  850. public $label;
  851. /**
  852. * @var int Entity
  853. */
  854. public $entity;
  855. public $filename;
  856. public $filepath;
  857. public $fullpath_orig;
  858. /**
  859. * @var string description
  860. */
  861. public $description;
  862. public $keywords;
  863. public $cover;
  864. public $position;
  865. public $gen_or_uploaded; // can be 'generated', 'uploaded', 'unknown'
  866. public $extraparams;
  867. public $date_c = '';
  868. public $date_m = '';
  869. /**
  870. * @var int ID
  871. */
  872. public $fk_user_c;
  873. /**
  874. * @var int ID
  875. */
  876. public $fk_user_m;
  877. public $acl;
  878. public $src_object_type;
  879. public $src_object_id;
  880. }