Passed
Push — developer ( 809f4f...4fbea3 )
by Mariusz
37:20 queued 03:40
created

DetailView::getTabsFromApi()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 2
nop 0
1
<?php
2
/**
3
 * Base file model for Detail View.
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    Tomasz Kur <[email protected]>
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 * @author Mariusz Krzaczkowski <[email protected]>
12
 */
13
14
namespace YF\Modules\Base\Model;
15
16
/**
17
 * Base class model for Detail View.
18
 */
19
class DetailView
20
{
21
	/** @var string Name of module. */
22
	protected $moduleName;
23
24
	/** @var \YF\Modules\Base\Model\Record Record model. */
25
	protected $record;
26
27
	/** @var Widget list */
28
	protected $widgets;
29
30
	/**
31
	 * Returns model for detail view.
32
	 *
33
	 * @param string $moduleName
34
	 *
35
	 * @return self
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

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...
36
	 */
37
	public static function getInstance(string $moduleName): self
38
	{
39
		$handlerModule = \App\Loader::getModuleClassName($moduleName, 'Model', 'DetailView');
40
		return new $handlerModule($moduleName);
41
	}
42
43
	/**
44
	 * Constructor.
45
	 *
46
	 * @param string $moduleName
47
	 */
48
	public function __construct(string $moduleName)
49
	{
50
		$this->moduleName = $moduleName;
51
	}
52
53
	/**
54
	 * Sets record model.
55
	 *
56
	 * @param \YF\Modules\Base\Model\Record $recordModel
57
	 *
58
	 * @return void
59
	 */
60
	public function setRecordModel(Record $recordModel): void
61
	{
62
		$this->record = $recordModel;
63
	}
64
65
	/**
66
	 * Get record model.
67
	 *
68
	 * @return \YF\Modules\Base\Model\Record
69
	 */
70
	public function getRecordModel(): Record
71
	{
72
		return $this->record;
73
	}
74
75
	/**
76
	 * Get detail view header links.
77
	 *
78
	 * @return array
79
	 */
80
	public function getLinksHeader(): array
81
	{
82
		$links = [];
83
		if (\YF\Modules\Base\Model\Module::isPermitted($this->moduleName, 'ExportPdf') && \App\Pdf::getTemplates($this->moduleName, $this->record->getId())) {
84
			$links[] = [
85
				'label' => 'BTN_EXPORT_PDF',
86
				'moduleName' => $this->moduleName,
87
				'data' => ['url' => 'index.php?module=' . $this->moduleName . '&view=Pdf&&record=' . $this->record->getId()],
88
				'icon' => 'fas fa-file-pdf',
89
				'class' => 'btn-sm btn-dark js-show-modal js-pdf',
90
				'showLabel' => 1,
91
			];
92
		}
93
		if ($this->record->isEditable()) {
94
			$links[] = [
95
				'label' => 'BTN_EDIT',
96
				'moduleName' => $this->moduleName,
97
				'href' => $this->record->getEditViewUrl(),
98
				'icon' => 'fas fa-edit',
99
				'class' => 'btn-sm btn-success',
100
				'showLabel' => 1,
101
			];
102
		}
103
		if ($this->record->isInventory()) {
104
			$links[] = [
105
				'label' => 'BTN_EDIT',
106
				'moduleName' => $this->moduleName,
107
				'href' => 'index.php?module=Products&view=ShoppingCart&reference_id=' . $this->record->getId() . '&reference_module=' . $this->moduleName,
108
				'icon' => 'fas fa-shopping-cart',
109
				'class' => 'btn-sm btn-success',
110
				'showLabel' => 1,
111
			];
112
		}
113
		if ($this->record->isDeletable()) {
114
			$links[] = [
115
				'label' => 'LBL_DELETE',
116
				'moduleName' => $this->moduleName,
117
				'data' => ['url' => $this->record->getDeleteUrl()],
118
				'icon' => 'fas fa-trash-alt',
119
				'class' => 'btn-sm btn-danger js-delete-record',
120
				'showLabel' => 1,
121
			];
122
		}
123
		return $links;
124
	}
125
126
	/**
127
	 * Gets widgets.
128
	 *
129
	 * @return array
130
	 */
131
	public function getWidgets(): array
132
	{
133
		if (null === $this->widgets) {
134
			$this->widgets = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<YF\Modules\Base\Model\Widget> of property $widgets.

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...
135
			foreach (\App\Widgets::getInstance($this->moduleName)->getAll() as $widgetObject) {
136
				$widgetObject->setRecordId($this->record->getId());
137
				$this->widgets[$widgetObject->getId()] = $widgetObject;
138
			}
139
		}
140
		return $this->widgets;
141
	}
142
}
143