Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/Admin/View/Helper/Option.php (4 issues)

Check for loose comparison of integers.

Best Practice Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
38 12
	public function __construct(Language $language)
39
	{
40 12
		$this->_language = $language;
41 12
	}
42
43
	/**
44
	 * get the toggle array
45
	 *
46
	 * @since 3.0.0
47
	 *
48
	 * @return array
49
	 */
50
51 1
	public function getToggleArray() : array
52
	{
53
		return
54
		[
55 1
			$this->_language->get('enable') => 1,
56 1
			$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
68 1
	public function getVisibleArray() : array
69
	{
70
		return
71
		[
72 1
			$this->_language->get('publish') => 1,
73 1
			$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
85 1
	public function getRobotArray() : array
86
	{
87
		return
88
		[
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 1
			$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
107 1
	public function getZoneArray() : array
108
	{
109 1
		return DateTimeZone::listIdentifiers();
110
	}
111
112
	/**
113
	 * get the time array
114
	 *
115
	 * @since 3.0.0
116
	 *
117
	 * @return array
118
	 */
119
120 1
	public function getTimeArray() : array
121
	{
122
		return
123
		[
124 1
			'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
137 1
	public function getDateArray() : array
138
	{
139
		return
140
		[
141 1
			'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
155 1
	public function getOrderArray() : array
156
	{
157
		return
158
		[
159 1
			$this->_language->get('ascending') => 'asc',
160 1
			$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
172 1
	public function getCaptchaArray() : array
173
	{
174
		return
175
		[
176 1
			$this->_language->get('random') => 1,
177 1
			$this->_language->get('addition') => 2,
178 1
			$this->_language->get('subtraction') => 3,
179 1
			$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
193 1
	public function getPermissionArray(string $table = null) : array
194
	{
195 1
		if ($table === 'modules')
196
		{
197
			return
198
			[
199 1
				$this->_language->get('install') => 1,
200 1
				$this->_language->get('edit') => 2,
201 1
				$this->_language->get('uninstall') => 3
202
			];
203
		}
204 1
		if ($table === 'settings')
205
		{
206
			return
207
			[
208 1
				$this->_language->get('none') => 1,
209 1
				$this->_language->get('edit') => 2,
210
			];
211
		}
212
		return
213
		[
214 1
			$this->_language->get('create') => 1,
215 1
			$this->_language->get('edit') => 2,
216 1
			$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
228 1
	public function getLanguageArray() : array
229
	{
230 1
		$languageFilesystem = new Filesystem\Filesystem();
231 1
		$languageFilesystem->init('languages');
232 1
		$languageFilesystemArray = $languageFilesystem->getSortArray();
233
		$languageArray =
234
		[
235 1
			$this->_language->get('select') => 'null'
236
		];
237
238
		/* process filesystem */
239
240 1
		foreach ($languageFilesystemArray as $value)
241
		{
242 1
			$value = substr($value, 0, 2);
243 1
			$languageArray[$this->_language->get($value, '_index')] = $value;
244
		}
245 1
		return $languageArray;
246
	}
247
248
	/**
249
	 * get the template array
250
	 *
251
	 * @since 3.0.0
252
	 *
253
	 * @return array
254
	 */
255
256 1
	public function getTemplateArray() : array
257
	{
258 1
		$templateFilesystem = new Filesystem\Filesystem();
259 1
		$templateFilesystem->init('templates', false,
260
		[
261 1
			'admin',
262
			'console',
263
			'install'
264
		]);
265 1
		$templateFilesystemArray = $templateFilesystem->getSortArray();
266
		$templateArray =
267
		[
268 1
			$this->_language->get('select') => 'null'
269
		];
270
271
		/* process filesystem */
272
273 1
		foreach ($templateFilesystemArray as $value)
274
		{
275 1
			$templateArray[$value] = $value;
276
		}
277 1
		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
		$query = Db::forTablePrefix('categories');
291
		return $this->_getContentArray($query);
292
	}
293
294
	/**
295
	 * get the article array
296
	 *
297
	 * @since 4.0.0
298
	 *
299
	 * @return array
300
	 */
301
302
	public function getArticleArray() : array
303
	{
304
		$query = Db::forTablePrefix('articles');
305
		return $this->_getContentArray($query);
306
	}
307
308
	/**
309
	 * get the sibling for article array
310
	 *
311
	 * @since 4.0.0
312
	 *
313
	 * @param int $articleId identifier of the article
314
	 *
315
	 * @return array
316
	 */
317
318
	public function getSiblingForArticleArray(int $articleId = null) : array
319
	{
320
		$query = Db::forTablePrefix('articles');
321
		if ($articleId)
0 ignored issues
show
Bug Best Practice introduced by
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...
322
		{
323
			$query->whereNotEqual('id', $articleId);
324
		}
325
		return $this->_getContentArray($query);
326
	}
327
328
	/**
329
	 * get the sibling for category array
330
	 *
331
	 * @since 4.0.0
332
	 *
333
	 * @param int $categoryId identifier of the category
334
	 *
335
	 * @return array
336
	 */
337
338
	public function getSiblingForCategoryArray(int $categoryId = null) : array
339
	{
340
		$query = Db::forTablePrefix('categories');
341
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by
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...
342
		{
343
			$query->whereNotEqual('id', $categoryId);
344
		}
345
		return $this->_getContentArray($query);
346
	}
347
348
	/**
349
	 * get the parent for category array
350
	 *
351
	 * @since 4.0.0
352
	 *
353
	 * @param int $categoryId identifier of the category
354
	 *
355
	 * @return array
356
	 */
357
358
	public function getParentForCategoryArray(int $categoryId = null) : array
359
	{
360
		$query = Db::forTablePrefix('categories')->whereNull('parent');
361
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by
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...
362
		{
363
			$query->whereNotEqual('id', $categoryId);
364
		}
365
		return $this->_getContentArray($query);
366
	}
367
368
	/**
369
	 * get the sibling for extra array
370
	 *
371
	 * @since 4.0.0
372
	 *
373
	 * @param int $extraId identifier of the extra
374
	 *
375
	 * @return array
376
	 */
377
378
	public function getSiblingForExtraArray(int $extraId = null) : array
379
	{
380
		$query = Db::forTablePrefix('extras');
381
		if ($extraId)
0 ignored issues
show
Bug Best Practice introduced by
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...
382
		{
383
			$query->whereNotEqual('id', $extraId);
384
		}
385
		return $this->_getContentArray($query);
386
	}
387
388
	/**
389
	 * get the article for comment array
390
	 *
391
	 * @since 4.0.0
392
	 *
393
	 * @return array
394
	 */
395
396
	public function getArticleForCommentArray() : array
397
	{
398
		$query = Db::forTablePrefix('articles')->where('comments', 1);
399
		return $this->_getContentArray($query);
400
	}
401
402
	/**
403
	 * get the content array
404
	 *
405
	 * @since 4.0.0
406
	 *
407
	 * @param object $query
408
	 *
409
	 * @return array
410
	 */
411
412
	protected function _getContentArray(object $query = null) : array
413
	{
414
		$contents = $query->orderByAsc('title')->findMany();
415
		$contentArray =
416
		[
417
			$this->_language->get('select') => 'null'
418
		];
419
420
		/* process contents */
421
422
		foreach ($contents as $value)
423
		{
424
			$contentKey = $value->title . ' (' . $value->id . ')';
425
			$contentArray[$contentKey] = $value->id;
426
		}
427
		return $contentArray;
428
	}
429
430
	/**
431
	 * get the group array
432
	 *
433
	 * @since 4.0.0
434
	 *
435
	 * @return array
436
	 */
437
438
	public function getGroupArray() : array
439 1
	{
440
		$access = Db::forTablePrefix('groups')->orderByAsc('name')->findMany();
441 1
		$accessArray = [];
442 1
443
		/* process access */
444
445
		foreach ($access as $value)
446 1
		{
447
			$accessArray[$value->name] = $value->id;
448 1
		}
449
		return $accessArray;
450 1
	}
451
}
452