Passed
Push — developer ( 19176d...6e2ae0 )
by Radosław
48:20 queued 13:37
created

Module::isPermittedByModule()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4555
c 0
b 0
f 0
cc 5
nc 8
nop 2
1
<?php
2
/**
3
 * Basic module 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    Mariusz Krzaczkowski <[email protected]>
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 */
12
13
namespace YF\Modules\Base\Model;
14
15
use App;
16
use App\Api;
17
18
class Module
19
{
20
	/** @var YF\Modules\Base\Model\Module[] Module model cache. */
21
	protected static $cache;
22
23
	/** @var string Module name. */
24
	protected $moduleName;
25
26
	/** @var array Fields. */
27
	protected $fields;
28
29
	/** @var array Fields models. */
30
	protected $fieldsModels;
31
32
	protected $defaultView = 'ListView';
33
34
	/**
35
	 * Get module model instance.
36
	 *
37
	 * @param string $moduleName
38
	 *
39
	 * @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...
40
	 */
41
	public static function getInstance(string $moduleName): self
42
	{
43
		if (isset(self::$cache[$moduleName])) {
44
			return self::$cache[$moduleName];
45
		}
46
		$handlerModule = App\Loader::getModuleClassName($moduleName, 'Model', 'Module');
47
		return self::$cache[$moduleName] = new $handlerModule($moduleName);
48
	}
49
50
	/**
51
	 * Constructor  function.
52
	 *
53
	 * @param string $moduleName
54
	 */
55
	public function __construct(string $moduleName)
56
	{
57
		$this->moduleName = $moduleName;
58
	}
59
60
	/**
61
	 * Function to check permission for a Module/Action.
62
	 *
63
	 * @param string $module
64
	 * @param string $action
65
	 *
66
	 * @return bool
67
	 */
68
	public static function isPermittedByModule(string $module, string $action)
69
	{
70
		if (!\App\Session::has('modulePermissions')) {
71
			\App\Session::set('modulePermissions', []);
72
		}
73
		$data = \App\Session::get('modulePermissions');
74
		if (!isset($data[$module])) {
75
			$data[$module] = Api::getInstance()->call($module . '/Privileges');
76
			\App\Session::set('modulePermissions', $data);
77
		}
78
		if (isset($data[$module][$action]) && !empty($data[$module][$action])) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return isset($data[$modu...ata[$module][$action]);.
Loading history...
79
			return true;
80
		}
81
		return false;
82
	}
83
84
	/**
85
	 * unction to check permission for a Module/Action.
86
	 *
87
	 * @param string $action
88
	 *
89
	 * @return bool
90
	 */
91
	public function isPermitted(string $action): bool
92
	{
93
		return self::isPermittedByModule($this->moduleName, $action);
94
	}
95
96
	/**
97
	 * Get all fields.
98
	 *
99
	 * @return array
100
	 */
101
	public function getFields(): array
102
	{
103
		if (isset($this->fields)) {
104
			return $this->fields;
105
		}
106
		$data = $this->getFieldsFromApi();
107
		$fields = [];
108
		foreach ($data['fields'] as $field) {
109
			$fields[$field['name']] = $field;
110
		}
111
		return $this->fields = $fields;
112
	}
113
114
	/**
115
	 * Get all fields models.
116
	 *
117
	 * @return \YF\Modules\Base\FieldTypes\BaseField[]
118
	 */
119
	public function getFieldsModels(): array
120
	{
121
		$fields = [];
122
		foreach (array_keys($this->getFields()) as $fieldName) {
123
			$fields[$fieldName] = $this->getFieldModel($fieldName);
124
		}
125
		return $fields;
126
	}
127
128
	/**
129
	 * Get fields and blocks.
130
	 *
131
	 * @return array
132
	 */
133
	public function getFieldsFromApi(): array
134
	{
135 View Code Duplication
		if (\App\Cache::has('moduleFields', $this->moduleName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
			$data = \App\Cache::get('moduleFields', $this->moduleName);
137
		} else {
138
			$data = Api::getInstance()->call($this->moduleName . '/Fields');
139
			\App\Cache::save('moduleFields', $this->moduleName, $data);
140
		}
141
		return $data;
142
	}
143
144
	/**
145
	 * Get tabs.
146
	 *
147
	 * @param int $record
148
	 *
149
	 * @return array
150
	 */
151
	public function getTabsFromApi(int $record): array
152
	{
153
		if (\App\Cache::has('moduleTabs', $this->moduleName)) {
154
			$data = \App\Cache::get('moduleTabs', $this->moduleName);
155
		} else {
156
			$data = Api::getInstance()->call($this->moduleName . '/RelatedModules');
157
			\App\Cache::save('moduleTabs', $this->moduleName, $data, \App\Cache::LONG);
158
		}
159
		$url = "index.php?module={$this->moduleName}&view=DetailView&record={$record}";
160
		foreach ($data['base'] as &$row) {
161
			$row['tabId'] = $row['type'];
162
			$row['url'] = "{$url}&tabId={$row['tabId']}&mode={$row['type']}";
163
		}
164
		foreach ($data['related'] as &$row) {
165
			$row['tabId'] = 'rel' . $row['relationId'];
166
			$row['url'] = "{$url}&tabId={$row['tabId']}&mode=relatedList&relationId={$row['relationId']}&relatedModuleName={$row['relatedModuleName']}";
167
		}
168
		return $data;
169
	}
170
171
	/**
172
	 * Get field by name.
173
	 *
174
	 * @param string $name
175
	 *
176
	 * @return array
177
	 */
178
	public function getField(string $name): array
179
	{
180
		if (empty($this->fields)) {
181
			$this->getFields();
182
		}
183
		if (empty($this->fields[$name])) {
184
			throw new \App\Exceptions\AppException("Field not found: {$name}");
185
		}
186
		return $this->fields[$name];
187
	}
188
189
	/**
190
	 * Get field model by name.
191
	 *
192
	 * @param string $name
193
	 *
194
	 * @return \YF\Modules\Base\FieldTypes\BaseField
195
	 */
196
	public function getFieldModel(string $name): \YF\Modules\Base\FieldTypes\BaseField
197
	{
198
		if (!isset($this->fieldsModels[$name])) {
199
			$this->fieldsModels[$name] = Field::getInstance($this->moduleName, $this->getField($name));
200
		}
201
		return $this->fieldsModels[$name];
202
	}
203
204
	/**
205
	 * Returns default view for module.
206
	 *
207
	 * @return string
208
	 */
209
	public function getDefaultView(): string
210
	{
211
		return $this->defaultView;
212
	}
213
214
	/**
215
	 * Returns default address url.
216
	 *
217
	 * @return string
218
	 */
219
	public function getDefaultUrl(): string
220
	{
221
		return "index.php?module={$this->moduleName}&view={$this->getDefaultView()}";
222
	}
223
}
224