This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
news_lister.php
237 lines (218 loc) · 7.15 KB
/
news_lister.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
defined('_JEXEC') or die;
/**
* Created by PhpStorm.
* Copyright: Bertschi AG, 2015
* User: jbaumann
* File: news_lister.php
* Date: 03.11.2015
* Time: 12:12
*
* @author Janik Baumann
* @copyright Bertschi AG, 2015
* @license ./LICENSE GNU General Public License version 3
*/
class plgContentNews_Lister extends JPlugin
{
/**
* The regex string which provides as exactly plugin trigger.
*
* @var string
*/
private $_regex = '/{newslist\s(.*)}/i';
/**
* All arguments which were given to the plugin.
*
* @var array
*/
private $_arguments = array();
/**
* The default values for the required arguments.
*
* @var array
*/
private $_default_options = array(
'template' => 'default',
'categories' => '',
'offset' => 0,
'limit' => 0,
);
/**
* All selected categories.
*
* @var array
*/
private $_categories = array();
/**
* All selected articles.
*
* @var array
*/
private $_articles = array();
/**
* The rendered contents.
*
* @var array
*/
private $_content = array();
/**
* All available and granted hooks.
*
* @var array
*/
private $_allowed_hooks = array(
'nl',
'article',
'category',
);
/**
* Plugin that loads a list of articles within content
*
* @param string $pContext The context of the content being passed to the plugin.
* @param object &$pArticle The article object. Note $article->text is also available
* @param mixed &$pParams The article params
* @param integer $pPage The 'page' number
*
* @return mixed true if there is an error. Void otherwise.
*/
public function onContentPrepare($pContext, &$pArticle, &$pParams, $pPage = 0)
{
if (!$this->_shouldRun($pContext) && !$this->_wasTriggered($pArticle)) {
return true;
}
if (!$this->_prepareArguments($pArticle)) {
return true;
}
define('NL_ROOT', dirname(__FILE__));
$this->_importClasses();
$this->_selectCategories();
$this->_selectArticles();
$this->_prepareContent();
$this->_replaceContext($pArticle);
return true;
}
/**
* Imports all necessary classes.
*/
private function _importClasses()
{
if (!class_exists('ContentPreparer')) require(NL_ROOT . '/engines/ContentPreparer.php');
}
/**
* Check if the plugin should run
*
* @param &$pContext
*
* @return bool True if the plugin should run
*/
private function _shouldRun(&$pContext)
{
if ($pContext == 'com_content.indexer') {
return false;
}
return true;
}
/**
* Checks if the plugin was triggered inaccurately.
*
* @param object $pArticle The article which should be parsed.
* @return bool Returns true if plugin is triggered inaccurate, false otherwise.
*/
private function _wasTriggered(&$pArticle)
{
if (strpos($pArticle->text, 'newslist') === false) {
return true;
}
return false;
}
/**
* Checks if the plugin was triggered accurately.
* Stores the arguments if plugin was triggered.
*
* @param object $pArticle The article which should be parsed.
* @return bool Returns true if the plugin is triggered accurate, false otherwise.
*/
private function _prepareArguments(&$pArticle)
{
preg_match_all($this->_regex, $pArticle->text, $matches, PREG_SET_ORDER);
if ($matches) {
foreach ($matches as $match) {
$tmp = json_decode($match[1], true);
if (!isset($tmp['template']) || !is_array($tmp['categories'])) {
return false;
}
$this->_arguments[] = $tmp;
}
} else {
return false;
}
return true;
}
/**
* Selects all categories which match the given arguments.
*/
private function _selectCategories()
{
foreach ($this->_arguments as $pValue) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from($db->quoteName('#__categories'));
$query->where($db->quoteName('extension') . ' = ' . $db->quote('com_content'));
$query->where($db->quoteName('path') . ' REGEXP ' . $db->quote('^' . implode('(.*)$|^', $pValue['categories']) . '(.*)$'));
$query->where($db->quoteName('published') . ' = ' . $db->quote('1'));
$query->where('(' . $db->quoteName('language') . ' = ' . $db->quote(JFactory::getLanguage()->getTag()) . ' OR ' . $db->quoteName('language') . ' = ' . $db->quote('*') . ')');
$query->order($db->quoteName('title') . ' ASC');
$db->setQuery($query);
$this->_categories[] = $db->loadObjectList();
}
}
/**
* Selects all articles which match the given arguments and categories.
*/
private function _selectArticles()
{
foreach ($this->_categories as $pKey => $pPass) {
$categories = null;
foreach ($pPass as $pValue) {
$categories[] = $pValue->id;
}
if ($categories != null) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$limit = (isset($this->_arguments[$pKey]['limit']) && is_numeric($this->_arguments[$pKey]['limit'])) ? $this->_arguments[$pKey]['limit'] : $this->_default_options['limit'];
$offset = (isset($this->_arguments[$pKey]['offset']) && is_numeric($this->_arguments[$pKey]['offset'])) ? $this->_arguments[$pKey]['offset'] : $this->_default_options['offset'];
$query
->select('*')
->from($db->quoteName('#__content'))
->where($db->quoteName('catid') . ' REGEXP ' . $db->quote('^' . implode('$|^', $categories) . '$'))
->where($db->quoteName('state') . ' = ' . $db->quote('1'))
->order($db->quoteName('created') . ' DESC');
$db->setQuery($query, $offset, $limit);
$this->_articles[] = $db->loadObjectList();
}
}
}
/**
* Starts the content preparer which prepares the content.
*/
private function _prepareContent()
{
$cp = new ContentPreparer($this->_allowed_hooks, $this->_default_options);
foreach ($this->_arguments as $pArgumentKey => $pArgumentValues) {
$cp->newValues($this->_categories[$pArgumentKey], $pArgumentValues);
foreach ($this->_articles[$pArgumentKey] as $pArticleValues) {
$this->_content[] = $cp->prepare($pArticleValues);
}
}
}
/**
* Replaces the none parsed content with the parsed one.
*
* @param object $pArticle The article which should be parsed.
*/
private function _replaceContext(&$pArticle)
{
$pArticle->text = preg_replace($this->_regex, implode('', $this->_content), $pArticle->text);
}
}