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';
48 public $resource_type;
57 public $fk_user_create;
62 public $cache_code_type_resource = array();
86 public function create($user, $notrigger = 0)
88 global $conf, $langs, $hookmanager;
93 if (isset($this->
ref)) {
94 $this->
ref = trim($this->
ref);
99 if (!is_numeric($this->country_id)) {
100 $this->country_id = 0;
102 if (isset($this->fk_code_type_resource)) {
103 $this->fk_code_type_resource = trim($this->fk_code_type_resource);
105 if (isset($this->note_public)) {
106 $this->note_public = trim($this->note_public);
108 if (isset($this->note_private)) {
109 $this->note_private = trim($this->note_private);
114 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
"(";
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).
"'").
",";
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).
"'");
134 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
137 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
141 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
156 if (!$error && !$notrigger) {
158 $result = $this->
call_trigger(
'RESOURCE_CREATE', $user);
167 foreach ($this->errors as $errmsg) {
168 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
169 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
171 $this->
db->rollback();
186 public function fetch($id, $ref =
'')
191 $sql .=
" t.entity,";
193 $sql .=
" t.description,";
194 $sql .=
" t.fk_country,";
195 $sql .=
" t.fk_code_type_resource,";
196 $sql .=
" t.note_public,";
197 $sql .=
" t.note_private,";
199 $sql .=
" ty.label as type_label";
200 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
201 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_type_resource as ty ON ty.code=t.fk_code_type_resource";
203 $sql .=
" WHERE t.rowid = ".((int) $id);
205 $sql .=
" WHERE t.ref = '".$this->db->escape($ref).
"'";
208 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
212 $obj = $this->
db->fetch_object(
$resql);
214 $this->
id = $obj->rowid;
215 $this->entity = $obj->entity;
216 $this->
ref = $obj->ref;
218 $this->country_id = $obj->fk_country;
219 $this->fk_code_type_resource = $obj->fk_code_type_resource;
220 $this->note_public = $obj->note_public;
221 $this->note_private = $obj->note_private;
222 $this->type_label = $obj->type_label;
232 $this->error =
"Error ".$this->db->lasterror();
233 dol_syslog(get_class($this).
"::fetch ".$this->error, LOG_ERR);
246 public function update($user =
null, $notrigger = 0)
248 global $conf, $langs, $hookmanager;
252 if (isset($this->
ref)) {
253 $this->
ref = trim($this->
ref);
255 if (isset($this->fk_code_type_resource)) {
256 $this->fk_code_type_resource = trim($this->fk_code_type_resource);
261 if (!is_numeric($this->country_id)) {
262 $this->country_id = 0;
265 if (empty($this->oldcopy)) {
266 $org =
new self($this->db);
267 $org->fetch($this->
id);
268 $this->oldcopy = $org;
272 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
273 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
274 $sql .=
" description=".(isset($this->
description) ?
"'".$this->db->escape($this->
description).
"'" :
"null").
",";
275 $sql .=
" fk_country=".($this->country_id > 0 ? $this->country_id :
"null").
",";
276 $sql .=
" fk_code_type_resource=".(isset($this->fk_code_type_resource) ?
"'".$this->db->escape($this->fk_code_type_resource).
"'" :
"null").
",";
277 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
"";
278 $sql .=
" WHERE rowid=".((int) $this->
id);
282 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
285 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
291 $result = $this->
call_trigger(
'RESOURCE_MODIFY', $user);
299 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
301 if (!empty($conf->resource->dir_output)) {
304 if (file_exists($olddir)) {
305 $res = @rename($olddir, $newdir);
307 $langs->load(
"errors");
308 $this->error = $langs->trans(
'ErrorFailToRenameDir', $olddir, $newdir);
329 foreach ($this->errors as $errmsg) {
330 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
331 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
333 $this->
db->rollback();
354 $sql .=
" t.resource_id,";
355 $sql .=
" t.resource_type,";
356 $sql .=
" t.element_id,";
357 $sql .=
" t.element_type,";
359 $sql .=
" t.mandatory,";
360 $sql .=
" t.fk_user_create,";
362 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_resources as t";
363 $sql .=
" WHERE t.rowid = ".((int) $id);
365 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
369 $obj = $this->
db->fetch_object(
$resql);
371 $this->
id = $obj->rowid;
372 $this->resource_id = $obj->resource_id;
373 $this->resource_type = $obj->resource_type;
374 $this->element_id = $obj->element_id;
375 $this->element_type = $obj->element_type;
376 $this->busy = $obj->busy;
377 $this->mandatory = $obj->mandatory;
378 $this->fk_user_create = $obj->fk_user_create;
380 if ($obj->resource_id && $obj->resource_type) {
383 if ($obj->element_id && $obj->element_type) {
391 $this->error =
"Error ".$this->db->lasterror();
403 public function delete($rowid, $notrigger = 0)
405 global $user, $langs, $conf;
406 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
412 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
413 $sql .=
" WHERE rowid = ".((int) $rowid);
416 if ($this->
db->query($sql)) {
417 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"element_resources";
418 $sql .=
" WHERE element_type='resource' AND resource_id = ".((int) $rowid);
419 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
422 $this->error = $this->
db->lasterror();
426 $this->error = $this->
db->lasterror();
435 dol_syslog(get_class($this).
"::delete error -3 ".$this->error, LOG_ERR);
441 $result = $this->
call_trigger(
'RESOURCE_DELETE', $user);
451 if (!empty($conf->resource->dir_output)) {
453 if (file_exists($dir)) {
456 $this->errors[] =
'ErrorFailToDeleteDir';
467 $this->
db->rollback();
483 public function fetchAll($sortorder, $sortfield, $limit, $offset, $filter =
'')
488 require_once DOL_DOCUMENT_ROOT.
'/core/class/extrafields.class.php';
493 $sql .=
" t.entity,";
495 $sql .=
" t.description,";
496 $sql .=
" t.fk_country,";
497 $sql .=
" t.fk_code_type_resource,";
500 if (!empty($extrafields->attributes[$this->table_element]) && !empty($extrafields->attributes[$this->table_element][
'label'])) {
501 foreach ($extrafields->attributes[$this->table_element][
'label'] as $key => $val) {
502 $sql .= ($extrafields->attributes[$this->table_element][
'type'][$key] !=
'separate' ?
"ef.".$key.
" as options_".$key.
', ' :
'');
505 $sql .=
" ty.label as type_label";
506 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" as t";
507 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_type_resource as ty ON ty.code=t.fk_code_type_resource";
508 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$this->table_element.
"_extrafields as ef ON ef.fk_object=t.rowid";
509 $sql .=
" WHERE t.entity IN (".getEntity(
'resource').
")";
511 if (!empty($filter)) {
512 foreach ($filter as $key => $value) {
513 if (strpos($key,
'date')) {
514 $sql .=
" AND ".$key.
" = '".$this->
db->idate($value).
"'";
515 } elseif (strpos($key,
'ef.') !==
false) {
518 $sql .=
" AND ".$key.
" LIKE '%".$this->
db->escape($value).
"%'";
522 $sql .= $this->
db->order($sortfield, $sortorder);
524 if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
525 $result = $this->
db->query($sql);
526 $this->num_all = $this->
db->num_rows($result);
529 $sql .= $this->
db->plimit($limit, $offset);
531 dol_syslog(get_class($this).
"::fetch_all", LOG_DEBUG);
533 $this->lines = array();
538 while ($obj = $this->
db->fetch_object(
$resql)) {
540 $line->id = $obj->rowid;
541 $line->ref = $obj->ref;
542 $line->description = $obj->description;
543 $line->country_id = $obj->fk_country;
544 $line->fk_code_type_resource = $obj->fk_code_type_resource;
545 $line->type_label = $obj->type_label;
549 $line->fetch_optionals();
551 $this->lines[] = $line;
557 $this->error = $this->
db->lasterror();
573 global $conf, $langs;
577 if (isset($this->resource_id)) {
578 $this->resource_id = trim($this->resource_id);
580 if (isset($this->resource_type)) {
581 $this->resource_type = trim($this->resource_type);
583 if (isset($this->element_id)) {
584 $this->element_id = trim($this->element_id);
586 if (isset($this->element_type)) {
587 $this->element_type = trim($this->element_type);
589 if (isset($this->busy)) {
590 $this->busy = trim($this->busy);
592 if (isset($this->mandatory)) {
593 $this->mandatory = trim($this->mandatory);
597 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"element_resources SET";
598 $sql .=
" resource_id=".(isset($this->resource_id) ?
"'".$this->db->escape($this->resource_id).
"'" :
"null").
",";
599 $sql .=
" resource_type=".(isset($this->resource_type) ?
"'".$this->db->escape($this->resource_type).
"'" :
"null").
",";
600 $sql .=
" element_id=".(isset($this->element_id) ? $this->element_id :
"null").
",";
601 $sql .=
" element_type=".(isset($this->element_type) ?
"'".$this->db->escape($this->element_type).
"'" :
"null").
",";
602 $sql .=
" busy=".(isset($this->busy) ? $this->busy :
"null").
",";
603 $sql .=
" mandatory=".(isset($this->mandatory) ? $this->mandatory :
"null").
",";
604 $sql .=
" tms=".(dol_strlen($this->tms) != 0 ?
"'".$this->db->idate($this->tms).
"'" :
'null').
"";
606 $sql .=
" WHERE rowid=".((int) $this->
id);
610 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
613 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
619 $result = $this->
call_trigger(
'RESOURCE_MODIFY', $user);
629 foreach ($this->errors as $errmsg) {
630 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
631 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
633 $this->
db->rollback();
652 $resources = array();
655 $sql =
'SELECT rowid, resource_id, resource_type, busy, mandatory';
656 $sql .=
' FROM '.MAIN_DB_PREFIX.
'element_resources';
657 $sql .=
" WHERE element_id=".((int) $element_id).
" AND element_type='".$this->
db->escape($element).
"'";
658 if ($resource_type) {
659 $sql .=
" AND resource_type LIKE '%".$this->db->escape($resource_type).
"%'";
661 $sql .=
' ORDER BY resource_type';
663 dol_syslog(get_class($this).
"::getElementResources", LOG_DEBUG);
665 $resources = array();
671 $obj = $this->
db->fetch_object(
$resql);
673 $resources[$i] = array(
674 'rowid' => $obj->rowid,
675 'resource_id' => $obj->resource_id,
676 'resource_type'=>$obj->resource_type,
678 'mandatory'=>$obj->mandatory
698 foreach ($resources as $nb => $resource) {
717 if (is_array($this->cache_code_type_resource) && count($this->cache_code_type_resource)) {
721 $sql =
"SELECT rowid, code, label, active";
722 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_type_resource";
723 $sql .=
" WHERE active > 0";
724 $sql .=
" ORDER BY rowid";
725 dol_syslog(get_class($this).
"::load_cache_code_type_resource", LOG_DEBUG);
731 $obj = $this->
db->fetch_object(
$resql);
733 $label = ($langs->trans(
"ResourceTypeShort".$obj->code) != (
"ResourceTypeShort".$obj->code) ? $langs->trans(
"ResourceTypeShort".$obj->code) : ($obj->label !=
'-' ? $obj->label :
''));
734 $this->cache_code_type_resource[$obj->rowid][
'code'] = $obj->code;
735 $this->cache_code_type_resource[$obj->rowid][
'label'] = $label;
736 $this->cache_code_type_resource[$obj->rowid][
'active'] = $obj->active;
757 public function getNomUrl($withpicto = 0, $option =
'', $get_params =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
759 global $conf, $langs, $hookmanager;
762 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Resource").
'</u>';
764 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
768 if (isset($this->type_label)) {
769 $label .=
'<br><b>'.$langs->trans(
"ResourceType").
":</b> ".$this->type_label;
772 $url = DOL_URL_ROOT.
'/resource/card.php?id='.$this->id;
774 if ($option !=
'nolink') {
776 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
777 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
778 $add_save_lastsearch_values = 1;
780 if ($add_save_lastsearch_values) {
781 $url .=
'&save_lastsearch_values=1';
786 if (empty($notooltip)) {
787 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
788 $label = $langs->trans(
"ShowMyObject");
789 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
791 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
792 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
794 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
797 $linkstart =
'<a href="'.$url.$get_params.
'"';
798 $linkstart .= $linkclose.
'>';
803 $result .= $linkstart;
805 $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);
807 if ($withpicto != 2) {
808 $result .= $this->ref;
813 $hookmanager->initHooks(array($this->element .
'dao'));
814 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
815 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
817 $result = $hookmanager->resPrint;
819 $result .= $hookmanager->resPrint;
833 return $this->
LibStatut($this->status, $mode);
865 $sql =
"SELECT count(r.rowid) as nb";
866 $sql .=
" FROM ".MAIN_DB_PREFIX.
"resource as r";
867 $sql .=
" WHERE r.entity IN (".getEntity(
'resource').
")";
871 while ($obj = $this->
db->fetch_object(
$resql)) {
872 $this->nb[
"dolresource"] = $obj->nb;
878 $this->error = $this->
db->error();