2020-12-17 12:18:40 +00:00
< ? php
2020-12-21 12:29:44 +00:00
/* Copyright ( C ) 2017 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2020 Gauthier VERDOL < gauthier . verdol @ atm - consulting . fr >
2026-04-19 14:10:54 +00:00
* Copyright ( C ) 2024 - 2026 MDW < mdeweerd @ users . noreply . github . com >
2026-06-10 17:31:35 +00:00
* Copyright ( C ) 2024 - 2026 Frédéric France < frederic . france @ free . fr >
2020-12-21 12:29:44 +00:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
/**
2024-03-20 07:41:27 +00:00
* \file htdocs / workstation / class / workstationresource . class . php
2020-12-21 12:29:44 +00:00
* \ingroup workstation
* \brief This file is a CRUD class file for WorkstationResource ( Create / Read / Update / Delete )
*/
2021-01-25 11:39:44 +00:00
2020-12-21 12:29:44 +00:00
/**
2021-01-25 11:39:44 +00:00
* Class to link resource with Workstations
2020-12-21 12:29:44 +00:00
*/
2020-12-17 12:18:40 +00:00
class WorkstationResource extends CommonObject
{
2025-11-04 13:16:17 +00:00
/** @var string Table name in SQL */
2020-12-17 12:18:40 +00:00
public $table_element = 'workstation_workstation_resource' ;
2025-11-04 13:16:17 +00:00
/** @var string Name of the element (tip for better integration in Dolibarr: this value should be the reflection of the class name with ucfirst() function) */
2020-12-17 12:18:40 +00:00
public $element = 'workstationresource' ;
2021-01-25 11:39:44 +00:00
/**
2026-04-19 14:10:54 +00:00
* @ var array < string , array { type : string , label : string , enabled : int < 0 , 2 >| string , position : int , visible : int <- 6 , 6 >| string , langfile ? : string , notnull ? : int <- 1 , 1 > , noteditable ? : int < 0 , 1 > , alwayseditable ? : int < 0 , 1 >| string , default ? : string | int , index ? : int < 0 , 1 > , foreignkey ? : string , searchall ? : int < 0 , 1 > , isameasure ? : int < 0 , 1 > , css ? : string , cssview ? : string , csslist ? : string , help ? : string , helplist ? : string , showoncombobox ? : int < 0 , 4 >| string , disabled ? : int < 0 , 1 >| string , arrayofkeyval ? : array < int | string , string > , autofocusoncreate ? : int < 0 , 1 > , comment ? : string , copytoclipboard ? : int < 1 , 2 > , validate ? : int < 0 , 1 >| string , showonheader ? : int < 0 , 1 > , searchmulti ? : int < 0 , 1 > , picto ? : string , required ? : int < 0 , 1 > , placeholder ? : string } > Array with all fields and their property . Do not use it as a static var . It may be modified by constructor .
2021-01-25 11:39:44 +00:00
*/
2021-01-25 15:23:09 +00:00
public $fields = array (
2024-03-20 07:41:27 +00:00
'fk_workstation' => array ( 'type' => 'integer' , 'label' => 'Workstation' , 'enabled' => 1 , 'position' => 10 , 'visible' => 1 ),
'fk_resource' => array ( 'type' => 'integer' , 'label' => 'UserGroup' , 'enabled' => 1 , 'position' => 20 , 'visible' => 1 ),
2020-12-17 12:18:40 +00:00
);
2021-01-25 15:24:27 +00:00
/**
* @ var int ID of workstation
*/
2021-01-25 15:23:09 +00:00
public $fk_workstation ;
2021-01-25 11:39:44 +00:00
2021-01-25 15:24:27 +00:00
/**
* @ var int ID of dolresource
*/
2021-01-25 15:23:09 +00:00
public $fk_resource ;
2021-01-25 11:39:44 +00:00
2020-12-17 12:18:40 +00:00
/**
* WorkstationResource constructor .
2021-01-25 11:39:44 +00:00
*
2020-12-17 12:18:40 +00:00
* @ param DoliDB $db Database connector
*/
public function __construct ( $db )
{
2021-01-25 11:39:44 +00:00
global $langs ;
2020-12-17 12:18:40 +00:00
$this -> db = $db ;
// Unset fields that are disabled
2021-02-22 17:50:45 +00:00
foreach ( $this -> fields as $key => $val ) {
if ( isset ( $val [ 'enabled' ]) && empty ( $val [ 'enabled' ])) {
2020-12-17 12:18:40 +00:00
unset ( $this -> fields [ $key ]);
}
}
// Translate some data of arrayofkeyval
2021-02-22 17:50:45 +00:00
if ( is_object ( $langs )) {
foreach ( $this -> fields as $key => $val ) {
if ( ! empty ( $val [ 'arrayofkeyval' ]) && is_array ( $val [ 'arrayofkeyval' ])) {
foreach ( $val [ 'arrayofkeyval' ] as $key2 => $val2 ) {
2020-12-17 12:18:40 +00:00
$this -> fields [ $key ][ 'arrayofkeyval' ][ $key2 ] = $langs -> trans ( $val2 );
}
}
}
}
}
2020-12-21 12:29:44 +00:00
/**
* Function used to get an array with all resources linked to a workstation
2021-01-25 11:39:44 +00:00
*
* @ param int $fk_workstation Id of workstation we need to get linked resources
2024-11-06 22:57:45 +00:00
* @ return int [] Array of record
2020-12-21 12:29:44 +00:00
*/
2021-02-22 17:50:45 +00:00
public static function getAllResourcesOfWorkstation ( $fk_workstation )
2020-12-21 11:34:51 +00:00
{
2020-12-17 12:18:40 +00:00
global $db ;
$obj = new self ( $db );
2026-06-10 17:31:35 +00:00
$all_items = parent :: getAllItemsLinkedByObjectID ( $fk_workstation , 'fk_resource' , 'fk_workstation' , $obj -> table_element );
if ( ! is_array ( $all_items )) {
return [];
}
return array_map ( 'intval' , $all_items );
2020-12-17 12:18:40 +00:00
}
2020-12-21 12:29:44 +00:00
/**
* Function used to remove all resources linked to a workstation
2021-01-25 11:39:44 +00:00
*
* @ param int $fk_workstation Id of workstation we need to remove linked resources
2023-12-06 14:46:39 +00:00
* @ return int Return integer < 0 if KO , 0 if nothing done , > 0 if OK and something done
2020-12-21 12:29:44 +00:00
*/
2021-02-22 17:50:45 +00:00
public static function deleteAllResourcesOfWorkstation ( $fk_workstation )
2020-12-21 11:34:51 +00:00
{
2020-12-17 12:18:40 +00:00
global $db ;
$obj = new self ( $db );
2021-01-11 10:36:19 +00:00
return parent :: deleteAllItemsLinkedByObjectID ( $fk_workstation , 'fk_workstation' , $obj -> table_element );
2020-12-17 12:18:40 +00:00
}
}