dolresource.class.php 27 KB

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