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