dolresource.class.php 30 KB

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