-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
272 lines (246 loc) · 5.76 KB
/
code.power
File metadata and controls
272 lines (246 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* Bundle Size
*
* @var int
* @since 3.2.0
*/
protected int $bundle = 300;
/**
* Search Config
*
* @var Config
* @since 3.2.0
*/
protected Config $config;
/**
* Search Table
*
* @var Table
* @since 3.2.0
*/
protected Table $table;
/**
* Search Model
*
* @var Model
* @since 3.2.0
*/
protected Model $model;
/**
* Database load class
*
* @var Database
* @since 3.2.0
**/
protected Database $load;
/**
* Constructor
*
* @param Config|null $config The search config object.
* @param Table|null $table The search table object.
* @param Model|null $model The search get model object.
* @param Database|null $load The database object.
*
* @since 3.2.0
*/
public function __construct(?Config $config = null, ?Table $table = null,
?Model $model = null, ?Database $load = null)
{
$this->config = $config ?: Factory::_('Config');
$this->table = $table ?: Factory::_('Table');
$this->model = $model ?: Factory::_('Load.Model');
$this->load = $load ?: Factory::_('Load');
}
/**
* Get a value from a given table
* Example: $this->value(23, 'value_key', 'table_name');
*
* @param int $id The item ID
* @param string $field The field key
* @param string|null $table The table
*
* @return mixed
* @since 3.2.0
*/
public function value(int $id, string $field, string $table = null)
{
// load the table
if (empty($table))
{
$table = $this->config->table_name;
}
// check if this is a valid table
if ($id > 0 && $this->table->exist($table, $field) &&
($value = $this->load->value(
["a.{$field}" => $field], // select
['a' => $table], // tables
['a.id' => $id] // where
)) !== null)
{
return $this->model->value(
$value, $field, $table
);
}
return null;
}
/**
* Get values from a given table
* Example: $this->item(23, 'table_name');
*
* @param int $id The item ID
* @param string| null $table The table
*
* @return object|null
* @since 3.2.0
*/
public function item(int $id, string $table = null): ?object
{
// load the table
if (empty($table))
{
$table = $this->config->table_name;
}
// check if this is a valid table
if ($id > 0 && ($fields = $this->setDatabaseFields($table)) !== null &&
($item = $this->load->item(
$fields, // select
['a' => $table], // tables
['a.id' => $id] // where
)) !== null)
{
// return found values
return $this->model->item($item, $table);
}
return null;
}
/**
* Get values from a given table
* Example: $this->items('table_name');
*
* @param string|null $table The table
* @param int $bundle The bundle to return (0 = all)
*
* @return array|null
* @since 3.2.0
*/
public function items(string $table = null, int $bundle = 0): ?array
{
// load the table
if (empty($table))
{
$table = $this->config->table_name;
}
// check if this is a valid table
if ( ($fields = $this->setDatabaseFields($table)) !== null)
{
// add a key to the selection return set
$fields['key'] = 'id';
// get the title value
$title = $this->table->titleName($table);
// set order
$order = ['a.' . $title => 'ASC'];
// select all
$where = null;
// no limit
$limit = null;
// add limitation and pagination
if ($bundle > 0)
{
// get the incremental number
$where = ['a.id' => [
'operator' => '>=',
'value' => $this->next($table, $bundle)
]
];
// only return a limited number
$limit = $this->bundle;
}
if (($items = $this->load->items(
$fields, // select
['a' => $table], // tables
$where,
$order,
$limit
)) !== null)
{
// return found values
return $this->model->items($items, $table);
}
}
return null;
}
/**
* Get next id to call
*
* @param string $table The table
* @param int $bundle The bundle to return
*
* @return int
* @since 3.2.0
*/
protected function next(string $table, int $bundle): int
{
if ($bundle == 1 || $bundle == 0)
{
return 1;
}
if (($number = $this->model->last($table)) !== null)
{
return $number + 1;
}
return $this->incremental($bundle);
}
/**
* Get Incremental number where the set starts
*
* @param int $bundle The bundle to return
*
* @return int
* @since 3.2.0
*/
protected function incremental(int $bundle): int
{
// just in case
if ($bundle == 1 || $bundle == 0)
{
return 1;
}
/** Number two set starts at 301
* 2 x 300 = 600
* 600 - 300 = 300
* 300 + 1 = 301 <--
* Number five set starts at 1201
* 5 x 300 = 1500
* 1500 - 300 = 1200
* 1200 + 1 = 1201 <--
**/
return (($bundle * $this->bundle) - $this->bundle) + 1;
}
/**
* Get Fields ready to use in database call
*
* @param string $table The table which fields we want to get
* @param string $key The table key to which the fields belong
* @param bool $addId The switch to add ID
*
* @return array|null
* @since 3.2.0
*/
protected function setDatabaseFields(string $table, string $key = 'a', bool $addId = true): ?array
{
if (($fields = $this->table->fields($table)) !== null)
{
// add the ID
if ($addId)
{
array_unshift($fields , 'id');
}
$bucket = [];
foreach ($fields as $field)
{
$bucket[$key . '.' . $field] = $field;
}
return $bucket;
}
return null;
}