CategoryView.php
8 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\MVC\View;
defined('JPATH_PLATFORM') or die;
/**
* Base HTML View class for the a Category list
*
* @since 3.2
*/
class CategoryView extends HtmlView
{
/**
* State data
*
* @var \Joomla\Registry\Registry
* @since 3.2
*/
protected $state;
/**
* Category items data
*
* @var array
* @since 3.2
*/
protected $items;
/**
* The category model object for this category
*
* @var \JModelCategory
* @since 3.2
*/
protected $category;
/**
* The list of other categories for this extension.
*
* @var array
* @since 3.2
*/
protected $categories;
/**
* Pagination object
*
* @var \JPagination
* @since 3.2
*/
protected $pagination;
/**
* Child objects
*
* @var array
* @since 3.2
*/
protected $children;
/**
* The name of the extension for the category
*
* @var string
* @since 3.2
*/
protected $extension;
/**
* The name of the view to link individual items to
*
* @var string
* @since 3.2
*/
protected $viewName;
/**
* Default title to use for page title
*
* @var string
* @since 3.2
*/
protected $defaultPageTitle;
/**
* Whether to run the standard Joomla plugin events.
* Off by default for b/c
*
* @var bool
* @since 3.5
*/
protected $runPlugins = false;
/**
* Method with common display elements used in category list displays
*
* @return boolean|\JException|void Boolean false or \JException instance on error, nothing otherwise
*
* @since 3.2
*/
public function commonCategoryDisplay()
{
$app = \JFactory::getApplication();
$user = \JFactory::getUser();
$params = $app->getParams();
// Get some data from the models
$model = $this->getModel();
$paramsModel = $model->getState('params');
$paramsModel->set('check_access_rights', 0);
$model->setState('params', $paramsModel);
$state = $this->get('State');
$category = $this->get('Category');
$children = $this->get('Children');
$parent = $this->get('Parent');
if ($category == false)
{
return \JError::raiseError(404, \JText::_('JGLOBAL_CATEGORY_NOT_FOUND'));
}
if ($parent == false)
{
return \JError::raiseError(404, \JText::_('JGLOBAL_CATEGORY_NOT_FOUND'));
}
// Check whether category access level allows access.
$groups = $user->getAuthorisedViewLevels();
if (!in_array($category->access, $groups))
{
return \JError::raiseError(403, \JText::_('JERROR_ALERTNOAUTHOR'));
}
$items = $this->get('Items');
$pagination = $this->get('Pagination');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
\JError::raiseError(500, implode("\n", $errors));
return false;
}
// Setup the category parameters.
$cparams = $category->getParams();
$category->params = clone $params;
$category->params->merge($cparams);
$children = array($category->id => $children);
// Escape strings for HTML output
$this->pageclass_sfx = htmlspecialchars($params->get('pageclass_sfx'));
if ($this->runPlugins)
{
\JPluginHelper::importPlugin('content');
foreach ($items as $itemElement)
{
$itemElement = (object) $itemElement;
$itemElement->event = new \stdClass;
// For some plugins.
!empty($itemElement->description) ? $itemElement->text = $itemElement->description : $itemElement->text = null;
$dispatcher = \JEventDispatcher::getInstance();
$dispatcher->trigger('onContentPrepare', array($this->extension . '.category', &$itemElement, &$itemElement->params, 0));
$results = $dispatcher->trigger('onContentAfterTitle', array($this->extension . '.category', &$itemElement, &$itemElement->core_params, 0));
$itemElement->event->afterDisplayTitle = trim(implode("\n", $results));
$results = $dispatcher->trigger('onContentBeforeDisplay', array($this->extension . '.category', &$itemElement, &$itemElement->core_params, 0));
$itemElement->event->beforeDisplayContent = trim(implode("\n", $results));
$results = $dispatcher->trigger('onContentAfterDisplay', array($this->extension . '.category', &$itemElement, &$itemElement->core_params, 0));
$itemElement->event->afterDisplayContent = trim(implode("\n", $results));
if ($itemElement->text)
{
$itemElement->description = $itemElement->text;
}
}
}
$maxLevel = $params->get('maxLevel', -1) < 0 ? PHP_INT_MAX : $params->get('maxLevel', PHP_INT_MAX);
$this->maxLevel = &$maxLevel;
$this->state = &$state;
$this->items = &$items;
$this->category = &$category;
$this->children = &$children;
$this->params = &$params;
$this->parent = &$parent;
$this->pagination = &$pagination;
$this->user = &$user;
// Check for layout override only if this is not the active menu item
// If it is the active menu item, then the view and category id will match
$active = $app->getMenu()->getActive();
if ($active
&& $active->component == $this->extension
&& isset($active->query['view'], $active->query['id'])
&& $active->query['view'] == 'category'
&& $active->query['id'] == $this->category->id)
{
if (isset($active->query['layout']))
{
$this->setLayout($active->query['layout']);
}
}
elseif ($layout = $category->params->get('category_layout'))
{
$this->setLayout($layout);
}
$this->category->tags = new \JHelperTags;
$this->category->tags->getItemTags($this->extension . '.category', $this->category->id);
}
/**
* Execute and display a template script.
*
* @param string $tpl The name of the template file to parse; automatically searches through the template paths.
*
* @return mixed A string if successful, otherwise an Error object.
*
* @since 3.2
*/
public function display($tpl = null)
{
$this->prepareDocument();
return parent::display($tpl);
}
/**
* Method to prepares the document
*
* @return void
*
* @since 3.2
*/
protected function prepareDocument()
{
$app = \JFactory::getApplication();
$menus = $app->getMenu();
$this->pathway = $app->getPathway();
$title = null;
// Because the application sets a default page title, we need to get it from the menu item itself
$this->menu = $menus->getActive();
if ($this->menu)
{
$this->params->def('page_heading', $this->params->get('page_title', $this->menu->title));
}
else
{
$this->params->def('page_heading', \JText::_($this->defaultPageTitle));
}
$title = $this->params->get('page_title', '');
if (empty($title))
{
$title = $app->get('sitename');
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
$title = \JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
elseif ($app->get('sitename_pagetitles', 0) == 2)
{
$title = \JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}
$this->document->setTitle($title);
if ($this->params->get('menu-meta_description'))
{
$this->document->setDescription($this->params->get('menu-meta_description'));
}
if ($this->params->get('menu-meta_keywords'))
{
$this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
}
if ($this->params->get('robots'))
{
$this->document->setMetadata('robots', $this->params->get('robots'));
}
}
/**
* Method to add an alternative feed link to a category layout.
*
* @return void
*
* @since 3.2
*/
protected function addFeed()
{
if ($this->params->get('show_feed_link', 1) == 1)
{
$link = '&format=feed&limitstart=';
$attribs = array('type' => 'application/rss+xml', 'title' => 'RSS 2.0');
$this->document->addHeadLink(\JRoute::_($link . '&type=rss'), 'alternate', 'rel', $attribs);
$attribs = array('type' => 'application/atom+xml', 'title' => 'Atom 1.0');
$this->document->addHeadLink(\JRoute::_($link . '&type=atom'), 'alternate', 'rel', $attribs);
}
}
}