Completed
Push — master ( 5f1ca1...a7b5ff )
by Henry
04:31
created

modules/Archive/Archive.php (1 issue)

Severity

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\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
 * archive ordered by years and months
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' => 'Archive ordered by years and months',
35
		'version' => '4.5.0',
36
		'license' => 'MIT'
37
	];
38
39
	/**
40
	 * array of the option
41
	 *
42
	 * @var array
43
	 */
44
45
	protected $_optionArray =
46
	[
47
		'className' =>
48
		[
49
			'title' => 'rs-title-content-sub rs-title-archive',
50
			'list' => 'rs-list-archive'
51
		]
52
	];
53
54
	/**
55
	 * render
56
	 *
57
	 * @since 2.2.0
58
	 *
59
	 * @return string
60
	 */
61
62
	public function render() : string
63
	{
64
		$output = null;
65
		$outputItem = null;
66
		$error = null;
67
		$articleModel = new Model\Article();
68
		$dater = new Dater();
69
70
		/* html element */
71
72
		$element = new Html\Element();
73
		$titleElement = $element
74
			->copy()
75
			->init('h3',
76
			[
77
				'class' => $this->_optionArray['className']['title']
78
			]);
79
		$listElement = $element
80
			->copy()
81
			->init('ul',
82
			[
83
				'class' => $this->_optionArray['className']['list']
84
			]);
85
		$itemElement = $element->copy()->init('li');
86
		$linkElement = $element->copy()->init('a');
87
88
		/* query articles */
89
90
		$monthArray = $this->_getMonthArrayByLanguage($this->_registry->get('language'));
91
92
		/* process articles */
93
94
		if (!$monthArray)
95
		{
96
			$error = $this->_language->get('article_no') . $this->_language->get('point');
97
		}
98
		else
99
		{
100
			foreach ($monthArray as $key => $articles)
101
			{
102
				foreach ($articles as $value)
103
				{
104
					$outputItem .= $itemElement
105
						->copy()
106
						->html($linkElement
107
							->copy()
108
							->attr('href', $this->_registry->get('parameterRoute') . $articleModel->getRouteById($value->id))
109
							->text($value->title)
110
						);
111
				}
112
113
				/* collect output */
114
115
				$dater->init(strtotime($key));
116
				$month = $dater->getDateTime()->format('n') - 1;
117
				$output .= $titleElement->text($this->_language->get('_month', $month) . ' ' . $dater->getDateTime()->format('Y'));
0 ignored issues
show
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...
118
				$output .= $listElement->html($outputItem);
119
				$outputItem = null;
120
			}
121
		}
122
123
		/* handle error */
124
125
		if ($error)
126
		{
127
			$output = $listElement->html(
128
				$itemElement->html($error)
129
			);
130
		}
131
		return $output;
132
	}
133
134
	/**
135
	 * get month array by language
136
	 *
137
	 * @since 3.3.0
138
	 *
139
	 * @param string $language
140
	 *
141
	 * @return array
142
	 */
143
144
	protected function _getMonthArrayByLanguage(string $language = null) : array
145
	{
146
		$monthArray = [];
147
		$dater = new Dater();
148
		$articles = Db::forTablePrefix('articles')
149
			->whereLanguageIs($language)
150
			->whereNull('access')
151
			->where('status', 1)
152
			->orderByDesc('date')
153
			->findMany();
154
155
		/* process article */
156
157
		foreach ($articles as $value)
158
		{
159
			$dater->init($value->date);
160
			$dateKey = $dater->getDateTime()->format('Y-m');
161
			$monthArray[$dateKey][] = $value;
162
		}
163
		return $monthArray;
164
	}
165
}
166