Passed
Push — developer ( 8db73e...c6f2b2 )
by Radosław
34:04
created

RelatedModule::getModuleName()   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
 * The file contains: Abstract class ListView.
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
 */
11
12
namespace YF\Modules\Base\Widget;
13
14
use YF\Modules\Base\Model\Record;
15
16
/**
17
 * Abstract class ListView.
18
 */
19
class RelatedModule extends \App\BaseModel
20
{
21
	/** @var string Widget type. */
22
	protected $type = 'RelatedModule';
23
24
	/** @var string Module name. */
25
	protected $moduleName;
26
27
	/** @var int Source record ID. */
28
	protected $recordId;
29
30
	/** @var array Entries. */
31
	protected $entries;
32
33
	/** @var array Field list. */
34
	protected $headers;
35
36
	/** @var bool More pages. */
37
	protected $isMorePages;
38
39
	/** @var array Scripts. */
40
	public $scripts = [];
41
42
	/**
43
	 * Constructor.
44
	 *
45
	 * @param string $moduleName
46
	 */
47
	public function __construct(string $moduleName)
48
	{
49
		$this->moduleName = $moduleName;
50
	}
51
52
	/**
53
	 * Gets widget ID.
54
	 *
55
	 * @return int
56
	 */
57
	public function getId()
58
	{
59
		return $this->get('id');
60
	}
61
62
	/**
63
	 * Gets widget type.
64
	 *
65
	 * @return string
66
	 */
67
	public function getType(): string
68
	{
69
		return $this->type;
70
	}
71
72
	/**
73
	 * Gets module name.
74
	 *
75
	 * @return string
76
	 */
77
	public function getModuleName(): string
78
	{
79
		return $this->moduleName;
80
	}
81
82
	/**
83
	 * Gets widget name.
84
	 *
85
	 * @return string
86
	 */
87
	public function getTitle(): string
88
	{
89
		return $this->get('name');
90
	}
91
92
	/**
93
	 * Sets record ID.
94
	 *
95
	 * @param int $recordId
96
	 *
97
	 * @return self
98
	 */
99
	public function setRecordId(int $recordId): self
100
	{
101
		$this->recordId = $recordId;
102
103
		return $this;
104
	}
105
106
	/**
107
	 * Get URL address.
108
	 *
109
	 * @return string
110
	 */
111
	public function getUrl(): string
112
	{
113
		return "index.php?module={$this->moduleName}&view=Widget&record={$this->recordId}&mode=getContent&widgetId={$this->getId()}";
114
	}
115
116
	/**
117
	 * Gets related module name.
118
	 *
119
	 * @return string
120
	 */
121
	public function getRelatedModuleName(): string
122
	{
123
		return $this->get('data')['relatedModuleName'];
124
	}
125
126
	/**
127
	 * Gets fields from related module.
128
	 *
129
	 * @return array
130
	 */
131
	public function getFields(): array
132
	{
133
		return $this->get('data')['relatedfields'] ?: [];
134
	}
135
136
	/**
137
	 * Gets relation ID.
138
	 *
139
	 * @return int|null
140
	 */
141
	public function getRelationId()
142
	{
143
		return $this->get('data')['relation_id'] ?? null;
144
	}
145
146
	/**
147
	 * Gets custom view ID.
148
	 *
149
	 * @return int|null
150
	 */
151
	public function getCvId()
152
	{
153
		return isset($this->get('data')['customView']) ? current($this->get('data')['customView']) : null;
154
	}
155
156
	/**
157
	 * Check if is more pages.
158
	 *
159
	 * @return bool
160
	 */
161
	public function isMorePages(): bool
162
	{
163
		return $this->isMorePages;
164
	}
165
166
	/**
167
	 * Gets entries.
168
	 *
169
	 * @return array
170
	 */
171
	public function getEntries(): array
172
	{
173
		if (null === $this->entries) {
174
			$this->loadData();
175
		}
176
		return $this->entries;
177
	}
178
179
	/**
180
	 * Gets headers.
181
	 *
182
	 * @return array
183
	 */
184
	public function getHeaders(): array
185
	{
186
		if (null === $this->headers) {
187
			$this->loadData();
188
		}
189
		return $this->headers;
190
	}
191
192
	public function loadData()
193
	{
194
		$this->headers = $this->entries = [];
195
		$apiHeaders = ['x-fields' => \App\Json::encode($this->getFields())];
196
		if ($orderBy = $this->get('data')['orderby'] ?? null) {
197
			$apiHeaders['x-order-by'] = \App\Json::encode($orderBy);
198
		}
199
		if ($limit = (int) ($this->get('data')['limit'] ?? 0)) {
200
			$apiHeaders['x-row-limit'] = $limit;
201
		}
202
		$api = \App\Api::getInstance();
203
		$api->setCustomHeaders($apiHeaders);
204
205
		$body = [];
206
		if ($relationId = $this->getRelationId()) {
207
			$body['relationId'] = $relationId;
208
		}
209
		if ($cvId = $this->getCvId()) {
210
			$body['cvId'] = $cvId;
211
		}
212
213
		$response = $api->call("{$this->moduleName}/RecordRelatedList/{$this->recordId}/{$this->getRelatedModuleName()}", $body) ?: [];
214
		if (!empty($response['records'])) {
215 View Code Duplication
			foreach ($response['records'] as $id => $data) {
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...
216
				$recordModel = Record::getInstance($this->getRelatedModuleName());
217
				$recordModel->setData($data);
218
				$recordModel->setId($id);
219
				$this->entries[$id] = $recordModel;
220
			}
221
		}
222
		$this->headers = array_intersect_key($response['headers'], array_flip($this->getFields()));
223
		$this->isMorePages = (bool) $response['isMorePages'];
224
	}
225
226
	/**
227
	 * Gets template path for widget.
228
	 *
229
	 * @return string
230
	 */
231
	public function getTemplatePath(): string
232
	{
233
		return "Widget/{$this->type}.tpl";
234
	}
235
236
	/**
237
	 * Gets template path for widget content.
238
	 *
239
	 * @return string
240
	 */
241
	public function getTemplateContentPath(): string
242
	{
243
		return "Widget/{$this->type}Content.tpl";
244
	}
245
246
	/**
247
	 * Set scripts.
248
	 *
249
	 * @param array $scripts
250
	 */
251
	public function setScriptsObject($scripts)
252
	{
253
		return $this->scripts = $scripts;
254
	}
255
256
	/**
257
	 * Gets scripts.
258
	 *
259
	 * @return array
260
	 */
261
	public function getScripts(): array
262
	{
263
		return [
264
			['layouts/' . \App\Viewer::getLayoutName() . "/modules/{$this->moduleName}/resources/Widget/{$this->type}.js", true],
265
			['layouts/' . \App\Viewer::getLayoutName() . "/modules/Base/resources/Widget/{$this->type}.js", true]
266
		];
267
	}
268
}
269