1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage form
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8 // Protect from unauthorized access
9 defined('FOF_INCLUDED') or die;
10
11 JFormHelper::loadFieldClass('groupedlist');
12
13 /**
14 * Form Field class for FOF
15 * Supports a generic list of options.
16 *
17 * @package FrameworkOnFramework
18 * @since 2.0
19 */
20 class FOFFormFieldGroupedlist extends JFormFieldGroupedList implements FOFFormField
21 {
22 protected $static;
23
24 protected $repeatable;
25
26 /** @var FOFTable The item being rendered in a repeatable form field */
27 public $item;
28
29 /** @var int A monotonically increasing number, denoting the row number in a repeatable view */
30 public $rowid;
31
32 /**
33 * Method to get certain otherwise inaccessible properties from the form field object.
34 *
35 * @param string $name The property name for which to the the value.
36 *
37 * @return mixed The property value or null.
38 *
39 * @since 2.0
40 */
41 public function __get($name)
42 {
43 switch ($name)
44 {
45 case 'static':
46 if (empty($this->static))
47 {
48 $this->static = $this->getStatic();
49 }
50
51 return $this->static;
52 break;
53
54 case 'repeatable':
55 if (empty($this->repeatable))
56 {
57 $this->repeatable = $this->getRepeatable();
58 }
59
60 return $this->repeatable;
61 break;
62
63 default:
64 return parent::__get($name);
65 }
66 }
67
68 /**
69 * Get the rendering of this field type for static display, e.g. in a single
70 * item view (typically a "read" task).
71 *
72 * @since 2.0
73 *
74 * @return string The field HTML
75 */
76 public function getStatic()
77 {
78 $class = $this->element['class'] ? (string) $this->element['class'] : '';
79
80 $selected = self::getOptionName($this->getGroups(), $this->value);
81
82 if (is_null($selected))
83 {
84 $selected = array(
85 'group' => '',
86 'item' => ''
87 );
88 }
89
90 return '<span id="' . $this->id . '-group" class="fof-groupedlist-group ' . $class . '>' .
91 htmlspecialchars($selected['group'], ENT_COMPAT, 'UTF-8') .
92 '</span>' .
93 '<span id="' . $this->id . '-item" class="fof-groupedlist-item ' . $class . '>' .
94 htmlspecialchars($selected['item'], ENT_COMPAT, 'UTF-8') .
95 '</span>';
96 }
97
98 /**
99 * Get the rendering of this field type for a repeatable (grid) display,
100 * e.g. in a view listing many item (typically a "browse" task)
101 *
102 * @since 2.0
103 *
104 * @return string The field HTML
105 */
106 public function getRepeatable()
107 {
108 $class = $this->element['class'] ? (string) $this->element['class'] : '';
109
110 $selected = self::getOptionName($this->getGroups(), $this->value);
111
112 if (is_null($selected))
113 {
114 $selected = array(
115 'group' => '',
116 'item' => ''
117 );
118 }
119
120 return '<span class="' . $this->id . '-group fof-groupedlist-group ' . $class . '">' .
121 htmlspecialchars($selected['group'], ENT_COMPAT, 'UTF-8') .
122 '</span>' .
123 '<span class="' . $this->id . '-item fof-groupedlist-item ' . $class . '">' .
124 htmlspecialchars($selected['item'], ENT_COMPAT, 'UTF-8') .
125 '</span>';
126 }
127
128 /**
129 * Gets the active option's label given an array of JHtml options
130 *
131 * @param array $data The JHtml options to parse
132 * @param mixed $selected The currently selected value
133 * @param string $groupKey Group name
134 * @param string $optKey Key name
135 * @param string $optText Value name
136 *
137 * @return mixed The label of the currently selected option
138 */
139 public static function getOptionName($data, $selected = null, $groupKey = 'items', $optKey = 'value', $optText = 'text')
140 {
141 $ret = null;
142
143 foreach ($data as $dataKey => $group)
144 {
145 $label = $dataKey;
146 $noGroup = is_int($dataKey);
147
148 if (is_array($group))
149 {
150 $subList = $group[$groupKey];
151 $label = $group[$optText];
152 $noGroup = false;
153 }
154 elseif (is_object($group))
155 {
156 // Sub-list is in a property of an object
157 $subList = $group->$groupKey;
158 $label = $group->$optText;
159 $noGroup = false;
160 }
161 else
162 {
163 throw new RuntimeException('Invalid group contents.', 1);
164 }
165
166 if ($noGroup)
167 {
168 $label = '';
169 }
170
171 $match = FOFFormFieldList::getOptionName($data, $selected, $optKey, $optText);
172
173 if (!is_null($match))
174 {
175 $ret = array(
176 'group' => $label,
177 'item' => $match
178 );
179 break;
180 }
181 }
182
183 return $ret;
184 }
185 }
186