dolresource.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. <?php
  2. /* Copyright (C) 2013-2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/resource/class/dolresource.class.php
  19. * \ingroup resource
  20. * \brief Class file for resource object
  21. */
  22. require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
  23. require_once DOL_DOCUMENT_ROOT."/core/lib/functions2.lib.php";
  24. /**
  25. * DAO Resource object
  26. */
  27. class Dolresource extends CommonObject
  28. {
  29. /**
  30. * @var string ID to identify managed object
  31. */
  32. public $element = 'dolresource';
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element = 'resource';
  37. /**
  38. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  39. */
  40. public $picto = 'resource';
  41. /**
  42. * @var int ID
  43. */
  44. public $fk_code_type_resource;
  45. public $type_label;
  46. public $description;
  47. public $fk_country;
  48. // Variable for a link of resource
  49. /**
  50. * @var int ID
  51. */
  52. public $resource_id;
  53. public $resource_type;
  54. public $element_id;
  55. public $element_type;
  56. public $busy;
  57. public $mandatory;
  58. /**
  59. * @var int ID
  60. */
  61. public $fk_user_create;
  62. public $tms = '';
  63. /**
  64. * @var array Cache of type of resources. TODO Use $conf->cache['type_of_resources'] instead
  65. */
  66. public $cache_code_type_resource = array();
  67. /**
  68. * @var Dolresource Clone of object before changing it
  69. */
  70. public $oldcopy;
  71. /**
  72. * Constructor
  73. *
  74. * @param DoliDb $db Database handler
  75. */
  76. public function __construct($db)
  77. {
  78. $this->db = $db;
  79. }
  80. /**
  81. * Create object into database
  82. *
  83. * @param User $user User that creates
  84. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  85. * @return int <0 if KO, Id of created object if OK
  86. */
  87. public function create($user, $notrigger = 0)
  88. {
  89. global $conf, $langs, $hookmanager;
  90. $error = 0;
  91. // Clean parameters
  92. if (isset($this->ref)) {
  93. $this->ref = trim($this->ref);
  94. }
  95. if (isset($this->description)) {
  96. $this->description = trim($this->description);
  97. }
  98. if (!is_numeric($this->country_id)) {
  99. $this->country_id = 0;
  100. }
  101. if (isset($this->fk_code_type_resource)) {
  102. $this->fk_code_type_resource = trim($this->fk_code_type_resource);
  103. }
  104. if (isset($this->note_public)) {
  105. $this->note_public = trim($this->note_public);
  106. }
  107. if (isset($this->note_private)) {
  108. $this->note_private = trim($this->note_private);
  109. }
  110. // Insert request
  111. $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
  112. $sql .= "entity,";
  113. $sql .= "ref,";
  114. $sql .= "description,";
  115. $sql .= "fk_country,";
  116. $sql .= "fk_code_type_resource,";
  117. $sql .= "note_public,";
  118. $sql .= "note_private";
  119. $sql .= ") VALUES (";
  120. $sql .= $conf->entity.", ";
  121. $sql .= " ".(!isset($this->ref) ? 'NULL' : "'".$this->db->escape($this->ref)."'").",";
  122. $sql .= " ".(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").",";
  123. $sql .= " ".($this->country_id > 0 ? $this->country_id : 'null').",";
  124. $sql .= " ".(!isset($this->fk_code_type_resource) ? 'NULL' : "'".$this->db->escape($this->fk_code_type_resource)."'").",";
  125. $sql .= " ".(!isset($this->note_public) ? 'NULL' : "'".$this->db->escape($this->note_public)."'").",";
  126. $sql .= " ".(!isset($this->note_private) ? 'NULL' : "'".$this->db->escape($this->note_private)."'");
  127. $sql .= ")";
  128. $this->db->begin();
  129. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  130. $resql = $this->db->query($sql);
  131. if (!$resql) {
  132. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  133. }
  134. if (!$error) {
  135. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  136. }
  137. if (!$error) {
  138. $action = 'create';
  139. // Actions on extra fields
  140. if (!$error) {
  141. $result = $this->insertExtraFields();
  142. if ($result < 0) {
  143. $error++;
  144. }
  145. }
  146. }
  147. if (!$error && !$notrigger) {
  148. // Call trigger
  149. $result = $this->call_trigger('RESOURCE_CREATE', $user);
  150. if ($result < 0) {
  151. $error++;
  152. }
  153. // End call triggers
  154. }
  155. // Commit or rollback
  156. if ($error) {
  157. foreach ($this->errors as $errmsg) {
  158. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  159. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  160. }
  161. $this->db->rollback();
  162. return -1 * $error;
  163. } else {
  164. $this->db->commit();
  165. return $this->id;
  166. }
  167. }
  168. /**
  169. * Load object in memory from database
  170. *
  171. * @param int $id Id of object
  172. * @param string $ref Ref of object
  173. * @return int <0 if KO, >0 if OK
  174. */
  175. public function fetch($id, $ref = '')
  176. {
  177. global $langs;
  178. $sql = "SELECT";
  179. $sql .= " t.rowid,";
  180. $sql .= " t.entity,";
  181. $sql .= " t.ref,";
  182. $sql .= " t.description,";
  183. $sql .= " t.fk_country,";
  184. $sql .= " t.fk_code_type_resource,";
  185. $sql .= " t.note_public,";
  186. $sql .= " t.note_private,";
  187. $sql .= " t.tms,";
  188. $sql .= " ty.label as type_label";
  189. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
  190. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_resource as ty ON ty.code=t.fk_code_type_resource";
  191. if ($id) {
  192. $sql .= " WHERE t.rowid = ".((int) $id);
  193. } else {
  194. $sql .= " WHERE t.ref = '".$this->db->escape($ref)."'";
  195. }
  196. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  197. $resql = $this->db->query($sql);
  198. if ($resql) {
  199. if ($this->db->num_rows($resql)) {
  200. $obj = $this->db->fetch_object($resql);
  201. $this->id = $obj->rowid;
  202. $this->entity = $obj->entity;
  203. $this->ref = $obj->ref;
  204. $this->description = $obj->description;
  205. $this->country_id = $obj->fk_country;
  206. $this->fk_code_type_resource = $obj->fk_code_type_resource;
  207. $this->note_public = $obj->note_public;
  208. $this->note_private = $obj->note_private;
  209. $this->type_label = $obj->type_label;
  210. // Retrieve all extrafield
  211. // fetch optionals attributes and labels
  212. $this->fetch_optionals();
  213. }
  214. $this->db->free($resql);
  215. return $this->id;
  216. } else {
  217. $this->error = "Error ".$this->db->lasterror();
  218. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  219. return -1;
  220. }
  221. }
  222. /**
  223. * Update object into database
  224. *
  225. * @param User $user User that modifies
  226. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  227. * @return int <0 if KO, >0 if OK
  228. */
  229. public function update($user = null, $notrigger = 0)
  230. {
  231. global $conf, $langs, $hookmanager;
  232. $error = 0;
  233. // Clean parameters
  234. if (isset($this->ref)) {
  235. $this->ref = trim($this->ref);
  236. }
  237. if (isset($this->fk_code_type_resource)) {
  238. $this->fk_code_type_resource = trim($this->fk_code_type_resource);
  239. }
  240. if (isset($this->description)) {
  241. $this->description = trim($this->description);
  242. }
  243. if (!is_numeric($this->country_id)) {
  244. $this->country_id = 0;
  245. }
  246. // $this->oldcopy should have been set by the caller of update (here properties were already modified)
  247. if (empty($this->oldcopy)) {
  248. $this->oldcopy = dol_clone($this);
  249. }
  250. // Update request
  251. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
  252. $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
  253. $sql .= " description=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
  254. $sql .= " fk_country=".($this->country_id > 0 ? $this->country_id : "null").",";
  255. $sql .= " fk_code_type_resource=".(isset($this->fk_code_type_resource) ? "'".$this->db->escape($this->fk_code_type_resource)."'" : "null").",";
  256. $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null')."";
  257. $sql .= " WHERE rowid=".((int) $this->id);
  258. $this->db->begin();
  259. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  260. $resql = $this->db->query($sql);
  261. if (!$resql) {
  262. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  263. }
  264. if (!$error) {
  265. if (!$notrigger) {
  266. // Call trigger
  267. $result = $this->call_trigger('RESOURCE_MODIFY', $user);
  268. if ($result < 0) {
  269. $error++;
  270. }
  271. // End call triggers
  272. }
  273. }
  274. if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
  275. // We remove directory
  276. if (!empty($conf->resource->dir_output)) {
  277. $olddir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->oldcopy->ref);
  278. $newdir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->ref);
  279. if (file_exists($olddir)) {
  280. $res = @rename($olddir, $newdir);
  281. if (!$res) {
  282. $langs->load("errors");
  283. $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir);
  284. $error++;
  285. }
  286. }
  287. }
  288. }
  289. if (!$error) {
  290. $action = 'update';
  291. // Actions on extra fields
  292. if (!$error) {
  293. $result = $this->insertExtraFields();
  294. if ($result < 0) {
  295. $error++;
  296. }
  297. }
  298. }
  299. // Commit or rollback
  300. if ($error) {
  301. foreach ($this->errors as $errmsg) {
  302. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  303. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  304. }
  305. $this->db->rollback();
  306. return -1 * $error;
  307. } else {
  308. $this->db->commit();
  309. return 1;
  310. }
  311. }
  312. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  313. /**
  314. * Load data of link in memory from database
  315. *
  316. * @param int $id Id of link element_resources
  317. * @return int <0 if KO, >0 if OK
  318. */
  319. public function fetch_element_resource($id)
  320. {
  321. // phpcs:enable
  322. global $langs;
  323. $sql = "SELECT";
  324. $sql .= " t.rowid,";
  325. $sql .= " t.resource_id,";
  326. $sql .= " t.resource_type,";
  327. $sql .= " t.element_id,";
  328. $sql .= " t.element_type,";
  329. $sql .= " t.busy,";
  330. $sql .= " t.mandatory,";
  331. $sql .= " t.fk_user_create,";
  332. $sql .= " t.tms";
  333. $sql .= " FROM ".MAIN_DB_PREFIX."element_resources as t";
  334. $sql .= " WHERE t.rowid = ".((int) $id);
  335. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  336. $resql = $this->db->query($sql);
  337. if ($resql) {
  338. if ($this->db->num_rows($resql)) {
  339. $obj = $this->db->fetch_object($resql);
  340. $this->id = $obj->rowid;
  341. $this->resource_id = $obj->resource_id;
  342. $this->resource_type = $obj->resource_type;
  343. $this->element_id = $obj->element_id;
  344. $this->element_type = $obj->element_type;
  345. $this->busy = $obj->busy;
  346. $this->mandatory = $obj->mandatory;
  347. $this->fk_user_create = $obj->fk_user_create;
  348. if ($obj->resource_id && $obj->resource_type) {
  349. $this->objresource = fetchObjectByElement($obj->resource_id, $obj->resource_type);
  350. }
  351. if ($obj->element_id && $obj->element_type) {
  352. $this->objelement = fetchObjectByElement($obj->element_id, $obj->element_type);
  353. }
  354. }
  355. $this->db->free($resql);
  356. return $this->id;
  357. } else {
  358. $this->error = "Error ".$this->db->lasterror();
  359. return -1;
  360. }
  361. }
  362. /**
  363. * Delete a resource object
  364. *
  365. * @param int $rowid Id of resource line to delete
  366. * @param int $notrigger Disable all triggers
  367. * @return int >0 if OK, <0 if KO
  368. */
  369. public function delete($rowid, $notrigger = 0)
  370. {
  371. global $user, $langs, $conf;
  372. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  373. $error = 0;
  374. $this->db->begin();
  375. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
  376. $sql .= " WHERE rowid = ".((int) $rowid);
  377. dol_syslog(get_class($this), LOG_DEBUG);
  378. if ($this->db->query($sql)) {
  379. $sql = "DELETE FROM ".MAIN_DB_PREFIX."element_resources";
  380. $sql .= " WHERE element_type='resource' AND resource_id = ".((int) $rowid);
  381. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  382. $resql = $this->db->query($sql);
  383. if (!$resql) {
  384. $this->error = $this->db->lasterror();
  385. $error++;
  386. }
  387. } else {
  388. $this->error = $this->db->lasterror();
  389. $error++;
  390. }
  391. // Removed extrafields
  392. if (!$error) {
  393. $result = $this->deleteExtraFields();
  394. if ($result < 0) {
  395. $error++;
  396. dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
  397. }
  398. }
  399. if (!$notrigger) {
  400. // Call trigger
  401. $result = $this->call_trigger('RESOURCE_DELETE', $user);
  402. if ($result < 0) {
  403. $error++;
  404. }
  405. // End call triggers
  406. }
  407. if (!$error) {
  408. // We remove directory
  409. $ref = dol_sanitizeFileName($this->ref);
  410. if (!empty($conf->resource->dir_output)) {
  411. $dir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->ref);
  412. if (file_exists($dir)) {
  413. $res = @dol_delete_dir_recursive($dir);
  414. if (!$res) {
  415. $this->errors[] = 'ErrorFailToDeleteDir';
  416. $error++;
  417. }
  418. }
  419. }
  420. }
  421. if (!$error) {
  422. $this->db->commit();
  423. return 1;
  424. } else {
  425. $this->db->rollback();
  426. return -1;
  427. }
  428. }
  429. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  430. /**
  431. * Load resource objects into $this->lines
  432. *
  433. * @param string $sortorder sort order
  434. * @param string $sortfield sort field
  435. * @param int $limit limit page
  436. * @param int $offset page
  437. * @param array $filter filter output
  438. * @return int <0 if KO, >0 if OK
  439. */
  440. public function fetchAll($sortorder, $sortfield, $limit, $offset, $filter = '')
  441. {
  442. // phpcs:enable
  443. global $conf;
  444. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  445. $extrafields = new ExtraFields($this->db);
  446. $sql = "SELECT ";
  447. $sql .= " t.rowid,";
  448. $sql .= " t.entity,";
  449. $sql .= " t.ref,";
  450. $sql .= " t.description,";
  451. $sql .= " t.fk_country,";
  452. $sql .= " t.fk_code_type_resource,";
  453. $sql .= " t.tms,";
  454. // Add fields from extrafields
  455. if (!empty($extrafields->attributes[$this->table_element]) && !empty($extrafields->attributes[$this->table_element]['label'])) {
  456. foreach ($extrafields->attributes[$this->table_element]['label'] as $key => $val) {
  457. $sql .= ($extrafields->attributes[$this->table_element]['type'][$key] != 'separate' ? "ef.".$key." as options_".$key.', ' : '');
  458. }
  459. }
  460. $sql .= " ty.label as type_label";
  461. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
  462. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_resource as ty ON ty.code=t.fk_code_type_resource";
  463. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$this->table_element."_extrafields as ef ON ef.fk_object=t.rowid";
  464. $sql .= " WHERE t.entity IN (".getEntity('resource').")";
  465. // Manage filter
  466. if (!empty($filter)) {
  467. foreach ($filter as $key => $value) {
  468. if (strpos($key, 'date')) {
  469. $sql .= " AND ".$key." = '".$this->db->idate($value)."'";
  470. } elseif (strpos($key, 'ef.') !== false) {
  471. $sql .= $value;
  472. } else {
  473. $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
  474. }
  475. }
  476. }
  477. $sql .= $this->db->order($sortfield, $sortorder);
  478. $this->num_all = 0;
  479. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  480. $result = $this->db->query($sql);
  481. $this->num_all = $this->db->num_rows($result);
  482. }
  483. if ($limit) {
  484. $sql .= $this->db->plimit($limit, $offset);
  485. }
  486. dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
  487. $this->lines = array();
  488. $resql = $this->db->query($sql);
  489. if ($resql) {
  490. $num = $this->db->num_rows($resql);
  491. if ($num) {
  492. while ($obj = $this->db->fetch_object($resql)) {
  493. $line = new Dolresource($this->db);
  494. $line->id = $obj->rowid;
  495. $line->ref = $obj->ref;
  496. $line->description = $obj->description;
  497. $line->country_id = $obj->fk_country;
  498. $line->fk_code_type_resource = $obj->fk_code_type_resource;
  499. $line->type_label = $obj->type_label;
  500. // fetch optionals attributes and labels
  501. $line->fetch_optionals();
  502. $this->lines[] = $line;
  503. }
  504. $this->db->free($resql);
  505. }
  506. return $num;
  507. } else {
  508. $this->error = $this->db->lasterror();
  509. return -1;
  510. }
  511. }
  512. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  513. /**
  514. * Update element resource into database
  515. *
  516. * @param User $user User that modifies
  517. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  518. * @return int <0 if KO, >0 if OK
  519. */
  520. public function update_element_resource($user = null, $notrigger = 0)
  521. {
  522. // phpcs:enable
  523. global $conf, $langs;
  524. $error = 0;
  525. // Clean parameters
  526. if (isset($this->resource_id)) {
  527. $this->resource_id = trim($this->resource_id);
  528. }
  529. if (isset($this->resource_type)) {
  530. $this->resource_type = trim($this->resource_type);
  531. }
  532. if (isset($this->element_id)) {
  533. $this->element_id = trim($this->element_id);
  534. }
  535. if (isset($this->element_type)) {
  536. $this->element_type = trim($this->element_type);
  537. }
  538. if (isset($this->busy)) {
  539. $this->busy = trim($this->busy);
  540. }
  541. if (isset($this->mandatory)) {
  542. $this->mandatory = trim($this->mandatory);
  543. }
  544. // Update request
  545. $sql = "UPDATE ".MAIN_DB_PREFIX."element_resources SET";
  546. $sql .= " resource_id=".(isset($this->resource_id) ? "'".$this->db->escape($this->resource_id)."'" : "null").",";
  547. $sql .= " resource_type=".(isset($this->resource_type) ? "'".$this->db->escape($this->resource_type)."'" : "null").",";
  548. $sql .= " element_id=".(isset($this->element_id) ? $this->element_id : "null").",";
  549. $sql .= " element_type=".(isset($this->element_type) ? "'".$this->db->escape($this->element_type)."'" : "null").",";
  550. $sql .= " busy=".(isset($this->busy) ? $this->busy : "null").",";
  551. $sql .= " mandatory=".(isset($this->mandatory) ? $this->mandatory : "null").",";
  552. $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null')."";
  553. $sql .= " WHERE rowid=".((int) $this->id);
  554. $this->db->begin();
  555. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  556. $resql = $this->db->query($sql);
  557. if (!$resql) {
  558. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  559. }
  560. if (!$error) {
  561. if (!$notrigger) {
  562. // Call trigger
  563. $result = $this->call_trigger('RESOURCE_MODIFY', $user);
  564. if ($result < 0) {
  565. $error++;
  566. }
  567. // End call triggers
  568. }
  569. }
  570. // Commit or rollback
  571. if ($error) {
  572. foreach ($this->errors as $errmsg) {
  573. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  574. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  575. }
  576. $this->db->rollback();
  577. return -1 * $error;
  578. } else {
  579. $this->db->commit();
  580. return 1;
  581. }
  582. }
  583. /**
  584. * Return an array with resources linked to the element
  585. *
  586. * @param string $element Element
  587. * @param int $element_id Id
  588. * @param string $resource_type Type
  589. * @return array Aray of resources
  590. */
  591. public function getElementResources($element, $element_id, $resource_type = '')
  592. {
  593. $resources = array();
  594. // Links beetween objects are stored in this table
  595. $sql = 'SELECT rowid, resource_id, resource_type, busy, mandatory';
  596. $sql .= ' FROM '.MAIN_DB_PREFIX.'element_resources';
  597. $sql .= " WHERE element_id=".((int) $element_id)." AND element_type='".$this->db->escape($element)."'";
  598. if ($resource_type) {
  599. $sql .= " AND resource_type LIKE '%".$this->db->escape($resource_type)."%'";
  600. }
  601. $sql .= ' ORDER BY resource_type';
  602. dol_syslog(get_class($this)."::getElementResources", LOG_DEBUG);
  603. $resources = array();
  604. $resql = $this->db->query($sql);
  605. if ($resql) {
  606. $num = $this->db->num_rows($resql);
  607. $i = 0;
  608. while ($i < $num) {
  609. $obj = $this->db->fetch_object($resql);
  610. $resources[$i] = array(
  611. 'rowid' => $obj->rowid,
  612. 'resource_id' => $obj->resource_id,
  613. 'resource_type'=>$obj->resource_type,
  614. 'busy'=>$obj->busy,
  615. 'mandatory'=>$obj->mandatory
  616. );
  617. $i++;
  618. }
  619. }
  620. return $resources;
  621. }
  622. /**
  623. * Return an int number of resources linked to the element
  624. *
  625. * @param string $element Element type
  626. * @param int $element_id Element id
  627. * @return int Nb of resources loaded
  628. */
  629. public function fetchElementResources($element, $element_id)
  630. {
  631. $resources = $this->getElementResources($element, $element_id);
  632. $i = 0;
  633. foreach ($resources as $nb => $resource) {
  634. $this->lines[$i] = fetchObjectByElement($resource['resource_id'], $resource['resource_type']);
  635. $i++;
  636. }
  637. return $i;
  638. }
  639. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  640. /**
  641. * Load in cache resource type code (setup in dictionary)
  642. *
  643. * @return int Number of lines loaded, 0 if already loaded, <0 if KO
  644. */
  645. public function load_cache_code_type_resource()
  646. {
  647. // phpcs:enable
  648. global $langs;
  649. if (is_array($this->cache_code_type_resource) && count($this->cache_code_type_resource)) {
  650. return 0; // Cache deja charge
  651. }
  652. $sql = "SELECT rowid, code, label, active";
  653. $sql .= " FROM ".MAIN_DB_PREFIX."c_type_resource";
  654. $sql .= " WHERE active > 0";
  655. $sql .= " ORDER BY rowid";
  656. dol_syslog(get_class($this)."::load_cache_code_type_resource", LOG_DEBUG);
  657. $resql = $this->db->query($sql);
  658. if ($resql) {
  659. $num = $this->db->num_rows($resql);
  660. $i = 0;
  661. while ($i < $num) {
  662. $obj = $this->db->fetch_object($resql);
  663. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  664. $label = ($langs->trans("ResourceTypeShort".$obj->code) != ("ResourceTypeShort".$obj->code) ? $langs->trans("ResourceTypeShort".$obj->code) : ($obj->label != '-' ? $obj->label : ''));
  665. $this->cache_code_type_resource[$obj->rowid]['code'] = $obj->code;
  666. $this->cache_code_type_resource[$obj->rowid]['label'] = $label;
  667. $this->cache_code_type_resource[$obj->rowid]['active'] = $obj->active;
  668. $i++;
  669. }
  670. return $num;
  671. } else {
  672. dol_print_error($this->db);
  673. return -1;
  674. }
  675. }
  676. /**
  677. * Return clicable link of object (with eventually picto)
  678. *
  679. * @param int $withpicto Add picto into link
  680. * @param string $option Where point the link ('compta', 'expedition', 'document', ...)
  681. * @param string $get_params Parametres added to url
  682. * @param int $notooltip 1=Disable tooltip
  683. * @param string $morecss Add more css on link
  684. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  685. * @return string String with URL
  686. */
  687. public function getNomUrl($withpicto = 0, $option = '', $get_params = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  688. {
  689. global $conf, $langs, $hookmanager;
  690. $result = '';
  691. $label = img_picto('', $this->picto).' <u>'.$langs->trans("Resource").'</u>';
  692. $label .= '<br>';
  693. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  694. /*if (isset($this->status)) {
  695. $label.= '<br><b>' . $langs->trans("Status").":</b> ".$this->getLibStatut(5);
  696. }*/
  697. if (isset($this->type_label)) {
  698. $label .= '<br><b>'.$langs->trans("ResourceType").":</b> ".$this->type_label;
  699. }
  700. $url = DOL_URL_ROOT.'/resource/card.php?id='.$this->id;
  701. if ($option != 'nolink') {
  702. // Add param to save lastsearch_values or not
  703. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  704. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  705. $add_save_lastsearch_values = 1;
  706. }
  707. if ($add_save_lastsearch_values) {
  708. $url .= '&save_lastsearch_values=1';
  709. }
  710. }
  711. $linkclose = '';
  712. if (empty($notooltip)) {
  713. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  714. $label = $langs->trans("ShowMyObject");
  715. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  716. }
  717. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  718. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  719. } else {
  720. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  721. }
  722. $linkstart = '<a href="'.$url.$get_params.'"';
  723. $linkstart .= $linkclose.'>';
  724. $linkend = '</a>';
  725. /*$linkstart = '<a href="'.DOL_URL_ROOT.'/resource/card.php?id='.$this->id.$get_params.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  726. $linkend = '</a>';*/
  727. $result .= $linkstart;
  728. if ($withpicto) {
  729. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  730. }
  731. if ($withpicto != 2) {
  732. $result .= $this->ref;
  733. }
  734. $result .= $linkend;
  735. global $action;
  736. $hookmanager->initHooks(array($this->element . 'dao'));
  737. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  738. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  739. if ($reshook > 0) {
  740. $result = $hookmanager->resPrint;
  741. } else {
  742. $result .= $hookmanager->resPrint;
  743. }
  744. return $result;
  745. }
  746. /**
  747. * Retourne le libelle du status d'un user (actif, inactif)
  748. *
  749. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  750. * @return string Label of status
  751. */
  752. public function getLibStatut($mode = 0)
  753. {
  754. return $this->LibStatut($this->status, $mode);
  755. }
  756. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  757. /**
  758. * Return the status
  759. *
  760. * @param int $status Id status
  761. * @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
  762. * @return string Label of status
  763. */
  764. public static function LibStatut($status, $mode = 0)
  765. {
  766. // phpcs:enable
  767. global $langs;
  768. return '';
  769. }
  770. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  771. /**
  772. * Charge indicateurs this->nb de tableau de bord
  773. *
  774. * @return int <0 if KO, >0 if OK
  775. */
  776. public function load_state_board()
  777. {
  778. // phpcs:enable
  779. global $conf;
  780. $this->nb = array();
  781. $sql = "SELECT count(r.rowid) as nb";
  782. $sql .= " FROM ".MAIN_DB_PREFIX."resource as r";
  783. $sql .= " WHERE r.entity IN (".getEntity('resource').")";
  784. $resql = $this->db->query($sql);
  785. if ($resql) {
  786. while ($obj = $this->db->fetch_object($resql)) {
  787. $this->nb["dolresource"] = $obj->nb;
  788. }
  789. $this->db->free($resql);
  790. return 1;
  791. } else {
  792. dol_print_error($this->db);
  793. $this->error = $this->db->error();
  794. return -1;
  795. }
  796. }
  797. }