Passed
Push — developer ( f00fa8...02e24e )
by Radosław
53:12 queued 18:13
created

DetailView::getWidgets()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
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 detail view header links.
67
	 *
68
	 * @return array
69
	 */
70
	public function getLinksHeader(): array
71
	{
72
		$links = [];
73
		if ($this->record->isPermitted('ExportPdf') && \App\Pdf::getTemplates($this->moduleName, $this->record->getId())) {
74
			$links[] = [
75
				'label' => 'BTN_EXPORT_PDF',
76
				'moduleName' => $this->moduleName,
77
				'data' => ['url' => 'index.php?module=' . $this->moduleName . '&view=Pdf&&record=' . $this->record->getId()],
78
				'icon' => 'fas fa-file-pdf',
79
				'class' => 'btn-sm btn-dark js-show-modal js-pdf',
80
				'showLabel' => 1,
81
			];
82
		}
83
		if ($this->record->isEditable()) {
84
			$links[] = [
85
				'label' => 'BTN_EDIT',
86
				'moduleName' => $this->moduleName,
87
				'href' => $this->record->getEditViewUrl(),
88
				'icon' => 'fas fa-edit',
89
				'class' => 'btn-sm btn-success',
90
				'showLabel' => 1,
91
			];
92
		}
93
		if ($this->record->isInventory()) {
94
			$links[] = [
95
				'label' => 'BTN_EDIT',
96
				'moduleName' => $this->moduleName,
97
				'href' => 'index.php?module=Products&view=ShoppingCart&reference_id=' . $this->record->getId() . '&reference_module=' . $this->moduleName,
98
				'icon' => 'fas fa-shopping-cart',
99
				'class' => 'btn-sm btn-success',
100
				'showLabel' => 1,
101
			];
102
		}
103
		if ($this->record->isDeletable()) {
104
			$links[] = [
105
				'label' => 'LBL_DELETE',
106
				'moduleName' => $this->moduleName,
107
				'data' => ['url' => $this->record->getDeleteUrl()],
108
				'icon' => 'fas fa-trash-alt',
109
				'class' => 'btn-sm btn-danger js-delete-record',
110
				'showLabel' => 1,
111
			];
112
		}
113
		return $links;
114
	}
115
116
	/**
117
	 * Get tabs.
118
	 *
119
	 * @return array
120
	 */
121
	public function getTabsFromApi(): array
122
	{
123
		if (\App\Cache::has('moduleTabs', $this->moduleName)) {
124
			$data = \App\Cache::get('moduleTabs', $this->moduleName);
125
		} else {
126
			$url = "index.php?module={$this->moduleName}&view=DetailView&record={$this->record->getId()}";
127
			$data = \App\Api::getInstance()->call($this->moduleName . '/RelatedModules/' . $this->record->getId());
128
			foreach ($data['base'] as &$row) {
129
				$row['tabId'] = $row['type'];
130
				$row['url'] = "{$url}&tabId={$row['tabId']}&mode={$row['type']}";
131
			}
132
			foreach ($data['related'] as &$row) {
133
				$row['tabId'] = 'rel' . $row['relationId'];
134
				$row['url'] = "{$url}&tabId={$row['tabId']}&mode=relatedList&relationId={$row['relationId']}&relatedModuleName={$row['relatedModuleName']}";
135
			}
136
			\App\Cache::save('moduleTabs', $this->moduleName, $data, \App\Cache::LONG);
137
		}
138
		return $data;
139
	}
140
141
	/**
142
	 * Gets widgets.
143
	 *
144
	 * @return array
145
	 */
146
	public function getWidgets(): array
147
	{
148
		if (null === $this->widgets) {
149
			$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...
150
			foreach (\App\Widgets::getInstance($this->moduleName)->getAll() as $widgetObject) {
151
				$widgetObject->setRecordId($this->record->getId());
152
				$this->widgets[$widgetObject->getId()] = $widgetObject;
153
			}
154
		}
155
		return $this->widgets;
156
	}
157
}
158