1 <?php
2 /**
3 * @package Joomla.Legacy
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 JFormHelper::loadFieldClass('list');
13
14 /**
15 * Form Field class for the Joomla Platform.
16 * Supports an HTML select list of categories
17 *
18 * @since 1.6
19 */
20 class JFormFieldCategory extends JFormFieldList
21 {
22 /**
23 * The form field type.
24 *
25 * @var string
26 * @since 1.6
27 */
28 public $type = 'Category';
29
30 /**
31 * Method to get the field options for category
32 * Use the extension attribute in a form to specify the.specific extension for
33 * which categories should be displayed.
34 * Use the show_root attribute to specify whether to show the global category root in the list.
35 *
36 * @return array The field option objects.
37 *
38 * @since 1.6
39 */
40 protected function getOptions()
41 {
42 $options = array();
43 $extension = $this->element['extension'] ? (string) $this->element['extension'] : (string) $this->element['scope'];
44 $published = (string) $this->element['published'];
45 $language = (string) $this->element['language'];
46
47 // Load the category options for a given extension.
48 if (!empty($extension))
49 {
50 // Filter over published state or not depending upon if it is present.
51 $filters = array();
52 if ($published)
53 {
54 $filters['filter.published'] = explode(',', $published);
55 }
56
57 // Filter over language depending upon if it is present.
58 if ($language)
59 {
60 $filters['filter.language'] = explode(',', $language);
61 }
62
63 if ($filters === array())
64 {
65 $options = JHtml::_('category.options', $extension);
66 }
67 else
68 {
69 $options = JHtml::_('category.options', $extension, $filters);
70 }
71
72 // Verify permissions. If the action attribute is set, then we scan the options.
73 if ((string) $this->element['action'])
74 {
75 // Get the current user object.
76 $user = JFactory::getUser();
77
78 foreach ($options as $i => $option)
79 {
80 /*
81 * To take save or create in a category you need to have create rights for that category
82 * unless the item is already in that category.
83 * Unset the option if the user isn't authorised for it. In this field assets are always categories.
84 */
85 if ($user->authorise('core.create', $extension . '.category.' . $option->value) != true)
86 {
87 unset($options[$i]);
88 }
89 }
90 }
91
92 if (isset($this->element['show_root']))
93 {
94 array_unshift($options, JHtml::_('select.option', '0', JText::_('JGLOBAL_ROOT')));
95 }
96 }
97 else
98 {
99 JLog::add(JText::_('JLIB_FORM_ERROR_FIELDS_CATEGORY_ERROR_EXTENSION_EMPTY'), JLog::WARNING, 'jerror');
100 }
101
102 // Merge any additional options in the XML definition.
103 $options = array_merge(parent::getOptions(), $options);
104
105 return $options;
106 }
107 }
108