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