Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
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
 * 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'));
90
91
		/* process articles */
92
93
		if (!$monthArray)
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
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)
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