1<?php 2/* Copyright (C) 2013-2015 Jean-François Ferry <jfefe@aternatik.fr> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18/** 19 * \file htdocs/resource/class/dolresource.class.php 20 * \ingroup resource 21 * \brief Class file for resource object 22 */ 23 24require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php"; 25require_once DOL_DOCUMENT_ROOT."/core/lib/functions2.lib.php"; 26 27/** 28 * DAO Resource object 29 */ 30class Dolresource extends CommonObject 31{ 32 /** 33 * @var string ID to identify managed object 34 */ 35 public $element = 'dolresource'; 36 37 /** 38 * @var string Name of table without prefix where object is stored 39 */ 40 public $table_element = 'resource'; 41 42 /** 43 * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png 44 */ 45 public $picto = 'resource'; 46 47 public $resource_id; 48 public $resource_type; 49 public $element_id; 50 public $element_type; 51 public $busy; 52 public $mandatory; 53 54 /** 55 * @var int ID 56 */ 57 public $fk_user_create; 58 59 public $type_label; 60 public $tms = ''; 61 62 public $cache_code_type_resource = array(); 63 64 /** 65 * @var Dolresource Clone of object before changing it 66 */ 67 public $oldcopy; 68 69 /** 70 * Constructor 71 * 72 * @param DoliDb $db Database handler 73 */ 74 public function __construct($db) 75 { 76 $this->db = $db; 77 } 78 79 /** 80 * Create object into database 81 * 82 * @param User $user User that creates 83 * @param int $notrigger 0=launch triggers after, 1=disable triggers 84 * @return int <0 if KO, Id of created object if OK 85 */ 86 public function create($user, $notrigger = 0) 87 { 88 global $conf, $langs, $hookmanager; 89 $error = 0; 90 91 // Clean parameters 92 93 if (isset($this->ref)) { 94 $this->ref = trim($this->ref); 95 } 96 if (isset($this->description)) { 97 $this->description = trim($this->description); 98 } 99 if (!is_numeric($this->country_id)) { 100 $this->country_id = 0; 101 } 102 if (isset($this->fk_code_type_resource)) { 103 $this->fk_code_type_resource = trim($this->fk_code_type_resource); 104 } 105 if (isset($this->note_public)) { 106 $this->note_public = trim($this->note_public); 107 } 108 if (isset($this->note_private)) { 109 $this->note_private = trim($this->note_private); 110 } 111 112 113 // Insert request 114 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."("; 115 $sql .= "entity,"; 116 $sql .= "ref,"; 117 $sql .= "description,"; 118 $sql .= "fk_country,"; 119 $sql .= "fk_code_type_resource,"; 120 $sql .= "note_public,"; 121 $sql .= "note_private"; 122 $sql .= ") VALUES ("; 123 $sql .= $conf->entity.", "; 124 $sql .= " ".(!isset($this->ref) ? 'NULL' : "'".$this->db->escape($this->ref)."'").","; 125 $sql .= " ".(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").","; 126 $sql .= " ".($this->country_id > 0 ? $this->country_id : 'null').","; 127 $sql .= " ".(!isset($this->fk_code_type_resource) ? 'NULL' : "'".$this->db->escape($this->fk_code_type_resource)."'").","; 128 $sql .= " ".(!isset($this->note_public) ? 'NULL' : "'".$this->db->escape($this->note_public)."'").","; 129 $sql .= " ".(!isset($this->note_private) ? 'NULL' : "'".$this->db->escape($this->note_private)."'"); 130 $sql .= ")"; 131 132 $this->db->begin(); 133 134 dol_syslog(get_class($this)."::create", LOG_DEBUG); 135 $resql = $this->db->query($sql); 136 if (!$resql) { 137 $error++; $this->errors[] = "Error ".$this->db->lasterror(); 138 } 139 140 if (!$error) { 141 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); 142 } 143 144 if (!$error) { 145 $action = 'create'; 146 147 // Actions on extra fields 148 if (!$error) { 149 $result = $this->insertExtraFields(); 150 if ($result < 0) { 151 $error++; 152 } 153 } 154 } 155 156 if (!$error && !$notrigger) { 157 // Call trigger 158 $result = $this->call_trigger('RESOURCE_CREATE', $user); 159 if ($result < 0) { 160 $error++; 161 } 162 // End call triggers 163 } 164 165 // Commit or rollback 166 if ($error) { 167 foreach ($this->errors as $errmsg) { 168 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR); 169 $this->error .= ($this->error ? ', '.$errmsg : $errmsg); 170 } 171 $this->db->rollback(); 172 return -1 * $error; 173 } else { 174 $this->db->commit(); 175 return $this->id; 176 } 177 } 178 179 /** 180 * Load object in memory from database 181 * 182 * @param int $id Id of object 183 * @param string $ref Ref of object 184 * @return int <0 if KO, >0 if OK 185 */ 186 public function fetch($id, $ref = '') 187 { 188 global $langs; 189 $sql = "SELECT"; 190 $sql .= " t.rowid,"; 191 $sql .= " t.entity,"; 192 $sql .= " t.ref,"; 193 $sql .= " t.description,"; 194 $sql .= " t.fk_country,"; 195 $sql .= " t.fk_code_type_resource,"; 196 $sql .= " t.note_public,"; 197 $sql .= " t.note_private,"; 198 $sql .= " t.tms,"; 199 $sql .= " ty.label as type_label"; 200 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t"; 201 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_resource as ty ON ty.code=t.fk_code_type_resource"; 202 if ($id) { 203 $sql .= " WHERE t.rowid = ".((int) $id); 204 } else { 205 $sql .= " WHERE t.ref = '".$this->db->escape($ref)."'"; 206 } 207 208 dol_syslog(get_class($this)."::fetch", LOG_DEBUG); 209 $resql = $this->db->query($sql); 210 if ($resql) { 211 if ($this->db->num_rows($resql)) { 212 $obj = $this->db->fetch_object($resql); 213 214 $this->id = $obj->rowid; 215 $this->entity = $obj->entity; 216 $this->ref = $obj->ref; 217 $this->description = $obj->description; 218 $this->country_id = $obj->fk_country; 219 $this->fk_code_type_resource = $obj->fk_code_type_resource; 220 $this->note_public = $obj->note_public; 221 $this->note_private = $obj->note_private; 222 $this->type_label = $obj->type_label; 223 224 // Retrieve all extrafield 225 // fetch optionals attributes and labels 226 $this->fetch_optionals(); 227 } 228 $this->db->free($resql); 229 230 return $this->id; 231 } else { 232 $this->error = "Error ".$this->db->lasterror(); 233 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR); 234 return -1; 235 } 236 } 237 238 239 /** 240 * Update object into database 241 * 242 * @param User $user User that modifies 243 * @param int $notrigger 0=launch triggers after, 1=disable triggers 244 * @return int <0 if KO, >0 if OK 245 */ 246 public function update($user = null, $notrigger = 0) 247 { 248 global $conf, $langs, $hookmanager; 249 $error = 0; 250 251 // Clean parameters 252 if (isset($this->ref)) { 253 $this->ref = trim($this->ref); 254 } 255 if (isset($this->fk_code_type_resource)) { 256 $this->fk_code_type_resource = trim($this->fk_code_type_resource); 257 } 258 if (isset($this->description)) { 259 $this->description = trim($this->description); 260 } 261 if (!is_numeric($this->country_id)) { 262 $this->country_id = 0; 263 } 264 265 if (empty($this->oldcopy)) { 266 $org = new self($this->db); 267 $org->fetch($this->id); 268 $this->oldcopy = $org; 269 } 270 271 // Update request 272 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; 273 $sql .= " ref=".(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").","; 274 $sql .= " description=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").","; 275 $sql .= " fk_country=".($this->country_id > 0 ? $this->country_id : "null").","; 276 $sql .= " fk_code_type_resource=".(isset($this->fk_code_type_resource) ? "'".$this->db->escape($this->fk_code_type_resource)."'" : "null").","; 277 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').""; 278 $sql .= " WHERE rowid=".((int) $this->id); 279 280 $this->db->begin(); 281 282 dol_syslog(get_class($this)."::update", LOG_DEBUG); 283 $resql = $this->db->query($sql); 284 if (!$resql) { 285 $error++; $this->errors[] = "Error ".$this->db->lasterror(); 286 } 287 288 if (!$error) { 289 if (!$notrigger) { 290 // Call trigger 291 $result = $this->call_trigger('RESOURCE_MODIFY', $user); 292 if ($result < 0) { 293 $error++; 294 } 295 // End call triggers 296 } 297 } 298 299 if (!$error && (is_object($this->oldcopy) && $this->oldcopy->ref !== $this->ref)) { 300 // We remove directory 301 if (!empty($conf->resource->dir_output)) { 302 $olddir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->oldcopy->ref); 303 $newdir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->ref); 304 if (file_exists($olddir)) { 305 $res = @rename($olddir, $newdir); 306 if (!$res) { 307 $langs->load("errors"); 308 $this->error = $langs->trans('ErrorFailToRenameDir', $olddir, $newdir); 309 $error++; 310 } 311 } 312 } 313 } 314 315 if (!$error) { 316 $action = 'update'; 317 318 // Actions on extra fields 319 if (!$error) { 320 $result = $this->insertExtraFields(); 321 if ($result < 0) { 322 $error++; 323 } 324 } 325 } 326 327 // Commit or rollback 328 if ($error) { 329 foreach ($this->errors as $errmsg) { 330 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); 331 $this->error .= ($this->error ? ', '.$errmsg : $errmsg); 332 } 333 $this->db->rollback(); 334 return -1 * $error; 335 } else { 336 $this->db->commit(); 337 return 1; 338 } 339 } 340 341 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 342 /** 343 * Load object in memory from database 344 * 345 * @param int $id id object 346 * @return int <0 if KO, >0 if OK 347 */ 348 public function fetch_element_resource($id) 349 { 350 // phpcs:enable 351 global $langs; 352 $sql = "SELECT"; 353 $sql .= " t.rowid,"; 354 $sql .= " t.resource_id,"; 355 $sql .= " t.resource_type,"; 356 $sql .= " t.element_id,"; 357 $sql .= " t.element_type,"; 358 $sql .= " t.busy,"; 359 $sql .= " t.mandatory,"; 360 $sql .= " t.fk_user_create,"; 361 $sql .= " t.tms"; 362 $sql .= " FROM ".MAIN_DB_PREFIX."element_resources as t"; 363 $sql .= " WHERE t.rowid = ".$this->db->escape($id); 364 365 dol_syslog(get_class($this)."::fetch", LOG_DEBUG); 366 $resql = $this->db->query($sql); 367 if ($resql) { 368 if ($this->db->num_rows($resql)) { 369 $obj = $this->db->fetch_object($resql); 370 371 $this->id = $obj->rowid; 372 $this->resource_id = $obj->resource_id; 373 $this->resource_type = $obj->resource_type; 374 $this->element_id = $obj->element_id; 375 $this->element_type = $obj->element_type; 376 $this->busy = $obj->busy; 377 $this->mandatory = $obj->mandatory; 378 $this->fk_user_create = $obj->fk_user_create; 379 380 if ($obj->resource_id && $obj->resource_type) { 381 $this->objresource = fetchObjectByElement($obj->resource_id, $obj->resource_type); 382 } 383 if ($obj->element_id && $obj->element_type) { 384 $this->objelement = fetchObjectByElement($obj->element_id, $obj->element_type); 385 } 386 } 387 $this->db->free($resql); 388 389 return $this->id; 390 } else { 391 $this->error = "Error ".$this->db->lasterror(); 392 return -1; 393 } 394 } 395 396 /** 397 * Delete a resource object 398 * 399 * @param int $rowid Id of resource line to delete 400 * @param int $notrigger Disable all triggers 401 * @return int >0 if OK, <0 if KO 402 */ 403 public function delete($rowid, $notrigger = 0) 404 { 405 global $user, $langs, $conf; 406 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; 407 408 $error = 0; 409 410 $this->db->begin(); 411 412 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element; 413 $sql .= " WHERE rowid = ".((int) $rowid); 414 415 dol_syslog(get_class($this), LOG_DEBUG); 416 if ($this->db->query($sql)) { 417 $sql = "DELETE FROM ".MAIN_DB_PREFIX."element_resources"; 418 $sql .= " WHERE element_type='resource' AND resource_id = ".((int) $rowid); 419 dol_syslog(get_class($this)."::delete", LOG_DEBUG); 420 $resql = $this->db->query($sql); 421 if (!$resql) { 422 $this->error = $this->db->lasterror(); 423 $error++; 424 } 425 } else { 426 $this->error = $this->db->lasterror(); 427 $error++; 428 } 429 430 // Removed extrafields 431 if (!$error) { 432 $result = $this->deleteExtraFields(); 433 if ($result < 0) { 434 $error++; 435 dol_syslog(get_class($this)."::delete error -3 ".$this->error, LOG_ERR); 436 } 437 } 438 439 if (!$notrigger) { 440 // Call trigger 441 $result = $this->call_trigger('RESOURCE_DELETE', $user); 442 if ($result < 0) { 443 $error++; 444 } 445 // End call triggers 446 } 447 448 if (!$error) { 449 // We remove directory 450 $ref = dol_sanitizeFileName($this->ref); 451 if (!empty($conf->resource->dir_output)) { 452 $dir = $conf->resource->dir_output."/".dol_sanitizeFileName($this->ref); 453 if (file_exists($dir)) { 454 $res = @dol_delete_dir_recursive($dir); 455 if (!$res) { 456 $this->errors[] = 'ErrorFailToDeleteDir'; 457 $error++; 458 } 459 } 460 } 461 } 462 463 if (!$error) { 464 $this->db->commit(); 465 return 1; 466 } else { 467 $this->db->rollback(); 468 return -1; 469 } 470 } 471 472 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 473 /** 474 * Load resource objects into $this->lines 475 * 476 * @param string $sortorder sort order 477 * @param string $sortfield sort field 478 * @param int $limit limit page 479 * @param int $offset page 480 * @param array $filter filter output 481 * @return int <0 if KO, >0 if OK 482 */ 483 public function fetch_all($sortorder, $sortfield, $limit, $offset, $filter = '') 484 { 485 // phpcs:enable 486 global $conf; 487 488 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; 489 $extrafields = new ExtraFields($this->db); 490 491 $sql = "SELECT "; 492 $sql .= " t.rowid,"; 493 $sql .= " t.entity,"; 494 $sql .= " t.ref,"; 495 $sql .= " t.description,"; 496 $sql .= " t.fk_country,"; 497 $sql .= " t.fk_code_type_resource,"; 498 $sql .= " t.tms,"; 499 // Add fields from extrafields 500 if (!empty($extrafields->attributes[$this->table_element]['label'])) { 501 foreach ($extrafields->attributes[$this->table_element]['label'] as $key => $val) { 502 $sql .= ($extrafields->attributes[$this->table_element]['type'][$key] != 'separate' ? "ef.".$key.' as options_'.$key.', ' : ''); 503 } 504 } 505 $sql .= " ty.label as type_label"; 506 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t"; 507 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_resource as ty ON ty.code=t.fk_code_type_resource"; 508 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$this->table_element."_extrafields as ef ON ef.fk_object=t.rowid"; 509 $sql .= " WHERE t.entity IN (".getEntity('resource').")"; 510 // Manage filter 511 if (!empty($filter)) { 512 foreach ($filter as $key => $value) { 513 if (strpos($key, 'date')) { 514 $sql .= ' AND '.$key.' = \''.$this->db->idate($value).'\''; 515 } elseif (strpos($key, 'ef.') !== false) { 516 $sql .= $value; 517 } else { 518 $sql .= ' AND '.$key.' LIKE \'%'.$this->db->escape($value).'%\''; 519 } 520 } 521 } 522 $sql .= $this->db->order($sortfield, $sortorder); 523 $this->num_all = 0; 524 if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) { 525 $result = $this->db->query($sql); 526 $this->num_all = $this->db->num_rows($result); 527 } 528 if ($limit) { 529 $sql .= $this->db->plimit($limit, $offset); 530 } 531 dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG); 532 533 $this->lines = array(); 534 $resql = $this->db->query($sql); 535 if ($resql) { 536 $num = $this->db->num_rows($resql); 537 if ($num) { 538 while ($obj = $this->db->fetch_object($resql)) { 539 $line = new Dolresource($this->db); 540 $line->id = $obj->rowid; 541 $line->ref = $obj->ref; 542 $line->description = $obj->description; 543 $line->country_id = $obj->fk_country; 544 $line->fk_code_type_resource = $obj->fk_code_type_resource; 545 $line->type_label = $obj->type_label; 546 547 // fetch optionals attributes and labels 548 549 $line->fetch_optionals(); 550 551 $this->lines[] = $line; 552 } 553 $this->db->free($resql); 554 } 555 return $num; 556 } else { 557 $this->error = $this->db->lasterror(); 558 return -1; 559 } 560 } 561 562 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 563 /** 564 * Load all objects into $this->lines 565 * 566 * @param string $sortorder sort order 567 * @param string $sortfield sort field 568 * @param int $limit limit page 569 * @param int $offset page 570 * @param array $filter filter output 571 * @return int <0 if KO, >0 if OK 572 */ 573 public function fetch_all_resources($sortorder, $sortfield, $limit, $offset, $filter = '') 574 { 575 // phpcs:enable 576 global $conf; 577 $sql = "SELECT "; 578 $sql .= " t.rowid,"; 579 $sql .= " t.resource_id,"; 580 $sql .= " t.resource_type,"; 581 $sql .= " t.element_id,"; 582 $sql .= " t.element_type,"; 583 $sql .= " t.busy,"; 584 $sql .= " t.mandatory,"; 585 $sql .= " t.fk_user_create,"; 586 $sql .= " t.tms"; 587 $sql .= ' FROM '.MAIN_DB_PREFIX.'element_resources as t '; 588 $sql .= " WHERE t.entity IN (".getEntity('resource').")"; 589 590 //Manage filter 591 if (!empty($filter)) { 592 foreach ($filter as $key => $value) { 593 if (strpos($key, 'date')) { 594 $sql .= ' AND '.$key.' = \''.$this->db->idate($value).'\''; 595 } else { 596 $sql .= ' AND '.$key.' LIKE \'%'.$this->db->escape($value).'%\''; 597 } 598 } 599 } 600 $sql .= $this->db->order($sortfield, $sortorder); 601 if ($limit) { 602 $sql .= $this->db->plimit($limit + 1, $offset); 603 } 604 dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG); 605 606 $resql = $this->db->query($sql); 607 if ($resql) { 608 $num = $this->db->num_rows($resql); 609 if ($num) { 610 while ($obj = $this->db->fetch_object($resql)) { 611 $line = new Dolresource($this->db); 612 $line->id = $obj->rowid; 613 $line->resource_id = $obj->resource_id; 614 $line->resource_type = $obj->resource_type; 615 $line->element_id = $obj->element_id; 616 $line->element_type = $obj->element_type; 617 $line->busy = $obj->busy; 618 $line->mandatory = $obj->mandatory; 619 $line->fk_user_create = $obj->fk_user_create; 620 621 if ($obj->resource_id && $obj->resource_type) { 622 $line->objresource = fetchObjectByElement($obj->resource_id, $obj->resource_type); 623 } 624 if ($obj->element_id && $obj->element_type) { 625 $line->objelement = fetchObjectByElement($obj->element_id, $obj->element_type); 626 } 627 $this->lines[] = $line; 628 } 629 $this->db->free($resql); 630 } 631 return $num; 632 } else { 633 $this->error = $this->db->lasterror(); 634 return -1; 635 } 636 } 637 638 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 639 /** 640 * Load all objects into $this->lines 641 * 642 * @param string $sortorder sort order 643 * @param string $sortfield sort field 644 * @param int $limit limit page 645 * @param int $offset page 646 * @param array $filter filter output 647 * @return int <0 if KO, >0 if OK 648 */ 649 public function fetch_all_used($sortorder, $sortfield, $limit, $offset = 1, $filter = '') 650 { 651 // phpcs:enable 652 global $conf; 653 654 if (!$sortorder) { 655 $sortorder = "ASC"; 656 } 657 if (!$sortfield) { 658 $sortfield = "t.rowid"; 659 } 660 661 $sql = "SELECT "; 662 $sql .= " t.rowid,"; 663 $sql .= " t.resource_id,"; 664 $sql .= " t.resource_type,"; 665 $sql .= " t.element_id,"; 666 $sql .= " t.element_type,"; 667 $sql .= " t.busy,"; 668 $sql .= " t.mandatory,"; 669 $sql .= " t.fk_user_create,"; 670 $sql .= " t.tms"; 671 $sql .= ' FROM '.MAIN_DB_PREFIX.'element_resources as t '; 672 $sql .= " WHERE t.entity IN (".getEntity('resource').")"; 673 674 //Manage filter 675 if (!empty($filter)) { 676 foreach ($filter as $key => $value) { 677 if (strpos($key, 'date')) { 678 $sql .= ' AND '.$key.' = \''.$this->db->idate($value).'\''; 679 } else { 680 $sql .= ' AND '.$key.' LIKE \'%'.$this->db->escape($value).'%\''; 681 } 682 } 683 } 684 $sql .= $this->db->order($sortfield, $sortorder); 685 if ($limit) { 686 $sql .= $this->db->plimit($limit + 1, $offset); 687 } 688 dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG); 689 690 $resql = $this->db->query($sql); 691 if ($resql) { 692 $num = $this->db->num_rows($resql); 693 if ($num) { 694 $this->lines = array(); 695 while ($obj = $this->db->fetch_object($resql)) { 696 $line = new Dolresource($this->db); 697 $line->id = $obj->rowid; 698 $line->resource_id = $obj->resource_id; 699 $line->resource_type = $obj->resource_type; 700 $line->element_id = $obj->element_id; 701 $line->element_type = $obj->element_type; 702 $line->busy = $obj->busy; 703 $line->mandatory = $obj->mandatory; 704 $line->fk_user_create = $obj->fk_user_create; 705 706 $this->lines[] = fetchObjectByElement($obj->resource_id, $obj->resource_type); 707 } 708 $this->db->free($resql); 709 } 710 return $num; 711 } else { 712 $this->error = $this->db->lasterror(); 713 return -1; 714 } 715 } 716 717 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 718 /** 719 * Fetch all resources available, declared by modules 720 * Load available resource in array $this->available_resources 721 * 722 * @return int number of available resources declared by modules 723 * @deprecated, remplaced by hook getElementResources 724 * @see getElementResources() 725 */ 726 public function fetch_all_available() 727 { 728 // phpcs:enable 729 global $conf; 730 731 if (!empty($conf->modules_parts['resources'])) { 732 $this->available_resources = (array) $conf->modules_parts['resources']; 733 734 return count($this->available_resources); 735 } 736 return 0; 737 } 738 739 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 740 /** 741 * Update element resource into database 742 * 743 * @param User $user User that modifies 744 * @param int $notrigger 0=launch triggers after, 1=disable triggers 745 * @return int <0 if KO, >0 if OK 746 */ 747 public function update_element_resource($user = null, $notrigger = 0) 748 { 749 // phpcs:enable 750 global $conf, $langs; 751 $error = 0; 752 753 // Clean parameters 754 if (isset($this->resource_id)) { 755 $this->resource_id = trim($this->resource_id); 756 } 757 if (isset($this->resource_type)) { 758 $this->resource_type = trim($this->resource_type); 759 } 760 if (isset($this->element_id)) { 761 $this->element_id = trim($this->element_id); 762 } 763 if (isset($this->element_type)) { 764 $this->element_type = trim($this->element_type); 765 } 766 if (isset($this->busy)) { 767 $this->busy = trim($this->busy); 768 } 769 if (isset($this->mandatory)) { 770 $this->mandatory = trim($this->mandatory); 771 } 772 773 // Update request 774 $sql = "UPDATE ".MAIN_DB_PREFIX."element_resources SET"; 775 $sql .= " resource_id=".(isset($this->resource_id) ? "'".$this->db->escape($this->resource_id)."'" : "null").","; 776 $sql .= " resource_type=".(isset($this->resource_type) ? "'".$this->db->escape($this->resource_type)."'" : "null").","; 777 $sql .= " element_id=".(isset($this->element_id) ? $this->element_id : "null").","; 778 $sql .= " element_type=".(isset($this->element_type) ? "'".$this->db->escape($this->element_type)."'" : "null").","; 779 $sql .= " busy=".(isset($this->busy) ? $this->busy : "null").","; 780 $sql .= " mandatory=".(isset($this->mandatory) ? $this->mandatory : "null").","; 781 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').""; 782 783 $sql .= " WHERE rowid=".((int) $this->id); 784 785 $this->db->begin(); 786 787 dol_syslog(get_class($this)."::update", LOG_DEBUG); 788 $resql = $this->db->query($sql); 789 if (!$resql) { 790 $error++; $this->errors[] = "Error ".$this->db->lasterror(); 791 } 792 793 if (!$error) { 794 if (!$notrigger) { 795 // Call trigger 796 $result = $this->call_trigger('RESOURCE_MODIFY', $user); 797 if ($result < 0) { 798 $error++; 799 } 800 // End call triggers 801 } 802 } 803 804 // Commit or rollback 805 if ($error) { 806 foreach ($this->errors as $errmsg) { 807 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); 808 $this->error .= ($this->error ? ', '.$errmsg : $errmsg); 809 } 810 $this->db->rollback(); 811 return -1 * $error; 812 } else { 813 $this->db->commit(); 814 return 1; 815 } 816 } 817 818 819 /** 820 * Return an array with resources linked to the element 821 * 822 * @param string $element Element 823 * @param int $element_id Id 824 * @param string $resource_type Type 825 * @return array Aray of resources 826 */ 827 public function getElementResources($element, $element_id, $resource_type = '') 828 { 829 $resources = array(); 830 831 // Links beetween objects are stored in this table 832 $sql = 'SELECT rowid, resource_id, resource_type, busy, mandatory'; 833 $sql .= ' FROM '.MAIN_DB_PREFIX.'element_resources'; 834 $sql .= " WHERE element_id=".((int) $element_id)." AND element_type='".$this->db->escape($element)."'"; 835 if ($resource_type) { 836 $sql .= " AND resource_type LIKE '%".$this->db->escape($resource_type)."%'"; 837 } 838 $sql .= ' ORDER BY resource_type'; 839 840 dol_syslog(get_class($this)."::getElementResources", LOG_DEBUG); 841 842 $resources = array(); 843 $resql = $this->db->query($sql); 844 if ($resql) { 845 $num = $this->db->num_rows($resql); 846 $i = 0; 847 while ($i < $num) { 848 $obj = $this->db->fetch_object($resql); 849 850 $resources[$i] = array( 851 'rowid' => $obj->rowid, 852 'resource_id' => $obj->resource_id, 853 'resource_type'=>$obj->resource_type, 854 'busy'=>$obj->busy, 855 'mandatory'=>$obj->mandatory 856 ); 857 $i++; 858 } 859 } 860 861 return $resources; 862 } 863 864 /** 865 * Return an int number of resources linked to the element 866 * 867 * @param string $element Element type 868 * @param int $element_id Element id 869 * @return int Nb of resources loaded 870 */ 871 public function fetchElementResources($element, $element_id) 872 { 873 $resources = $this->getElementResources($element, $element_id); 874 $i = 0; 875 foreach ($resources as $nb => $resource) { 876 $this->lines[$i] = fetchObjectByElement($resource['resource_id'], $resource['resource_type']); 877 $i++; 878 } 879 return $i; 880 } 881 882 883 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 884 /** 885 * Load in cache resource type code (setup in dictionary) 886 * 887 * @return int Number of lines loaded, 0 if already loaded, <0 if KO 888 */ 889 public function load_cache_code_type_resource() 890 { 891 // phpcs:enable 892 global $langs; 893 894 if (is_array($this->cache_code_type_resource) && count($this->cache_code_type_resource)) { 895 return 0; // Cache deja charge 896 } 897 898 $sql = "SELECT rowid, code, label, active"; 899 $sql .= " FROM ".MAIN_DB_PREFIX."c_type_resource"; 900 $sql .= " WHERE active > 0"; 901 $sql .= " ORDER BY rowid"; 902 dol_syslog(get_class($this)."::load_cache_code_type_resource", LOG_DEBUG); 903 $resql = $this->db->query($sql); 904 if ($resql) { 905 $num = $this->db->num_rows($resql); 906 $i = 0; 907 while ($i < $num) { 908 $obj = $this->db->fetch_object($resql); 909 // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut 910 $label = ($langs->trans("ResourceTypeShort".$obj->code) != ("ResourceTypeShort".$obj->code) ? $langs->trans("ResourceTypeShort".$obj->code) : ($obj->label != '-' ? $obj->label : '')); 911 $this->cache_code_type_resource[$obj->rowid]['code'] = $obj->code; 912 $this->cache_code_type_resource[$obj->rowid]['label'] = $label; 913 $this->cache_code_type_resource[$obj->rowid]['active'] = $obj->active; 914 $i++; 915 } 916 return $num; 917 } else { 918 dol_print_error($this->db); 919 return -1; 920 } 921 } 922 923 /** 924 * Return clicable link of object (with eventually picto) 925 * 926 * @param int $withpicto Add picto into link 927 * @param string $option Where point the link ('compta', 'expedition', 'document', ...) 928 * @param string $get_params Parametres added to url 929 * @param int $notooltip 1=Disable tooltip 930 * @param string $morecss Add more css on link 931 * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking 932 * @return string String with URL 933 */ 934 public function getNomUrl($withpicto = 0, $option = '', $get_params = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1) 935 { 936 global $conf, $langs; 937 938 $result = ''; 939 $label = img_picto('', $this->picto).' <u>'.$langs->trans("Resource").'</u>'; 940 $label .= '<br>'; 941 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref; 942 /*if (isset($this->status)) { 943 $label.= '<br><b>' . $langs->trans("Status").":</b> ".$this->getLibStatut(5); 944 }*/ 945 if (isset($this->type_label)) { 946 $label .= '<br><b>'.$langs->trans("ResourceType").":</b> ".$this->type_label; 947 } 948 949 $url = DOL_URL_ROOT.'/resource/card.php?id='.$this->id; 950 951 if ($option != 'nolink') { 952 // Add param to save lastsearch_values or not 953 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); 954 if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { 955 $add_save_lastsearch_values = 1; 956 } 957 if ($add_save_lastsearch_values) { 958 $url .= '&save_lastsearch_values=1'; 959 } 960 } 961 962 $linkclose = ''; 963 if (empty($notooltip)) { 964 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) { 965 $label = $langs->trans("ShowMyObject"); 966 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"'; 967 } 968 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"'; 969 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"'; 970 } else { 971 $linkclose = ($morecss ? ' class="'.$morecss.'"' : ''); 972 } 973 974 $linkstart = '<a href="'.$url.$get_params.'"'; 975 $linkstart .= $linkclose.'>'; 976 $linkend = '</a>'; 977 /*$linkstart = '<a href="'.dol_buildpath('/resource/card.php', 1).'?id='.$this->id.$get_params.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">'; 978 $linkend = '</a>';*/ 979 980 $result .= $linkstart; 981 if ($withpicto) { 982 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1); 983 } 984 if ($withpicto != 2) { 985 $result .= $this->ref; 986 } 987 $result .= $linkend; 988 989 return $result; 990 } 991 992 993 /** 994 * Retourne le libelle du status d'un user (actif, inactif) 995 * 996 * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto 997 * @return string Label of status 998 */ 999 public function getLibStatut($mode = 0) 1000 { 1001 return $this->LibStatut($this->status, $mode); 1002 } 1003 1004 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps 1005 /** 1006 * Return the status 1007 * 1008 * @param int $status Id status 1009 * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 5=Long label + Picto 1010 * @return string Label of status 1011 */ 1012 public static function LibStatut($status, $mode = 0) 1013 { 1014 // phpcs:enable 1015 global $langs; 1016 1017 return ''; 1018 } 1019} 1020