1<?php
2/**
3 * @package     Joomla.Administrator
4 * @subpackage  com_modules
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 * View class for a list of modules.
14 *
15 * @since  1.6
16 */
17class ModulesViewModules extends JViewLegacy
18{
19	protected $items;
20
21	protected $pagination;
22
23	protected $state;
24
25	/**
26	 * Display the view
27	 *
28	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
29	 *
30	 * @return  mixed  A string if successful, otherwise an Error object.
31	 *
32	 * @since   1.6
33	 */
34	public function display($tpl = null)
35	{
36		$this->items         = $this->get('Items');
37		$this->pagination    = $this->get('Pagination');
38		$this->state         = $this->get('State');
39		$this->total         = $this->get('Total');
40		$this->filterForm    = $this->get('FilterForm');
41		$this->activeFilters = $this->get('ActiveFilters');
42		$this->clientId      = $this->state->get('client_id');
43
44		// Check for errors.
45		if (count($errors = $this->get('Errors')))
46		{
47			throw new Exception(implode("\n", $errors), 500);
48		}
49
50		// We do not need the Language filter when modules are not filtered
51		if ($this->clientId == 1 && !JModuleHelper::isAdminMultilang())
52		{
53			unset($this->activeFilters['language']);
54			$this->filterForm->removeField('language', 'filter');
55		}
56
57		// We don't need the toolbar in the modal window.
58		if ($this->getLayout() !== 'modal')
59		{
60			$this->addToolbar();
61		}
62		// If in modal layout.
63		else
64		{
65			// Client id selector should not exist.
66			$this->filterForm->removeField('client_id', '');
67
68			// If in the frontend state and language should not activate the search tools.
69			if (JFactory::getApplication()->isClient('site'))
70			{
71				unset($this->activeFilters['state']);
72				unset($this->activeFilters['language']);
73			}
74		}
75
76		// Include the component HTML helpers.
77		JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');
78
79		return parent::display($tpl);
80	}
81
82	/**
83	 * Add the page title and toolbar.
84	 *
85	 * @return  void
86	 *
87	 * @since   1.6
88	 */
89	protected function addToolbar()
90	{
91		$state = $this->get('State');
92		$canDo = JHelperContent::getActions('com_modules');
93		$user  = JFactory::getUser();
94
95		// Get the toolbar object instance
96		$bar = JToolbar::getInstance('toolbar');
97
98		if ($state->get('client_id') == 1)
99		{
100			JToolbarHelper::title(JText::_('COM_MODULES_MANAGER_MODULES_ADMIN'), 'cube module');
101		}
102		else
103		{
104			JToolbarHelper::title(JText::_('COM_MODULES_MANAGER_MODULES_SITE'), 'cube module');
105		}
106
107		if ($canDo->get('core.create'))
108		{
109			// Instantiate a new JLayoutFile instance and render the layout
110			$layout = new JLayoutFile('toolbar.newmodule');
111
112			$bar->appendButton('Custom', $layout->render(array()), 'new');
113		}
114
115		if ($canDo->get('core.edit'))
116		{
117			JToolbarHelper::editList('module.edit');
118		}
119
120		if ($canDo->get('core.create'))
121		{
122			JToolbarHelper::custom('modules.duplicate', 'copy.png', 'copy_f2.png', 'JTOOLBAR_DUPLICATE', true);
123		}
124
125		if ($canDo->get('core.edit.state'))
126		{
127			JToolbarHelper::publish('modules.publish', 'JTOOLBAR_PUBLISH', true);
128			JToolbarHelper::unpublish('modules.unpublish', 'JTOOLBAR_UNPUBLISH', true);
129			JToolbarHelper::checkin('modules.checkin');
130		}
131
132		// Add a batch button
133		if ($user->authorise('core.create', 'com_modules') && $user->authorise('core.edit', 'com_modules')
134			&& $user->authorise('core.edit.state', 'com_modules'))
135		{
136			JHtml::_('bootstrap.renderModal', 'collapseModal');
137			$title = JText::_('JTOOLBAR_BATCH');
138
139			// Instantiate a new JLayoutFile instance and render the batch button
140			$layout = new JLayoutFile('joomla.toolbar.batch');
141
142			$dhtml = $layout->render(array('title' => $title));
143			$bar->appendButton('Custom', $dhtml, 'batch');
144		}
145
146		if ($state->get('filter.state') == -2 && $canDo->get('core.delete'))
147		{
148			JToolbarHelper::deleteList('JGLOBAL_CONFIRM_DELETE', 'modules.delete', 'JTOOLBAR_EMPTY_TRASH');
149		}
150		elseif ($canDo->get('core.edit.state'))
151		{
152			JToolbarHelper::trash('modules.trash');
153		}
154
155		if ($canDo->get('core.admin'))
156		{
157			JToolbarHelper::preferences('com_modules');
158		}
159
160		JToolbarHelper::help('JHELP_EXTENSIONS_MODULE_MANAGER');
161
162		if (JHtmlSidebar::getEntries())
163		{
164			$this->sidebar = JHtmlSidebar::render();
165		}
166	}
167
168	/**
169	 * Returns an array of fields the table can be sorted by
170	 *
171	 * @return  array  Array containing the field name to sort by as the key and display text as value
172	 *
173	 * @since   3.0
174	 */
175	protected function getSortFields()
176	{
177		$this->state = $this->get('State');
178
179		if ($this->state->get('client_id') == 0)
180		{
181			if ($this->getLayout() == 'default')
182			{
183				return array(
184					'ordering'       => JText::_('JGRID_HEADING_ORDERING'),
185					'a.published'    => JText::_('JSTATUS'),
186					'a.title'        => JText::_('JGLOBAL_TITLE'),
187					'position'       => JText::_('COM_MODULES_HEADING_POSITION'),
188					'name'           => JText::_('COM_MODULES_HEADING_MODULE'),
189					'pages'          => JText::_('COM_MODULES_HEADING_PAGES'),
190					'a.access'       => JText::_('JGRID_HEADING_ACCESS'),
191					'language_title' => JText::_('JGRID_HEADING_LANGUAGE'),
192					'a.id'           => JText::_('JGRID_HEADING_ID')
193				);
194			}
195
196			return array(
197				'a.title'        => JText::_('JGLOBAL_TITLE'),
198				'position'       => JText::_('COM_MODULES_HEADING_POSITION'),
199				'name'           => JText::_('COM_MODULES_HEADING_MODULE'),
200				'pages'          => JText::_('COM_MODULES_HEADING_PAGES'),
201				'a.access'       => JText::_('JGRID_HEADING_ACCESS'),
202				'language_title' => JText::_('JGRID_HEADING_LANGUAGE'),
203				'a.id'           => JText::_('JGRID_HEADING_ID')
204			);
205		}
206		else
207		{
208			if ($this->getLayout() == 'default')
209			{
210				return array(
211					'ordering'       => JText::_('JGRID_HEADING_ORDERING'),
212					'a.published'    => JText::_('JSTATUS'),
213					'a.title'        => JText::_('JGLOBAL_TITLE'),
214					'position'       => JText::_('COM_MODULES_HEADING_POSITION'),
215					'name'           => JText::_('COM_MODULES_HEADING_MODULE'),
216					'a.access'       => JText::_('JGRID_HEADING_ACCESS'),
217					'a.language'     => JText::_('JGRID_HEADING_LANGUAGE'),
218					'a.id'           => JText::_('JGRID_HEADING_ID')
219				);
220			}
221
222			return array(
223					'a.title'        => JText::_('JGLOBAL_TITLE'),
224					'position'       => JText::_('COM_MODULES_HEADING_POSITION'),
225					'name'           => JText::_('COM_MODULES_HEADING_MODULE'),
226					'a.access'       => JText::_('JGRID_HEADING_ACCESS'),
227					'a.language'     => JText::_('JGRID_HEADING_LANGUAGE'),
228					'a.id'           => JText::_('JGRID_HEADING_ID')
229			);
230		}
231	}
232}
233