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