1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Basic module model class. |
4
|
|
|
* |
5
|
|
|
* @package Model |
6
|
|
|
* |
7
|
|
|
* @copyright YetiForce Sp. z o.o. |
8
|
|
|
* @license YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com) |
9
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
10
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace YF\Modules\Base\Model; |
14
|
|
|
|
15
|
|
|
use App; |
16
|
|
|
use App\Api; |
17
|
|
|
|
18
|
|
|
class Module |
19
|
|
|
{ |
20
|
|
|
/** @var YF\Modules\Base\Model\Module[] Module model cache. */ |
21
|
|
|
protected static $cache; |
22
|
|
|
|
23
|
|
|
/** @var string Module name. */ |
24
|
|
|
protected $moduleName; |
25
|
|
|
|
26
|
|
|
/** @var array Fields. */ |
27
|
|
|
protected $fields; |
28
|
|
|
|
29
|
|
|
/** @var array Fields models. */ |
30
|
|
|
protected $fieldsModels; |
31
|
|
|
|
32
|
|
|
protected $defaultView = 'ListView'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get module model instance. |
36
|
|
|
* |
37
|
|
|
* @param string $moduleName |
38
|
|
|
* |
39
|
|
|
* @return self |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public static function getInstance(string $moduleName): self |
42
|
|
|
{ |
43
|
|
|
if (isset(self::$cache[$moduleName])) { |
44
|
|
|
return self::$cache[$moduleName]; |
45
|
|
|
} |
46
|
|
|
$handlerModule = App\Loader::getModuleClassName($moduleName, 'Model', 'Module'); |
47
|
|
|
return self::$cache[$moduleName] = new $handlerModule($moduleName); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor function. |
52
|
|
|
* |
53
|
|
|
* @param string $moduleName |
54
|
|
|
*/ |
55
|
|
|
public function __construct(string $moduleName) |
56
|
|
|
{ |
57
|
|
|
$this->moduleName = $moduleName; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Function to check permission for a Module/Action. |
62
|
|
|
* |
63
|
|
|
* @param string $module |
64
|
|
|
* @param string $action |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public static function isPermitted(string $module, string $action) |
69
|
|
|
{ |
70
|
|
|
if (!\App\Session::has('modulePermissions')) { |
71
|
|
|
\App\Session::set('modulePermissions', []); |
72
|
|
|
} |
73
|
|
|
$data = \App\Session::get('modulePermissions'); |
74
|
|
|
if (!isset($data[$module])) { |
75
|
|
|
$data[$module] = Api::getInstance()->call($module . '/Privileges'); |
76
|
|
|
\App\Session::set('modulePermissions', $data); |
77
|
|
|
} |
78
|
|
|
if (isset($data[$module][$action]) && !empty($data[$module][$action])) { |
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get all fields. |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function getFields(): array |
90
|
|
|
{ |
91
|
|
|
if (isset($this->fields)) { |
92
|
|
|
return $this->fields; |
93
|
|
|
} |
94
|
|
|
$data = $this->getFieldsFromApi(); |
95
|
|
|
$fields = []; |
96
|
|
|
foreach ($data['fields'] as $field) { |
97
|
|
|
$fields[$field['name']] = $field; |
98
|
|
|
} |
99
|
|
|
return $this->fields = $fields; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get all fields models. |
104
|
|
|
* |
105
|
|
|
* @return \YF\Modules\Base\FieldTypes\BaseField[] |
106
|
|
|
*/ |
107
|
|
|
public function getFieldsModels(): array |
108
|
|
|
{ |
109
|
|
|
$fields = []; |
110
|
|
|
foreach (array_keys($this->getFields()) as $fieldName) { |
111
|
|
|
$fields[$fieldName] = $this->getFieldModel($fieldName); |
112
|
|
|
} |
113
|
|
|
return $fields; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get fields and blocks. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function getFieldsFromApi(): array |
122
|
|
|
{ |
123
|
|
View Code Duplication |
if (\App\Cache::has('moduleFields', $this->moduleName)) { |
|
|
|
|
124
|
|
|
$data = \App\Cache::get('moduleFields', $this->moduleName); |
125
|
|
|
} else { |
126
|
|
|
$data = Api::getInstance()->call($this->moduleName . '/Fields'); |
127
|
|
|
\App\Cache::save('moduleFields', $this->moduleName, $data); |
128
|
|
|
} |
129
|
|
|
return $data; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get tabs. |
134
|
|
|
* |
135
|
|
|
* @param int $record |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function getTabsFromApi(int $record): array |
140
|
|
|
{ |
141
|
|
|
if (\App\Cache::has('moduleTabs', $this->moduleName)) { |
142
|
|
|
$data = \App\Cache::get('moduleTabs', $this->moduleName); |
143
|
|
|
} else { |
144
|
|
|
$data = Api::getInstance()->call($this->moduleName . '/RelatedModules'); |
145
|
|
|
\App\Cache::save('moduleTabs', $this->moduleName, $data, \App\Cache::LONG); |
146
|
|
|
} |
147
|
|
|
$url = "index.php?module={$this->moduleName}&view=DetailView&record={$record}"; |
148
|
|
|
foreach ($data['base'] as &$row) { |
149
|
|
|
$row['tabId'] = $row['type']; |
150
|
|
|
$row['url'] = "{$url}&tabId={$row['tabId']}&mode={$row['type']}"; |
151
|
|
|
} |
152
|
|
|
foreach ($data['related'] as &$row) { |
153
|
|
|
$row['tabId'] = 'rel' . $row['relationId']; |
154
|
|
|
$row['url'] = "{$url}&tabId={$row['tabId']}&mode=relatedList&relationId={$row['relationId']}&relatedModuleName={$row['relatedModuleName']}"; |
155
|
|
|
} |
156
|
|
|
return $data; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get field by name. |
161
|
|
|
* |
162
|
|
|
* @param string $name |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function getField(string $name): array |
167
|
|
|
{ |
168
|
|
|
if (empty($this->fields)) { |
169
|
|
|
$this->getFields(); |
170
|
|
|
} |
171
|
|
|
if (empty($this->fields[$name])) { |
172
|
|
|
throw new \App\Exceptions\AppException("Field not found: {$name}"); |
173
|
|
|
} |
174
|
|
|
return $this->fields[$name]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get field model by name. |
179
|
|
|
* |
180
|
|
|
* @param string $name |
181
|
|
|
* |
182
|
|
|
* @return \YF\Modules\Base\FieldTypes\BaseField |
183
|
|
|
*/ |
184
|
|
|
public function getFieldModel(string $name): \YF\Modules\Base\FieldTypes\BaseField |
185
|
|
|
{ |
186
|
|
|
if (!isset($this->fieldsModels[$name])) { |
187
|
|
|
$this->fieldsModels[$name] = Field::getInstance($this->moduleName, $this->getField($name)); |
188
|
|
|
} |
189
|
|
|
return $this->fieldsModels[$name]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns default view for module. |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function getDefaultView(): string |
198
|
|
|
{ |
199
|
|
|
return $this->defaultView; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Returns default address url. |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function getDefaultUrl(): string |
208
|
|
|
{ |
209
|
|
|
return "index.php?module={$this->moduleName}&view={$this->getDefaultView()}"; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.