1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Fields dependency file. |
4
|
|
|
* |
5
|
|
|
* @package App |
6
|
|
|
* |
7
|
|
|
* @copyright YetiForce S.A. |
8
|
|
|
* @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com) |
9
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Fields dependency class. |
16
|
|
|
*/ |
17
|
|
|
class FieldsDependency |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array Views labels |
21
|
|
|
*/ |
22
|
|
|
const VIEWS = [ |
23
|
|
|
'Create' => 'LBL_VIEW_CREATE', |
24
|
|
|
'Edit' => 'LBL_VIEW_EDIT', |
25
|
|
|
'Detail' => 'LBL_VIEW_DETAIL', |
26
|
|
|
'QuickCreate' => 'LBL_QUICK_CREATE', |
27
|
|
|
'QuickEdit' => 'LBL_QUICK_EDIT', |
28
|
|
|
]; |
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
public const GUI_BACKEND = 0; |
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
public const GUI_FRONTEND = 1; |
37
|
|
|
/** |
38
|
|
|
* Cache variable for list of fields to hide for a record in a view. |
39
|
|
|
* |
40
|
|
|
* @see FieldsDependency::getByRecordModel() |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
public static $recordModelCache = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the dependency list for module. |
48
|
|
|
* |
49
|
|
|
* @param int $tabId |
50
|
|
|
* @param int|null $gui |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public static function getByModule(int $tabId, ?int $gui = null): array |
55
|
|
|
{ |
56
|
|
|
if (Cache::has('FieldsDependency', $tabId)) { |
57
|
|
|
$fields = Cache::get('FieldsDependency', $tabId); |
58
|
|
|
} else { |
59
|
|
|
$fields = []; |
60
|
|
|
$dataReader = (new \App\Db\Query())->from('s_#__fields_dependency')->where(['status' => 0, 'tabid' => $tabId]) |
61
|
|
|
->createCommand()->query(); |
62
|
|
|
while ($row = $dataReader->read()) { |
63
|
|
|
$row['gui'] = (int) $row['gui']; |
64
|
|
|
$row['mandatory'] = (int) $row['mandatory']; |
65
|
|
|
$row['conditions'] = Json::decode($row['conditions']) ?? []; |
66
|
|
|
$row['fields'] = Json::decode($row['fields']) ?? []; |
67
|
|
|
$row['conditionsFields'] = Json::decode($row['conditionsFields']) ?? []; |
68
|
|
|
$views = Json::decode($row['views']) ?? []; |
69
|
|
|
unset($row['views']); |
70
|
|
|
foreach ($views as $view) { |
71
|
|
|
$fields[$view][] = $row; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
Cache::save('FieldsDependency', $tabId, $fields); |
75
|
|
|
} |
76
|
|
|
if (isset($gui)) { |
77
|
|
|
foreach ($fields as $view => $rows) { |
78
|
|
|
foreach ($rows as $key => $row) { |
79
|
|
|
if ($gui !== $row['gui']) { |
80
|
|
|
unset($fields[$view][$key]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
return $fields; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the list of fields to hide for a record in a view. |
90
|
|
|
* |
91
|
|
|
* @see FieldsDependency::$recordModelCache |
92
|
|
|
* |
93
|
|
|
* @param string $view |
94
|
|
|
* @param \Vtiger_Record_Model $recordModel |
95
|
|
|
* @param bool $cache |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public static function getByRecordModel(string $view, \Vtiger_Record_Model $recordModel, bool $cache = true): array |
100
|
|
|
{ |
101
|
|
|
$cacheKey = $view . $recordModel->getId(); |
102
|
|
|
if ($cache && isset(self::$recordModelCache[$cacheKey])) { |
103
|
|
|
return self::$recordModelCache[$cacheKey]; |
104
|
|
|
} |
105
|
|
|
$return = [ |
106
|
|
|
'show' => ['backend' => [], 'frontend' => [], 'mandatory' => []], |
107
|
|
|
'hide' => ['backend' => [], 'frontend' => [], 'mandatory' => []], |
108
|
|
|
'mandatory' => [], |
109
|
|
|
'conditionsFields' => [], |
110
|
|
|
]; |
111
|
|
|
$fields = self::getByModule($recordModel->getModule()->getId()); |
112
|
|
|
if ($fields && isset($fields[$view])) { |
|
|
|
|
113
|
|
|
foreach ($fields[$view] as $row) { |
114
|
|
|
$status = (!$row['conditions'] || Condition::checkConditions($row['conditions'], $recordModel)) ? 'show' : 'hide'; |
115
|
|
|
if (self::GUI_FRONTEND === $row['gui']) { |
116
|
|
|
$return[$status]['frontend'] = array_merge($return[$status]['frontend'], $row['fields']); |
117
|
|
|
} else { |
118
|
|
|
$return[$status]['backend'] = array_merge($return[$status]['backend'], $row['fields']); |
119
|
|
|
} |
120
|
|
|
if (1 === $row['mandatory']) { |
121
|
|
|
$return[$status]['mandatory'] = array_merge($return[$status]['mandatory'], $row['fields']); |
122
|
|
|
$return['mandatory'] = array_merge($return['mandatory'], $row['fields']); |
123
|
|
|
} |
124
|
|
|
$return['conditionsFields'] = array_merge($return['conditionsFields'], $row['conditionsFields']); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
return self::$recordModelCache[$cacheKey] = $return; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.