dolibarr  13.0.2
dolresource.class.php
Go to the documentation of this file.
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 
24 require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
25 require_once DOL_DOCUMENT_ROOT."/core/lib/functions2.lib.php";
26 
31 {
35  public $element = 'dolresource';
36 
40  public $table_element = 'resource';
41 
45  public $picto = 'resource';
46 
47  public $resource_id;
48  public $resource_type;
49  public $element_id;
50  public $element_type;
51  public $busy;
52  public $mandatory;
53 
57  public $fk_user_create;
58 
59  public $type_label;
60  public $tms = '';
61 
62  public $cache_code_type_resource = array();
63 
64  public $oldcopy;
65 
71  public function __construct($db)
72  {
73  $this->db = $db;
74  }
75 
83  public function create($user, $notrigger = 0)
84  {
85  global $conf, $langs, $hookmanager;
86  $error = 0;
87 
88  // Clean parameters
89 
90  if (isset($this->ref)) $this->ref = trim($this->ref);
91  if (isset($this->description)) $this->description = trim($this->description);
92  if (!is_numeric($this->country_id)) $this->country_id = 0;
93  if (isset($this->fk_code_type_resource)) $this->fk_code_type_resource = trim($this->fk_code_type_resource);
94  if (isset($this->note_public)) $this->note_public = trim($this->note_public);
95  if (isset($this->note_private)) $this->note_private = trim($this->note_private);
96 
97 
98  // Insert request
99  $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
100  $sql .= "entity,";
101  $sql .= "ref,";
102  $sql .= "description,";
103  $sql .= "fk_country,";
104  $sql .= "fk_code_type_resource,";
105  $sql .= "note_public,";
106  $sql .= "note_private";
107  $sql .= ") VALUES (";
108  $sql .= $conf->entity.", ";
109  $sql .= " ".(!isset($this->ref) ? 'NULL' : "'".$this->db->escape($this->ref)."'").",";
110  $sql .= " ".(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").",";
111  $sql .= " ".($this->country_id > 0 ? $this->country_id : 'null').",";
112  $sql .= " ".(!isset($this->fk_code_type_resource) ? 'NULL' : "'".$this->db->escape($this->fk_code_type_resource)."'").",";
113  $sql .= " ".(!isset($this->note_public) ? 'NULL' : "'".$this->db->escape($this->note_public)."'").",";
114  $sql .= " ".(!isset($this->note_private) ? 'NULL' : "'".$this->db->escape($this->note_private)."'");
115  $sql .= ")";
116 
117  $this->db->begin();
118 
119  dol_syslog(get_class($this)."::create", LOG_DEBUG);
120  $resql = $this->db->query($sql);
121  if (!$resql) {
122  $error++; $this->errors[] = "Error ".$this->db->lasterror();
123  }
124 
125  if (!$error)
126  {
127  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
128  }
129 
130  if (!$error)
131  {
132  $action = 'create';
133 
134  // Actions on extra fields
135  if (!$error)
136  {
137  $result = $this->insertExtraFields();
138  if ($result < 0)
139  {
140  $error++;
141  }
142  }
143  }
144 
145  if (!$error && !$notrigger)
146  {
147  // Call trigger
148  $result = $this->call_trigger('RESOURCE_CREATE', $user);
149  if ($result < 0) $error++;
150  // End call triggers
151  }
152 
153  // Commit or rollback
154  if ($error)
155  {
156  foreach ($this->errors as $errmsg)
157  {
158  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
159  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
160  }
161  $this->db->rollback();
162  return -1 * $error;
163  } else {
164  $this->db->commit();
165  return $this->id;
166  }
167  }
168 
176  public function fetch($id, $ref = '')
177  {
178  global $langs;
179  $sql = "SELECT";
180  $sql .= " t.rowid,";
181  $sql .= " t.entity,";
182  $sql .= " t.ref,";
183  $sql .= " t.description,";
184  $sql .= " t.fk_country,";
185  $sql .= " t.fk_code_type_resource,";
186  $sql .= " t.note_public,";
187  $sql .= " t.note_private,";
188  $sql .= " t.tms,";
189  $sql .= " ty.label as type_label";
190  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
191  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_resource as ty ON ty.code=t.fk_code_type_resource";
192  if ($id) $sql .= " WHERE t.rowid = ".$this->db->escape($id);
193  else $sql .= " WHERE t.ref = '".$this->db->escape($ref)."'";
194 
195  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
196  $resql = $this->db->query($sql);
197  if ($resql)
198  {
199  if ($this->db->num_rows($resql))
200  {
201  $obj = $this->db->fetch_object($resql);
202 
203  $this->id = $obj->rowid;
204  $this->entity = $obj->entity;
205  $this->ref = $obj->ref;
206  $this->description = $obj->description;
207  $this->country_id = $obj->fk_country;
208  $this->fk_code_type_resource = $obj->fk_code_type_resource;
209  $this->note_public = $obj->note_public;
210  $this->note_private = $obj->note_private;
211  $this->type_label = $obj->type_label;
212 
213  // Retrieve all extrafield
214  // fetch optionals attributes and labels
215  $this->fetch_optionals();
216  }
217  $this->db->free($resql);
218 
219  return $this->id;
220  } else {
221  $this->error = "Error ".$this->db->lasterror();
222  dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
223  return -1;
224  }
225  }
226 
227 
235  public function update($user = null, $notrigger = 0)
236  {
237  global $conf, $langs, $hookmanager;
238  $error = 0;
239 
240  // Clean parameters
241  if (isset($this->ref)) $this->ref = trim($this->ref);
242  if (isset($this->fk_code_type_resource)) $this->fk_code_type_resource = trim($this->fk_code_type_resource);
243  if (isset($this->description)) $this->description = trim($this->description);
244  if (!is_numeric($this->country_id)) $this->country_id = 0;
245 
246  if (empty($this->oldcopy))
247  {
248  $org = new self($this->db);
249  $org->fetch($this->id);
250  $this->oldcopy = $org;
251  }
252 
253  // Update request
254  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
255  $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
256  $sql .= " description=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
257  $sql .= " fk_country=".($this->country_id > 0 ? $this->country_id : "null").",";
258  $sql .= " fk_code_type_resource=".(isset($this->fk_code_type_resource) ? "'".$this->db->escape($this->fk_code_type_resource)."'" : "null").",";
259  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null')."";
260  $sql .= " WHERE rowid=".$this->id;
261 
262  $this->db->begin();
263 
264  dol_syslog(get_class($this)."::update", LOG_DEBUG);
265  $resql = $this->db->query($sql);
266  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
267 
268  if (!$error)
269  {
270  if (!$notrigger)
271  {
272  // Call trigger
273  $result = $this->call_trigger('RESOURCE_MODIFY', $user);
274  if ($result < 0) $error++;
275  // End call triggers
276  }
277  }
278 
279  if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref))
280  {
281  // We remove directory
282  if (!empty($conf->resource->dir_output))
283  {
284  $olddir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->oldcopy->ref);
285  $newdir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->ref);
286  if (file_exists($olddir))
287  {
288  $res = @rename($olddir, $newdir);
289  if (!$res)
290  {
291  $langs->load("errors");
292  $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir);
293  $error++;
294  }
295  }
296  }
297  }
298 
299  if (!$error)
300  {
301  $action = 'update';
302 
303  // Actions on extra fields
304  if (!$error)
305  {
306  $result = $this->insertExtraFields();
307  if ($result < 0)
308  {
309  $error++;
310  }
311  }
312  }
313 
314  // Commit or rollback
315  if ($error)
316  {
317  foreach ($this->errors as $errmsg)
318  {
319  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
320  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
321  }
322  $this->db->rollback();
323  return -1 * $error;
324  } else {
325  $this->db->commit();
326  return 1;
327  }
328  }
329 
330  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
337  public function fetch_element_resource($id)
338  {
339  // phpcs:enable
340  global $langs;
341  $sql = "SELECT";
342  $sql .= " t.rowid,";
343  $sql .= " t.resource_id,";
344  $sql .= " t.resource_type,";
345  $sql .= " t.element_id,";
346  $sql .= " t.element_type,";
347  $sql .= " t.busy,";
348  $sql .= " t.mandatory,";
349  $sql .= " t.fk_user_create,";
350  $sql .= " t.tms";
351  $sql .= " FROM ".MAIN_DB_PREFIX."element_resources as t";
352  $sql .= " WHERE t.rowid = ".$this->db->escape($id);
353 
354  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
355  $resql = $this->db->query($sql);
356  if ($resql)
357  {
358  if ($this->db->num_rows($resql))
359  {
360  $obj = $this->db->fetch_object($resql);
361 
362  $this->id = $obj->rowid;
363  $this->resource_id = $obj->resource_id;
364  $this->resource_type = $obj->resource_type;
365  $this->element_id = $obj->element_id;
366  $this->element_type = $obj->element_type;
367  $this->busy = $obj->busy;
368  $this->mandatory = $obj->mandatory;
369  $this->fk_user_create = $obj->fk_user_create;
370 
371  if ($obj->resource_id && $obj->resource_type) {
372  $this->objresource = fetchObjectByElement($obj->resource_id, $obj->resource_type);
373  }
374  if ($obj->element_id && $obj->element_type) {
375  $this->objelement = fetchObjectByElement($obj->element_id, $obj->element_type);
376  }
377  }
378  $this->db->free($resql);
379 
380  return $this->id;
381  } else {
382  $this->error = "Error ".$this->db->lasterror();
383  return -1;
384  }
385  }
386 
394  public function delete($rowid, $notrigger = 0)
395  {
396  global $user, $langs, $conf;
397  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
398 
399  $error = 0;
400 
401  $this->db->begin();
402 
403  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
404  $sql .= " WHERE rowid =".$rowid;
405 
406  dol_syslog(get_class($this), LOG_DEBUG);
407  if ($this->db->query($sql))
408  {
409  $sql = "DELETE FROM ".MAIN_DB_PREFIX."element_resources";
410  $sql .= " WHERE element_type='resource' AND resource_id =".$this->db->escape($rowid);
411  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
412  $resql = $this->db->query($sql);
413  if (!$resql)
414  {
415  $this->error = $this->db->lasterror();
416  $error++;
417  }
418  } else {
419  $this->error = $this->db->lasterror();
420  $error++;
421  }
422 
423  // Removed extrafields
424  if (!$error) {
425  $result = $this->deleteExtraFields();
426  if ($result < 0)
427  {
428  $error++;
429  dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
430  }
431  }
432 
433  if (!$notrigger)
434  {
435  // Call trigger
436  $result = $this->call_trigger('RESOURCE_DELETE', $user);
437  if ($result < 0) $error++;
438  // End call triggers
439  }
440 
441  if (!$error)
442  {
443  // We remove directory
444  $ref = dol_sanitizeFileName($this->ref);
445  if (!empty($conf->resource->dir_output))
446  {
447  $dir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->ref);
448  if (file_exists($dir))
449  {
450  $res = @dol_delete_dir_recursive($dir);
451  if (!$res)
452  {
453  $this->errors[] = 'ErrorFailToDeleteDir';
454  $error++;
455  }
456  }
457  }
458  }
459 
460  if (!$error)
461  {
462  $this->db->commit();
463  return 1;
464  } else {
465  $this->db->rollback();
466  return -1;
467  }
468  }
469 
470  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
481  public function fetch_all($sortorder, $sortfield, $limit, $offset, $filter = '')
482  {
483  // phpcs:enable
484  global $conf;
485 
486  require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
487  $extrafields = new ExtraFields($this->db);
488 
489  $sql = "SELECT ";
490  $sql .= " t.rowid,";
491  $sql .= " t.entity,";
492  $sql .= " t.ref,";
493  $sql .= " t.description,";
494  $sql .= " t.fk_code_type_resource,";
495  $sql .= " t.tms,";
496  // Add fields from extrafields
497  if (!empty($extrafields->attributes[$this->table_element]['label']))
498  foreach ($extrafields->attributes[$this->table_element]['label'] as $key => $val) $sql .= ($extrafields->attributes[$this->table_element]['type'][$key] != 'separate' ? "ef.".$key.' as options_'.$key.', ' : '');
499  $sql .= " ty.label as type_label";
500  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
501  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_resource as ty ON ty.code=t.fk_code_type_resource";
502  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$this->table_element."_extrafields as ef ON ef.fk_object=t.rowid";
503  $sql .= " WHERE t.entity IN (".getEntity('resource').")";
504  // Manage filter
505  if (!empty($filter)) {
506  foreach ($filter as $key => $value) {
507  if (strpos($key, 'date')) {
508  $sql .= ' AND '.$key.' = \''.$this->db->idate($value).'\'';
509  } elseif (strpos($key, 'ef.') !== false) {
510  $sql .= $value;
511  } else {
512  $sql .= ' AND '.$key.' LIKE \'%'.$this->db->escape($value).'%\'';
513  }
514  }
515  }
516  $sql .= $this->db->order($sortfield, $sortorder);
517  $this->num_all = 0;
518  if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
519  {
520  $result = $this->db->query($sql);
521  $this->num_all = $this->db->num_rows($result);
522  }
523  if ($limit) $sql .= $this->db->plimit($limit, $offset);
524  dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
525 
526  $this->lines = array();
527  $resql = $this->db->query($sql);
528  if ($resql)
529  {
530  $num = $this->db->num_rows($resql);
531  if ($num)
532  {
533  while ($obj = $this->db->fetch_object($resql))
534  {
535  $line = new Dolresource($this->db);
536  $line->id = $obj->rowid;
537  $line->ref = $obj->ref;
538  $line->description = $obj->description;
539  $line->country_id = $obj->fk_country;
540  $line->fk_code_type_resource = $obj->fk_code_type_resource;
541  $line->type_label = $obj->type_label;
542 
543  // fetch optionals attributes and labels
544 
545  $line->fetch_optionals();
546 
547  $this->lines[] = $line;
548  }
549  $this->db->free($resql);
550  }
551  return $num;
552  } else {
553  $this->error = $this->db->lasterror();
554  return -1;
555  }
556  }
557 
558  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
569  public function fetch_all_resources($sortorder, $sortfield, $limit, $offset, $filter = '')
570  {
571  // phpcs:enable
572  global $conf;
573  $sql = "SELECT ";
574  $sql .= " t.rowid,";
575  $sql .= " t.resource_id,";
576  $sql .= " t.resource_type,";
577  $sql .= " t.element_id,";
578  $sql .= " t.element_type,";
579  $sql .= " t.busy,";
580  $sql .= " t.mandatory,";
581  $sql .= " t.fk_user_create,";
582  $sql .= " t.tms";
583  $sql .= ' FROM '.MAIN_DB_PREFIX.'element_resources as t ';
584  $sql .= " WHERE t.entity IN (".getEntity('resource').")";
585 
586  //Manage filter
587  if (!empty($filter)) {
588  foreach ($filter as $key => $value) {
589  if (strpos($key, 'date')) {
590  $sql .= ' AND '.$key.' = \''.$this->db->idate($value).'\'';
591  } else {
592  $sql .= ' AND '.$key.' LIKE \'%'.$this->db->escape($value).'%\'';
593  }
594  }
595  }
596  $sql .= $this->db->order($sortfield, $sortorder);
597  if ($limit) $sql .= $this->db->plimit($limit + 1, $offset);
598  dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
599 
600  $resql = $this->db->query($sql);
601  if ($resql)
602  {
603  $num = $this->db->num_rows($resql);
604  if ($num)
605  {
606  while ($obj = $this->db->fetch_object($resql))
607  {
608  $line = new Dolresource($this->db);
609  $line->id = $obj->rowid;
610  $line->resource_id = $obj->resource_id;
611  $line->resource_type = $obj->resource_type;
612  $line->element_id = $obj->element_id;
613  $line->element_type = $obj->element_type;
614  $line->busy = $obj->busy;
615  $line->mandatory = $obj->mandatory;
616  $line->fk_user_create = $obj->fk_user_create;
617 
618  if ($obj->resource_id && $obj->resource_type)
619  $line->objresource = fetchObjectByElement($obj->resource_id, $obj->resource_type);
620  if ($obj->element_id && $obj->element_type)
621  $line->objelement = fetchObjectByElement($obj->element_id, $obj->element_type);
622  $this->lines[] = $line;
623  }
624  $this->db->free($resql);
625  }
626  return $num;
627  } else {
628  $this->error = $this->db->lasterror();
629  return -1;
630  }
631  }
632 
633  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
644  public function fetch_all_used($sortorder, $sortfield, $limit, $offset = 1, $filter = '')
645  {
646  // phpcs:enable
647  global $conf;
648 
649  if (!$sortorder) $sortorder = "ASC";
650  if (!$sortfield) $sortfield = "t.rowid";
651 
652  $sql = "SELECT ";
653  $sql .= " t.rowid,";
654  $sql .= " t.resource_id,";
655  $sql .= " t.resource_type,";
656  $sql .= " t.element_id,";
657  $sql .= " t.element_type,";
658  $sql .= " t.busy,";
659  $sql .= " t.mandatory,";
660  $sql .= " t.fk_user_create,";
661  $sql .= " t.tms";
662  $sql .= ' FROM '.MAIN_DB_PREFIX.'element_resources as t ';
663  $sql .= " WHERE t.entity IN (".getEntity('resource').")";
664 
665  //Manage filter
666  if (!empty($filter)) {
667  foreach ($filter as $key => $value) {
668  if (strpos($key, 'date')) {
669  $sql .= ' AND '.$key.' = \''.$this->db->idate($value).'\'';
670  } else {
671  $sql .= ' AND '.$key.' LIKE \'%'.$this->db->escape($value).'%\'';
672  }
673  }
674  }
675  $sql .= $this->db->order($sortfield, $sortorder);
676  if ($limit) $sql .= $this->db->plimit($limit + 1, $offset);
677  dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
678 
679  $resql = $this->db->query($sql);
680  if ($resql)
681  {
682  $num = $this->db->num_rows($resql);
683  if ($num)
684  {
685  $this->lines = array();
686  while ($obj = $this->db->fetch_object($resql))
687  {
688  $line = new Dolresource($this->db);
689  $line->id = $obj->rowid;
690  $line->resource_id = $obj->resource_id;
691  $line->resource_type = $obj->resource_type;
692  $line->element_id = $obj->element_id;
693  $line->element_type = $obj->element_type;
694  $line->busy = $obj->busy;
695  $line->mandatory = $obj->mandatory;
696  $line->fk_user_create = $obj->fk_user_create;
697 
698  $this->lines[] = fetchObjectByElement($obj->resource_id, $obj->resource_type);
699  }
700  $this->db->free($resql);
701  }
702  return $num;
703  } else {
704  $this->error = $this->db->lasterror();
705  return -1;
706  }
707  }
708 
709  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
718  public function fetch_all_available()
719  {
720  // phpcs:enable
721  global $conf;
722 
723  if (!empty($conf->modules_parts['resources']))
724  {
725  $this->available_resources = (array) $conf->modules_parts['resources'];
726 
727  return count($this->available_resources);
728  }
729  return 0;
730  }
731 
732  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
740  public function update_element_resource($user = null, $notrigger = 0)
741  {
742  // phpcs:enable
743  global $conf, $langs;
744  $error = 0;
745 
746  // Clean parameters
747  if (isset($this->resource_id)) $this->resource_id = trim($this->resource_id);
748  if (isset($this->resource_type)) $this->resource_type = trim($this->resource_type);
749  if (isset($this->element_id)) $this->element_id = trim($this->element_id);
750  if (isset($this->element_type)) $this->element_type = trim($this->element_type);
751  if (isset($this->busy)) $this->busy = trim($this->busy);
752  if (isset($this->mandatory)) $this->mandatory = trim($this->mandatory);
753 
754  // Update request
755  $sql = "UPDATE ".MAIN_DB_PREFIX."element_resources SET";
756  $sql .= " resource_id=".(isset($this->resource_id) ? "'".$this->db->escape($this->resource_id)."'" : "null").",";
757  $sql .= " resource_type=".(isset($this->resource_type) ? "'".$this->db->escape($this->resource_type)."'" : "null").",";
758  $sql .= " element_id=".(isset($this->element_id) ? $this->element_id : "null").",";
759  $sql .= " element_type=".(isset($this->element_type) ? "'".$this->db->escape($this->element_type)."'" : "null").",";
760  $sql .= " busy=".(isset($this->busy) ? $this->busy : "null").",";
761  $sql .= " mandatory=".(isset($this->mandatory) ? $this->mandatory : "null").",";
762  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null')."";
763 
764  $sql .= " WHERE rowid=".$this->id;
765 
766  $this->db->begin();
767 
768  dol_syslog(get_class($this)."::update", LOG_DEBUG);
769  $resql = $this->db->query($sql);
770  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
771 
772  if (!$error)
773  {
774  if (!$notrigger)
775  {
776  // Call trigger
777  $result = $this->call_trigger('RESOURCE_MODIFY', $user);
778  if ($result < 0) $error++;
779  // End call triggers
780  }
781  }
782 
783  // Commit or rollback
784  if ($error)
785  {
786  foreach ($this->errors as $errmsg)
787  {
788  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
789  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
790  }
791  $this->db->rollback();
792  return -1 * $error;
793  } else {
794  $this->db->commit();
795  return 1;
796  }
797  }
798 
799 
808  public function getElementResources($element, $element_id, $resource_type = '')
809  {
810  $resources = array();
811 
812  // Links beetween objects are stored in this table
813  $sql = 'SELECT rowid, resource_id, resource_type, busy, mandatory';
814  $sql .= ' FROM '.MAIN_DB_PREFIX.'element_resources';
815  $sql .= " WHERE element_id=".$element_id." AND element_type='".$this->db->escape($element)."'";
816  if ($resource_type)
817  $sql .= " AND resource_type LIKE '%".$this->db->escape($resource_type)."%'";
818  $sql .= ' ORDER BY resource_type';
819 
820  dol_syslog(get_class($this)."::getElementResources", LOG_DEBUG);
821 
822  $resources = array();
823  $resql = $this->db->query($sql);
824  if ($resql)
825  {
826  $num = $this->db->num_rows($resql);
827  $i = 0;
828  while ($i < $num)
829  {
830  $obj = $this->db->fetch_object($resql);
831 
832  $resources[$i] = array(
833  'rowid' => $obj->rowid,
834  'resource_id' => $obj->resource_id,
835  'resource_type'=>$obj->resource_type,
836  'busy'=>$obj->busy,
837  'mandatory'=>$obj->mandatory
838  );
839  $i++;
840  }
841  }
842 
843  return $resources;
844  }
845 
853  public function fetchElementResources($element, $element_id)
854  {
855  $resources = $this->getElementResources($element, $element_id);
856  $i = 0;
857  foreach ($resources as $nb => $resource) {
858  $this->lines[$i] = fetchObjectByElement($resource['resource_id'], $resource['resource_type']);
859  $i++;
860  }
861  return $i;
862  }
863 
864 
865  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
872  {
873  // phpcs:enable
874  global $langs;
875 
876  if (is_array($this->cache_code_type_resource) && count($this->cache_code_type_resource)) return 0; // Cache deja charge
877 
878  $sql = "SELECT rowid, code, label, active";
879  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_resource";
880  $sql .= " WHERE active > 0";
881  $sql .= " ORDER BY rowid";
882  dol_syslog(get_class($this)."::load_cache_code_type_resource", LOG_DEBUG);
883  $resql = $this->db->query($sql);
884  if ($resql)
885  {
886  $num = $this->db->num_rows($resql);
887  $i = 0;
888  while ($i < $num)
889  {
890  $obj = $this->db->fetch_object($resql);
891  // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
892  $label = ($langs->trans("ResourceTypeShort".$obj->code) != ("ResourceTypeShort".$obj->code) ? $langs->trans("ResourceTypeShort".$obj->code) : ($obj->label != '-' ? $obj->label : ''));
893  $this->cache_code_type_resource[$obj->rowid]['code'] = $obj->code;
894  $this->cache_code_type_resource[$obj->rowid]['label'] = $label;
895  $this->cache_code_type_resource[$obj->rowid]['active'] = $obj->active;
896  $i++;
897  }
898  return $num;
899  } else {
900  dol_print_error($this->db);
901  return -1;
902  }
903  }
904 
916  public function getNomUrl($withpicto = 0, $option = '', $get_params = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
917  {
918  global $conf, $langs;
919 
920  $result = '';
921  $label = img_picto('', $this->picto).' <u>'.$langs->trans("Resource").'</u>';
922  $label .= '<br>';
923  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
924  /*if (isset($this->status)) {
925  $label.= '<br><b>' . $langs->trans("Status").":</b> ".$this->getLibStatut(5);
926  }*/
927  if (isset($this->type_label)) {
928  $label .= '<br><b>'.$langs->trans("ResourceType").":</b> ".$this->type_label;
929  }
930 
931  $url = DOL_URL_ROOT.'/resource/card.php?id='.$this->id;
932 
933  if ($option != 'nolink')
934  {
935  // Add param to save lastsearch_values or not
936  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
937  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
938  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
939  }
940 
941  $linkclose = '';
942  if (empty($notooltip))
943  {
944  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
945  {
946  $label = $langs->trans("ShowMyObject");
947  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
948  }
949  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
950  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
951  } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
952 
953  $linkstart = '<a href="'.$url.$get_params.'"';
954  $linkstart .= $linkclose.'>';
955  $linkend = '</a>';
956  /*$linkstart = '<a href="'.dol_buildpath('/resource/card.php', 1).'?id='.$this->id.$get_params.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
957  $linkend = '</a>';*/
958 
959  $result .= $linkstart;
960  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);
961  if ($withpicto != 2) $result .= $this->ref;
962  $result .= $linkend;
963 
964  return $result;
965  }
966 
967 
974  public function getLibStatut($mode = 0)
975  {
976  return $this->LibStatut($this->status, $mode);
977  }
978 
979  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
987  public static function LibStatut($status, $mode = 0)
988  {
989  // phpcs:enable
990  global $langs;
991 
992  return '';
993  }
994 }
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:750
static LibStatut($status, $mode=0)
Return the status.
create($user, $notrigger=0)
Create object into database.
fetch_all_available()
Fetch all resources available, declared by modules Load available resource in array $this-&gt;available_...
$conf db
API class for accounts.
Definition: inc.php:54
fetch_all_resources($sortorder, $sortfield, $limit, $offset, $filter= '')
Load all objects into $this-&gt;lines.
insertExtraFields($trigger= '', $userused=null)
Add/Update all extra fields values for the current object.
fetch_element_resource($id)
Load object in memory from database.
Class to manage standard extra fields.
fetch_all_used($sortorder, $sortfield, $limit, $offset=1, $filter= '')
Load all objects into $this-&gt;lines.
update_element_resource($user=null, $notrigger=0)
Update element resource into database.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
getNomUrl($withpicto=0, $option= '', $get_params= '', $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Return clicable link of object (with eventually picto)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getLibStatut($mode=0)
Retourne le libelle du status d&#39;un user (actif, inactif)
fetchElementResources($element, $element_id)
Return an int number of resources linked to the element.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories) ...
Definition: files.lib.php:1286
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
__construct($db)
Constructor.
deleteExtraFields()
Delete all extra fields values for the current object.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this-&gt;array_options This method is in most cases call...
fetch_all($sortorder, $sortfield, $limit, $offset, $filter= '')
Load resource objects into $this-&gt;lines.
print $_SERVER["PHP_SELF"]
Edit parameters.
call_trigger($triggerName, $user)
Call trigger based on this instance.
load_cache_code_type_resource()
Load in cache resource type code (setup in dictionary)
fetch($id, $ref= '')
Load object in memory from database.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getElementResources($element, $element_id, $resource_type= '')
Return an array with resources linked to the element.
fetchObjectByElement($element_id, $element_type, $element_ref= '')
Fetch an object from its id and element_type Inclusion of classes is automatic.
DAO Resource object.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
update($user=null, $notrigger=0)
Update object into database.