|
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 Arkadiusz Adach <[email protected]> |
|
10
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
|
11
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace YF\Modules\Base\Model; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Abstract class ListView. |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractListView |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var string Module name. */ |
|
22
|
|
|
protected $moduleName; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string[] Column fields */ |
|
25
|
|
|
protected $fields = []; |
|
26
|
|
|
|
|
27
|
|
|
/** @var array Records list from api. */ |
|
28
|
|
|
protected $recordsList = []; |
|
29
|
|
|
|
|
30
|
|
|
/** @var int Current page. */ |
|
31
|
|
|
private $page = 1; |
|
32
|
|
|
|
|
33
|
|
|
/** @var int The number of items on the page. */ |
|
34
|
|
|
protected $limit = 0; |
|
35
|
|
|
|
|
36
|
|
|
/** @var int Offset. */ |
|
37
|
|
|
protected $offset = 0; |
|
38
|
|
|
|
|
39
|
|
|
/** @var string Sorting direction. */ |
|
40
|
|
|
protected $order; |
|
41
|
|
|
|
|
42
|
|
|
/** @var string Sets the ORDER BY part of the query record list. */ |
|
43
|
|
|
protected $orderField; |
|
44
|
|
|
|
|
45
|
|
|
/** @var array Conditions. */ |
|
46
|
|
|
protected $conditions = []; |
|
47
|
|
|
|
|
48
|
|
|
/** @var bool Use raw data. */ |
|
49
|
|
|
protected $rawData = false; |
|
50
|
|
|
|
|
51
|
|
|
protected $actionName = 'RecordsList'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get instance. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $moduleName |
|
57
|
|
|
* @param string $viewName |
|
58
|
|
|
* |
|
59
|
|
|
* @return self |
|
|
|
|
|
|
60
|
|
|
*/ |
|
61
|
|
|
public static function getInstance(string $moduleName, string $viewName = 'ListView'): self |
|
62
|
|
|
{ |
|
63
|
|
|
$handlerModule = \App\Loader::getModuleClassName($moduleName, 'Model', $viewName); |
|
64
|
|
|
$self = new $handlerModule(); |
|
65
|
|
|
$self->moduleName = $moduleName; |
|
66
|
|
|
$self->limit = \App\Config::$itemsPrePage ?: 15; |
|
67
|
|
|
return $self; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Function to get the Module Model. |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getModuleName(): string |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->moduleName; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Function to set raw data. |
|
82
|
|
|
* |
|
83
|
|
|
* @param bool $rawData |
|
84
|
|
|
* |
|
85
|
|
|
* @return self |
|
86
|
|
|
*/ |
|
87
|
|
|
public function setRawData(bool $rawData): self |
|
88
|
|
|
{ |
|
89
|
|
|
$this->rawData = $rawData; |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set custom fields. |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $fields |
|
97
|
|
|
* |
|
98
|
|
|
* @return self |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setFields(array $fields): self |
|
101
|
|
|
{ |
|
102
|
|
|
$this->fields = $fields; |
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Set limit. |
|
108
|
|
|
* |
|
109
|
|
|
* @param int $limit |
|
110
|
|
|
* |
|
111
|
|
|
* @return self |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setLimit(int $limit): self |
|
114
|
|
|
{ |
|
115
|
|
|
$this->limit = $limit; |
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Set offset. |
|
121
|
|
|
* |
|
122
|
|
|
* @param int $offset |
|
123
|
|
|
* |
|
124
|
|
|
* @return self |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setOffset(int $offset): self |
|
127
|
|
|
{ |
|
128
|
|
|
$this->offset = $offset; |
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set order. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $field |
|
136
|
|
|
* @param string $direction |
|
137
|
|
|
* |
|
138
|
|
|
* @return self |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setOrder(string $field, string $direction): self |
|
141
|
|
|
{ |
|
142
|
|
|
$this->orderField = $field; |
|
143
|
|
|
$this->order = $direction; |
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Set conditions. |
|
149
|
|
|
* |
|
150
|
|
|
* @param array $conditions |
|
151
|
|
|
* |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
|
public function setConditions(array $conditions) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->conditions = $conditions; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Load a list of records from the API. |
|
161
|
|
|
* |
|
162
|
|
|
* @return self |
|
163
|
|
|
*/ |
|
164
|
|
|
public function loadRecordsList(): self |
|
165
|
|
|
{ |
|
166
|
|
|
$headers = [ |
|
167
|
|
|
'x-row-count' => 1, |
|
168
|
|
|
'x-row-limit' => $this->limit, |
|
169
|
|
|
'x-row-offset' => $this->offset, |
|
170
|
|
|
]; |
|
171
|
|
|
if (!empty($this->fields)) { |
|
172
|
|
|
$headers['x-fields'] = \App\Json::encode($this->fields); |
|
173
|
|
|
} |
|
174
|
|
|
if (!empty($this->conditions)) { |
|
175
|
|
|
$headers['x-condition'] = \App\Json::encode($this->conditions); |
|
176
|
|
|
} |
|
177
|
|
|
if ($this->rawData) { |
|
178
|
|
|
$headers['x-raw-data'] = 1; |
|
179
|
|
|
} |
|
180
|
|
|
if (!empty($this->order)) { |
|
181
|
|
|
$headers['x-row-order-field'] = $this->orderField; |
|
182
|
|
|
$headers['x-row-order'] = $this->order; |
|
183
|
|
|
} |
|
184
|
|
|
$this->recordsList = $this->getFromApi($headers); |
|
185
|
|
|
return $this; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Get data from api. |
|
190
|
|
|
* |
|
191
|
|
|
* @param array $headers |
|
192
|
|
|
* |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function getFromApi(array $headers): array |
|
196
|
|
|
{ |
|
197
|
|
|
$api = \App\Api::getInstance(); |
|
198
|
|
|
$api->setCustomHeaders($headers); |
|
199
|
|
|
return $api->call($this->getModuleName() . '/' . $this->actionName); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Get records list model. |
|
204
|
|
|
* |
|
205
|
|
|
* @return Record[] |
|
206
|
|
|
*/ |
|
207
|
|
|
public function getRecordsListModel(): array |
|
208
|
|
|
{ |
|
209
|
|
|
$recordsModel = []; |
|
210
|
|
|
if (!empty($this->recordsList['records'])) { |
|
211
|
|
|
foreach ($this->recordsList['records'] as $id => $value) { |
|
212
|
|
|
$recordModel = Record::getInstance($this->getModuleName()); |
|
213
|
|
|
if (isset($value['recordLabel'])) { |
|
214
|
|
|
$recordModel->setName($value['recordLabel']); |
|
215
|
|
|
unset($value['recordLabel']); |
|
216
|
|
|
} |
|
217
|
|
|
$recordModel->setData($value)->setId($id); |
|
218
|
|
|
$recordsModel[$id] = $recordModel; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
if (!empty($this->recordsList['rawData'])) { |
|
222
|
|
|
foreach ($this->recordsList['rawData'] as $id => $value) { |
|
223
|
|
|
$recordsModel[$id]->setRawData($value); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
return $recordsModel; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Get headers of list. |
|
231
|
|
|
* |
|
232
|
|
|
* @return array |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getHeaders(): array |
|
235
|
|
|
{ |
|
236
|
|
|
if (empty($this->recordsList)) { |
|
237
|
|
|
$this->recordsList = $this->getFromApi([ |
|
238
|
|
|
'x-only-column' => 1, |
|
239
|
|
|
]); |
|
240
|
|
|
} |
|
241
|
|
|
return $this->recordsList['headers'] ?? []; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Get all rows count. |
|
246
|
|
|
* |
|
247
|
|
|
* @return int |
|
248
|
|
|
*/ |
|
249
|
|
|
public function getCount(): int |
|
250
|
|
|
{ |
|
251
|
|
|
return $this->recordsList['numberOfAllRecords'] ?? 0; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Get current page. |
|
256
|
|
|
* |
|
257
|
|
|
* @return int |
|
258
|
|
|
*/ |
|
259
|
|
|
public function getPage(): int |
|
260
|
|
|
{ |
|
261
|
|
|
if (!$this->page) { |
|
262
|
|
|
$this->page = floor($this->recordsList['numberOfRecords'] / ($this->recordsList['numberOfAllRecords'] ?: 1)) ?: 1; |
|
|
|
|
|
|
263
|
|
|
} |
|
264
|
|
|
return $this->page; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Sets page number. |
|
269
|
|
|
* |
|
270
|
|
|
* @param int $page |
|
271
|
|
|
* |
|
272
|
|
|
* @return $this |
|
273
|
|
|
*/ |
|
274
|
|
|
public function setPage(int $page) |
|
275
|
|
|
{ |
|
276
|
|
|
$this->page = $page; |
|
277
|
|
|
return $this; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Is there more pages. |
|
282
|
|
|
* |
|
283
|
|
|
* @return bool |
|
284
|
|
|
*/ |
|
285
|
|
|
public function isMorePages(): bool |
|
286
|
|
|
{ |
|
287
|
|
|
return $this->recordsList['isMorePages'] ?? false; |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.