Completed
Push — master ( de9e2d...f59dfb )
by Henry
08:37
created

Option::getGroupArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 0
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
namespace Redaxscript\Admin\View\Helper;
3
4
use DateTimeZone;
5
use Redaxscript\Db;
6
use Redaxscript\Filesystem;
7
use Redaxscript\Language;
8
use function substr;
9
10
/**
11
 * helper class to create various options
12
 *
13
 * @since 3.0.0
14
 *
15
 * @package Redaxscript
16
 * @category View
17
 * @author Henry Ruhs
18
 */
19
20
class Option
21
{
22
	/**
23
	 * instance of the language class
24
	 *
25
	 * @var Language
26
	 */
27
28
	protected $_language;
29
30
	/**
31
	 * constructor of the class
32
	 *
33
	 * @since 3.2.0
34
	 *
35
	 * @param Language $language instance of the language class
36
	 */
37 13
38
	public function __construct(Language $language)
39 13
	{
40 13
		$this->_language = $language;
41
	}
42
43
	/**
44
	 * get the toggle array
45
	 *
46
	 * @since 3.0.0
47
	 *
48
	 * @return array
49
	 */
50 1
51
	public function getToggleArray() : array
52
	{
53
		return
54 1
		[
55 1
			$this->_language->get('enable') => 1,
56
			$this->_language->get('disable') => 0
57
		];
58
	}
59
60
	/**
61
	 * get the visible array
62
	 *
63
	 * @since 3.0.0
64
	 *
65
	 * @return array
66
	 */
67 1
68
	public function getVisibleArray() : array
69
	{
70
		return
71 1
		[
72 1
			$this->_language->get('publish') => 1,
73
			$this->_language->get('unpublish') => 0
74
		];
75
	}
76
77
	/**
78
	 * get the robot array
79
	 *
80
	 * @since 3.0.0
81
	 *
82
	 * @return array
83
	 */
84 1
85
	public function getRobotArray() : array
86
	{
87
		return
88 1
		[
89 1
			$this->_language->get('select') => 'null',
90 1
			$this->_language->get('all') => 1,
91 1
			$this->_language->get('index') => 2,
92 1
			$this->_language->get('follow') => 3,
93 1
			$this->_language->get('index_no') => 4,
94 1
			$this->_language->get('follow_no') => 5,
95
			$this->_language->get('none') => 6
96
		];
97
	}
98
99
	/**
100
	 * get the zone array
101
	 *
102
	 * @since 4.0.0
103
	 *
104
	 * @return array
105
	 */
106 1
107
	public function getZoneArray() : array
108 1
	{
109
		return DateTimeZone::listIdentifiers();
110
	}
111
112
	/**
113
	 * get the time array
114
	 *
115
	 * @since 3.0.0
116
	 *
117
	 * @return array
118
	 */
119 1
120
	public function getTimeArray() : array
121
	{
122
		return
123 1
		[
124
			'24h' => 'H:i',
125
			'12h' => 'h:i a'
126
		];
127
	}
128
129
	/**
130
	 * get the date array
131
	 *
132
	 * @since 3.0.0
133
	 *
134
	 * @return array
135
	 */
136 1
137
	public function getDateArray() : array
138
	{
139
		return
140 1
		[
141
			'DD.MM.YYYY' => 'd.m.Y',
142
			'MM.DD.YYYY' => 'm.d.Y',
143
			'YYYY.MM.DD' => 'Y.m.d'
144
		];
145
	}
146
147
	/**
148
	 * get the order array
149
	 *
150
	 * @since 3.0.0
151
	 *
152
	 * @return array
153
	 */
154 1
155
	public function getOrderArray() : array
156
	{
157
		return
158 1
		[
159 1
			$this->_language->get('ascending') => 'asc',
160
			$this->_language->get('descending') => 'desc'
161
		];
162
	}
163
164
	/**
165
	 * get the captcha array
166
	 *
167
	 * @since 3.0.0
168
	 *
169
	 * @return array
170
	 */
171 1
172
	public function getCaptchaArray() : array
173
	{
174
		return
175 1
		[
176 1
			$this->_language->get('random') => 1,
177 1
			$this->_language->get('addition') => 2,
178 1
			$this->_language->get('subtraction') => 3,
179
			$this->_language->get('disable') => 0
180
		];
181
	}
182
183
	/**
184
	 * get the permission array
185
	 *
186
	 * @since 3.0.0
187
	 *
188
	 * @param string $table name of the table
189
	 *
190
	 * @return array
191
	 */
192 1
193
	public function getPermissionArray(string $table = null) : array
194 1
	{
195
		if ($table === 'modules')
196
		{
197
			return
198 1
			[
199 1
				$this->_language->get('install') => 1,
200 1
				$this->_language->get('edit') => 2,
201
				$this->_language->get('uninstall') => 3
202
			];
203 1
		}
204
		if ($table === 'settings')
205
		{
206
			return
207 1
			[
208 1
				$this->_language->get('none') => 1,
209
				$this->_language->get('edit') => 2,
210
			];
211
		}
212
		return
213 1
		[
214 1
			$this->_language->get('create') => 1,
215 1
			$this->_language->get('edit') => 2,
216
			$this->_language->get('delete') => 3
217
		];
218
	}
219
220
	/**
221
	 * get the language array
222
	 *
223
	 * @since 3.0.0
224
	 *
225
	 * @return array
226
	 */
227 1
228
	public function getLanguageArray() : array
229 1
	{
230 1
		$languageFilesystem = new Filesystem\Filesystem();
231 1
		$languageFilesystem->init('languages');
232
		$languageFilesystemArray = $languageFilesystem->getSortArray();
233
		$languageArray =
234 1
		[
235
			$this->_language->get('select') => 'null'
236
		];
237
238
		/* process filesystem */
239 1
240
		foreach ($languageFilesystemArray as $value)
241 1
		{
242 1
			$value = substr($value, 0, 2);
243
			$languageArray[$this->_language->get($value, '_index')] = $value;
244 1
		}
245
		return $languageArray;
246
	}
247
248
	/**
249
	 * get the template array
250
	 *
251
	 * @since 3.0.0
252
	 *
253
	 * @return array
254
	 */
255 1
256
	public function getTemplateArray() : array
257 1
	{
258 1
		$templateFilesystem = new Filesystem\Filesystem();
259
		$templateFilesystem->init('templates', false,
260 1
		[
261
			'admin',
262
			'console',
263
			'install'
264 1
		]);
265
		$templateFilesystemArray = $templateFilesystem->getSortArray();
266
		$templateArray =
267 1
		[
268
			$this->_language->get('select') => 'null'
269
		];
270
271
		/* process filesystem */
272 1
273
		foreach ($templateFilesystemArray as $value)
274 1
		{
275
			$templateArray[$value] = $value;
276 1
		}
277
		return $templateArray;
278
	}
279
280
	/**
281
	 * get the category array
282
	 *
283
	 * @since 4.0.0
284
	 *
285
	 * @return array
286
	 */
287
288
	public function getCategoryArray() : array
289
	{
290 1
		$query = Db::forTablePrefix('categories');
291
		return $this->_getContentArray($query);
292 1
	}
293 1
294
295 1
	/**
296
	 * get the article array
297 1
	 *
298
	 * @since 4.0.0
299
	 *
300 1
	 * @return array
301
	 */
302
303
	public function getArticleArray() : array
304
	{
305 1
		$query = Db::forTablePrefix('articles');
306
		return $this->_getContentArray($query);
307 1
	}
308 1
309
	/**
310 1
	 * get the sibling for article array
311
	 *
312
	 * @since 4.0.0
313
	 *
314
	 * @param int $articleId identifier of the article
315
	 *
316
	 * @return array
317
	 */
318
319
	public function getSiblingForArticleArray(int $articleId = null) : array
320
	{
321
		$query = Db::forTablePrefix('articles');
322
		if ($articleId)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $articleId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
323 1
		{
324
			$query->whereNotEqual('id', $articleId);
325 1
		}
326 1
		return $this->_getContentArray($query);
327
	}
328
329
	/**
330 1
	 * get the sibling for category array
331
	 *
332 1
	 * @since 4.0.0
333
	 *
334 1
	 * @param int $categoryId identifier of the category
335
	 *
336
	 * @return array
337
	 */
338
339
	public function getSiblingForCategoryArray(int $categoryId = null) : array
340
	{
341
		$query = Db::forTablePrefix('categories');
342
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $categoryId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
343
		{
344
			$query->whereNotEqual('id', $categoryId);
345
		}
346
		return $this->_getContentArray($query);
347
	}
348
349
	/**
350
	 * get the parent for category array
351
	 *
352
	 * @since 4.0.0
353
	 *
354
	 * @param int $categoryId identifier of the category
355
	 *
356
	 * @return array
357
	 */
358
359
	public function getParentForCategoryArray(int $categoryId = null) : array
360
	{
361
		$query = Db::forTablePrefix('categories')->whereNull('parent');
362
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $categoryId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
363
		{
364
			$query->whereNotEqual('id', $categoryId);
365
		}
366
		return $this->_getContentArray($query);
367
	}
368
369
	/**
370
	 * get the sibling for extra array
371
	 *
372
	 * @since 4.0.0
373
	 *
374
	 * @param int $extraId identifier of the extra
375
	 *
376
	 * @return array
377
	 */
378
379
	public function getSiblingForExtraArray(int $extraId = null) : array
380
	{
381
		$query = Db::forTablePrefix('extras');
382
		if ($extraId)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $extraId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
383
		{
384
			$query->whereNotEqual('id', $extraId);
385
		}
386
		return $this->_getContentArray($query);
387
	}
388
389
	/**
390
	 * get the article for comment array
391
	 *
392
	 * @since 4.0.0
393
	 *
394
	 * @return array
395
	 */
396
397
	public function getArticleForCommentArray() : array
398
	{
399
		$query = Db::forTablePrefix('articles')->where('comments', 1);
400
		return $this->_getContentArray($query);
401
	}
402
403
	/**
404
	 * get the content array
405
	 *
406
	 * @since 4.0.0
407
	 *
408
	 * @param object $query
409
	 *
410
	 * @return array
411
	 */
412
413
	protected function _getContentArray(object $query = null) : array
414
	{
415
		$contents = $query->orderByAsc('title')->findMany();
416
		$contentArray =
417
		[
418
			$this->_language->get('select') => 'null'
419
		];
420
421
		/* process contents */
422
423
		foreach ($contents as $value)
424
		{
425
			$contentKey = $value->title . ' (' . $value->id . ')';
426
			$contentArray[$contentKey] = $value->id;
427
		}
428
		return $contentArray;
429
	}
430
431
	/**
432
	 * get the group array
433
	 *
434
	 * @since 4.0.0
435
	 *
436
	 * @return array
437
	 */
438
439
	public function getGroupArray() : array
440
	{
441
		$access = Db::forTablePrefix('groups')->orderByAsc('name')->findMany();
442
		$accessArray = [];
443
444
		/* process access */
445
446
		foreach ($access as $value)
447
		{
448
			$accessArray[$value->name] = $value->id;
449
		}
450
		return $accessArray;
451
	}
452
}
453