1<?php
2/**
3 * @package     Joomla.Administrator
4 * @subpackage  com_associations
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 articles.
14 *
15 * @since  3.7.0
16 */
17class AssociationsViewAssociations extends JViewLegacy
18{
19	/**
20	 * An array of items
21	 *
22	 * @var   array
23	 *
24	 * @since  3.7.0
25	 */
26	protected $items;
27
28	/**
29	 * The pagination object
30	 *
31	 * @var    JPagination
32	 *
33	 * @since  3.7.0
34	 */
35	protected $pagination;
36
37	/**
38	 * The model state
39	 *
40	 * @var    object
41	 *
42	 * @since  3.7.0
43	 */
44	protected $state;
45
46	/**
47	 * Selected item type properties.
48	 *
49	 * @var    Registry
50	 *
51	 * @since  3.7.0
52	 */
53	public $itemType = null;
54
55	/**
56	 * Display the view
57	 *
58	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
59	 *
60	 * @return  void
61	 *
62	 * @since   3.7.0
63	 */
64	public function display($tpl = null)
65	{
66		$this->state         = $this->get('State');
67		$this->filterForm    = $this->get('FilterForm');
68		$this->activeFilters = $this->get('ActiveFilters');
69
70		if (!JLanguageAssociations::isEnabled())
71		{
72			$link = JRoute::_('index.php?option=com_plugins&task=plugin.edit&extension_id=' . AssociationsHelper::getLanguagefilterPluginId());
73			JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_ASSOCIATIONS_ERROR_NO_ASSOC', $link), 'warning');
74		}
75		elseif ($this->state->get('itemtype') == '' || $this->state->get('language') == '')
76		{
77			JFactory::getApplication()->enqueueMessage(JText::_('COM_ASSOCIATIONS_NOTICE_NO_SELECTORS'), 'notice');
78		}
79		else
80		{
81			$type = null;
82
83			list($extensionName, $typeName) = explode('.', $this->state->get('itemtype'));
84
85			$extension = AssociationsHelper::getSupportedExtension($extensionName);
86
87			$types = $extension->get('types');
88
89			if (array_key_exists($typeName, $types))
90			{
91				$type = $types[$typeName];
92			}
93
94			$this->itemType = $type;
95
96			if (is_null($type))
97			{
98				JFactory::getApplication()->enqueueMessage(JText::_('COM_ASSOCIATIONS_ERROR_NO_TYPE'), 'warning');
99			}
100			else
101			{
102				$this->extensionName = $extensionName;
103				$this->typeName      = $typeName;
104				$this->typeSupports  = array();
105				$this->typeFields    = array();
106
107				$details = $type->get('details');
108
109				if (array_key_exists('support', $details))
110				{
111					$support = $details['support'];
112					$this->typeSupports = $support;
113				}
114
115				if (array_key_exists('fields', $details))
116				{
117					$fields = $details['fields'];
118					$this->typeFields = $fields;
119				}
120
121				// Dynamic filter form.
122				// This selectors doesn't have to activate the filter bar.
123				unset($this->activeFilters['itemtype']);
124				unset($this->activeFilters['language']);
125
126				// Remove filters options depending on selected type.
127				if (empty($support['state']))
128				{
129					unset($this->activeFilters['state']);
130					$this->filterForm->removeField('state', 'filter');
131				}
132
133				if (empty($support['category']))
134				{
135					unset($this->activeFilters['category_id']);
136					$this->filterForm->removeField('category_id', 'filter');
137				}
138
139				if ($extensionName !== 'com_menus')
140				{
141					unset($this->activeFilters['menutype']);
142					$this->filterForm->removeField('menutype', 'filter');
143				}
144
145				if (empty($support['level']))
146				{
147					unset($this->activeFilters['level']);
148					$this->filterForm->removeField('level', 'filter');
149				}
150
151				if (empty($support['acl']))
152				{
153					unset($this->activeFilters['access']);
154					$this->filterForm->removeField('access', 'filter');
155				}
156
157				// Add extension attribute to category filter.
158				if (empty($support['catid']))
159				{
160					$this->filterForm->setFieldAttribute('category_id', 'extension', $extensionName, 'filter');
161
162					if ($this->getLayout() == 'modal')
163					{
164						// We need to change the category filter to only show categories tagged to All or to the forced language.
165						if ($forcedLanguage = JFactory::getApplication()->input->get('forcedLanguage', '', 'CMD'))
166						{
167							$this->filterForm->setFieldAttribute('category_id', 'language', '*,' . $forcedLanguage, 'filter');
168						}
169					}
170				}
171
172				$this->items      = $this->get('Items');
173				$this->pagination = $this->get('Pagination');
174
175				$linkParameters = array(
176					'layout'     => 'edit',
177					'itemtype'   => $extensionName . '.' . $typeName,
178					'task'       => 'association.edit',
179				);
180
181				$this->editUri = 'index.php?option=com_associations&view=association&' . http_build_query($linkParameters);
182			}
183		}
184
185		// Check for errors.
186		if (count($errors = $this->get('Errors')))
187		{
188			throw new Exception(implode("\n", $errors), 500);
189		}
190
191		$this->addToolbar();
192
193		// Will add sidebar if needed $this->sidebar = JHtmlSidebar::render();
194		parent::display($tpl);
195	}
196
197	/**
198	 * Add the page title and toolbar.
199	 *
200	 * @return  void
201	 *
202	 * @since   3.7.0
203	 */
204	protected function addToolbar()
205	{
206		$user = JFactory::getUser();
207
208		if (isset($this->typeName) && isset($this->extensionName))
209		{
210			$helper = AssociationsHelper::getExtensionHelper($this->extensionName);
211			$title  = $helper->getTypeTitle($this->typeName);
212
213			$languageKey = strtoupper($this->extensionName . '_' . $title . 'S');
214
215			if ($this->typeName === 'category')
216			{
217				$languageKey = strtoupper($this->extensionName) . '_CATEGORIES';
218			}
219
220			JToolbarHelper::title(
221				JText::sprintf(
222					'COM_ASSOCIATIONS_TITLE_LIST', JText::_($this->extensionName), JText::_($languageKey)
223				), 'contract assoc'
224			);
225		}
226		else
227		{
228			JToolbarHelper::title(JText::_('COM_ASSOCIATIONS_TITLE_LIST_SELECT'), 'contract assoc');
229		}
230
231		if ($user->authorise('core.admin', 'com_associations') || $user->authorise('core.options', 'com_associations'))
232		{
233			if (!isset($this->typeName))
234			{
235				JToolbarHelper::custom('associations.purge', 'purge', 'purge', 'COM_ASSOCIATIONS_PURGE', false, false);
236				JToolbarHelper::custom('associations.clean', 'refresh', 'refresh', 'COM_ASSOCIATIONS_DELETE_ORPHANS', false, false);
237			}
238
239			JToolbarHelper::preferences('com_associations');
240		}
241
242		JToolbarHelper::help('JHELP_COMPONENTS_ASSOCIATIONS');
243	}
244}
245