workstationresource.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2020 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file class/workstationresource.class.php
  20. * \ingroup workstation
  21. * \brief This file is a CRUD class file for WorkstationResource (Create/Read/Update/Delete)
  22. */
  23. /**
  24. * Class to link resource with Workstations
  25. */
  26. class WorkstationResource extends CommonObject
  27. {
  28. /** @var string $table_element Table name in SQL */
  29. public $table_element = 'workstation_workstation_resource';
  30. /** @var string $element Name of the element (tip for better integration in Dolibarr: this value should be the reflection of the class name with ucfirst() function) */
  31. public $element = 'workstationresource';
  32. /**
  33. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  34. */
  35. public $fields = array(
  36. 'fk_workstation' => array ('type' => 'integer'),
  37. 'fk_resource' => array ('type' => 'integer')
  38. );
  39. /**
  40. * @var int ID of workstation
  41. */
  42. public $fk_workstation;
  43. /**
  44. * @var int ID of dolresource
  45. */
  46. public $fk_resource;
  47. /**
  48. * WorkstationResource constructor.
  49. *
  50. * @param DoliDB $db Database connector
  51. */
  52. public function __construct($db)
  53. {
  54. global $langs;
  55. $this->db = $db;
  56. // Unset fields that are disabled
  57. foreach ($this->fields as $key => $val) {
  58. if (isset($val['enabled']) && empty($val['enabled'])) {
  59. unset($this->fields[$key]);
  60. }
  61. }
  62. // Translate some data of arrayofkeyval
  63. if (is_object($langs)) {
  64. foreach ($this->fields as $key => $val) {
  65. if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
  66. foreach ($val['arrayofkeyval'] as $key2 => $val2) {
  67. $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * Function used to get an array with all resources linked to a workstation
  75. *
  76. * @param int $fk_workstation Id of workstation we need to get linked resources
  77. * @return array Array of record
  78. */
  79. public static function getAllResourcesOfWorkstation($fk_workstation)
  80. {
  81. global $db;
  82. $obj = new self($db);
  83. return parent::getAllItemsLinkedByObjectID($fk_workstation, 'fk_resource', 'fk_workstation', $obj->table_element);
  84. }
  85. /**
  86. * Function used to remove all resources linked to a workstation
  87. *
  88. * @param int $fk_workstation Id of workstation we need to remove linked resources
  89. * @return int <0 if KO, 0 if nothing done, >0 if OK and something done
  90. */
  91. public static function deleteAllResourcesOfWorkstation($fk_workstation)
  92. {
  93. global $db;
  94. $obj = new self($db);
  95. return parent::deleteAllItemsLinkedByObjectID($fk_workstation, 'fk_workstation', $obj->table_element);
  96. }
  97. }