ecmfiles.class.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. public $fields = array(
  132. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>'1', 'position'=>1, 'notnull'=>1, 'visible'=>0, 'noteditable'=>'1', 'index'=>1, 'css'=>'left', 'comment'=>"Id"),
  133. 'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>'1', 'position'=>20, 'notnull'=>1, 'visible'=>-1, 'index'=>1, 'searchall'=>1, 'showoncombobox'=>'1', 'validate'=>'1', 'comment'=>"contains hash from filename+filepath"),
  134. 'label' => array('type'=>'varchar(128)', 'label'=>'Label', 'enabled'=>'1', 'position'=>30, 'notnull'=>0, 'visible'=>-1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1', 'comment'=>"contains hash of file content"),
  135. 'share' => array('type'=>'varchar(128)', 'label'=>'Share', 'enabled'=>'1', 'position'=>40, 'notnull'=>0, 'visible'=>-1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1', 'comment' => "contains hash for file sharing"),
  136. 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => 1, 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 50, 'index' => 1),
  137. 'filepath' => array('type'=>'varchar(255)', 'label'=>'FilePath', 'enabled'=>'1', 'position'=>60, 'notnull'=>0, 'visible'=>0, 'searchall'=>0, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=> "relative to dolibarr document dir. Example module/def"),
  138. 'filename' => array('type'=>'varchar(255)', 'label'=>'FileName', 'enabled'=>'1', 'position'=>70, 'notnull'=>0, 'visible'=>1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=>"file name only without any directory"),
  139. 'src_object_type' => array('type'=>'varchar(64)', 'label'=>'SourceType', 'enabled'=>'1', 'position'=>80, 'notnull'=>0, 'visible'=>0, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=> "Source object type ('proposal', 'invoice', ...)"),
  140. 'src_object_id' => array('type' => 'integer', 'label' => 'SourceID', 'default' => 1, 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'position' => 90, 'index' => 1, 'comment' => "Source object id"),
  141. 'fullpath_orig' => array('type'=>'varchar(750)', 'label'=>'FullPathOrig', 'enabled'=>'1', 'position'=>100, 'notnull'=>0, 'visible'=>0, 'searchall'=>0, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=>"full path of original filename, when file is uploaded from a local computer"),
  142. 'description' => array('type' => 'text', 'label' => 'Description', 'enabled' => 1, 'visible' => 0, 'position' => 110),
  143. 'keywords' => array('type'=>'varchar(750)', 'label'=>'Keywords', 'enabled'=>'1', 'position'=>120, 'notnull'=>0, 'visible'=>1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=>"list of keywords, separated with comma. Must be limited to most important keywords."),
  144. 'cover' => array('type' => 'text', 'label' => 'Cover', 'enabled' => 1, 'visible' => 0, 'position' => 130, 'comment'=>"is this file a file to use for a cover"),
  145. 'position' => array('type' => 'integer', 'label' => 'Position', 'default' => 1, 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 140, 'index' => 1, 'comment' => "position of file among others"),
  146. 'gen_or_uploaded' => array('type'=>'varchar(12)', 'label'=>'GenOrUpload', 'enabled'=>'1', 'position'=>150, 'notnull'=>0, 'visible'=>-1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1','comment'=>"'generated' or 'uploaded'"),
  147. 'extraparams' => array('type'=>'varchar(255)', 'label'=>'ExtraParams', 'enabled'=>'1', 'position'=>160, 'notnull'=>0, 'visible'=>1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1', 'comment' => "for stocking other parameters with json format"),
  148. 'date_c' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 170),
  149. 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 175),
  150. 'fk_user_c' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>'1', 'position'=>510, 'notnull'=>1, 'visible'=>-2, 'foreignkey'=>'user.rowid',),
  151. 'fk_user_m' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>'1', 'position'=>511, 'notnull'=>-1, 'visible'=>-2,),
  152. 'note_public' => array('type' => 'text', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 155),
  153. 'note_private' => array('type' => 'text', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 160),
  154. 'acl' => array('type' => 'text', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 160, 'comment' => "for future permission 'per file'"),
  155. );
  156. /**
  157. * Constructor
  158. *
  159. * @param DoliDb $db Database handler
  160. */
  161. public function __construct(DoliDB $db)
  162. {
  163. $this->db = $db;
  164. }
  165. /**
  166. * Create object into database
  167. *
  168. * @param User $user User that creates
  169. * @param bool $notrigger false=launch triggers after, true=disable triggers
  170. * @return int <0 if KO, Id of created object if OK
  171. */
  172. public function create(User $user, $notrigger = false)
  173. {
  174. global $conf;
  175. dol_syslog(__METHOD__, LOG_DEBUG);
  176. $error = 0;
  177. // Clean parameters
  178. if (isset($this->ref)) {
  179. $this->ref = trim($this->ref);
  180. }
  181. if (isset($this->label)) {
  182. $this->label = trim($this->label);
  183. }
  184. if (isset($this->share)) {
  185. $this->share = trim($this->share);
  186. }
  187. if (isset($this->entity)) {
  188. $this->entity = (int) $this->entity;
  189. }
  190. if (isset($this->filename)) {
  191. $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
  192. }
  193. if (isset($this->filepath)) {
  194. $this->filepath = trim($this->filepath);
  195. $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
  196. }
  197. if (isset($this->fullpath_orig)) {
  198. $this->fullpath_orig = trim($this->fullpath_orig);
  199. }
  200. if (isset($this->description)) {
  201. $this->description = trim($this->description);
  202. }
  203. if (isset($this->keywords)) {
  204. $this->keywords = trim($this->keywords);
  205. }
  206. if (isset($this->cover)) {
  207. $this->cover = trim($this->cover);
  208. }
  209. if (isset($this->gen_or_uploaded)) {
  210. $this->gen_or_uploaded = trim($this->gen_or_uploaded);
  211. }
  212. if (isset($this->extraparams)) {
  213. $this->extraparams = trim($this->extraparams);
  214. }
  215. if (isset($this->fk_user_c)) {
  216. $this->fk_user_c = (int) $this->fk_user_c;
  217. }
  218. if (isset($this->fk_user_m)) {
  219. $this->fk_user_m = (int) $this->fk_user_m;
  220. }
  221. if (isset($this->acl)) {
  222. $this->acl = trim($this->acl);
  223. }
  224. if (isset($this->src_object_type)) {
  225. $this->src_object_type = trim($this->src_object_type);
  226. }
  227. if (empty($this->date_c)) {
  228. $this->date_c = dol_now();
  229. }
  230. if (empty($this->date_m)) {
  231. $this->date_m = dol_now();
  232. }
  233. // If ref not defined
  234. if (empty($this->ref)) {
  235. include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
  236. $this->ref = dol_hash($this->filepath.'/'.$this->filename, 3);
  237. }
  238. $maxposition = 0;
  239. if (empty($this->position)) {
  240. // Get max used
  241. $sql = "SELECT MAX(position) as maxposition FROM ".MAIN_DB_PREFIX.$this->table_element;
  242. $sql .= " WHERE filepath ='".$this->db->escape($this->filepath)."'";
  243. $resql = $this->db->query($sql);
  244. if ($resql) {
  245. $obj = $this->db->fetch_object($resql);
  246. $maxposition = (int) $obj->maxposition;
  247. } else {
  248. $this->errors[] = 'Error '.$this->db->lasterror();
  249. return --$error;
  250. }
  251. $maxposition = $maxposition + 1;
  252. } else {
  253. $maxposition = $this->position;
  254. }
  255. // Check parameters
  256. if (empty($this->filename) || empty($this->filepath)) {
  257. $this->errors[] = 'Bad property filename or filepath';
  258. return --$error;
  259. }
  260. if (!isset($this->entity)) {
  261. $this->entity = $conf->entity;
  262. }
  263. // Put here code to add control on parameters values
  264. // Insert request
  265. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
  266. $sql .= 'ref,';
  267. $sql .= 'label,';
  268. $sql .= 'share,';
  269. $sql .= 'entity,';
  270. $sql .= 'filename,';
  271. $sql .= 'filepath,';
  272. $sql .= 'fullpath_orig,';
  273. $sql .= 'description,';
  274. $sql .= 'keywords,';
  275. $sql .= 'cover,';
  276. $sql .= 'position,';
  277. $sql .= 'gen_or_uploaded,';
  278. $sql .= 'extraparams,';
  279. $sql .= 'date_c,';
  280. $sql .= 'tms,';
  281. $sql .= 'fk_user_c,';
  282. $sql .= 'fk_user_m,';
  283. $sql .= 'acl,';
  284. $sql .= 'src_object_type,';
  285. $sql .= 'src_object_id';
  286. $sql .= ') VALUES (';
  287. $sql .= " '".$this->db->escape($this->ref)."', ";
  288. $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
  289. $sql .= ' '.(!isset($this->share) ? 'NULL' : "'".$this->db->escape($this->share)."'").',';
  290. $sql .= ' '.((int) $this->entity).',';
  291. $sql .= ' '.(!isset($this->filename) ? 'NULL' : "'".$this->db->escape($this->filename)."'").',';
  292. $sql .= ' '.(!isset($this->filepath) ? 'NULL' : "'".$this->db->escape($this->filepath)."'").',';
  293. $sql .= ' '.(!isset($this->fullpath_orig) ? 'NULL' : "'".$this->db->escape($this->fullpath_orig)."'").',';
  294. $sql .= ' '.(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").',';
  295. $sql .= ' '.(!isset($this->keywords) ? 'NULL' : "'".$this->db->escape($this->keywords)."'").',';
  296. $sql .= ' '.(!isset($this->cover) ? 'NULL' : "'".$this->db->escape($this->cover)."'").',';
  297. $sql .= ' '.((int) $maxposition).',';
  298. $sql .= ' '.(!isset($this->gen_or_uploaded) ? 'NULL' : "'".$this->db->escape($this->gen_or_uploaded)."'").',';
  299. $sql .= ' '.(!isset($this->extraparams) ? 'NULL' : "'".$this->db->escape($this->extraparams)."'").',';
  300. $sql .= " '".$this->db->idate($this->date_c)."',";
  301. $sql .= ' '.(!isset($this->date_m) || dol_strlen($this->date_m) == 0 ? 'NULL' : "'".$this->db->idate($this->date_m)."'").',';
  302. $sql .= ' '.(!isset($this->fk_user_c) ? $user->id : $this->fk_user_c).',';
  303. $sql .= ' '.(!isset($this->fk_user_m) ? 'NULL' : $this->fk_user_m).',';
  304. $sql .= ' '.(!isset($this->acl) ? 'NULL' : "'".$this->db->escape($this->acl)."'").',';
  305. $sql .= ' '.(!isset($this->src_object_type) ? 'NULL' : "'".$this->db->escape($this->src_object_type)."'").',';
  306. $sql .= ' '.(!isset($this->src_object_id) ? 'NULL' : $this->src_object_id);
  307. $sql .= ')';
  308. $this->db->begin();
  309. $resql = $this->db->query($sql);
  310. if (!$resql) {
  311. $error++;
  312. if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  313. $this->errors[] = 'Error DB_ERROR_RECORD_ALREADY_EXISTS : '.$this->db->lasterror();
  314. } else {
  315. $this->errors[] = 'Error '.$this->db->lasterror();
  316. }
  317. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  318. }
  319. if (!$error) {
  320. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  321. $this->position = $maxposition;
  322. // Triggers
  323. if (!$notrigger) {
  324. // Call triggers
  325. $result = $this->call_trigger(strtoupper(get_class($this)).'_CREATE', $user);
  326. if ($result < 0) {
  327. $error++;
  328. }
  329. // End call triggers
  330. }
  331. }
  332. // Commit or rollback
  333. if ($error) {
  334. $this->db->rollback();
  335. return -1 * $error;
  336. } else {
  337. $this->db->commit();
  338. return $this->id;
  339. }
  340. }
  341. /**
  342. * Load object in memory from the database
  343. *
  344. * @param int $id Id object
  345. * @param string $ref Hash of file name (filename+filepath). Not always defined on some version.
  346. * @param string $relativepath Relative path of file from document directory. Example: 'path/path2/file' or 'path/path2/*'
  347. * @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.
  348. * @param string $hashforshare Hash of file sharing or 'shared'
  349. * @param string $src_object_type src_object_type to search (value of object->table_element)
  350. * @param string $src_object_id src_object_id to search
  351. * @return int <0 if KO, 0 if not found, >0 if OK
  352. */
  353. public function fetch($id, $ref = '', $relativepath = '', $hashoffile = '', $hashforshare = '', $src_object_type = '', $src_object_id = 0)
  354. {
  355. global $conf;
  356. dol_syslog(__METHOD__, LOG_DEBUG);
  357. $sql = 'SELECT';
  358. $sql .= ' t.rowid,';
  359. $sql .= " t.ref,";
  360. $sql .= " t.label,";
  361. $sql .= " t.share,";
  362. $sql .= " t.entity,";
  363. $sql .= " t.filename,";
  364. $sql .= " t.filepath,";
  365. $sql .= " t.fullpath_orig,";
  366. $sql .= " t.description,";
  367. $sql .= " t.keywords,";
  368. $sql .= " t.cover,";
  369. $sql .= " t.position,";
  370. $sql .= " t.gen_or_uploaded,";
  371. $sql .= " t.extraparams,";
  372. $sql .= " t.date_c,";
  373. $sql .= " t.tms as date_m,";
  374. $sql .= " t.fk_user_c,";
  375. $sql .= " t.fk_user_m,";
  376. $sql .= ' t.note_private,';
  377. $sql .= ' t.note_public,';
  378. $sql .= " t.acl,";
  379. $sql .= " t.src_object_type,";
  380. $sql .= " t.src_object_id";
  381. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  382. $sql .= ' WHERE 1 = 1';
  383. /* Fetching this table depends on filepath+filename, it must not depends on entity because filesystem on disk does not know what is Dolibarr entities
  384. if (isModEnabled('multicompany')) {
  385. $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
  386. }*/
  387. if ($relativepath) {
  388. $relativepathwithnoexe = preg_replace('/\.noexe$/', '', $relativepath); // We must never have the .noexe into the database
  389. $sql .= " AND t.filepath = '".$this->db->escape(dirname($relativepath))."'";
  390. $filename = basename($relativepathwithnoexe);
  391. if ($filename != '*') {
  392. $sql .= " AND t.filename = '".$this->db->escape($filename)."'";
  393. }
  394. $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
  395. } elseif (!empty($ref)) { // hash of file path
  396. $sql .= " AND t.ref = '".$this->db->escape($ref)."'";
  397. $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
  398. } elseif (!empty($hashoffile)) { // hash of content
  399. $sql .= " AND t.label = '".$this->db->escape($hashoffile)."'";
  400. $sql .= " AND t.entity = ".$conf->entity; // unique key include the entity so each company has its own index
  401. } elseif (!empty($hashforshare)) {
  402. if ($hashforshare != 'shared') {
  403. $sql .= " AND t.share = '".$this->db->escape($hashforshare)."'";
  404. } else {
  405. $sql .= " AND t.share IS NOT NULL AND t.share <> ''";
  406. }
  407. //$sql .= " AND t.entity = ".$conf->entity; // hashforshare already unique
  408. } elseif ($src_object_type && $src_object_id) {
  409. // Warning: May return several record, and only first one is returned !
  410. $sql .= " AND t.src_object_type = '".$this->db->escape($src_object_type)."' AND t.src_object_id = ".((int) $src_object_id);
  411. $sql .= " AND t.entity = ".((int) $conf->entity);
  412. } else {
  413. $sql .= ' AND t.rowid = '.((int) $id); // rowid already unique
  414. }
  415. $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
  416. $this->db->order('t.rowid', 'ASC');
  417. $resql = $this->db->query($sql);
  418. if ($resql) {
  419. $numrows = $this->db->num_rows($resql);
  420. if ($numrows) {
  421. $obj = $this->db->fetch_object($resql);
  422. $this->id = $obj->rowid;
  423. $this->ref = $obj->ref;
  424. $this->label = $obj->label;
  425. $this->share = $obj->share;
  426. $this->entity = $obj->entity;
  427. $this->filename = $obj->filename;
  428. $this->filepath = $obj->filepath;
  429. $this->fullpath_orig = $obj->fullpath_orig;
  430. $this->description = $obj->description;
  431. $this->keywords = $obj->keywords;
  432. $this->cover = $obj->cover;
  433. $this->position = $obj->position;
  434. $this->gen_or_uploaded = $obj->gen_or_uploaded;
  435. $this->extraparams = $obj->extraparams;
  436. $this->date_c = $this->db->jdate($obj->date_c);
  437. $this->date_m = $this->db->jdate($obj->date_m);
  438. $this->fk_user_c = $obj->fk_user_c;
  439. $this->fk_user_m = $obj->fk_user_m;
  440. $this->note_private = $obj->note_private;
  441. $this->note_public = $obj->note_public;
  442. $this->acl = $obj->acl;
  443. $this->src_object_type = $obj->src_object_type;
  444. $this->src_object_id = $obj->src_object_id;
  445. }
  446. // Retrieve all extrafields for ecm_files
  447. // fetch optionals attributes and labels
  448. $this->fetch_optionals();
  449. // $this->fetch_lines();
  450. $this->db->free($resql);
  451. if ($numrows) {
  452. return 1;
  453. } else {
  454. return 0;
  455. }
  456. } else {
  457. $this->errors[] = 'Error '.$this->db->lasterror();
  458. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  459. return -1;
  460. }
  461. }
  462. /**
  463. * Load object in memory from the database
  464. *
  465. * @param string $sortorder Sort Order
  466. * @param string $sortfield Sort field
  467. * @param int $limit offset limit
  468. * @param int $offset offset limit
  469. * @param array $filter filter array
  470. * @param string $filtermode filter mode (AND or OR)
  471. *
  472. * @return int <0 if KO, >0 if OK
  473. */
  474. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  475. {
  476. dol_syslog(__METHOD__, LOG_DEBUG);
  477. $sql = 'SELECT';
  478. $sql .= ' t.rowid,';
  479. $sql .= " t.label,";
  480. $sql .= " t.share,";
  481. $sql .= " t.entity,";
  482. $sql .= " t.filename,";
  483. $sql .= " t.filepath,";
  484. $sql .= " t.fullpath_orig,";
  485. $sql .= " t.description,";
  486. $sql .= " t.keywords,";
  487. $sql .= " t.cover,";
  488. $sql .= " t.position,";
  489. $sql .= " t.gen_or_uploaded,";
  490. $sql .= " t.extraparams,";
  491. $sql .= " t.date_c,";
  492. $sql .= " t.tms as date_m,";
  493. $sql .= " t.fk_user_c,";
  494. $sql .= " t.fk_user_m,";
  495. $sql .= " t.acl,";
  496. $sql .= " t.src_object_type,";
  497. $sql .= " t.src_object_id";
  498. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  499. // Manage filter
  500. $sqlwhere = array();
  501. if (count($filter) > 0) {
  502. foreach ($filter as $key => $value) {
  503. if ($key == 't.src_object_id') {
  504. $sqlwhere[] = $key." = ".((int) $value);
  505. } else {
  506. $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
  507. }
  508. }
  509. }
  510. $sql .= ' WHERE 1 = 1';
  511. /* Fetching this table depends on filepath+filename, it must not depends on entity
  512. if (isModEnabled('multicompany')) {
  513. $sql .= " AND entity IN (" . getEntity('ecmfiles') . ")";
  514. }*/
  515. if (count($sqlwhere) > 0) {
  516. $sql .= ' AND '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
  517. }
  518. if (!empty($sortfield)) {
  519. $sql .= $this->db->order($sortfield, $sortorder);
  520. }
  521. if (!empty($limit)) {
  522. $sql .= $this->db->plimit($limit, $offset);
  523. }
  524. $this->lines = array();
  525. $resql = $this->db->query($sql);
  526. if ($resql) {
  527. $num = $this->db->num_rows($resql);
  528. while ($obj = $this->db->fetch_object($resql)) {
  529. $line = new EcmfilesLine();
  530. $line->id = $obj->rowid;
  531. $line->ref = $obj->rowid;
  532. $line->label = $obj->label;
  533. $line->share = $obj->share;
  534. $line->entity = $obj->entity;
  535. $line->filename = $obj->filename;
  536. $line->filepath = $obj->filepath;
  537. $line->fullpath_orig = $obj->fullpath_orig;
  538. $line->description = $obj->description;
  539. $line->keywords = $obj->keywords;
  540. $line->cover = $obj->cover;
  541. $line->position = $obj->position;
  542. $line->gen_or_uploaded = $obj->gen_or_uploaded;
  543. $line->extraparams = $obj->extraparams;
  544. $line->date_c = $this->db->jdate($obj->date_c);
  545. $line->date_m = $this->db->jdate($obj->date_m);
  546. $line->fk_user_c = $obj->fk_user_c;
  547. $line->fk_user_m = $obj->fk_user_m;
  548. $line->acl = $obj->acl;
  549. $line->src_object_type = $obj->src_object_type;
  550. $line->src_object_id = $obj->src_object_id;
  551. $this->lines[] = $line;
  552. }
  553. $this->db->free($resql);
  554. return $num;
  555. } else {
  556. $this->errors[] = 'Error '.$this->db->lasterror();
  557. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  558. return -1;
  559. }
  560. }
  561. /**
  562. * Update object into database
  563. *
  564. * @param User $user User that modifies
  565. * @param bool $notrigger false=launch triggers after, true=disable triggers
  566. *
  567. * @return int <0 if KO, >0 if OK
  568. */
  569. public function update(User $user, $notrigger = false)
  570. {
  571. global $conf;
  572. $error = 0;
  573. dol_syslog(__METHOD__, LOG_DEBUG);
  574. // Clean parameters
  575. if (isset($this->ref)) {
  576. $this->ref = trim($this->ref);
  577. }
  578. if (isset($this->label)) {
  579. $this->label = trim($this->label);
  580. }
  581. if (isset($this->share)) {
  582. $this->share = trim($this->share);
  583. }
  584. if (isset($this->entity)) {
  585. $this->entity = trim($this->entity);
  586. }
  587. if (isset($this->filename)) {
  588. $this->filename = preg_replace('/\.noexe$/', '', trim($this->filename));
  589. }
  590. if (isset($this->filepath)) {
  591. $this->filepath = trim($this->filepath);
  592. $this->filepath = preg_replace('/[\\/]+$/', '', $this->filepath); // Remove last /
  593. }
  594. if (isset($this->fullpath_orig)) {
  595. $this->fullpath_orig = trim($this->fullpath_orig);
  596. }
  597. if (isset($this->description)) {
  598. $this->description = trim($this->description);
  599. }
  600. if (isset($this->keywords)) {
  601. $this->keywords = trim($this->keywords);
  602. }
  603. if (isset($this->cover)) {
  604. $this->cover = trim($this->cover);
  605. }
  606. if (isset($this->gen_or_uploaded)) {
  607. $this->gen_or_uploaded = trim($this->gen_or_uploaded);
  608. }
  609. if (isset($this->extraparams)) {
  610. $this->extraparams = trim($this->extraparams);
  611. }
  612. if (isset($this->fk_user_m)) {
  613. $this->fk_user_m = trim($this->fk_user_m);
  614. }
  615. if (isset($this->acl)) {
  616. $this->acl = trim($this->acl);
  617. }
  618. if (isset($this->src_object_type)) {
  619. $this->src_object_type = trim($this->src_object_type);
  620. }
  621. // Check parameters
  622. // Put here code to add a control on parameters values
  623. // Update request
  624. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
  625. $sql .= " ref = '".$this->db->escape(dol_hash($this->filepath."/".$this->filename, 3))."',";
  626. $sql .= ' label = '.(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
  627. $sql .= ' share = '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null").',';
  628. $sql .= ' entity = '.(isset($this->entity) ? $this->entity : $conf->entity).',';
  629. $sql .= ' filename = '.(isset($this->filename) ? "'".$this->db->escape($this->filename)."'" : "null").',';
  630. $sql .= ' filepath = '.(isset($this->filepath) ? "'".$this->db->escape($this->filepath)."'" : "null").',';
  631. $sql .= ' fullpath_orig = '.(isset($this->fullpath_orig) ? "'".$this->db->escape($this->fullpath_orig)."'" : "null").',';
  632. $sql .= ' description = '.(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").',';
  633. $sql .= ' keywords = '.(isset($this->keywords) ? "'".$this->db->escape($this->keywords)."'" : "null").',';
  634. $sql .= ' cover = '.(isset($this->cover) ? "'".$this->db->escape($this->cover)."'" : "null").',';
  635. $sql .= ' position = '.(isset($this->position) ? $this->db->escape($this->position) : "0").',';
  636. $sql .= ' gen_or_uploaded = '.(isset($this->gen_or_uploaded) ? "'".$this->db->escape($this->gen_or_uploaded)."'" : "null").',';
  637. $sql .= ' extraparams = '.(isset($this->extraparams) ? "'".$this->db->escape($this->extraparams)."'" : "null").',';
  638. $sql .= ' date_c = '.(!isset($this->date_c) || dol_strlen($this->date_c) != 0 ? "'".$this->db->idate($this->date_c)."'" : 'null').',';
  639. //$sql .= ' tms = '.(! isset($this->date_m) || dol_strlen($this->date_m) != 0 ? "'".$this->db->idate($this->date_m)."'" : 'null').','; // Field automatically updated
  640. $sql .= ' fk_user_m = '.($this->fk_user_m > 0 ? $this->fk_user_m : $user->id).',';
  641. $sql .= ' acl = '.(isset($this->acl) ? "'".$this->db->escape($this->acl)."'" : "null").',';
  642. $sql .= ' src_object_id = '.($this->src_object_id > 0 ? $this->src_object_id : "null").',';
  643. $sql .= ' src_object_type = '.(isset($this->src_object_type) ? "'".$this->db->escape($this->src_object_type)."'" : "null");
  644. $sql .= ' WHERE rowid='.((int) $this->id);
  645. $this->db->begin();
  646. $resql = $this->db->query($sql);
  647. if (!$resql) {
  648. $error++;
  649. $this->errors[] = 'Error '.$this->db->lasterror();
  650. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  651. }
  652. // Triggers
  653. if (!$error && !$notrigger) {
  654. // Call triggers
  655. $result = $this->call_trigger(strtoupper(get_class($this)).'_MODIFY', $user);
  656. if ($result < 0) {
  657. $error++;
  658. } //Do also here what you must do to rollback action if trigger fail
  659. // End call triggers
  660. }
  661. // Commit or rollback
  662. if ($error) {
  663. $this->db->rollback();
  664. return -1 * $error;
  665. } else {
  666. $this->db->commit();
  667. return 1;
  668. }
  669. }
  670. /**
  671. * Delete object in database
  672. *
  673. * @param User $user User that deletes
  674. * @param bool $notrigger false=launch triggers after, true=disable triggers
  675. *
  676. * @return int <0 if KO, >0 if OK
  677. */
  678. public function delete(User $user, $notrigger = false)
  679. {
  680. dol_syslog(__METHOD__, LOG_DEBUG);
  681. $error = 0;
  682. $this->db->begin();
  683. // Triggers
  684. if (!$notrigger) {
  685. // Call triggers
  686. $result = $this->call_trigger(strtoupper(get_class($this)).'_DELETE', $user);
  687. if ($result < 0) {
  688. $error++;
  689. } //Do also here what you must do to rollback action if trigger fail
  690. // End call triggers
  691. }
  692. // If you need to delete child tables to, you can insert them here
  693. if (!$error) {
  694. $result = $this->deleteExtraFields();
  695. if (!$result) {
  696. dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
  697. $error++;
  698. }
  699. }
  700. if (!$error) {
  701. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
  702. $sql .= ' WHERE rowid='.((int) $this->id);
  703. $resql = $this->db->query($sql);
  704. if (!$resql) {
  705. $error++;
  706. $this->errors[] = 'Error '.$this->db->lasterror();
  707. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  708. }
  709. }
  710. // Commit or rollback
  711. if ($error) {
  712. $this->db->rollback();
  713. return -1 * $error;
  714. } else {
  715. $this->db->commit();
  716. return 1;
  717. }
  718. }
  719. /**
  720. * Load an object from its id and create a new one in database
  721. *
  722. * @param User $user User making the clone
  723. * @param int $fromid Id of object to clone
  724. * @return int New id of clone
  725. */
  726. public function createFromClone(User $user, $fromid)
  727. {
  728. dol_syslog(__METHOD__, LOG_DEBUG);
  729. $error = 0;
  730. $object = new Ecmfiles($this->db);
  731. $this->db->begin();
  732. // Load source object
  733. $object->fetch($fromid);
  734. // Reset object
  735. $object->id = 0;
  736. // Clear fields
  737. // ...
  738. // Create clone
  739. $object->context['createfromclone'] = 'createfromclone';
  740. $result = $object->create($user);
  741. // Other options
  742. if ($result < 0) {
  743. $error++;
  744. $this->errors = $object->errors;
  745. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  746. }
  747. unset($object->context['createfromclone']);
  748. // End
  749. if (!$error) {
  750. $this->db->commit();
  751. return $object->id;
  752. } else {
  753. $this->db->rollback();
  754. return -1;
  755. }
  756. }
  757. /**
  758. * Return a link to the object card (with optionaly the picto)
  759. *
  760. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  761. * @param string $option On what the link point to
  762. * @param int $notooltip 1=Disable tooltip
  763. * @param int $maxlen Max length of visible user name
  764. * @param string $morecss Add more css on link
  765. * @return string String with URL
  766. */
  767. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
  768. {
  769. global $db, $conf, $langs;
  770. global $dolibarr_main_authentication, $dolibarr_main_demo;
  771. global $menumanager, $hookmanager;
  772. if (!empty($conf->dol_no_mouse_hover)) {
  773. $notooltip = 1; // Force disable tooltips
  774. }
  775. $result = '';
  776. $label = '<u>'.$langs->trans("MyModule").'</u>';
  777. $label .= '<br>';
  778. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  779. $url = DOL_URL_ROOT.'/ecm/'.$this->table_name.'_card.php?id='.$this->id;
  780. $linkclose = '';
  781. if (empty($notooltip)) {
  782. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  783. $label = $langs->trans("ShowProject");
  784. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  785. }
  786. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  787. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  788. } else {
  789. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  790. }
  791. $linkstart = '<a href="'.$url.'"';
  792. $linkstart .= $linkclose.'>';
  793. $linkend = '</a>';
  794. if ($withpicto) {
  795. $result .= ($linkstart.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
  796. if ($withpicto != 2) {
  797. $result .= ' ';
  798. }
  799. }
  800. $result .= $linkstart.$this->ref.$linkend;
  801. global $action;
  802. $hookmanager->initHooks(array($this->element . 'dao'));
  803. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  804. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  805. if ($reshook > 0) {
  806. $result = $hookmanager->resPrint;
  807. } else {
  808. $result .= $hookmanager->resPrint;
  809. }
  810. return $result;
  811. }
  812. /**
  813. * Return the label of the status
  814. *
  815. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  816. * @return string Label of status
  817. */
  818. public function getLibStatut($mode = 0)
  819. {
  820. return $this->LibStatut($this->status, $mode);
  821. }
  822. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  823. /**
  824. * Return the status
  825. *
  826. * @param int $status Id status
  827. * @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
  828. * @return string Label of status
  829. */
  830. public static function LibStatut($status, $mode = 0)
  831. {
  832. // phpcs:enable
  833. global $langs;
  834. return '';
  835. }
  836. /**
  837. * Initialise object with example values
  838. * Id must be 0 if object instance is a specimen
  839. *
  840. * @return void
  841. */
  842. public function initAsSpecimen()
  843. {
  844. global $conf, $user;
  845. $this->id = 0;
  846. $this->specimen = 1;
  847. $this->label = '0a1b2c3e4f59999999';
  848. $this->entity = 1;
  849. $this->filename = 'myspecimenfilefile.pdf';
  850. $this->filepath = '/aaa/bbb';
  851. $this->fullpath_orig = 'c:/file on my disk.pdf';
  852. $this->description = 'This is a long description of file';
  853. $this->keywords = 'key1,key2';
  854. $this->cover = '1';
  855. $this->position = 5;
  856. $this->gen_or_uploaded = 'uploaded';
  857. $this->extraparams = '';
  858. $this->date_c = (dol_now() - 3600 * 24 * 10);
  859. $this->date_m = '';
  860. $this->fk_user_c = $user->id;
  861. $this->fk_user_m = '';
  862. $this->acl = '';
  863. $this->src_object_type = 'product';
  864. $this->src_object_id = 1;
  865. }
  866. }
  867. /**
  868. * Class of an index line of a document
  869. */
  870. class EcmfilesLine
  871. {
  872. /**
  873. * @var string ECM files line label
  874. */
  875. public $label;
  876. /**
  877. * @var int Entity
  878. */
  879. public $entity;
  880. public $filename;
  881. public $filepath;
  882. public $fullpath_orig;
  883. /**
  884. * @var string description
  885. */
  886. public $description;
  887. public $keywords;
  888. public $cover;
  889. public $position;
  890. public $gen_or_uploaded; // can be 'generated', 'uploaded', 'unknown'
  891. public $extraparams;
  892. public $date_c = '';
  893. public $date_m = '';
  894. /**
  895. * @var int ID
  896. */
  897. public $fk_user_c;
  898. /**
  899. * @var int ID
  900. */
  901. public $fk_user_m;
  902. public $acl;
  903. public $src_object_type;
  904. public $src_object_id;
  905. }