Passed
Push — developer ( e5c82c...bcac4b )
by Mariusz
32:34
created

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