dolresource.class.php 29 KB

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