groupedlist.php
1 month ago
language.php
1 month ago
list.php
1 month ago
media.php
1 month ago
menuitem.php
1 month ago
modulelayout.php
1 month ago
number.php
1 month ago
radio.php
1 month ago
spacer.php
1 month ago
sql.php
1 month ago
text.php
1 month ago
textarea.php
1 month ago
groupedlist.php
159 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikWP - Libraries |
| 4 | * @subpackage adapter.form |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | jimport('joomla.form.formfield'); |
| 15 | |
| 16 | /** |
| 17 | * Form field class to handle dropdown fields with groups. |
| 18 | * |
| 19 | * @since 10.1.51 |
| 20 | */ |
| 21 | class JFormFieldGroupedlist extends JFormField |
| 22 | { |
| 23 | /** |
| 24 | * The layout identifier for list fields. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | protected $layoutId = 'html.form.fields.groupedlist'; |
| 29 | |
| 30 | /** |
| 31 | * Internally saves the default option groups. |
| 32 | * |
| 33 | * @var array[] |
| 34 | */ |
| 35 | protected $groups = []; |
| 36 | |
| 37 | /** |
| 38 | * Helper method to setup the field. |
| 39 | * |
| 40 | * @param SimpleXMLElement $field The field element. |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | protected function setup($element) |
| 45 | { |
| 46 | parent::setup($element); |
| 47 | |
| 48 | // unset group and option erroneously registered |
| 49 | unset($this->option, $this->group); |
| 50 | |
| 51 | $this->groups = []; |
| 52 | $label = ''; |
| 53 | |
| 54 | foreach ($element->children() as $child) |
| 55 | { |
| 56 | switch ($child->getName()) |
| 57 | { |
| 58 | // the element is an <option /> |
| 59 | case 'option': |
| 60 | // initialize the group if necessary |
| 61 | if (!isset($this->groups[$label])) |
| 62 | { |
| 63 | $this->groups[$label] = []; |
| 64 | } |
| 65 | |
| 66 | $disabled = (string) $child['disabled']; |
| 67 | $disabled = ($disabled === 'true' || $disabled === 'disabled' || $disabled === '1'); |
| 68 | |
| 69 | // create a new option object based on the <option /> element |
| 70 | $this->groups[$label][] = JHtml::fetch( |
| 71 | 'select.option', |
| 72 | $child['value'] ? (string) $child['value'] : trim((string) $child), |
| 73 | JText::translate(trim((string) $child)), |
| 74 | 'value', |
| 75 | 'text', |
| 76 | $disabled |
| 77 | ); |
| 78 | |
| 79 | break; |
| 80 | |
| 81 | // the element is a <group /> |
| 82 | case 'group': |
| 83 | // get the group label |
| 84 | if ($groupLabel = (string) $child['label']) |
| 85 | { |
| 86 | $label = JText::translate($groupLabel); |
| 87 | } |
| 88 | |
| 89 | // initialize the group if necessary |
| 90 | if (!isset($groups[$label])) |
| 91 | { |
| 92 | $groups[$label] = []; |
| 93 | } |
| 94 | |
| 95 | // Iterate through the children and build an array of options. |
| 96 | foreach ($child->children() as $option) |
| 97 | { |
| 98 | // only add <option /> elements |
| 99 | if ($option->getName() !== 'option') |
| 100 | { |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | $disabled = (string) $option['disabled']; |
| 105 | $disabled = ($disabled === 'true' || $disabled === 'disabled' || $disabled === '1'); |
| 106 | |
| 107 | // create a new option object based on the <option /> element |
| 108 | $this->groups[$label][] = JHtml::fetch( |
| 109 | 'select.option', |
| 110 | $option['value'] ? (string) $option['value'] : JText::translate(trim((string) $option)), |
| 111 | JText::translate(trim((string) $option)), |
| 112 | 'value', |
| 113 | 'text', |
| 114 | $disabled |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | reset($this->groups); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Method to get the options to populate list |
| 127 | * |
| 128 | * @return array The field option objects. |
| 129 | */ |
| 130 | public function getGroups() |
| 131 | { |
| 132 | return $this->groups; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @override |
| 137 | * Method to get the data to be passed to the layout for rendering. |
| 138 | * |
| 139 | * @return array An associative array of display data. |
| 140 | */ |
| 141 | public function getLayoutData() |
| 142 | { |
| 143 | $data = array(); |
| 144 | $data['name'] = $this->name; |
| 145 | $data['class'] = $this->class; |
| 146 | $data['id'] = $this->id; |
| 147 | $data['value'] = is_null($this->value) ? $this->default : $this->value; |
| 148 | $data['required'] = $this->required === "true" || $this->required === true ? true : false; |
| 149 | $data['multiple'] = !is_null($this->multiple) && $this->multiple != "false" ? true : false; |
| 150 | $data['disabled'] = $this->disabled === "true" || $this->disabled === true ? true : false; |
| 151 | $data['groups'] = $this->getGroups(); |
| 152 | |
| 153 | // flatten the groups list as the options might be used by other components |
| 154 | $data['options'] = array_merge([], ...array_values($data['groups'])); |
| 155 | |
| 156 | return $data; |
| 157 | } |
| 158 | } |
| 159 |