ecmfiles.class.php 26 KB

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