24 require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
25 require_once DOL_DOCUMENT_ROOT.
"/core/lib/functions2.lib.php";
35 public $element =
'dolresource';
40 public $table_element =
'resource';
45 public $picto =
'resource';
51 public $fk_code_type_resource;
69 public $resource_type;
77 public $fk_user_create;
83 public $cache_code_type_resource = array();
108 public function create($user, $notrigger = 0)
110 global $conf, $langs, $hookmanager;
115 if (isset($this->
ref)) {
116 $this->
ref = trim($this->
ref);
121 if (!is_numeric($this->country_id)) {
122 $this->country_id = 0;
124 if (isset($this->fk_code_type_resource)) {
125 $this->fk_code_type_resource = trim($this->fk_code_type_resource);
127 if (isset($this->note_public)) {
128 $this->note_public = trim($this->note_public);
130 if (isset($this->note_private)) {
131 $this->note_private = trim($this->note_private);
136 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
"(";
139 $sql .=
"description,";
140 $sql .=
"fk_country,";
141 $sql .=
"fk_code_type_resource,";
142 $sql .=
"note_public,";
143 $sql .=
"note_private";
144 $sql .=
") VALUES (";
145 $sql .= $conf->entity.
", ";
146 $sql .=
" ".(!isset($this->
ref) ?
'NULL' :
"'".$this->db->escape($this->
ref).
"'").
",";
148 $sql .=
" ".($this->country_id > 0 ? $this->country_id :
'null').
",";
149 $sql .=
" ".(!isset($this->fk_code_type_resource) ?
'NULL' :
"'".$this->db->escape($this->fk_code_type_resource).
"'").
",";
150 $sql .=
" ".(!isset($this->note_public) ?
'NULL' :
"'".$this->db->escape($this->note_public).
"'").
",";
151 $sql .=
" ".(!isset($this->note_private) ?
'NULL' :
"'".$this->db->escape($this->note_private).
"'");
156 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
157 $resql = $this->db->query(
$sql);
159 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
163 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
178 if (!$error && !$notrigger) {
180 $result = $this->
call_trigger(
'RESOURCE_CREATE', $user);
189 foreach ($this->errors as $errmsg) {
190 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
191 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
193 $this->db->rollback();
208 public function fetch($id, $ref =
'')
213 $sql .=
" t.entity,";
215 $sql .=
" t.description,";
216 $sql .=
" t.fk_country,";
217 $sql .=
" t.fk_code_type_resource,";
218 $sql .=
" t.note_public,";
219 $sql .=
" t.note_private,";
221 $sql .=
" ty.label as type_label";
222 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
223 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_type_resource as ty ON ty.code=t.fk_code_type_resource";
225 $sql .=
" WHERE t.rowid = ".((int) $id);
227 $sql .=
" WHERE t.ref = '".$this->db->escape($ref).
"'";
230 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
231 $resql = $this->db->query(
$sql);
233 if ($this->db->num_rows($resql)) {
234 $obj = $this->db->fetch_object($resql);
236 $this->
id = $obj->rowid;
237 $this->entity = $obj->entity;
238 $this->
ref = $obj->ref;
240 $this->country_id = $obj->fk_country;
241 $this->fk_code_type_resource = $obj->fk_code_type_resource;
242 $this->note_public = $obj->note_public;
243 $this->note_private = $obj->note_private;
244 $this->type_label = $obj->type_label;
250 $this->db->free($resql);
254 $this->error =
"Error ".$this->db->lasterror();
255 dol_syslog(get_class($this).
"::fetch ".$this->error, LOG_ERR);
268 public function update($user =
null, $notrigger = 0)
270 global $conf, $langs, $hookmanager;
274 if (isset($this->
ref)) {
275 $this->
ref = trim($this->
ref);
277 if (isset($this->fk_code_type_resource)) {
278 $this->fk_code_type_resource = trim($this->fk_code_type_resource);
283 if (!is_numeric($this->country_id)) {
284 $this->country_id = 0;
288 if (empty($this->oldcopy)) {
293 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
294 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
296 $sql .=
" fk_country=".($this->country_id > 0 ? $this->country_id :
"null").
",";
297 $sql .=
" fk_code_type_resource=".(isset($this->fk_code_type_resource) ?
"'".$this->db->escape($this->fk_code_type_resource).
"'" :
"null").
",";
298 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null');
299 $sql .=
" WHERE rowid=".((int) $this->
id);
303 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
304 $resql = $this->db->query(
$sql);
306 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
312 $result = $this->
call_trigger(
'RESOURCE_MODIFY', $user);
320 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
322 if (!empty($conf->resource->dir_output)) {
325 if (file_exists($olddir)) {
326 $res = @rename($olddir, $newdir);
328 $langs->load(
"errors");
329 $this->error = $langs->trans(
'ErrorFailToRenameDir', $olddir, $newdir);
350 foreach ($this->errors as $errmsg) {
351 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
352 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
354 $this->db->rollback();
375 $sql .=
" t.resource_id,";
376 $sql .=
" t.resource_type,";
377 $sql .=
" t.element_id,";
378 $sql .=
" t.element_type,";
380 $sql .=
" t.mandatory,";
381 $sql .=
" t.fk_user_create,";
383 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_resources as t";
384 $sql .=
" WHERE t.rowid = ".((int) $id);
386 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
387 $resql = $this->db->query(
$sql);
389 if ($this->db->num_rows($resql)) {
390 $obj = $this->db->fetch_object($resql);
392 $this->
id = $obj->rowid;
393 $this->resource_id = $obj->resource_id;
394 $this->resource_type = $obj->resource_type;
395 $this->element_id = $obj->element_id;
396 $this->element_type = $obj->element_type;
397 $this->busy = $obj->busy;
398 $this->mandatory = $obj->mandatory;
399 $this->fk_user_create = $obj->fk_user_create;
401 if ($obj->resource_id && $obj->resource_type) {
404 if ($obj->element_id && $obj->element_type) {
408 $this->db->free($resql);
412 $this->error =
"Error ".$this->db->lasterror();
424 public function delete($rowid, $notrigger = 0)
426 global $user, $langs, $conf;
427 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
433 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
434 $sql .=
" WHERE rowid = ".((int) $rowid);
437 if ($this->db->query(
$sql)) {
438 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"element_resources";
439 $sql .=
" WHERE element_type='resource' AND resource_id = ".((int) $rowid);
440 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
441 $resql = $this->db->query(
$sql);
443 $this->error = $this->db->lasterror();
447 $this->error = $this->db->lasterror();
456 dol_syslog(get_class($this).
"::delete error -3 ".$this->error, LOG_ERR);
462 $result = $this->
call_trigger(
'RESOURCE_DELETE', $user);
472 if (!empty($conf->resource->dir_output)) {
474 if (file_exists($dir)) {
477 $this->errors[] =
'ErrorFailToDeleteDir';
488 $this->db->rollback();
504 public function fetchAll($sortorder, $sortfield, $limit, $offset, $filter =
'')
509 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
514 $sql .=
" t.entity,";
516 $sql .=
" t.description,";
517 $sql .=
" t.fk_country,";
518 $sql .=
" t.fk_code_type_resource,";
521 if (!empty($extrafields->attributes[$this->table_element]) && !empty($extrafields->attributes[$this->table_element][
'label'])) {
522 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
523 $sql .= ($extrafields->attributes[$this->table_element][
'type'][$key] !=
'separate' ?
"ef.".$key.
" as options_".$key.
', ' :
'');
526 $sql .=
" ty.label as type_label";
527 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
528 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_type_resource as ty ON ty.code=t.fk_code_type_resource";
529 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$this->table_element.
"_extrafields as ef ON ef.fk_object=t.rowid";
530 $sql .=
" WHERE t.entity IN (".getEntity(
'resource').
")";
532 if (!empty($filter)) {
533 foreach ($filter as $key => $value) {
534 if (strpos($key,
'date')) {
535 $sql .=
" AND ".$key.
" = '".$this->db->idate($value).
"'";
536 } elseif (strpos($key,
'ef.') !==
false) {
539 $sql .=
" AND ".$key.
" LIKE '%".$this->db->escape($value).
"%'";
543 $sql .= $this->db->order($sortfield, $sortorder);
546 $result = $this->db->query(
$sql);
547 $this->num_all = $this->db->num_rows($result);
550 $sql .= $this->db->plimit($limit, $offset);
552 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
554 $this->lines = array();
555 $resql = $this->db->query(
$sql);
557 $num = $this->db->num_rows($resql);
559 while ($obj = $this->db->fetch_object($resql)) {
561 $line->id = $obj->rowid;
562 $line->ref = $obj->ref;
563 $line->description = $obj->description;
564 $line->country_id = $obj->fk_country;
565 $line->fk_code_type_resource = $obj->fk_code_type_resource;
566 $line->type_label = $obj->type_label;
570 $line->fetch_optionals();
572 $this->lines[] = $line;
574 $this->db->free($resql);
578 $this->error = $this->db->lasterror();
594 global $conf, $langs;
598 if (isset($this->resource_id)) {
599 $this->resource_id = trim($this->resource_id);
601 if (isset($this->resource_type)) {
602 $this->resource_type = trim($this->resource_type);
604 if (isset($this->element_id)) {
605 $this->element_id = trim($this->element_id);
607 if (isset($this->element_type)) {
608 $this->element_type = trim($this->element_type);
610 if (isset($this->busy)) {
611 $this->busy = trim($this->busy);
613 if (isset($this->mandatory)) {
614 $this->mandatory = trim($this->mandatory);
618 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"element_resources SET";
619 $sql .=
" resource_id=".(isset($this->resource_id) ?
"'".$this->db->escape($this->resource_id).
"'" :
"null").
",";
620 $sql .=
" resource_type=".(isset($this->resource_type) ?
"'".$this->db->escape($this->resource_type).
"'" :
"null").
",";
621 $sql .=
" element_id=".(isset($this->element_id) ? $this->element_id :
"null").
",";
622 $sql .=
" element_type=".(isset($this->element_type) ?
"'".$this->db->escape($this->element_type).
"'" :
"null").
",";
623 $sql .=
" busy=".(isset($this->busy) ? $this->busy :
"null").
",";
624 $sql .=
" mandatory=".(isset($this->mandatory) ? $this->mandatory :
"null").
",";
625 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null');
627 $sql .=
" WHERE rowid=".((int) $this->
id);
631 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
632 $resql = $this->db->query(
$sql);
634 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
640 $result = $this->
call_trigger(
'RESOURCE_MODIFY', $user);
650 foreach ($this->errors as $errmsg) {
651 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
652 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
654 $this->db->rollback();
673 $resources = array();
676 $sql =
'SELECT rowid, resource_id, resource_type, busy, mandatory';
677 $sql .=
' FROM '.MAIN_DB_PREFIX.
'element_resources';
678 $sql .=
" WHERE element_id=".((int) $element_id).
" AND element_type='".$this->db->escape($element).
"'";
679 if ($resource_type) {
680 $sql .=
" AND resource_type LIKE '%".$this->db->escape($resource_type).
"%'";
682 $sql .=
' ORDER BY resource_type';
684 dol_syslog(get_class($this).
"::getElementResources", LOG_DEBUG);
686 $resources = array();
687 $resql = $this->db->query(
$sql);
689 $num = $this->db->num_rows($resql);
692 $obj = $this->db->fetch_object($resql);
694 $resources[$i] = array(
695 'rowid' => $obj->rowid,
696 'resource_id' => $obj->resource_id,
697 'resource_type'=>$obj->resource_type,
699 'mandatory'=>$obj->mandatory
719 foreach ($resources as $nb => $resource) {
738 if (is_array($this->cache_code_type_resource) && count($this->cache_code_type_resource)) {
742 $sql =
"SELECT rowid, code, label, active";
743 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_type_resource";
744 $sql .=
" WHERE active > 0";
745 $sql .=
" ORDER BY rowid";
746 dol_syslog(get_class($this).
"::load_cache_code_type_resource", LOG_DEBUG);
747 $resql = $this->db->query(
$sql);
749 $num = $this->db->num_rows($resql);
752 $obj = $this->db->fetch_object($resql);
754 $label = ($langs->trans(
"ResourceTypeShort".$obj->code) != (
"ResourceTypeShort".$obj->code) ? $langs->trans(
"ResourceTypeShort".$obj->code) : ($obj->label !=
'-' ? $obj->label :
''));
755 $this->cache_code_type_resource[$obj->rowid][
'code'] = $obj->code;
756 $this->cache_code_type_resource[$obj->rowid][
'label'] = $label;
757 $this->cache_code_type_resource[$obj->rowid][
'active'] = $obj->active;
776 global $conf, $langs;
778 $langs->load(
'resource');
782 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Resource").
'</u>';
783 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
787 if (isset($this->type_label)) {
788 $datas[
'label'] =
'<br><b>'.$langs->trans(
"ResourceType").
":</b> ".$this->type_label;
805 public function getNomUrl($withpicto = 0, $option =
'', $get_params =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
807 global $conf, $langs, $hookmanager;
812 'objecttype' => $this->element,
814 $classfortooltip =
'classfortooltip';
817 $classfortooltip =
'classforajaxtooltip';
818 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
824 $url = DOL_URL_ROOT.
'/resource/card.php?id='.$this->id;
826 if ($option !=
'nolink') {
828 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
829 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
830 $add_save_lastsearch_values = 1;
832 if ($add_save_lastsearch_values) {
833 $url .=
'&save_lastsearch_values=1';
838 if (empty($notooltip)) {
839 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
840 $label = $langs->trans(
"ShowMyObject");
841 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
843 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
844 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
846 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
849 $linkstart =
'<a href="'.$url.$get_params.
'"';
850 $linkstart .= $linkclose.
'>';
855 $result .= $linkstart;
857 $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);
859 if ($withpicto != 2) {
860 $result .= $this->ref;
865 $hookmanager->initHooks(array($this->element .
'dao'));
866 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
867 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
869 $result = $hookmanager->resPrint;
871 $result .= $hookmanager->resPrint;
885 return $this->
LibStatut($this->status, $mode);
917 $sql =
"SELECT count(r.rowid) as nb";
918 $sql .=
" FROM ".MAIN_DB_PREFIX.
"resource as r";
919 $sql .=
" WHERE r.entity IN (".getEntity(
'resource').
")";
921 $resql = $this->db->query(
$sql);
923 while ($obj = $this->db->fetch_object($resql)) {
924 $this->nb[
"dolresource"] = $obj->nb;
926 $this->db->free($resql);
930 $this->error = $this->db->error();