Completed
Push — master ( 858dca...453367 )
by Henry
06:29
created

Option::getCharsetArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 function_exists;
9
use function mb_list_encodings;
10
use function resourcebundle_locales;
11
use function substr;
12
13
/**
14
 * helper class to create various options
15
 *
16
 * @since 3.0.0
17
 *
18
 * @package Redaxscript
19
 * @category View
20
 * @author Henry Ruhs
21
 */
22
23
class Option
24
{
25
	/**
26
	 * instance of the language class
27
	 *
28
	 * @var Language
29
	 */
30
31
	protected $_language;
32
33
	/**
34
	 * constructor of the class
35
	 *
36
	 * @since 3.2.0
37
	 *
38 10
	 * @param Language $language instance of the language class
39
	 */
40 10
41 10
	public function __construct(Language $language)
42
	{
43
		$this->_language = $language;
44
	}
45
46
	/**
47
	 * get the robot array
48
	 *
49
	 * @since 3.0.0
50
	 *
51 1
	 * @return array
52
	 */
53
54
	public function getRobotArray() : array
55 1
	{
56 1
		return
57 1
		[
58 1
			$this->_language->get('select') => 'null',
59 1
			$this->_language->get('all') => 1,
60 1
			$this->_language->get('index') => 2,
61 1
			$this->_language->get('follow') => 3,
62
			$this->_language->get('index_no') => 4,
63
			$this->_language->get('follow_no') => 5,
64
			$this->_language->get('none') => 6
65
		];
66
	}
67
68
	/**
69
	 * get the charset array
70
	 *
71
	 * @since 4.4.0
72
	 *
73 1
	 * @return array
74
	 */
75 1
76
	public function getCharsetArray() : array
77
	{
78
		return mb_list_encodings();
79
	}
80
81
	/**
82
	 * get the locale array
83
	 *
84
	 * @since 4.4.0
85
	 *
86 1
	 * @return array
87
	 */
88
89
	public function getLocaleArray() : array
90 1
	{
91
		return function_exists('resourcebundle_locales') ? resourcebundle_locales(null) : [];
92
	}
93
94
	/**
95
	 * get the zone array
96
	 *
97
	 * @since 4.0.0
98
	 *
99
	 * @return array
100
	 */
101
102
	public function getZoneArray() : array
103 1
	{
104
		return DateTimeZone::listIdentifiers();
105
	}
106
107 1
	/**
108
	 * get the time array
109
	 *
110
	 * @since 3.0.0
111
	 *
112
	 * @return array
113
	 */
114
115
	public function getTimeArray() : array
116
	{
117
		return
118
		[
119
			'24h' => 'H:i',
120
			'12h' => 'h:i a'
121 1
		];
122
	}
123
124
	/**
125 1
	 * get the date array
126 1
	 *
127
	 * @since 3.0.0
128
	 *
129
	 * @return array
130
	 */
131
132
	public function getDateArray() : array
133
	{
134
		return
135
		[
136
			'DD.MM.YYYY' => 'd.m.Y',
137
			'MM.DD.YYYY' => 'm.d.Y',
138 1
			'YYYY.MM.DD' => 'Y.m.d'
139
		];
140
	}
141
142 1
	/**
143 1
	 * get the order array
144 1
	 *
145 1
	 * @since 3.0.0
146
	 *
147
	 * @return array
148
	 */
149
150
	public function getOrderArray() : array
151
	{
152
		return
153
		[
154
			$this->_language->get('ascending') => 'asc',
155
			$this->_language->get('descending') => 'desc'
156
		];
157
	}
158
159 1
	/**
160
	 * get the captcha array
161 1
	 *
162
	 * @since 3.0.0
163
	 *
164
	 * @return array
165 1
	 */
166 1
167 1
	public function getCaptchaArray() : array
168
	{
169
		return
170 1
		[
171
			$this->_language->get('random') => 1,
172
			$this->_language->get('addition') => 2,
173
			$this->_language->get('subtraction') => 3,
174 1
			$this->_language->get('disable') => 0
175 1
		];
176
	}
177
178
	/**
179
	 * get the permission array
180 1
	 *
181 1
	 * @since 3.0.0
182 1
	 *
183
	 * @param string $table name of the table
184
	 *
185
	 * @return array
186
	 */
187
188
	public function getPermissionArray(string $table = null) : array
189
	{
190
		if ($table === 'modules')
191
		{
192
			return
193
			[
194 1
				$this->_language->get('install') => 1,
195
				$this->_language->get('edit') => 2,
196 1
				$this->_language->get('uninstall') => 3
197 1
			];
198 1
		}
199
		if ($table === 'settings')
200
		{
201 1
			return
202
			[
203
				$this->_language->get('none') => 1,
204
				$this->_language->get('edit') => 2,
205
			];
206 1
		}
207
		return
208 1
		[
209 1
			$this->_language->get('create') => 1,
210
			$this->_language->get('edit') => 2,
211 1
			$this->_language->get('delete') => 3
212
		];
213
	}
214
215
	/**
216
	 * get the language array
217
	 *
218
	 * @since 3.0.0
219
	 *
220
	 * @return array
221
	 */
222 1
223
	public function getLanguageArray() : array
224 1
	{
225 1
		$languageFilesystem = new Filesystem\Filesystem();
226
		$languageFilesystem->init('languages');
227 1
		$languageFilesystemArray = $languageFilesystem->getSortArray();
228
		$languageArray =
229
		[
230
			$this->_language->get('select') => 'null'
231 1
		];
232
233
		/* process filesystem */
234 1
235
		foreach ($languageFilesystemArray as $value)
236
		{
237
			$value = substr($value, 0, 2);
238
			$languageArray[$this->_language->get('_language')[$value]] = $value;
239 1
		}
240
		return $languageArray;
241 1
	}
242
243 1
	/**
244
	 * get the template array
245
	 *
246
	 * @since 3.0.0
247
	 *
248
	 * @return array
249
	 */
250
251
	public function getTemplateArray() : array
252
	{
253
		$templateFilesystem = new Filesystem\Filesystem();
254
		$templateFilesystem->init('templates', false,
255
		[
256
			'admin',
257
			'console',
258
			'install'
259
		]);
260
		$templateFilesystemArray = $templateFilesystem->getSortArray();
261
		$templateArray =
262
		[
263
			$this->_language->get('select') => 'null'
264
		];
265
266
		/* process filesystem */
267
268
		foreach ($templateFilesystemArray as $value)
269
		{
270
			$templateArray[$value] = $value;
271
		}
272
		return $templateArray;
273
	}
274
275
	/**
276
	 * get the category array
277
	 *
278
	 * @since 4.0.0
279
	 *
280
	 * @return array
281
	 */
282
283
	public function getCategoryArray() : array
284
	{
285
		$query = Db::forTablePrefix('categories');
286
		return $this->_getContentArray($query);
287
	}
288
289
	/**
290
	 * get the article array
291
	 *
292
	 * @since 4.0.0
293
	 *
294
	 * @return array
295
	 */
296
297
	public function getArticleArray() : array
298
	{
299
		$query = Db::forTablePrefix('articles');
300
		return $this->_getContentArray($query);
301
	}
302
303
	/**
304
	 * get the sibling for article array
305
	 *
306
	 * @since 4.0.0
307
	 *
308
	 * @param int $articleId identifier of the article
309
	 *
310
	 * @return array
311
	 */
312
313
	public function getSiblingForArticleArray(int $articleId = null) : array
314
	{
315
		$query = Db::forTablePrefix('articles');
316
		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...
317
		{
318
			$query->whereNotEqual('id', $articleId);
319
		}
320
		return $this->_getContentArray($query);
321
	}
322
323
	/**
324
	 * get the sibling for category array
325
	 *
326
	 * @since 4.0.0
327
	 *
328
	 * @param int $categoryId identifier of the category
329
	 *
330
	 * @return array
331
	 */
332
333
	public function getSiblingForCategoryArray(int $categoryId = null) : array
334
	{
335
		$query = Db::forTablePrefix('categories');
336
		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...
337
		{
338
			$query->whereNotEqual('id', $categoryId);
339
		}
340
		return $this->_getContentArray($query);
341
	}
342
343
	/**
344
	 * get the parent for category array
345
	 *
346
	 * @since 4.0.0
347
	 *
348
	 * @param int $categoryId identifier of the category
349
	 *
350
	 * @return array
351
	 */
352
353
	public function getParentForCategoryArray(int $categoryId = null) : array
354
	{
355
		$query = Db::forTablePrefix('categories')->whereNull('parent');
356
		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...
357
		{
358
			$query->whereNotEqual('id', $categoryId);
359
		}
360
		return $this->_getContentArray($query);
361
	}
362
363
	/**
364
	 * get the sibling for extra array
365
	 *
366
	 * @since 4.0.0
367
	 *
368
	 * @param int $extraId identifier of the extra
369
	 *
370
	 * @return array
371
	 */
372
373
	public function getSiblingForExtraArray(int $extraId = null) : array
374
	{
375
		$query = Db::forTablePrefix('extras');
376
		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...
377
		{
378
			$query->whereNotEqual('id', $extraId);
379
		}
380
		return $this->_getContentArray($query);
381
	}
382
383
	/**
384
	 * get the article for comment array
385
	 *
386
	 * @since 4.0.0
387
	 *
388
	 * @return array
389
	 */
390
391
	public function getArticleForCommentArray() : array
392
	{
393
		$query = Db::forTablePrefix('articles')->where('comments', 1);
394
		return $this->_getContentArray($query);
395
	}
396
397
	/**
398
	 * get the content array
399
	 *
400
	 * @since 4.0.0
401
	 *
402
	 * @param object $query
403
	 *
404 1
	 * @return array
405
	 */
406 1
407 1
	protected function _getContentArray(object $query = null) : array
408
	{
409
		$contents = $query->orderByAsc('title')->findMany();
410
		$contentArray =
411 1
		[
412
			$this->_language->get('select') => 'null'
413 1
		];
414
415 1
		/* process contents */
416
417
		foreach ($contents as $value)
418
		{
419
			$contentKey = $value->title . ' (' . $value->id . ')';
420
			$contentArray[$contentKey] = $value->id;
421
		}
422
		return $contentArray;
423
	}
424
425
	/**
426
	 * get the group array
427
	 *
428
	 * @since 4.0.0
429
	 *
430
	 * @return array
431
	 */
432
433
	public function getGroupArray() : array
434
	{
435
		$access = Db::forTablePrefix('groups')->orderByAsc('name')->findMany();
436
		$accessArray = [];
437
438
		/* process access */
439
440
		foreach ($access as $value)
441
		{
442
			$accessArray[$value->name] = $value->id;
443
		}
444
		return $accessArray;
445
	}
446
}
447