Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

Archive::_getMonthArrayByLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace Redaxscript\Modules\Archive;
3
4
use Redaxscript\Dater;
5
use Redaxscript\Db;
6
use Redaxscript\Html;
7
use Redaxscript\Model;
8
use Redaxscript\Module;
9
use function strtotime;
10
11
/**
12
 * generate a archive tree
13
 *
14
 * @since 2.2.0
15
 *
16
 * @package Redaxscript
17
 * @category Modules
18
 * @author Henry Ruhs
19
 */
20
21
class Archive extends Module\Module
22
{
23
	/**
24
	 * array of the module
25
	 *
26
	 * @var array
27
	 */
28
29
	protected static $_moduleArray =
30
	[
31
		'name' => 'Archive',
32
		'alias' => 'Archive',
33
		'author' => 'Redaxmedia',
34
		'description' => 'Generate a archive tree',
35
		'version' => '4.0.0'
36
	];
37
38
	/**
39
	 * array of the option
40
	 *
41
	 * @var array
42
	 */
43
44
	protected $_optionArray =
45
	[
46
		'className' =>
47
		[
48
			'title' => 'rs-title-content-sub rs-title-archive',
49
			'list' => 'rs-list-default rs-list-archive'
50
		]
51
	];
52
53
	/**
54
	 * render
55
	 *
56
	 * @since 2.2.0
57
	 *
58
	 * @return string
59
	 */
60
61
	public function render() : string
62
	{
63
		$output = null;
64
		$outputItem = null;
65
		$error = null;
66
		$articleModel = new Model\Article();
67
		$dater = new Dater();
68
69
		/* html element */
70
71
		$element = new Html\Element();
72
		$titleElement = $element
73
			->copy()
74
			->init('h3',
75
			[
76
				'class' => $this->_optionArray['className']['title']
77
			]);
78
		$listElement = $element
79
			->copy()
80
			->init('ul',
81
			[
82
				'class' => $this->_optionArray['className']['list']
83
			]);
84
		$itemElement = $element->copy()->init('li');
85
		$linkElement = $element->copy()->init('a');
86
87
		/* query articles */
88
89
		$monthArray = $this->_getMonthArrayByLanguage($this->_registry->get('language'));
0 ignored issues
show
Bug introduced by
It seems like $this->_registry->get('language') targeting Redaxscript\Registry::get() can also be of type array; however, Redaxscript\Modules\Arch...tMonthArrayByLanguage() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
90
91
		/* process articles */
92
93
		if (!$monthArray)
0 ignored issues
show
Bug Best Practice introduced by
The expression $monthArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
94
		{
95
			$error = $this->_language->get('article_no') . $this->_language->get('point');
96
		}
97
		else
98
		{
99
			foreach ($monthArray as $key => $articles)
100
			{
101
				foreach ($articles as $value)
102
				{
103
					$outputItem .= $itemElement
104
						->copy()
105
						->html($linkElement
106
							->copy()
107
							->attr('href', $this->_registry->get('parameterRoute') . $articleModel->getRouteById($value->id))
108
							->text($value->title)
109
						);
110
				}
111
112
				/* collect output */
113
114
				$dater->init(strtotime($key));
115
				$month = $dater->getDateTime()->format('n') - 1;
116
				$output .= $titleElement->text($this->_language->get('_month', $month) . ' ' . $dater->getDateTime()->format('Y'));
0 ignored issues
show
Unused Code introduced by
The call to Language::get() has too many arguments starting with $month.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
117
				$output .= $listElement->html($outputItem);
118
				$outputItem = null;
119
			}
120
		}
121
122
		/* handle error */
123
124
		if ($error)
0 ignored issues
show
Bug Best Practice introduced by
The expression $error of type string|null is loosely compared to true; this is ambiguous if the string can be empty. 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 string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
125
		{
126
			$output = $listElement->html(
127
				$itemElement->html($error)
128
			);
129
		}
130
		return $output;
131
	}
132
133
	/**
134
	 * get month array by language
135
	 *
136
	 * @since 3.3.0
137
	 *
138
	 * @param string $language
139
	 *
140
	 * @return array
141
	 */
142
143
	protected function _getMonthArrayByLanguage(string $language = null) : array
144
	{
145
		$monthArray = [];
146
		$dater = new Dater();
147
		$articles = Db::forTablePrefix('articles')
148
			->whereLanguageIs($language)
149
			->whereNull('access')
150
			->where('status', 1)
151
			->orderByDesc('date')
152
			->findMany();
153
154
		/* process article */
155
156
		foreach ($articles as $value)
157
		{
158
			$dater->init($value->date);
159
			$dateKey = $dater->getDateTime()->format('Y-m');
160
			$monthArray[$dateKey][] = $value;
161
		}
162
		return $monthArray;
163
	}
164
}
165