PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / forms / src / Forms / Controls / SelectBox.php

SelectBox.php in Packeta 2.1, at deps/nette/forms/src/Forms/Controls/SelectBox.php

102 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file is part of the Nette Framework (https://nette.org)
5 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6 */
7 declare (strict_types=1);
8 namespace Packetery\Nette\Forms\Controls;
9
10 use Packetery\Nette;
11 /**
12 * Select box control that allows single item selection.
13 */
14 class SelectBox extends ChoiceControl
15 {
16 /** validation rule */
17 public const VALID = ':selectBoxValid';
18 /** @var array of option / optgroup */
19 private $options = [];
20 /** @var string|object|false */
21 private $prompt = \false;
22 /** @var array */
23 private $optionAttributes = [];
24 public function __construct($label = null, array $items = null)
25 {
26 parent::__construct($label, $items);
27 $this->setOption('type', 'select');
28 $this->addCondition(function () {
29 return $this->prompt === \false && $this->options && $this->control->size < 2;
30 })->addRule(\Packetery\Nette\Forms\Form::FILLED, \Packetery\Nette\Forms\Validator::$messages[self::VALID]);
31 }
32 /**
33 * Sets first prompt item in select box.
34 * @param string|object|false $prompt
35 * @return static
36 */
37 public function setPrompt($prompt)
38 {
39 $this->prompt = $prompt;
40 return $this;
41 }
42 /**
43 * Returns first prompt item?
44 * @return string|object|false
45 */
46 public function getPrompt()
47 {
48 return $this->prompt;
49 }
50 /**
51 * Sets options and option groups from which to choose.
52 * @return static
53 */
54 public function setItems(array $items, bool $useKeys = \true)
55 {
56 if (!$useKeys) {
57 $res = [];
58 foreach ($items as $key => $value) {
59 unset($items[$key]);
60 if (\is_array($value)) {
61 foreach ($value as $val) {
62 $res[$key][(string) $val] = $val;
63 }
64 } else {
65 $res[(string) $value] = $value;
66 }
67 }
68 $items = $res;
69 }
70 $this->options = $items;
71 return parent::setItems(\Packetery\Nette\Utils\Arrays::flatten($items, \true));
72 }
73 public function getControl() : \Packetery\Nette\Utils\Html
74 {
75 $items = $this->prompt === \false ? [] : ['' => $this->translate($this->prompt)];
76 foreach ($this->options as $key => $value) {
77 $items[\is_array($value) ? $this->translate($key) : $key] = $this->translate($value);
78 }
79 return \Packetery\Nette\Forms\Helpers::createSelectBox($items, ['disabled:' => \is_array($this->disabled) ? $this->disabled : null] + $this->optionAttributes, $this->value)->addAttributes(parent::getControl()->attrs);
80 }
81 /** @return static */
82 public function addOptionAttributes(array $attributes)
83 {
84 $this->optionAttributes = $attributes + $this->optionAttributes;
85 return $this;
86 }
87 /** @return static */
88 public function setOptionAttribute(string $name, $value = \true)
89 {
90 $this->optionAttributes[$name] = $value;
91 return $this;
92 }
93 public function isOk() : bool
94 {
95 return $this->isDisabled() || $this->prompt !== \false || $this->getValue() !== null || !$this->options || $this->control->size > 1;
96 }
97 public function getOptionAttributes() : array
98 {
99 return $this->optionAttributes;
100 }
101 }
102