dolibarr  20.0.0-beta
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  * Copyright (C) 2023-2024 William Mead <william.mead@manchenumerique.fr>
4  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
26 require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
27 require_once DOL_DOCUMENT_ROOT."/core/lib/functions2.lib.php";
28 require_once DOL_DOCUMENT_ROOT.'/core/class/commonpeople.class.php';
29 
34 {
35  use CommonPeople;
36 
40  public $element = 'dolresource';
41 
45  public $table_element = 'resource';
46 
50  public $picto = 'resource';
51 
55  public $description;
56 
60  public $phone;
61 
65  public $max_users;
66 
70  public $fk_code_type_resource;
71 
72  public $type_label;
73 
80  public $resource_id;
81 
85  public $resource_type;
86 
93  public $element_id;
94 
98  public $element_type;
99 
103  public $busy;
104 
108  public $mandatory;
109 
113  public $fulldayevent;
114 
118  public $fk_user_create;
119 
123  public $objelement;
124 
128  public $cache_code_type_resource;
129 
133  public $oldcopy;
134 
135 
141  public function __construct(DoliDb $db)
142  {
143  $this->db = $db;
144  $this->status = 0;
145 
146  $this->cache_code_type_resource = array();
147  }
148 
156  public function create(User $user, int $no_trigger = 0)
157  {
158  $error = 0;
159  $this->date_creation = dol_now();
160 
161  // Clean parameters
162  $new_resource_values = [
163  $this->ref,
164  $this->address,
165  $this->zip,
166  $this->town,
167  $this->country_id,
168  $this->state_id,
169  $this->description,
170  $this->phone,
171  $this->email,
172  $this->max_users,
173  $this->url,
174  $this->fk_code_type_resource,
175  $this->note_public,
176  $this->note_private,
177  ];
178  foreach ($new_resource_values as $key => $value) {
179  if (isset($value)) {
180  $new_resource_values[$key] = trim($value);
181  }
182  }
183 
184  // Insert request
185  $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
186  $sql .= "entity,";
187  $sql .= "ref,";
188  $sql .= "address,";
189  $sql .= "zip,";
190  $sql .= "town,";
191  $sql .= "fk_country,";
192  $sql .= "fk_state,";
193  $sql .= "description,";
194  $sql .= "phone,";
195  $sql .= "email,";
196  $sql .= "max_users,";
197  $sql .= "url,";
198  $sql .= "fk_code_type_resource,";
199  $sql .= "note_public,";
200  $sql .= "note_private, ";
201  $sql .= "datec, ";
202  $sql .= "fk_user_author ";
203  $sql .= ") VALUES (";
204  $sql .= getEntity('resource') . ", ";
205  foreach ($new_resource_values as $value) {
206  $sql .= " " . ((isset($value) && $value > 0) ? "'" . $this->db->escape($value) . "'" : 'NULL') . ",";
207  }
208  $sql .= " '" . $this->db->idate($this->date_creation) . "',";
209  $sql .= " " . (!empty($user->id) ? ((int) $user->id) : "null");
210  $sql .= ")";
211 
212  // Database session
213  $this->db->begin();
214  try {
215  dol_syslog(get_class($this) . "::create", LOG_DEBUG);
216  } catch (Exception $exception) {
217  error_log('dol_syslog error: ' . $exception->getMessage());
218  }
219  $resql = $this->db->query($sql);
220  if (!$resql) {
221  $error++;
222  $this->errors[] = "Error ".$this->db->lasterror();
223  }
224 
225  if (!$error) {
226  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
227  $result = $this->insertExtraFields();
228  if ($result < 0) {
229  $error = -1;
230  }
231  }
232 
233  if (!$error && !$no_trigger) {
234  $result = $this->call_trigger('RESOURCE_CREATE', $user);
235  if ($result < 0) {
236  $error = -1;
237  }
238  }
239 
240  // Commit or rollback
241  if ($error) {
242  foreach ($this->errors as $errmsg) {
243  try {
244  dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
245  } catch (Exception $exception) {
246  error_log('dol_syslog error: ' . $exception->getMessage());
247  }
248  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
249  }
250  $this->db->rollback();
251  return $error;
252  } else {
253  $this->db->commit();
254  return $this->id;
255  }
256  }
257 
265  public function fetch(int $id, string $ref = '')
266  {
267  $sql = "SELECT";
268  $sql .= " t.rowid,";
269  $sql .= " t.entity,";
270  $sql .= " t.ref,";
271  $sql .= " t.address,";
272  $sql .= " t.zip,";
273  $sql .= " t.town,";
274  $sql .= " t.fk_country,";
275  $sql .= " t.fk_state,";
276  $sql .= " t.description,";
277  $sql .= " t.phone,";
278  $sql .= " t.email,";
279  $sql .= " t.max_users,";
280  $sql .= " t.url,";
281  $sql .= " t.fk_code_type_resource,";
282  $sql .= " t.note_public,";
283  $sql .= " t.note_private,";
284  $sql .= " t.tms as date_modification,";
285  $sql .= " t.datec as date_creation,";
286  $sql .= " ty.label as type_label";
287  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
288  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_resource as ty ON ty.code=t.fk_code_type_resource";
289  if ($id) {
290  $sql .= " WHERE t.rowid = ".($id);
291  } else {
292  $sql .= " WHERE t.ref = '".$this->db->escape($ref)."'";
293  }
294 
295  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
296  $resql = $this->db->query($sql);
297  if ($resql) {
298  if ($this->db->num_rows($resql)) {
299  $obj = $this->db->fetch_object($resql);
300 
301  $this->id = $obj->rowid;
302  $this->entity = $obj->entity;
303  $this->ref = $obj->ref;
304  $this->address = $obj->address;
305  $this->zip = $obj->zip;
306  $this->town = $obj->town;
307  $this->country_id = $obj->fk_country;
308  $this->state_id = $obj->fk_state;
309  $this->description = $obj->description;
310  $this->phone = $obj->phone;
311  $this->email = $obj->email;
312  $this->max_users = $obj->max_users;
313  $this->url = $obj->url;
314  $this->fk_code_type_resource = $obj->fk_code_type_resource;
315  $this->note_public = $obj->note_public;
316  $this->note_private = $obj->note_private;
317  $this->date_creation = $this->db->jdate($obj->date_creation);
318  $this->date_modification = $this->db->jdate($obj->date_modification);
319  $this->type_label = $obj->type_label;
320 
321  // Retrieve all extrafield
322  // fetch optionals attributes and labels
323  $this->fetch_optionals();
324  }
325  $this->db->free($resql);
326 
327  return $this->id;
328  } else {
329  $this->error = "Error ".$this->db->lasterror();
330  dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
331  return -1;
332  }
333  }
334 
335 
343  public function update(User $user = null, int $notrigger = 0)
344  {
345  global $conf, $langs;
346  $error = 0;
347  $this->date_modification = dol_now();
348 
349  // Clean parameters
350  if (isset($this->ref)) {
351  $this->ref = trim($this->ref);
352  }
353  if (isset($this->address)) {
354  $this->address = trim($this->address);
355  }
356  if (isset($this->zip)) {
357  $this->zip = trim($this->zip);
358  }
359  if (isset($this->town)) {
360  $this->town = trim($this->town);
361  }
362  if (!is_numeric($this->country_id)) {
363  $this->country_id = 0;
364  }
365  if (!is_numeric($this->state_id)) {
366  $this->state_id = 0;
367  }
368  if (isset($this->description)) {
369  $this->description = trim($this->description);
370  }
371  if (isset($this->phone)) {
372  $this->phone = trim($this->phone);
373  }
374  if (isset($this->email)) {
375  $this->email = trim($this->email);
376  }
377  if (!is_numeric($this->max_users)) {
378  $this->max_users = 0;
379  }
380  if (isset($this->url)) {
381  $this->url = trim($this->url);
382  }
383  if (isset($this->fk_code_type_resource)) {
384  $this->fk_code_type_resource = trim($this->fk_code_type_resource);
385  }
386 
387  // $this->oldcopy should have been set by the caller of update (here properties were already modified)
388  if (empty($this->oldcopy)) {
389  $this->oldcopy = dol_clone($this, 2);
390  }
391 
392  // Update request
393  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
394  $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").",";
395  $sql .= " address=".(isset($this->address) ? "'".$this->db->escape($this->address)."'" : "null").",";
396  $sql .= " zip=".(isset($this->zip) ? "'".$this->db->escape($this->zip)."'" : "null").",";
397  $sql .= " town=".(isset($this->town) ? "'".$this->db->escape($this->town)."'" : "null").",";
398  $sql .= " fk_country=".($this->country_id > 0 ? (int) $this->country_id : "null").",";
399  $sql .= " fk_state=".($this->state_id > 0 ? (int) $this->state_id : "null").",";
400  $sql .= " description=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
401  $sql .= " phone=".(isset($this->phone) ? "'".$this->db->escape($this->phone)."'" : "null").",";
402  $sql .= " email=".(isset($this->email) ? "'".$this->db->escape($this->email)."'" : "null").",";
403  $sql .= " max_users=".(isset($this->max_users) ? (int) $this->max_users : "null").",";
404  $sql .= " url=".(isset($this->url) ? "'".$this->db->escape($this->url)."'" : "null").",";
405  $sql .= " fk_code_type_resource=".(isset($this->fk_code_type_resource) ? "'".$this->db->escape($this->fk_code_type_resource)."'" : "null").",";
406  $sql .= " tms=" . ("'" . $this->db->idate($this->date_modification) . "',");
407  $sql .= " fk_user_modif=" . (!empty($user->id) ? ((int) $user->id) : "null");
408  $sql .= " WHERE rowid=".((int) $this->id);
409 
410  $this->db->begin();
411 
412  dol_syslog(get_class($this)."::update", LOG_DEBUG);
413  $resql = $this->db->query($sql);
414  if (!$resql) {
415  $error++;
416  $this->errors[] = "Error ".$this->db->lasterror();
417  }
418 
419  if (!$error) {
420  if (!$notrigger) {
421  // Call trigger
422  $result = $this->call_trigger('RESOURCE_MODIFY', $user);
423  if ($result < 0) {
424  $error++;
425  }
426  // End call triggers
427  }
428  }
429 
430  if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) {
431  // We remove directory
432  if (!empty($conf->resource->dir_output)) {
433  $olddir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->oldcopy->ref);
434  $newdir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->ref);
435  if (file_exists($olddir)) {
436  $res = @rename($olddir, $newdir);
437  if (!$res) {
438  $langs->load("errors");
439  $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir);
440  $error++;
441  }
442  }
443  }
444  }
445 
446  if (!$error) {
447  // Actions on extra fields
448  $result = $this->insertExtraFields();
449  if ($result < 0) {
450  $error++;
451  }
452  }
453 
454  // Commit or rollback
455  if ($error) {
456  foreach ($this->errors as $errmsg) {
457  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
458  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
459  }
460  $this->db->rollback();
461  return -1 * $error;
462  } else {
463  $this->db->commit();
464  return 1;
465  }
466  }
467 
474  public function fetchElementResource(int $id)
475  {
476  $sql = "SELECT";
477  $sql .= " t.rowid,";
478  $sql .= " t.resource_id,";
479  $sql .= " t.resource_type,";
480  $sql .= " t.element_id,";
481  $sql .= " t.element_type,";
482  $sql .= " t.busy,";
483  $sql .= " t.mandatory,";
484  $sql .= " t.fk_user_create,";
485  $sql .= " t.tms as date_modification";
486  $sql .= " FROM ".MAIN_DB_PREFIX."element_resources as t";
487  $sql .= " WHERE t.rowid = ".($id);
488 
489  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
490  $resql = $this->db->query($sql);
491  if ($resql) {
492  if ($this->db->num_rows($resql)) {
493  $obj = $this->db->fetch_object($resql);
494 
495  $this->id = $obj->rowid;
496  $this->resource_id = $obj->resource_id;
497  $this->resource_type = $obj->resource_type;
498  $this->element_id = $obj->element_id;
499  $this->element_type = $obj->element_type;
500  $this->busy = $obj->busy;
501  $this->mandatory = $obj->mandatory;
502  $this->fk_user_create = $obj->fk_user_create;
503  $this->date_modification = $obj->date_modification;
504 
505  /*if ($obj->resource_id && $obj->resource_type) {
506  $this->objresource = fetchObjectByElement($obj->resource_id, $obj->resource_type);
507  }*/
508  if ($obj->element_id && $obj->element_type) {
509  $this->objelement = fetchObjectByElement($obj->element_id, $obj->element_type);
510  }
511  }
512  $this->db->free($resql);
513 
514  return $this->id;
515  } else {
516  $this->error = "Error ".$this->db->lasterror();
517  return -1;
518  }
519  }
520 
528  public function delete(User $user, int $notrigger = 0)
529  {
530  global $conf;
531 
532  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
533 
534  $rowid = $this->id;
535 
536  $error = 0;
537 
538  $this->db->begin();
539 
540  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
541  $sql .= " WHERE rowid = ".($rowid);
542 
543  dol_syslog(get_class($this), LOG_DEBUG);
544  if ($this->db->query($sql)) {
545  $sql = "DELETE FROM ".MAIN_DB_PREFIX."element_resources";
546  $sql .= " WHERE element_type='resource' AND resource_id = ".((int) $rowid);
547  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
548  $resql = $this->db->query($sql);
549  if (!$resql) {
550  $this->error = $this->db->lasterror();
551  $error++;
552  }
553  } else {
554  $this->error = $this->db->lasterror();
555  $error++;
556  }
557 
558  // Removed extrafields
559  if (!$error) {
560  $result = $this->deleteExtraFields();
561  if ($result < 0) {
562  $error++;
563  dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR);
564  }
565  }
566 
567  if (!$notrigger) {
568  // Call trigger
569  $result = $this->call_trigger('RESOURCE_DELETE', $user);
570  if ($result < 0) {
571  $error++;
572  }
573  // End call triggers
574  }
575 
576  if (!$error) {
577  // We remove directory
578  dol_sanitizeFileName($this->ref);
579  if (!empty($conf->resource->dir_output)) {
580  $dir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->ref);
581  if (file_exists($dir)) {
582  $res = @dol_delete_dir_recursive($dir);
583  if (!$res) {
584  $this->errors[] = 'ErrorFailToDeleteDir';
585  $error++;
586  }
587  }
588  }
589  }
590 
591  if (!$error) {
592  $this->db->commit();
593  return 1;
594  } else {
595  $this->db->rollback();
596  return -1;
597  }
598  }
599 
610  public function fetchAll(string $sortorder, string $sortfield, int $limit, int $offset, $filter = '')
611  {
612  require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
613  $extrafields = new ExtraFields($this->db);
614 
615  $sql = "SELECT ";
616  $sql .= " t.rowid,";
617  $sql .= " t.entity,";
618  $sql .= " t.ref,";
619  $sql .= " t.address,";
620  $sql .= " t.zip,";
621  $sql .= " t.town,";
622  $sql .= " t.fk_country,";
623  $sql .= " t.fk_state,";
624  $sql .= " t.description,";
625  $sql .= " t.phone,";
626  $sql .= " t.email,";
627  $sql .= " t.max_users,";
628  $sql .= " t.url,";
629  $sql .= " t.fk_code_type_resource,";
630  $sql .= " t.tms as date_modification,";
631  $sql .= " t.datec as date_creation,";
632  // Add fields from extrafields
633  if (!empty($extrafields->attributes[$this->table_element]) && !empty($extrafields->attributes[$this->table_element]['label'])) {
634  foreach ($extrafields->attributes[$this->table_element]['label'] as $key => $val) {
635  $sql .= ($extrafields->attributes[$this->table_element]['type'][$key] != 'separate' ? "ef.".$key." as options_".$key.', ' : '');
636  }
637  }
638  $sql .= " ty.label as type_label";
639  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
640  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_resource as ty ON ty.code=t.fk_code_type_resource";
641  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$this->table_element."_extrafields as ef ON ef.fk_object=t.rowid";
642  $sql .= " WHERE t.entity IN (".getEntity('resource').")";
643 
644  // Manage filter
645  if (is_array($filter)) {
646  foreach ($filter as $key => $value) {
647  if (strpos($key, 'date')) {
648  $sql .= " AND ".$this->db->sanitize($key)." = '".$this->db->idate($value)."'";
649  } elseif (strpos($key, 'ef.') !== false) {
650  $sql .= " AND ".$this->db->sanitize($key)." = ".((float) $value);
651  } else {
652  $sql .= " AND ".$this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
653  }
654  }
655 
656  $filter = '';
657  }
658 
659  // Manage filter
660  $errormessage = '';
661  $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
662  if ($errormessage) {
663  $this->errors[] = $errormessage;
664  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
665  return -1;
666  }
667 
668  $sql .= $this->db->order($sortfield, $sortorder);
669  if ($limit) {
670  $sql .= $this->db->plimit($limit, $offset);
671  }
672 
673  dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
674 
675  $this->lines = array();
676  $resql = $this->db->query($sql);
677  if ($resql) {
678  $num = $this->db->num_rows($resql);
679  if ($num) {
680  while ($obj = $this->db->fetch_object($resql)) {
681  $line = new Dolresource($this->db);
682  $line->id = $obj->rowid;
683  $line->ref = $obj->ref;
684  $line->address = $obj->address;
685  $line->zip = $obj->zip;
686  $line->town = $obj->town;
687  $line->country_id = $obj->fk_country;
688  $line->state_id = $obj->fk_state;
689  $line->description = $obj->description;
690  $this->phone = $obj->phone;
691  $this->email = $obj->email;
692  $this->max_users = $obj->max_users;
693  $this->url = $obj->url;
694  $line->fk_code_type_resource = $obj->fk_code_type_resource;
695  $line->date_modification = $obj->date_modification;
696  $line->date_creation = $obj->date_creation;
697  $line->type_label = $obj->type_label;
698 
699  // fetch optionals attributes and labels
700 
701  $line->fetch_optionals();
702 
703  $this->lines[] = $line;
704  }
705  $this->db->free($resql);
706  }
707  return $num;
708  } else {
709  $this->error = $this->db->lasterror();
710  return -1;
711  }
712  }
713 
721  public function updateElementResource(User $user = null, int $notrigger = 0)
722  {
723  $error = 0;
724  $this->date_modification = dol_now();
725 
726  // Clean parameters
727  if (!is_numeric($this->resource_id)) {
728  $this->resource_id = 0;
729  }
730  if (isset($this->resource_type)) {
731  $this->resource_type = trim($this->resource_type);
732  }
733  if (!is_numeric($this->element_id)) {
734  $this->element_id = 0;
735  }
736  if (isset($this->element_type)) {
737  $this->element_type = trim($this->element_type);
738  }
739  if (isset($this->busy)) {
740  $this->busy = (int) $this->busy;
741  }
742  if (isset($this->mandatory)) {
743  $this->mandatory = (int) $this->mandatory;
744  }
745 
746  // Update request
747  $sql = "UPDATE ".MAIN_DB_PREFIX."element_resources SET";
748  $sql .= " resource_id = ".(isset($this->resource_id) ? (int) $this->resource_id : "null").",";
749  $sql .= " resource_type = ".(isset($this->resource_type) ? "'".$this->db->escape($this->resource_type)."'" : "null").",";
750  $sql .= " element_id = ".(isset($this->element_id) ? (int) $this->element_id : "null").",";
751  $sql .= " element_type = ".(isset($this->element_type) ? "'".$this->db->escape($this->element_type)."'" : "null").",";
752  $sql .= " busy = ".(isset($this->busy) ? (int) $this->busy : "null").",";
753  $sql .= " mandatory = ".(isset($this->mandatory) ? (int) $this->mandatory : "null").",";
754  $sql .= " tms = ".(dol_strlen($this->date_modification) != 0 ? "'".$this->db->idate($this->date_modification)."'" : 'null');
755  $sql .= " WHERE rowid=".((int) $this->id);
756 
757  $this->db->begin();
758 
759  dol_syslog(get_class($this)."::update", LOG_DEBUG);
760  $resql = $this->db->query($sql);
761  if (!$resql) {
762  $error++;
763  $this->errors[] = "Error ".$this->db->lasterror();
764  }
765 
766  if (!$error) {
767  if (!$notrigger) {
768  // Call trigger
769  $result = $this->call_trigger('RESOURCE_MODIFY', $user);
770  if ($result < 0) {
771  $error++;
772  }
773  // End call triggers
774  }
775  }
776 
777  // Commit or rollback
778  if ($error) {
779  foreach ($this->errors as $errmsg) {
780  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
781  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
782  }
783  $this->db->rollback();
784  return -1 * $error;
785  } else {
786  $this->db->commit();
787  return 1;
788  }
789  }
790 
791 
800  public function getElementResources(string $element, int $element_id, string $resource_type = '')
801  {
802  // Links between objects are stored in this table
803  $sql = 'SELECT rowid, resource_id, resource_type, busy, mandatory';
804  $sql .= ' FROM '.MAIN_DB_PREFIX.'element_resources';
805  $sql .= " WHERE element_id=".((int) $element_id)." AND element_type='".$this->db->escape($element)."'";
806  if ($resource_type) {
807  $sql .= " AND resource_type LIKE '%".$this->db->escape($resource_type)."%'";
808  }
809  $sql .= ' ORDER BY resource_type';
810 
811  dol_syslog(get_class($this)."::getElementResources", LOG_DEBUG);
812 
813  $resources = array();
814  $resql = $this->db->query($sql);
815  if ($resql) {
816  $num = $this->db->num_rows($resql);
817  $i = 0;
818  while ($i < $num) {
819  $obj = $this->db->fetch_object($resql);
820 
821  $resources[$i] = array(
822  'rowid' => $obj->rowid,
823  'resource_id' => $obj->resource_id,
824  'resource_type' => $obj->resource_type,
825  'busy' => $obj->busy,
826  'mandatory' => $obj->mandatory
827  );
828  $i++;
829  }
830  }
831 
832  return $resources;
833  }
834 
842  public function fetchElementResources(string $elementType, int $elementId)
843  {
844  $resources = $this->getElementResources($elementType, $elementId);
845  $i = 0;
846  foreach ($resources as $resource) {
847  $this->lines[$i] = fetchObjectByElement($resource['resource_id'], $resource['resource_type']);
848  $i++;
849  }
850  return $i;
851  }
852 
858  public function loadCacheCodeTypeResource()
859  {
860  global $langs;
861 
862  if (is_array($this->cache_code_type_resource) && count($this->cache_code_type_resource)) {
863  return 0; // Cache deja charge
864  }
865 
866  $sql = "SELECT rowid, code, label, active";
867  $sql .= " FROM ".MAIN_DB_PREFIX."c_type_resource";
868  $sql .= " WHERE active > 0";
869  $sql .= " ORDER BY rowid";
870  dol_syslog(get_class($this)."::load_cache_code_type_resource", LOG_DEBUG);
871  $resql = $this->db->query($sql);
872  if ($resql) {
873  $num = $this->db->num_rows($resql);
874  $i = 0;
875  while ($i < $num) {
876  $obj = $this->db->fetch_object($resql);
877 
878  $label = ($langs->trans("ResourceTypeShort".$obj->code) != "ResourceTypeShort".$obj->code ? $langs->trans("ResourceTypeShort".$obj->code) : ($obj->label != '-' ? $obj->label : ''));
879  $this->cache_code_type_resource[$obj->rowid]['code'] = $obj->code;
880  $this->cache_code_type_resource[$obj->rowid]['label'] = $label;
881  $this->cache_code_type_resource[$obj->rowid]['active'] = $obj->active;
882  $i++;
883  }
884  return $num;
885  } else {
886  dol_print_error($this->db);
887  return -1;
888  }
889  }
890 
897  public function getTooltipContentArray($params)
898  {
899  global $langs;
900 
901  $langs->load('resource');
902 
903  $datas = [];
904 
905  $datas['picto'] = img_picto('', $this->picto).' <u>'.$langs->trans("Resource").'</u>';
906  $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
907  /*if (isset($this->status)) {
908  $datas['status'] = '<br><b>' . $langs->trans("Status").":</b> ".$this->getLibStatut(5);
909  }*/
910  if (isset($this->type_label)) {
911  $datas['label'] = '<br><b>'.$langs->trans("ResourceType").":</b> ".$this->type_label;
912  }
913 
914  return $datas;
915  }
916 
928  public function getNomUrl(int $withpicto = 0, string $option = '', string $get_params = '', int $notooltip = 0, string $morecss = '', int $save_lastsearch_value = -1)
929  {
930  global $langs, $hookmanager, $action;
931 
932  $result = '';
933  $params = [
934  'id' => $this->id,
935  'objecttype' => $this->element,
936  ];
937  $classfortooltip = 'classfortooltip';
938  $dataparams = '';
939  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
940  $classfortooltip = 'classforajaxtooltip';
941  $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
942  $label = '';
943  } else {
944  $label = implode($this->getTooltipContentArray($params));
945  }
946 
947  $url = DOL_URL_ROOT.'/resource/card.php?id='.$this->id;
948 
949  if ($option != 'nolink') {
950  // Add param to save lastsearch_values or not
951  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
952  if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
953  $add_save_lastsearch_values = 1;
954  }
955  if ($add_save_lastsearch_values) {
956  $url .= '&save_lastsearch_values=1';
957  }
958  }
959 
960  $linkclose = '';
961  if (empty($notooltip)) {
962  if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
963  $label = $langs->trans("ShowMyObject");
964  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
965  }
966  $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
967  $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
968  } else {
969  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
970  }
971 
972  $linkstart = '<a href="'.$url.$get_params.'"';
973  $linkstart .= $linkclose.'>';
974  $linkend = '</a>';
975  /*$linkstart = '<a href="'.DOL_URL_ROOT.'/resource/card.php?id='.$this->id.$get_params.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
976  $linkend = '</a>';*/
977 
978  $result .= $linkstart;
979  if ($withpicto) {
980  $result .= img_object(($notooltip ? '' : $label), ($this->picto ?: 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1);
981  }
982  if ($withpicto != 2) {
983  $result .= $this->ref;
984  }
985  $result .= $linkend;
986 
987  $hookmanager->initHooks(array($this->element . 'dao'));
988  $parameters = array('id' => $this->id, 'getnomurl' => &$result);
989  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
990  if ($reshook > 0) {
991  $result = $hookmanager->resPrint;
992  } else {
993  $result .= $hookmanager->resPrint;
994  }
995  return $result;
996  }
997 
998 
1005  public function getLibStatut(int $mode = 0)
1006  {
1007  return $this->getLibStatusLabel($this->status, $mode);
1008  }
1009 
1017  public static function getLibStatusLabel(int $status, int $mode = 0)
1018  {
1019  return '';
1020  }
1021 
1027  public function loadStateBoard()
1028  {
1029  $this->nb = array();
1030 
1031  $sql = "SELECT count(r.rowid) as nb";
1032  $sql .= " FROM ".MAIN_DB_PREFIX."resource as r";
1033  $sql .= " WHERE r.entity IN (".getEntity('resource').")";
1034 
1035  $resql = $this->db->query($sql);
1036  if ($resql) {
1037  while ($obj = $this->db->fetch_object($resql)) {
1038  $this->nb["dolresource"] = $obj->nb;
1039  }
1040  $this->db->free($resql);
1041  return 1;
1042  } else {
1043  dol_print_error($this->db);
1044  $this->error = $this->db->error();
1045  return -1;
1046  }
1047  }
1048 }
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition: security.php:607
$object ref
Definition: info.php:79
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
deleteExtraFields()
Delete all extra fields values for the current object.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
DAO Resource object.
fetchAll(string $sortorder, string $sortfield, int $limit, int $offset, $filter='')
Load resource objects into $this->lines.
loadStateBoard()
Load indicators this->nb for state board.
getNomUrl(int $withpicto=0, string $option='', string $get_params='', int $notooltip=0, string $morecss='', int $save_lastsearch_value=-1)
Return clickable link of object (with optional picto)
fetch(int $id, string $ref='')
Load object into memory from database.
$objelement
Used by fetchElementResource() to return an object.
fetchElementResources(string $elementType, int $elementId)
Return an int number of resources linked to the element.
__construct(DoliDb $db)
Constructor.
updateElementResource(User $user=null, int $notrigger=0)
Update element resource in database.
static getLibStatusLabel(int $status, int $mode=0)
Get status.
loadCacheCodeTypeResource()
Load in cache resource type code (setup in dictionary)
getTooltipContentArray($params)
getTooltipContentArray
fetchElementResource(int $id)
Load data of resource links into memory from database.
update(User $user=null, int $notrigger=0)
Update object in database.
getElementResources(string $element, int $element_id, string $resource_type='')
Return an array with resources linked to the element.
create(User $user, int $no_trigger=0)
Create object in database.
getLibStatut(int $mode=0)
Get status label.
Class to manage standard extra fields.
Class to manage Dolibarr users.
Definition: user.class.php:50
trait CommonPeople
Support class for thirdparties, contacts, members, users or resources.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
Definition: files.lib.php:1609
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
fetchObjectByElement($element_id, $element_type, $element_ref='', $useCache=0, $maxCacheByType=10)
Fetch an object from its id and element_type Inclusion of classes is automatic.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
div float
Buy price without taxes.
Definition: style.css.php:960