1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Basic record 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 Radosław Skrzypczak <[email protected]> |
10
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace YF\Modules\Base\Model; |
14
|
|
|
|
15
|
|
|
class Record extends \App\BaseModel |
16
|
|
|
{ |
17
|
|
|
/** @var string Module name. */ |
18
|
|
|
protected $moduleName; |
19
|
|
|
|
20
|
|
|
/** @var \YF\Modules\Base\Model\Module Module model instance. */ |
21
|
|
|
protected $moduleModel; |
22
|
|
|
|
23
|
|
|
/** @var array Information about inventory. */ |
24
|
|
|
protected $inventoryData = []; |
25
|
|
|
|
26
|
|
|
/** @var array Information about summary inventory. */ |
27
|
|
|
protected $inventorySummaryData = []; |
28
|
|
|
|
29
|
|
|
/** @var string Record ID. */ |
30
|
|
|
protected $id; |
31
|
|
|
|
32
|
|
|
/** @var string Record name. */ |
33
|
|
|
protected $name = ''; |
34
|
|
|
|
35
|
|
|
/** @var array Privileges. */ |
36
|
|
|
protected $privileges = []; |
37
|
|
|
|
38
|
|
|
/** @var array Custom data. */ |
39
|
|
|
protected $customData = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Static Function to get the instance of a clean Record for the given module name. |
43
|
|
|
* |
44
|
|
|
* @param string $module |
45
|
|
|
* |
46
|
|
|
* @return \self |
47
|
|
|
*/ |
48
|
|
|
public static function getInstance(string $module): self |
49
|
|
|
{ |
50
|
|
|
$handlerModule = \App\Loader::getModuleClassName($module, 'Model', 'Record'); |
51
|
|
|
$instance = new $handlerModule(); |
52
|
|
|
return $instance->setModuleName($module); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Static function to get the instance of record. |
57
|
|
|
* |
58
|
|
|
* @param string $moduleName |
59
|
|
|
* @param int $recordId |
60
|
|
|
* @param array $headers |
61
|
|
|
* |
62
|
|
|
* @return \self |
63
|
|
|
*/ |
64
|
|
|
public static function getInstanceById(string $moduleName, int $recordId, array $headers = []): self |
65
|
|
|
{ |
66
|
|
|
$api = \App\Api::getInstance(); |
67
|
|
|
if ($headers) { |
|
|
|
|
68
|
|
|
$api->setCustomHeaders($headers); |
69
|
|
|
} |
70
|
|
|
$result = $api->call("{$moduleName}/Record/{$recordId}"); |
71
|
|
|
$instance = self::getInstance($moduleName); |
72
|
|
|
$instance->setData($result['data'] ?? []); |
73
|
|
|
$instance->setInventoryData($result['inventory'] ?? [], $result['summaryInventory'] ?? []); |
74
|
|
|
$instance->privileges = $result['privileges'] ?? []; |
75
|
|
|
$instance->name = $result['name'] ?? ''; |
76
|
|
|
unset($result['data'], $result['inventory'], $result['summaryInventory'], $result['privileges'], $result['name']); |
77
|
|
|
$instance->customData = $result; |
78
|
|
|
$instance->setId($recordId); |
79
|
|
|
return $instance; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets information about inventory. |
84
|
|
|
* |
85
|
|
|
* @param array $values |
86
|
|
|
* @param array $summary |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function setInventoryData(array $values, array $summary = []) |
91
|
|
|
{ |
92
|
|
|
$this->inventoryData = $values; |
93
|
|
|
$this->inventorySummaryData = $summary; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns information about inventory. |
98
|
|
|
* |
99
|
|
|
* @return void |
|
|
|
|
100
|
|
|
*/ |
101
|
|
|
public function getInventoryData() |
102
|
|
|
{ |
103
|
|
|
return $this->inventoryData; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns information about summary inventory. |
108
|
|
|
* |
109
|
|
|
* @return void |
|
|
|
|
110
|
|
|
*/ |
111
|
|
|
public function getInventorySummary() |
112
|
|
|
{ |
113
|
|
|
return $this->inventorySummaryData; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function isInventory() |
117
|
|
|
{ |
118
|
|
|
return !empty($this->inventoryData); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Function to get the id of the record. |
123
|
|
|
* |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
|
|
public function getId(): int |
127
|
|
|
{ |
128
|
|
|
return $this->id; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Function to set the id of the record. |
133
|
|
|
* |
134
|
|
|
* @param int $value |
135
|
|
|
* |
136
|
|
|
* @return self |
137
|
|
|
*/ |
138
|
|
|
public function setId(int $value): self |
139
|
|
|
{ |
140
|
|
|
$this->id = $value; |
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Function to get the raw value. |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function getRawData(): array |
150
|
|
|
{ |
151
|
|
|
return $this->customData['rawData'] ?? []; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set raw data. |
156
|
|
|
* |
157
|
|
|
* @param array $rawData |
158
|
|
|
* |
159
|
|
|
* @return self |
160
|
|
|
*/ |
161
|
|
|
public function setRawData(array $rawData): self |
162
|
|
|
{ |
163
|
|
|
$this->customData['rawData'] = $rawData; |
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Function to get the raw value for a given key. |
169
|
|
|
* |
170
|
|
|
* @param string $key |
171
|
|
|
* |
172
|
|
|
* @return mixed |
173
|
|
|
*/ |
174
|
|
|
public function getRawValue(string $key) |
175
|
|
|
{ |
176
|
|
|
return $this->customData['rawData'][$key] ?? ''; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Function to set the raw value for a given key. |
181
|
|
|
* |
182
|
|
|
* @param string $key |
183
|
|
|
* @param mixed $value |
184
|
|
|
* |
185
|
|
|
* @return self |
186
|
|
|
*/ |
187
|
|
|
public function setRawValue(string $key, $value): self |
188
|
|
|
{ |
189
|
|
|
$this->customData['rawData'][$key] = $value; |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Function to get the name of the module to which the record belongs. |
195
|
|
|
* |
196
|
|
|
* @return string - Record Module Name |
197
|
|
|
*/ |
198
|
|
|
public function getModuleName(): string |
199
|
|
|
{ |
200
|
|
|
return $this->moduleName; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get record module model instance. |
205
|
|
|
* |
206
|
|
|
* @return \YF\Modules\Base\Model\Module - Record module model instance |
207
|
|
|
*/ |
208
|
|
|
public function getModuleModel(): Module |
209
|
|
|
{ |
210
|
|
|
return $this->moduleModel; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Set record name. |
215
|
|
|
* |
216
|
|
|
* @param string $name |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function setName(string $name) |
221
|
|
|
{ |
222
|
|
|
return $this->name = $name; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Record name. |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function getName() |
231
|
|
|
{ |
232
|
|
|
return $this->name; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Get custom data. |
237
|
|
|
* |
238
|
|
|
* @return array |
239
|
|
|
*/ |
240
|
|
|
public function getCustomData(): array |
241
|
|
|
{ |
242
|
|
|
return $this->customData; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Function to get the raw value for a given key. |
247
|
|
|
* |
248
|
|
|
* @param string $key |
249
|
|
|
* |
250
|
|
|
* @return string |
251
|
|
|
*/ |
252
|
|
|
public function getDisplayValue(string $key): string |
253
|
|
|
{ |
254
|
|
|
return \App\Purifier::encodeHtml($this->get($key)); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get list display value. |
259
|
|
|
* |
260
|
|
|
* @param string $key |
261
|
|
|
* |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
|
|
public function getListDisplayValue(string $key): string |
265
|
|
|
{ |
266
|
|
|
$fieldModel = $this->getModuleModel()->getFieldModel($key); |
267
|
|
|
$value = ''; |
268
|
|
|
if ($fieldModel->isViewable()) { |
269
|
|
|
if ($this->has($key)) { |
270
|
|
|
$fieldModel->setDisplayValue($this->get($key)); |
271
|
|
|
if (isset($this->customData['rawData'][$key])) { |
272
|
|
|
$fieldModel->setRawValue($this->customData['rawData'][$key]); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
$value = $fieldModel->getListDisplayValue(); |
276
|
|
|
} |
277
|
|
|
return $value; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Function to set the name of the module to which the record belongs. |
282
|
|
|
* |
283
|
|
|
* @param string $moduleName |
284
|
|
|
* |
285
|
|
|
* @return \self |
|
|
|
|
286
|
|
|
*/ |
287
|
|
|
public function setModuleName(string $moduleName): self |
288
|
|
|
{ |
289
|
|
|
$this->moduleName = $moduleName; |
290
|
|
|
$this->moduleModel = \YF\Modules\Base\Model\Module::getInstance($moduleName); |
|
|
|
|
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Function to get the list view actions for the record. |
296
|
|
|
* |
297
|
|
|
* @return string |
298
|
|
|
*/ |
299
|
|
|
public function getRecordListViewActions(): string |
300
|
|
|
{ |
301
|
|
|
$recordLinks = []; |
302
|
|
|
if ($this->isViewable()) { |
303
|
|
|
$recordLinks[] = [ |
304
|
|
|
'label' => 'LBL_SHOW_COMPLETE_DETAILS', |
305
|
|
|
'moduleName' => $this->getModuleName(), |
306
|
|
|
'href' => $this->getDetailViewUrl(), |
307
|
|
|
'icon' => 'fas fa-th-list', |
308
|
|
|
'class' => 'btn-sm btn-info active js-popover-tooltip', |
309
|
|
|
]; |
310
|
|
|
} |
311
|
|
|
if ($this->isEditable()) { |
312
|
|
|
$recordLinks[] = [ |
313
|
|
|
'label' => 'BTN_EDIT', |
314
|
|
|
'moduleName' => $this->getModuleName(), |
315
|
|
|
'href' => $this->getEditViewUrl(), |
316
|
|
|
'icon' => 'fas fa-edit', |
317
|
|
|
'class' => 'btn-sm btn-success active js-popover-tooltip', |
318
|
|
|
]; |
319
|
|
|
} |
320
|
|
|
if ($this->isDeletable()) { |
321
|
|
|
$recordLinks[] = [ |
322
|
|
|
'label' => 'LBL_DELETE', |
323
|
|
|
'moduleName' => $this->getModuleName(), |
324
|
|
|
'data' => ['url' => $this->getDeleteUrl()], |
325
|
|
|
'icon' => 'fas fa-trash-alt', |
326
|
|
|
'class' => 'btn-sm btn-danger active js-delete-record js-popover-tooltip', |
327
|
|
|
]; |
328
|
|
|
} |
329
|
|
|
return \App\Layout\Action::getListViewActions($recordLinks); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Function checks if there are permissions to preview record. |
334
|
|
|
* |
335
|
|
|
* @return bool |
336
|
|
|
*/ |
337
|
|
|
public function isViewable() |
338
|
|
|
{ |
339
|
|
|
return true; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Function to get the Detail View url for the record. |
344
|
|
|
* |
345
|
|
|
* @return string - Record Detail View Url |
346
|
|
|
*/ |
347
|
|
|
public function getDetailViewUrl() |
348
|
|
|
{ |
349
|
|
|
return 'index.php?module=' . $this->getModuleName() . '&view=DetailView&record=' . $this->getId(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Function checks if there are permissions to edit record. |
354
|
|
|
* |
355
|
|
|
* @return bool |
356
|
|
|
*/ |
357
|
|
View Code Duplication |
public function isEditable(): bool |
|
|
|
|
358
|
|
|
{ |
359
|
|
|
if (!isset($this->privileges['isEditable'])) { |
360
|
|
|
$this->privileges['isEditable'] = \YF\Modules\Base\Model\Module::isPermitted($this->getModuleName(), 'EditView', $this->getId()); |
|
|
|
|
361
|
|
|
} |
362
|
|
|
return $this->privileges['isEditable']; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Function to get the Edit View url for the record. |
367
|
|
|
* |
368
|
|
|
* @return string - Record Edit View Url |
369
|
|
|
*/ |
370
|
|
|
public function getEditViewUrl() |
371
|
|
|
{ |
372
|
|
|
return 'index.php?module=' . $this->getModuleName() . '&view=EditView&record=' . $this->getId(); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Function checks if there are permissions to delete record. |
377
|
|
|
* |
378
|
|
|
* @return bool |
379
|
|
|
*/ |
380
|
|
View Code Duplication |
public function isDeletable(): bool |
|
|
|
|
381
|
|
|
{ |
382
|
|
|
if (!isset($this->privileges['moveToTrash'])) { |
383
|
|
|
$this->privileges['moveToTrash'] = \YF\Modules\Base\Model\Module::isPermitted($this->getModuleName(), 'MoveToTrash', $this->getId()); |
|
|
|
|
384
|
|
|
} |
385
|
|
|
return $this->privileges['moveToTrash']; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Function checks permissions to action. |
390
|
|
|
* |
391
|
|
|
* @param string $actionName |
392
|
|
|
* |
393
|
|
|
* @return bool |
394
|
|
|
*/ |
395
|
|
|
public function isPermitted(string $actionName): bool |
396
|
|
|
{ |
397
|
|
|
if (!isset($this->privileges[$actionName])) { |
398
|
|
|
$this->privileges[$actionName] = \YF\Modules\Base\Model\Module::isPermitted($this->getModuleName(), $actionName, $this->getId()); |
|
|
|
|
399
|
|
|
} |
400
|
|
|
return $this->privileges[$actionName]; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Function to get the delete action url for the record. |
405
|
|
|
* |
406
|
|
|
* @return string |
407
|
|
|
*/ |
408
|
|
|
public function getDeleteUrl() |
409
|
|
|
{ |
410
|
|
|
return 'index.php?module=' . $this->getModuleName() . '&action=Delete&record=' . $this->getId(); |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
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.