Passed
Branch develop (aaddda)
by
unknown
27:46
created

WorkstationResource::__construct()   B

Complexity

Conditions 9
Paths 15

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 9
nc 15
nop 1
dl 0
loc 23
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2017  Laurent Destailleur <[email protected]>
3
 * Copyright (C) 2020 Gauthier VERDOL <[email protected]>
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
/**
20
 * \file        class/workstationresource.class.php
21
 * \ingroup     workstation
22
 * \brief       This file is a CRUD class file for WorkstationResource (Create/Read/Update/Delete)
23
 */
24
25
/**
26
 * Class for WorkstationResource
27
 */
28
29
class WorkstationResource extends CommonObject
30
{
31
	/** @var string $table_element Table name in SQL */
32
	public $table_element = 'workstation_workstation_resource';
33
34
	/** @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) */
35
	public $element = 'workstationresource';
36
37
	public $fields = array(
38
		'fk_workstation' => array ('type' => 'integer'),
39
		'fk_resource' => array ('type' => 'integer')
40
	);
41
42
	/**
43
	 * WorkstationResource constructor.
44
	 * @param DoliDB    $db    Database connector
45
	 */
46
	public function __construct($db)
47
	{
48
		$this->db = $db;
49
50
		// Unset fields that are disabled
51
		foreach ($this->fields as $key => $val)
52
		{
53
			if (isset($val['enabled']) && empty($val['enabled']))
54
			{
55
				unset($this->fields[$key]);
56
			}
57
		}
58
59
		// Translate some data of arrayofkeyval
60
		if (is_object($langs))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $langs seems to be never defined.
Loading history...
61
		{
62
			foreach ($this->fields as $key => $val)
63
			{
64
				if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))
65
				{
66
					foreach ($val['arrayofkeyval'] as $key2 => $val2)
67
					{
68
						$this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
69
					}
70
				}
71
			}
72
		}
73
	}
74
75
	/**
76
	 * Function used to get an array with all resources linked to a workstation
77
	 * @param	int		$fk_workstation		id of workstation we need to get linked resources
78
	 * @return array
79
	 */
80
	static public function getAllResourcesOfWorkstation($fk_workstation)
81
	{
82
		global $db;
83
		$obj = new self($db);
84
		return parent::getAllItemsLinkedByObjectID($fk_workstation, 'fk_resource', 'fk_workstation', $obj->table_element);
85
	}
86
87
	/**
88
	 * Function used to remove all resources linked to a workstation
89
	 * @param	int		$fk_workstation		id of workstation we need to remove linked resources
90
	 * @return int
91
	 */
92
	static public function deleteAllResourcesOfWorkstation($fk_workstation)
93
	{
94
		global $db;
95
		$obj = new self($db);
96
		return parent::deleteAllItemsLinkedByObjectID($fk_workstation, 'fk_workstation', $obj->table_element);
97
	}
98
}
99