@@ -12,297 +12,297 @@ |
||
| 12 | 12 | class Select extends Field |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The select's placeholder |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - private $placeholder = null; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * The Select's options |
|
| 24 | - * |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - protected $options; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The select's element |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $element = 'select'; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The select's self-closing state |
|
| 38 | - * |
|
| 39 | - * @var boolean |
|
| 40 | - */ |
|
| 41 | - protected $isSelfClosing = false; |
|
| 42 | - |
|
| 43 | - //////////////////////////////////////////////////////////////////// |
|
| 44 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 45 | - //////////////////////////////////////////////////////////////////// |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Easier arguments order for selects |
|
| 49 | - * |
|
| 50 | - * @param Container $app The Container instance |
|
| 51 | - * @param string $type select |
|
| 52 | - * @param string $name Field name |
|
| 53 | - * @param string $label Its label |
|
| 54 | - * @param array $options The select's options |
|
| 55 | - * @param string $selected The selected option |
|
| 56 | - * @param array $attributes Attributes |
|
| 57 | - */ |
|
| 58 | - public function __construct(Container $app, $type, $name, $label, $options, $selected, $attributes) |
|
| 59 | - { |
|
| 60 | - if ($selected) { |
|
| 61 | - $this->value = $selected; |
|
| 62 | - } |
|
| 63 | - if ($options) { |
|
| 64 | - $this->options($options); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - parent::__construct($app, $type, $name, $label, $selected, $attributes); |
|
| 68 | - |
|
| 69 | - // Nested models population |
|
| 70 | - if (str_contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) { |
|
| 71 | - $this->fromQuery($this->value); |
|
| 72 | - $this->value = $selected ?: null; |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Renders the select |
|
| 78 | - * |
|
| 79 | - * @return string A <select> tag |
|
| 80 | - */ |
|
| 81 | - public function render() |
|
| 82 | - { |
|
| 83 | - // Multiselects |
|
| 84 | - if ($this->isOfType('multiselect')) { |
|
| 85 | - if (!isset($this->attributes['id'])) { |
|
| 86 | - $this->setAttribute('id', $this->name); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $this->multiple(); |
|
| 90 | - $this->name .= '[]'; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - if ( ! $this->value instanceOf \ArrayAccess) { |
|
| 94 | - $this->value = (array) $this->value; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // Mark selected values as selected |
|
| 98 | - if ($this->hasChildren() and !empty($this->value)) { |
|
| 99 | - foreach ($this->value as $value) { |
|
| 100 | - if (is_object($value) && method_exists($value, 'getKey')) { |
|
| 101 | - $value = $value->getKey(); |
|
| 102 | - } |
|
| 103 | - $this->selectValue($value); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // Add placeholder text if any |
|
| 108 | - if ($placeholder = $this->getPlaceholder()) { |
|
| 109 | - array_unshift($this->children, $placeholder); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - $this->value = null; |
|
| 113 | - |
|
| 114 | - return parent::render(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Select a value in the field's children |
|
| 119 | - * |
|
| 120 | - * @param mixed $value |
|
| 121 | - * @param Element $parent |
|
| 122 | - * |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - protected function selectValue($value, $parent = null) |
|
| 126 | - { |
|
| 127 | - // If no parent element defined, use direct children |
|
| 128 | - if (!$parent) { |
|
| 129 | - $parent = $this; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - foreach ($parent->getChildren() as $child) { |
|
| 133 | - // Search by value |
|
| 134 | - |
|
| 135 | - if ($child->getAttribute('value') === $value || is_numeric($value) && $child->getAttribute('value') === (int)$value ) { |
|
| 136 | - $child->selected('selected'); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Else iterate over subchilds |
|
| 140 | - if ($child->hasChildren()) { |
|
| 141 | - $this->selectValue($value, $child); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get the Select's placeholder |
|
| 148 | - * |
|
| 149 | - * @return Element |
|
| 150 | - */ |
|
| 151 | - protected function getPlaceholder() |
|
| 152 | - { |
|
| 153 | - if (!$this->placeholder) { |
|
| 154 | - return false; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $attributes = array('value' => '', 'disabled' => 'disabled'); |
|
| 158 | - if (!$this->value) { |
|
| 159 | - $attributes['selected'] = 'selected'; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return Element::create('option', $this->placeholder, $attributes); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - //////////////////////////////////////////////////////////////////// |
|
| 166 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 167 | - //////////////////////////////////////////////////////////////////// |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Set the select options |
|
| 171 | - * |
|
| 172 | - * @param array $_options The options as an array |
|
| 173 | - * @param mixed $selected Facultative selected entry |
|
| 174 | - * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 175 | - * the option's values instead of the array's keys |
|
| 176 | - */ |
|
| 177 | - public function options($_options, $selected = null, $valuesAsKeys = false) |
|
| 178 | - { |
|
| 179 | - $options = array(); |
|
| 180 | - |
|
| 181 | - // If valuesAsKeys is true, use the values as keys |
|
| 182 | - if ($valuesAsKeys) { |
|
| 183 | - foreach ($_options as $v) { |
|
| 184 | - $options[$v] = $v; |
|
| 185 | - } |
|
| 186 | - } else { |
|
| 187 | - $options = $_options; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - // Add the various options |
|
| 191 | - foreach ($options as $value => $text) { |
|
| 192 | - if (is_array($text) and isset($text['value'])) { |
|
| 193 | - $attributes = $text; |
|
| 194 | - $text = $value; |
|
| 195 | - $value = null; |
|
| 196 | - } else { |
|
| 197 | - $attributes = array(); |
|
| 198 | - } |
|
| 199 | - $this->addOption($text, $value, $attributes); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // Set the selected value |
|
| 203 | - if (!is_null($selected)) { |
|
| 204 | - $this->select($selected); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $this; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Creates a list of options from a range |
|
| 212 | - * |
|
| 213 | - * @param integer $from |
|
| 214 | - * @param integer $to |
|
| 215 | - * @param integer $step |
|
| 216 | - */ |
|
| 217 | - public function range($from, $to, $step = 1) |
|
| 218 | - { |
|
| 219 | - $range = range($from, $to, $step); |
|
| 220 | - $this->options($range, null, true); |
|
| 221 | - |
|
| 222 | - return $this; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Add an option to the Select's options |
|
| 227 | - * |
|
| 228 | - * @param array|string $text It's value or an array of values |
|
| 229 | - * @param string $value It's text |
|
| 230 | - * @param array $attributes The option's attributes |
|
| 231 | - */ |
|
| 232 | - public function addOption($text = null, $value = null, $attributes = array()) |
|
| 233 | - { |
|
| 234 | - // Get the option's value |
|
| 235 | - $childrenKey = !is_null($value) ? $value : sizeof($this->children); |
|
| 236 | - |
|
| 237 | - // If we passed an options group |
|
| 238 | - if (is_array($text)) { |
|
| 239 | - $this->children[$childrenKey] = Element::create('optgroup')->label($value); |
|
| 240 | - foreach ($text as $key => $value) { |
|
| 241 | - $option = Element::create('option', $value)->setAttribute('value', $key); |
|
| 242 | - $this->children[$childrenKey]->nest($option); |
|
| 243 | - } |
|
| 244 | - // Else if it's a simple option |
|
| 245 | - } else { |
|
| 246 | - if (!isset($attributes['value'])) { |
|
| 247 | - $attributes['value'] = $value; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - $this->children[$attributes['value']] = Element::create('option', $text)->setAttributes($attributes); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - return $this; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Use the results from a Fluent/Eloquent query as options |
|
| 258 | - * |
|
| 259 | - * @param array $results An array of Eloquent models |
|
| 260 | - * @param string|function $text The value to use as text |
|
| 261 | - * @param string|array $attributes The data to use as attributes |
|
| 262 | - * @param string $selected The default selected item |
|
| 263 | - */ |
|
| 264 | - public function fromQuery($results, $text = null, $attributes = null, $selected = null) |
|
| 265 | - { |
|
| 266 | - $this->options(Helpers::queryToArray($results, $text, $attributes), $selected); |
|
| 267 | - |
|
| 268 | - return $this; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Select a particular list item |
|
| 273 | - * |
|
| 274 | - * @param mixed $selected Selected item |
|
| 275 | - */ |
|
| 276 | - public function select($selected) |
|
| 277 | - { |
|
| 278 | - $this->value = $selected; |
|
| 279 | - |
|
| 280 | - return $this; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Add a placeholder to the current select |
|
| 285 | - * |
|
| 286 | - * @param string $placeholder The placeholder text |
|
| 287 | - */ |
|
| 288 | - public function placeholder($placeholder) |
|
| 289 | - { |
|
| 290 | - $this->placeholder = Helpers::translate($placeholder); |
|
| 291 | - |
|
| 292 | - return $this; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - //////////////////////////////////////////////////////////////////// |
|
| 296 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 297 | - //////////////////////////////////////////////////////////////////// |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Returns the current options in memory for manipulations |
|
| 301 | - * |
|
| 302 | - * @return array The current options array |
|
| 303 | - */ |
|
| 304 | - public function getOptions() |
|
| 305 | - { |
|
| 306 | - return $this->children; |
|
| 307 | - } |
|
| 15 | + /** |
|
| 16 | + * The select's placeholder |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + private $placeholder = null; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * The Select's options |
|
| 24 | + * |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + protected $options; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The select's element |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $element = 'select'; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The select's self-closing state |
|
| 38 | + * |
|
| 39 | + * @var boolean |
|
| 40 | + */ |
|
| 41 | + protected $isSelfClosing = false; |
|
| 42 | + |
|
| 43 | + //////////////////////////////////////////////////////////////////// |
|
| 44 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 45 | + //////////////////////////////////////////////////////////////////// |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Easier arguments order for selects |
|
| 49 | + * |
|
| 50 | + * @param Container $app The Container instance |
|
| 51 | + * @param string $type select |
|
| 52 | + * @param string $name Field name |
|
| 53 | + * @param string $label Its label |
|
| 54 | + * @param array $options The select's options |
|
| 55 | + * @param string $selected The selected option |
|
| 56 | + * @param array $attributes Attributes |
|
| 57 | + */ |
|
| 58 | + public function __construct(Container $app, $type, $name, $label, $options, $selected, $attributes) |
|
| 59 | + { |
|
| 60 | + if ($selected) { |
|
| 61 | + $this->value = $selected; |
|
| 62 | + } |
|
| 63 | + if ($options) { |
|
| 64 | + $this->options($options); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + parent::__construct($app, $type, $name, $label, $selected, $attributes); |
|
| 68 | + |
|
| 69 | + // Nested models population |
|
| 70 | + if (str_contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) { |
|
| 71 | + $this->fromQuery($this->value); |
|
| 72 | + $this->value = $selected ?: null; |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Renders the select |
|
| 78 | + * |
|
| 79 | + * @return string A <select> tag |
|
| 80 | + */ |
|
| 81 | + public function render() |
|
| 82 | + { |
|
| 83 | + // Multiselects |
|
| 84 | + if ($this->isOfType('multiselect')) { |
|
| 85 | + if (!isset($this->attributes['id'])) { |
|
| 86 | + $this->setAttribute('id', $this->name); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $this->multiple(); |
|
| 90 | + $this->name .= '[]'; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + if ( ! $this->value instanceOf \ArrayAccess) { |
|
| 94 | + $this->value = (array) $this->value; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // Mark selected values as selected |
|
| 98 | + if ($this->hasChildren() and !empty($this->value)) { |
|
| 99 | + foreach ($this->value as $value) { |
|
| 100 | + if (is_object($value) && method_exists($value, 'getKey')) { |
|
| 101 | + $value = $value->getKey(); |
|
| 102 | + } |
|
| 103 | + $this->selectValue($value); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // Add placeholder text if any |
|
| 108 | + if ($placeholder = $this->getPlaceholder()) { |
|
| 109 | + array_unshift($this->children, $placeholder); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + $this->value = null; |
|
| 113 | + |
|
| 114 | + return parent::render(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Select a value in the field's children |
|
| 119 | + * |
|
| 120 | + * @param mixed $value |
|
| 121 | + * @param Element $parent |
|
| 122 | + * |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + protected function selectValue($value, $parent = null) |
|
| 126 | + { |
|
| 127 | + // If no parent element defined, use direct children |
|
| 128 | + if (!$parent) { |
|
| 129 | + $parent = $this; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + foreach ($parent->getChildren() as $child) { |
|
| 133 | + // Search by value |
|
| 134 | + |
|
| 135 | + if ($child->getAttribute('value') === $value || is_numeric($value) && $child->getAttribute('value') === (int)$value ) { |
|
| 136 | + $child->selected('selected'); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Else iterate over subchilds |
|
| 140 | + if ($child->hasChildren()) { |
|
| 141 | + $this->selectValue($value, $child); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get the Select's placeholder |
|
| 148 | + * |
|
| 149 | + * @return Element |
|
| 150 | + */ |
|
| 151 | + protected function getPlaceholder() |
|
| 152 | + { |
|
| 153 | + if (!$this->placeholder) { |
|
| 154 | + return false; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $attributes = array('value' => '', 'disabled' => 'disabled'); |
|
| 158 | + if (!$this->value) { |
|
| 159 | + $attributes['selected'] = 'selected'; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return Element::create('option', $this->placeholder, $attributes); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + //////////////////////////////////////////////////////////////////// |
|
| 166 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 167 | + //////////////////////////////////////////////////////////////////// |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Set the select options |
|
| 171 | + * |
|
| 172 | + * @param array $_options The options as an array |
|
| 173 | + * @param mixed $selected Facultative selected entry |
|
| 174 | + * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 175 | + * the option's values instead of the array's keys |
|
| 176 | + */ |
|
| 177 | + public function options($_options, $selected = null, $valuesAsKeys = false) |
|
| 178 | + { |
|
| 179 | + $options = array(); |
|
| 180 | + |
|
| 181 | + // If valuesAsKeys is true, use the values as keys |
|
| 182 | + if ($valuesAsKeys) { |
|
| 183 | + foreach ($_options as $v) { |
|
| 184 | + $options[$v] = $v; |
|
| 185 | + } |
|
| 186 | + } else { |
|
| 187 | + $options = $_options; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + // Add the various options |
|
| 191 | + foreach ($options as $value => $text) { |
|
| 192 | + if (is_array($text) and isset($text['value'])) { |
|
| 193 | + $attributes = $text; |
|
| 194 | + $text = $value; |
|
| 195 | + $value = null; |
|
| 196 | + } else { |
|
| 197 | + $attributes = array(); |
|
| 198 | + } |
|
| 199 | + $this->addOption($text, $value, $attributes); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // Set the selected value |
|
| 203 | + if (!is_null($selected)) { |
|
| 204 | + $this->select($selected); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $this; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Creates a list of options from a range |
|
| 212 | + * |
|
| 213 | + * @param integer $from |
|
| 214 | + * @param integer $to |
|
| 215 | + * @param integer $step |
|
| 216 | + */ |
|
| 217 | + public function range($from, $to, $step = 1) |
|
| 218 | + { |
|
| 219 | + $range = range($from, $to, $step); |
|
| 220 | + $this->options($range, null, true); |
|
| 221 | + |
|
| 222 | + return $this; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Add an option to the Select's options |
|
| 227 | + * |
|
| 228 | + * @param array|string $text It's value or an array of values |
|
| 229 | + * @param string $value It's text |
|
| 230 | + * @param array $attributes The option's attributes |
|
| 231 | + */ |
|
| 232 | + public function addOption($text = null, $value = null, $attributes = array()) |
|
| 233 | + { |
|
| 234 | + // Get the option's value |
|
| 235 | + $childrenKey = !is_null($value) ? $value : sizeof($this->children); |
|
| 236 | + |
|
| 237 | + // If we passed an options group |
|
| 238 | + if (is_array($text)) { |
|
| 239 | + $this->children[$childrenKey] = Element::create('optgroup')->label($value); |
|
| 240 | + foreach ($text as $key => $value) { |
|
| 241 | + $option = Element::create('option', $value)->setAttribute('value', $key); |
|
| 242 | + $this->children[$childrenKey]->nest($option); |
|
| 243 | + } |
|
| 244 | + // Else if it's a simple option |
|
| 245 | + } else { |
|
| 246 | + if (!isset($attributes['value'])) { |
|
| 247 | + $attributes['value'] = $value; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + $this->children[$attributes['value']] = Element::create('option', $text)->setAttributes($attributes); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + return $this; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Use the results from a Fluent/Eloquent query as options |
|
| 258 | + * |
|
| 259 | + * @param array $results An array of Eloquent models |
|
| 260 | + * @param string|function $text The value to use as text |
|
| 261 | + * @param string|array $attributes The data to use as attributes |
|
| 262 | + * @param string $selected The default selected item |
|
| 263 | + */ |
|
| 264 | + public function fromQuery($results, $text = null, $attributes = null, $selected = null) |
|
| 265 | + { |
|
| 266 | + $this->options(Helpers::queryToArray($results, $text, $attributes), $selected); |
|
| 267 | + |
|
| 268 | + return $this; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Select a particular list item |
|
| 273 | + * |
|
| 274 | + * @param mixed $selected Selected item |
|
| 275 | + */ |
|
| 276 | + public function select($selected) |
|
| 277 | + { |
|
| 278 | + $this->value = $selected; |
|
| 279 | + |
|
| 280 | + return $this; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Add a placeholder to the current select |
|
| 285 | + * |
|
| 286 | + * @param string $placeholder The placeholder text |
|
| 287 | + */ |
|
| 288 | + public function placeholder($placeholder) |
|
| 289 | + { |
|
| 290 | + $this->placeholder = Helpers::translate($placeholder); |
|
| 291 | + |
|
| 292 | + return $this; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + //////////////////////////////////////////////////////////////////// |
|
| 296 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 297 | + //////////////////////////////////////////////////////////////////// |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Returns the current options in memory for manipulations |
|
| 301 | + * |
|
| 302 | + * @return array The current options array |
|
| 303 | + */ |
|
| 304 | + public function getOptions() |
|
| 305 | + { |
|
| 306 | + return $this->children; |
|
| 307 | + } |
|
| 308 | 308 | } |