Passed
Push — developer ( e0199d...a3edbd )
by Mariusz
63:10 queued 29:14
created

Record::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Basic record model file.
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
/**
16
 * Basic record model class.
17
 */
18
class Record extends \App\BaseModel
19
{
20
	/** @var string Record ID. */
21
	protected $id;
22
23
	/** @var string Record name. */
24
	protected $name = '';
25
26
	/** @var string Module name. */
27
	protected $moduleName;
28
29
	/** @var array Information about inventory. */
30
	protected $inventoryData = [];
31
32
	/** @var array Information about summary inventory. */
33
	protected $inventorySummaryData = [];
34
35
	/** @var string Privileges. */
36
	protected $privileges = [];
37
38
	/** @var array Custom data. */
39
	protected $customData = [];
40
41
	/** @var \YF\Modules\Base\Model\Module Module model instance. */
42
	protected $moduleModel;
43
44
	/**
45
	 * Static Function to get the instance of a clean Record for the given module name.
46
	 *
47
	 * @param string $module
48
	 *
49
	 * @return \self
50
	 */
51
	public static function getInstance(string $module): self
52
	{
53
		$handlerModule = \App\Loader::getModuleClassName($module, 'Model', 'Record');
54
		$instance = new $handlerModule();
55
		return $instance->setModuleName($module);
56
	}
57
58
	/**
59
	 * Static function to get the instance of record.
60
	 *
61
	 * @param string $moduleName
62
	 * @param int    $recordId
63
	 * @param array  $headers
64
	 *
65
	 * @return \self
66
	 */
67
	public static function getInstanceById(string $moduleName, int $recordId, array $headers = []): self
68
	{
69
		$api = \App\Api::getInstance();
70
		if ($headers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $headers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
71
			$api->setCustomHeaders($headers);
72
		}
73
		$result = $api->call("{$moduleName}/Record/{$recordId}");
74
		$instance = self::getInstance($moduleName);
75
		$instance->setData($result['data'] ?? []);
76
		$instance->setInventoryData($result['inventory'] ?? [], $result['summaryInventory'] ?? []);
77
		$instance->setPrivileges($result['privileges'] ?? []);
78
		$instance->name = $result['name'] ?? '';
79
		unset($result['data'], $result['inventory'], $result['summaryInventory'], $result['privileges'], $result['name']);
80
		$instance->customData = $result;
81
		$instance->setId($recordId);
82
		return $instance;
83
	}
84
85
	/**
86
	 * Sets information about inventory.
87
	 *
88
	 * @param array $values
89
	 * @param array $summary
90
	 *
91
	 * @return void
92
	 */
93
	public function setInventoryData(array $values, array $summary = [])
94
	{
95
		$this->inventoryData = $values;
96
		$this->inventorySummaryData = $summary;
97
	}
98
99
	/**
100
	 * Returns information about inventory.
101
	 *
102
	 * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
103
	 */
104
	public function getInventoryData()
105
	{
106
		return $this->inventoryData;
107
	}
108
109
	/**
110
	 * Returns information about summary inventory.
111
	 *
112
	 * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
113
	 */
114
	public function getInventorySummary()
115
	{
116
		return $this->inventorySummaryData;
117
	}
118
119
	public function isInventory()
120
	{
121
		return !empty($this->inventoryData);
122
	}
123
124
	/**
125
	 * Function to get the id of the record.
126
	 *
127
	 * @return int|null
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

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.

Loading history...
128
	 */
129
	public function getId(): ?int
130
	{
131
		return $this->id;
132
	}
133
134
	/**
135
	 * Function to set the id of the record.
136
	 *
137
	 * @param int $value
138
	 *
139
	 * @return self
140
	 */
141
	public function setId(int $value): self
142
	{
143
		$this->id = $value;
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type string, but $value is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
144
		return $this;
145
	}
146
147
	/**
148
	 * Function to get the raw value.
149
	 *
150
	 * @return array
151
	 */
152
	public function getRawData(): array
153
	{
154
		return $this->customData['rawData'] ?? [];
155
	}
156
157
	/**
158
	 * Set raw data.
159
	 *
160
	 * @param array $rawData
161
	 *
162
	 * @return self
163
	 */
164
	public function setRawData(array $rawData): self
165
	{
166
		$this->customData['rawData'] = $rawData;
167
		return $this;
168
	}
169
170
	/**
171
	 * Function to get the raw value for a given key.
172
	 *
173
	 * @param string $key
174
	 *
175
	 * @return mixed
176
	 */
177
	public function getRawValue(string $key)
178
	{
179
		return $this->customData['rawData'][$key] ?? '';
180
	}
181
182
	/**
183
	 * Function to set the raw value for a given key.
184
	 *
185
	 * @param string $key
186
	 * @param mixed  $value
187
	 *
188
	 * @return self
189
	 */
190
	public function setRawValue(string $key, $value): self
191
	{
192
		$this->customData['rawData'][$key] = $value;
193
		return $this;
194
	}
195
196
	/**
197
	 * Function to get the name of the module to which the record belongs.
198
	 *
199
	 * @return string - Record Module Name
200
	 */
201
	public function getModuleName(): string
202
	{
203
		return $this->moduleName;
204
	}
205
206
	/**
207
	 * Get record module model instance.
208
	 *
209
	 * @return \YF\Modules\Base\Model\Module - Record module model instance
210
	 */
211
	public function getModuleModel(): Module
212
	{
213
		return $this->moduleModel;
214
	}
215
216
	/**
217
	 * Set record name.
218
	 *
219
	 * @param string $name
220
	 *
221
	 * @return string
222
	 */
223
	public function setName(string $name)
224
	{
225
		return $this->name = $name;
226
	}
227
228
	/**
229
	 * Record name.
230
	 *
231
	 * @return string
232
	 */
233
	public function getName()
234
	{
235
		return $this->name;
236
	}
237
238
	/**
239
	 * Get custom data.
240
	 *
241
	 * @return array
242
	 */
243
	public function getCustomData(): array
244
	{
245
		return $this->customData;
246
	}
247
248
	/**
249
	 * Set privileges.
250
	 *
251
	 * @param bool[] $privileges
252
	 *
253
	 * @return self
254
	 */
255
	public function setPrivileges(array $privileges): self
256
	{
257
		$this->privileges = $privileges;
0 ignored issues
show
Documentation Bug introduced by
It seems like $privileges of type array<integer,boolean> is incompatible with the declared type string of property $privileges.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
258
		return $this;
259
	}
260
261
	/**
262
	 * Function to get the raw value for a given key.
263
	 *
264
	 * @param string $key
265
	 *
266
	 * @return string
267
	 */
268
	public function getDisplayValue(string $key): string
269
	{
270
		return !isset($this->getModuleModel()->getFields()[$key]) ? \App\Purifier::encodeHtml($this->get($key)) : $this->getModuleModel()->getFieldModel($key)->getDisplayValue($this->get($key), $this);
271
	}
272
273
	/**
274
	 * Get list display value.
275
	 *
276
	 * @param string $key
277
	 *
278
	 * @return string
279
	 */
280
	public function getListDisplayValue(string $key): string
281
	{
282
		$fieldModel = $this->getModuleModel()->getFieldModel($key);
283
		$value = '';
284
		if ($fieldModel->isViewable()) {
285
			$value = $fieldModel->getListDisplayValue($this->get($key), $this);
286
		}
287
		return $value;
288
	}
289
290
	/**
291
	 * Function to set the name of the module to which the record belongs.
292
	 *
293
	 * @param string $moduleName
294
	 *
295
	 * @return \self
0 ignored issues
show
Documentation introduced by
Should the return type not be Record?

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.

Loading history...
296
	 */
297
	public function setModuleName(string $moduleName): self
298
	{
299
		$this->moduleName = $moduleName;
300
		$this->moduleModel = \YF\Modules\Base\Model\Module::getInstance($moduleName);
0 ignored issues
show
Documentation Bug introduced by
It seems like \YF\Modules\Base\Model\M...etInstance($moduleName) of type object<self> is incompatible with the declared type object<YF\Modules\Base\Model\Module> of property $moduleModel.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
301
		return $this;
302
	}
303
304
	/**
305
	 * Function checks if there are permissions to preview record.
306
	 *
307
	 * @return bool
308
	 */
309
	public function isViewable()
310
	{
311
		return true;
312
	}
313
314
	/**
315
	 * Function checks if there are permissions to edit record.
316
	 *
317
	 * @return bool
318
	 */
319
	public function isEditable(): bool
320
	{
321
		return $this->privileges['isEditable'];
322
	}
323
324
	/**
325
	 * Function checks if there are permissions to delete record.
326
	 *
327
	 * @return bool
328
	 */
329
	public function isDeletable(): bool
330
	{
331
		return $this->privileges['moveToTrash'];
332
	}
333
334
	/**
335
	 * Function checks permissions to action.
336
	 *
337
	 * @param string $actionName
338
	 *
339
	 * @return bool
340
	 */
341
	public function isPermitted(string $actionName): bool
342
	{
343
		return $this->privileges[$actionName];
344
	}
345
346
	/**
347
	 * Function to get the list view actions for the record.
348
	 *
349
	 * @return string
350
	 */
351
	public function getListViewActions(): string
352
	{
353
		$recordLinks = [];
354
		if ($this->isViewable()) {
355
			$recordLinks[] = [
356
				'label' => 'LBL_SHOW_COMPLETE_DETAILS',
357
				'moduleName' => $this->getModuleName(),
358
				'href' => $this->getDetailViewUrl(),
359
				'icon' => 'fas fa-th-list',
360
				'class' => 'btn-sm btn-info active js-popover-tooltip',
361
			];
362
		}
363
		if ($this->isEditable()) {
364
			$recordLinks[] = [
365
				'label' => 'BTN_EDIT',
366
				'moduleName' => $this->getModuleName(),
367
				'href' => $this->getEditViewUrl(),
368
				'icon' => 'fas fa-edit',
369
				'class' => 'btn-sm btn-success active js-popover-tooltip',
370
			];
371
		}
372
		if ($this->isDeletable()) {
373
			$recordLinks[] = [
374
				'label' => 'LBL_DELETE',
375
				'moduleName' => $this->getModuleName(),
376
				'data' => ['url' => $this->getDeleteUrl()],
377
				'icon' => 'fas fa-trash-alt',
378
				'class' => 'btn-sm btn-danger active js-delete-record js-popover-tooltip',
379
			];
380
		}
381
		return \App\Layout\Action::getListViewActions($recordLinks);
382
	}
383
384
	/**
385
	 * Function to get the list view actions for the record.
386
	 *
387
	 * @return string
388
	 */
389
	public function getRelatedListActions(): string
390
	{
391
		$recordLinks = [];
392
		if ($this->isViewable()) {
393
			$recordLinks[] = [
394
				'label' => 'LBL_SHOW_COMPLETE_DETAILS',
395
				'moduleName' => $this->getModuleName(),
396
				'href' => $this->getDetailViewUrl(),
397
				'icon' => 'fas fa-th-list',
398
				'class' => 'btn-sm btn-info active js-popover-tooltip',
399
			];
400
		}
401
		if ($this->isEditable()) {
402
			$recordLinks[] = [
403
				'label' => 'BTN_EDIT',
404
				'moduleName' => $this->getModuleName(),
405
				'href' => $this->getEditViewUrl(),
406
				'icon' => 'fas fa-edit',
407
				'class' => 'btn-sm btn-success active js-popover-tooltip',
408
			];
409
		}
410
		return \App\Layout\Action::getListViewActions($recordLinks);
411
	}
412
413
	/**
414
	 * Function to get the Detail View url for the record.
415
	 *
416
	 * @return string - Record Detail View Url
417
	 */
418
	public function getDetailViewUrl()
419
	{
420
		return 'index.php?module=' . $this->getModuleName() . '&view=DetailView&record=' . $this->getId();
421
	}
422
423
	/**
424
	 * Function to get the Edit View url for the record.
425
	 *
426
	 * @return string - Record Edit View Url
427
	 */
428
	public function getEditViewUrl()
429
	{
430
		return 'index.php?module=' . $this->getModuleName() . '&view=EditView&record=' . $this->getId();
431
	}
432
433
	/**
434
	 * Function to get the delete action url for the record.
435
	 *
436
	 * @return string
437
	 */
438
	public function getDeleteUrl()
439
	{
440
		return 'index.php?module=' . $this->getModuleName() . '&action=Delete&record=' . $this->getId();
441
	}
442
}
443