1<?php
2/**
3 * @package     Joomla.Administrator
4 * @subpackage  com_categories
5 *
6 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
7 * @license     GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10defined('_JEXEC') or die;
11
12/**
13 * Categories view class for the Category package.
14 *
15 * @since  1.6
16 */
17class CategoriesViewCategories extends JViewLegacy
18{
19	/**
20	 * An array of items
21	 *
22	 * @var  array
23	 */
24	protected $items;
25
26	/**
27	 * The pagination object
28	 *
29	 * @var  JPagination
30	 */
31	protected $pagination;
32
33	/**
34	 * The model state
35	 *
36	 * @var  object
37	 */
38	protected $state;
39
40	/**
41	 * Flag if an association exists
42	 *
43	 * @var  boolean
44	 */
45	protected $assoc;
46
47	/**
48	 * Form object for search filters
49	 *
50	 * @var  JForm
51	 */
52	public $filterForm;
53
54	/**
55	 * The active search filters
56	 *
57	 * @var  array
58	 */
59	public $activeFilters;
60
61	/**
62	 * The sidebar markup
63	 *
64	 * @var  string
65	 */
66	protected $string;
67
68	/**
69	 * Display the view
70	 *
71	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
72	 *
73	 * @return  mixed  A string if successful, otherwise an Error object.
74	 */
75	public function display($tpl = null)
76	{
77		$this->state         = $this->get('State');
78		$this->items         = $this->get('Items');
79		$this->pagination    = $this->get('Pagination');
80		$this->assoc         = $this->get('Assoc');
81		$this->filterForm    = $this->get('FilterForm');
82		$this->activeFilters = $this->get('ActiveFilters');
83
84		// Check for errors.
85		if (count($errors = $this->get('Errors')))
86		{
87			throw new Exception(implode("\n", $errors), 500);
88		}
89
90		// Preprocess the list of items to find ordering divisions.
91		foreach ($this->items as &$item)
92		{
93			$this->ordering[$item->parent_id][] = $item->id;
94		}
95
96		// Levels filter - Used in Hathor.
97		$this->f_levels = array(
98			JHtml::_('select.option', '1', JText::_('J1')),
99			JHtml::_('select.option', '2', JText::_('J2')),
100			JHtml::_('select.option', '3', JText::_('J3')),
101			JHtml::_('select.option', '4', JText::_('J4')),
102			JHtml::_('select.option', '5', JText::_('J5')),
103			JHtml::_('select.option', '6', JText::_('J6')),
104			JHtml::_('select.option', '7', JText::_('J7')),
105			JHtml::_('select.option', '8', JText::_('J8')),
106			JHtml::_('select.option', '9', JText::_('J9')),
107			JHtml::_('select.option', '10', JText::_('J10')),
108		);
109
110		// We don't need toolbar in the modal window.
111		if ($this->getLayout() !== 'modal')
112		{
113			$this->addToolbar();
114			$this->sidebar = JHtmlSidebar::render();
115		}
116		else
117		{
118			// In article associations modal we need to remove language filter if forcing a language.
119			if ($forcedLanguage = JFactory::getApplication()->input->get('forcedLanguage', '', 'CMD'))
120			{
121				// If the language is forced we can't allow to select the language, so transform the language selector filter into a hidden field.
122				$languageXml = new SimpleXMLElement('<field name="language" type="hidden" default="' . $forcedLanguage . '" />');
123				$this->filterForm->setField($languageXml, 'filter', true);
124
125				// Also, unset the active language filter so the search tools is not open by default with this filter.
126				unset($this->activeFilters['language']);
127			}
128		}
129
130		return parent::display($tpl);
131	}
132
133	/**
134	 * Add the page title and toolbar.
135	 *
136	 * @return  void
137	 *
138	 * @since   1.6
139	 */
140	protected function addToolbar()
141	{
142		$categoryId = $this->state->get('filter.category_id');
143		$component  = $this->state->get('filter.component');
144		$section    = $this->state->get('filter.section');
145		$canDo      = JHelperContent::getActions($component, 'category', $categoryId);
146		$user       = JFactory::getUser();
147
148		// Get the toolbar object instance
149		$bar = JToolbar::getInstance('toolbar');
150
151		// Avoid nonsense situation.
152		if ($component == 'com_categories')
153		{
154			return;
155		}
156
157		// Need to load the menu language file as mod_menu hasn't been loaded yet.
158		$lang = JFactory::getLanguage();
159		$lang->load($component, JPATH_BASE, null, false, true)
160		|| $lang->load($component, JPATH_ADMINISTRATOR . '/components/' . $component, null, false, true);
161
162		// Load the category helper.
163		JLoader::register('CategoriesHelper', JPATH_ADMINISTRATOR . '/components/com_categories/helpers/categories.php');
164
165		// If a component categories title string is present, let's use it.
166		if ($lang->hasKey($component_title_key = strtoupper($component . ($section ? "_$section" : '')) . '_CATEGORIES_TITLE'))
167		{
168			$title = JText::_($component_title_key);
169		}
170		elseif ($lang->hasKey($component_section_key = strtoupper($component . ($section ? "_$section" : ''))))
171		// Else if the component section string exits, let's use it
172		{
173			$title = JText::sprintf('COM_CATEGORIES_CATEGORIES_TITLE', $this->escape(JText::_($component_section_key)));
174		}
175		else
176		// Else use the base title
177		{
178			$title = JText::_('COM_CATEGORIES_CATEGORIES_BASE_TITLE');
179		}
180
181		// Load specific css component
182		JHtml::_('stylesheet', $component . '/administrator/categories.css', array('version' => 'auto', 'relative' => true));
183
184		// Prepare the toolbar.
185		JToolbarHelper::title($title, 'folder categories ' . substr($component, 4) . ($section ? "-$section" : '') . '-categories');
186
187		if ($canDo->get('core.create') || count($user->getAuthorisedCategories($component, 'core.create')) > 0)
188		{
189			JToolbarHelper::addNew('category.add');
190		}
191
192		if ($canDo->get('core.edit') || $canDo->get('core.edit.own'))
193		{
194			JToolbarHelper::editList('category.edit');
195		}
196
197		if ($canDo->get('core.edit.state'))
198		{
199			JToolbarHelper::publish('categories.publish', 'JTOOLBAR_PUBLISH', true);
200			JToolbarHelper::unpublish('categories.unpublish', 'JTOOLBAR_UNPUBLISH', true);
201			JToolbarHelper::archiveList('categories.archive');
202		}
203
204		if (JFactory::getUser()->authorise('core.admin'))
205		{
206			JToolbarHelper::checkin('categories.checkin');
207		}
208
209		// Add a batch button
210		if ($canDo->get('core.create')
211			&& $canDo->get('core.edit')
212			&& $canDo->get('core.edit.state'))
213		{
214			$title = JText::_('JTOOLBAR_BATCH');
215
216			// Instantiate a new JLayoutFile instance and render the batch button
217			$layout = new JLayoutFile('joomla.toolbar.batch');
218
219			$dhtml = $layout->render(array('title' => $title));
220			$bar->appendButton('Custom', $dhtml, 'batch');
221		}
222
223		if ($canDo->get('core.admin'))
224		{
225			JToolbarHelper::custom('categories.rebuild', 'refresh.png', 'refresh_f2.png', 'JTOOLBAR_REBUILD', false);
226		}
227
228		if ($canDo->get('core.admin') || $canDo->get('core.options'))
229		{
230			JToolbarHelper::preferences($component);
231		}
232
233		if ($this->state->get('filter.published') == -2 && $canDo->get('core.delete', $component))
234		{
235			JToolbarHelper::deleteList('JGLOBAL_CONFIRM_DELETE', 'categories.delete', 'JTOOLBAR_EMPTY_TRASH');
236		}
237		elseif ($canDo->get('core.edit.state'))
238		{
239			JToolbarHelper::trash('categories.trash');
240		}
241
242		// Compute the ref_key if it does exist in the component
243		if (!$lang->hasKey($ref_key = strtoupper($component . ($section ? "_$section" : '')) . '_CATEGORIES_HELP_KEY'))
244		{
245			$ref_key = 'JHELP_COMPONENTS_' . strtoupper(substr($component, 4) . ($section ? "_$section" : '')) . '_CATEGORIES';
246		}
247
248		/*
249		 * Get help for the categories view for the component by
250		 * -remotely searching in a language defined dedicated URL: *component*_HELP_URL
251		 * -locally  searching in a component help file if helpURL param exists in the component and is set to ''
252		 * -remotely searching in a component URL if helpURL param exists in the component and is NOT set to ''
253		 */
254		if ($lang->hasKey($lang_help_url = strtoupper($component) . '_HELP_URL'))
255		{
256			$debug = $lang->setDebug(false);
257			$url = JText::_($lang_help_url);
258			$lang->setDebug($debug);
259		}
260		else
261		{
262			$url = null;
263		}
264
265		JToolbarHelper::help($ref_key, JComponentHelper::getParams($component)->exists('helpURL'), $url);
266	}
267
268	/**
269	 * Returns an array of fields the table can be sorted by
270	 *
271	 * @return  array  Array containing the field name to sort by as the key and display text as value
272	 *
273	 * @since   3.0
274	 */
275	protected function getSortFields()
276	{
277		return array(
278			'a.lft'       => JText::_('JGRID_HEADING_ORDERING'),
279			'a.published' => JText::_('JSTATUS'),
280			'a.title'     => JText::_('JGLOBAL_TITLE'),
281			'a.access'    => JText::_('JGRID_HEADING_ACCESS'),
282			'language'    => JText::_('JGRID_HEADING_LANGUAGE'),
283			'a.id'        => JText::_('JGRID_HEADING_ID'),
284		);
285	}
286}
287