Passed
Push — developer ( 452080...af1fea )
by Mariusz
255:53 queued 220:57
created

DetailView   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 6
dl 0
loc 149
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 5 1
A __construct() 0 4 1
A setRecordModel() 0 4 1
A getRecordModel() 0 4 1
B getLinksHeader() 0 45 6
A getTabsFromApi() 0 19 4
A getWidgets() 0 11 3
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 ($this->record->isPermitted('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
	 * Get tabs.
128
	 *
129
	 * @return array
130
	 */
131
	public function getTabsFromApi(): array
132
	{
133
		if (\App\Cache::has('moduleTabs', $this->moduleName)) {
134
			$data = \App\Cache::get('moduleTabs', $this->moduleName);
135
		} else {
136
			$url = "index.php?module={$this->moduleName}&view=DetailView&record={$this->record->getId()}";
137
			$data = \App\Api::getInstance()->call($this->moduleName . '/RelatedModules/' . $this->record->getId());
138
			foreach ($data['base'] as &$row) {
139
				$row['tabId'] = $row['type'];
140
				$row['url'] = "{$url}&tabId={$row['tabId']}&mode={$row['type']}";
141
			}
142
			foreach ($data['related'] as &$row) {
143
				$row['tabId'] = 'rel' . $row['relationId'];
144
				$row['url'] = "{$url}&tabId={$row['tabId']}&mode=relatedList&relationId={$row['relationId']}&relatedModuleName={$row['relatedModuleName']}";
145
			}
146
			\App\Cache::save('moduleTabs', $this->moduleName, $data, \App\Cache::LONG);
147
		}
148
		return $data;
149
	}
150
151
	/**
152
	 * Gets widgets.
153
	 *
154
	 * @return array
155
	 */
156
	public function getWidgets(): array
157
	{
158
		if (null === $this->widgets) {
159
			$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...
160
			foreach (\App\Widgets::getInstance($this->moduleName)->getAll() as $widgetObject) {
161
				$widgetObject->setRecordId($this->record->getId());
162
				$this->widgets[$widgetObject->getId()] = $widgetObject;
163
			}
164
		}
165
		return $this->widgets;
166
	}
167
}
168