1 <?php
2 /**
3 * @package Joomla.Libraries
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('groupedlist');
13
14 // Import the com_menus helper.
15 require_once realpath(JPATH_ADMINISTRATOR . '/components/com_menus/helpers/menus.php');
16
17 /**
18 * Supports an HTML grouped select list of menu item grouped by menu
19 *
20 * @since 1.6
21 */
22 class JFormFieldMenuitem extends JFormFieldGroupedList
23 {
24 /**
25 * The form field type.
26 *
27 * @var string
28 * @since 1.6
29 */
30 public $type = 'MenuItem';
31
32 /**
33 * The menu type.
34 *
35 * @var string
36 * @since 3.2
37 */
38 protected $menuType;
39
40 /**
41 * The client id.
42 *
43 * @var string
44 * @since 3.2
45 */
46 protected $clientId;
47
48 /**
49 * The language.
50 *
51 * @var array
52 * @since 3.2
53 */
54 protected $language;
55
56 /**
57 * The published status.
58 *
59 * @var array
60 * @since 3.2
61 */
62 protected $published;
63
64 /**
65 * The disabled status.
66 *
67 * @var array
68 * @since 3.2
69 */
70 protected $disable;
71
72 /**
73 * Method to get certain otherwise inaccessible properties from the form field object.
74 *
75 * @param string $name The property name for which to the the value.
76 *
77 * @return mixed The property value or null.
78 *
79 * @since 3.2
80 */
81 public function __get($name)
82 {
83 switch ($name)
84 {
85 case 'menuType':
86 case 'clientId':
87 case 'language':
88 case 'published':
89 case 'disable':
90 return $this->$name;
91 }
92
93 return parent::__get($name);
94 }
95
96 /**
97 * Method to set certain otherwise inaccessible properties of the form field object.
98 *
99 * @param string $name The property name for which to the the value.
100 * @param mixed $value The value of the property.
101 *
102 * @return void
103 *
104 * @since 3.2
105 */
106 public function __set($name, $value)
107 {
108 switch ($name)
109 {
110 case 'menuType':
111 $this->menuType = (string) $value;
112 break;
113
114 case 'clientId':
115 $this->clientId = (int) $value;
116 break;
117
118 case 'language':
119 case 'published':
120 case 'disable':
121 $value = (string) $value;
122 $this->$name = $value ? explode(',', $value) : array();
123 break;
124
125 default:
126 parent::__set($name, $value);
127 }
128 }
129
130 /**
131 * Method to attach a JForm object to the field.
132 *
133 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
134 * @param mixed $value The form field value to validate.
135 * @param string $group The field name group control value. This acts as an array container for the field.
136 * For example if the field has name="foo" and the group value is set to "bar" then the
137 * full field name would end up being "bar[foo]".
138 *
139 * @return boolean True on success.
140 *
141 * @see JFormField::setup()
142 * @since 3.2
143 */
144 public function setup(SimpleXMLElement $element, $value, $group = null)
145 {
146 $result = parent::setup($element, $value, $group);
147
148 if ($result === true)
149 {
150 $this->menuType = (string) $this->element['menu_type'];
151 $this->clientId = (int) $this->element['client_id'];
152 $this->published = $this->element['published'] ? explode(',', (string) $this->element['published']) : array();
153 $this->disable = $this->element['disable'] ? explode(',', (string) $this->element['disable']) : array();
154 $this->language = $this->element['language'] ? explode(',', (string) $this->element['language']) : array();
155 }
156
157 return $result;
158 }
159
160 /**
161 * Method to get the field option groups.
162 *
163 * @return array The field option objects as a nested array in groups.
164 *
165 * @since 1.6
166 */
167 protected function getGroups()
168 {
169 $groups = array();
170
171 $menuType = $this->menuType;
172
173 // Get the menu items.
174 $items = MenusHelper::getMenuLinks($menuType, 0, 0, $this->published, $this->language, $this->clientId);
175
176 // Build group for a specific menu type.
177 if ($menuType)
178 {
179 // If the menutype is empty, group the items by menutype.
180 $db = JFactory::getDbo();
181 $query = $db->getQuery(true)
182 ->select($db->quoteName('title'))
183 ->from($db->quoteName('#__menu_types'))
184 ->where($db->quoteName('menutype') . ' = ' . $db->quote($menuType));
185 $db->setQuery($query);
186
187 try
188 {
189 $menuTitle = $db->loadResult();
190 }
191 catch (RuntimeException $e)
192 {
193 $menuTitle = $menuType;
194 }
195
196 // Initialize the group.
197 $groups[$menuTitle] = array();
198
199 // Build the options array.
200 foreach ($items as $link)
201 {
202 $levelPrefix = str_repeat('- ', max(0, $link->level - 1));
203
204 // Displays language code if not set to All
205 if ($link->language !== '*')
206 {
207 $lang = ' (' . $link->language . ')';
208 }
209 else
210 {
211 $lang = '';
212 }
213
214 $groups[$menuTitle][] = JHtml::_('select.option',
215 $link->value, $levelPrefix . $link->text . $lang,
216 'value',
217 'text',
218 in_array($link->type, $this->disable)
219 );
220 }
221 }
222 // Build groups for all menu types.
223 else
224 {
225 // Build the groups arrays.
226 foreach ($items as $menu)
227 {
228 // Initialize the group.
229 $groups[$menu->title] = array();
230
231 // Build the options array.
232 foreach ($menu->links as $link)
233 {
234 $levelPrefix = str_repeat('- ', $link->level - 1);
235
236 // Displays language code if not set to All
237 if ($link->language !== '*')
238 {
239 $lang = ' (' . $link->language . ')';
240 }
241 else
242 {
243 $lang = '';
244 }
245
246 $groups[$menu->title][] = JHtml::_('select.option',
247 $link->value, $levelPrefix . $link->text . $lang,
248 'value',
249 'text',
250 in_array($link->type, $this->disable)
251 );
252 }
253 }
254 }
255
256 // Merge any additional groups in the XML definition.
257 $groups = array_merge(parent::getGroups(), $groups);
258
259 return $groups;
260 }
261 }
262