|
1 | | -<?php |
2 | | - |
3 | | -/** |
4 | | - * This file is part of the NasExt extensions of Nette Framework |
5 | | - * |
6 | | - * Copyright (c) 2013 Dusan Hudak (http://dusan-hudak.com) |
7 | | - * |
8 | | - * For the full copyright and license information, please view |
9 | | - * the file license.md that was distributed with this source code. |
10 | | - */ |
11 | | - |
12 | | -namespace NasExt\Forms\Controls; |
13 | | - |
14 | | -use NasExt; |
15 | | -use Nette; |
16 | | - |
17 | | - |
18 | | -/** |
19 | | - * @author Jáchym Toušek |
20 | | - * @author Dusan Hudak |
21 | | - * @author Ales Wita |
22 | | - * @license MIT |
23 | | - */ |
24 | | -class DependentMultiSelectBox extends Nette\Forms\Controls\MultiSelectBox implements Nette\Application\UI\ISignalReceiver |
25 | | -{ |
26 | | - use NasExt\Forms\DependentTrait; |
27 | | - |
28 | | - /** @var string */ |
29 | | - const SIGNAL_NAME = DependentSelectBox::SIGNAL_NAME; |
30 | | - |
31 | | - |
32 | | - /** |
33 | | - * @param string |
34 | | - * @param array[Nette\Forms\IControl] |
35 | | - */ |
36 | | - public function __construct($label, array $parents) |
37 | | - { |
38 | | - $this->parents = $parents; |
39 | | - parent::__construct($label); |
40 | | - } |
41 | | - |
42 | | - |
43 | | - /** |
44 | | - * @throws |
45 | | - * @param bool |
46 | | - * @return self |
47 | | - */ |
48 | | - public function setDisabled($value = true) |
49 | | - { |
50 | | - if (is_array($value)) { |
51 | | - throw new Nette\InvalidArgumentException('NasExt\\Forms\\Controls\\DependentMultiSelectBox not supported disabled items!'); |
52 | | - } |
53 | | - return parent::setDisabled($value); |
54 | | - } |
55 | | - |
56 | | - |
57 | | - /** |
58 | | - * @param string |
59 | | - * @return void |
60 | | - */ |
61 | | - public function signalReceived($signal) |
62 | | - { |
63 | | - $presenter = $this->lookup('Nette\\Application\\UI\\Presenter'); |
64 | | - |
65 | | - if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) { |
66 | | - $parentsNames = []; |
67 | | - foreach ($this->parents as $parent) { |
68 | | - $value = $presenter->getParameter($this->getNormalizeName($parent)); |
69 | | - $parent->setValue($value); |
70 | | - |
71 | | - $parentsNames[$parent->getName()] = $parent->getValue(); |
72 | | - } |
73 | | - |
74 | | - $data = $this->getDependentData([$parentsNames]); |
75 | | - $presenter->payload->dependentselectbox = [ |
76 | | - 'id' => $this->getHtmlId(), |
77 | | - 'items' => $data->getPreparedItems(!is_array($this->disabled) ?: $this->disabled), |
78 | | - 'value' => $data->getValue(), |
79 | | - 'prompt' => false, |
80 | | - 'disabledWhenEmpty' => $this->disabledWhenEmpty, |
81 | | - ]; |
82 | | - $presenter->sendPayload(); |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - |
87 | | - /** |
88 | | - * @return void |
89 | | - */ |
90 | | - private function tryLoadItems() |
91 | | - { |
92 | | - if ($this->parents === array_filter($this->parents, function ($p) {return !$p->hasErrors();})) { |
93 | | - $parentsValues = []; |
94 | | - foreach ($this->parents as $parent) { |
95 | | - $parentsValues[$parent->getName()] = $parent->getValue(); |
96 | | - } |
97 | | - |
98 | | - $data = $this->getDependentData([$parentsValues]); |
99 | | - $items = $data->getItems(); |
100 | | - |
101 | | - |
102 | | - if ($this->getForm()->isSubmitted()) { |
103 | | - $this->setValue($this->value); |
104 | | - |
105 | | - } elseif ($this->tempValue !== null) { |
106 | | - $this->setValue($this->tempValue); |
107 | | - |
108 | | - } else { |
109 | | - $this->setValue($data->getValue()); |
110 | | - } |
111 | | - |
112 | | - |
113 | | - $this->loadHttpData(); |
114 | | - $this->setItems($items); |
115 | | - |
116 | | - if (count($items) === 0) { |
117 | | - if ($this->disabledWhenEmpty === true && !$this->isDisabled()) { |
118 | | - $this->setDisabled(); |
119 | | - } |
120 | | - } |
121 | | - } |
122 | | - } |
123 | | -} |
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the NasExt extensions of Nette Framework |
| 5 | + * |
| 6 | + * Copyright (c) 2013 Dusan Hudak (http://dusan-hudak.com) |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the file license.md that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace NasExt\Forms\Controls; |
| 13 | + |
| 14 | +use NasExt; |
| 15 | +use Nette; |
| 16 | + |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Jáchym Toušek |
| 20 | + * @author Dusan Hudak |
| 21 | + * @author Ales Wita |
| 22 | + * @license MIT |
| 23 | + */ |
| 24 | +class DependentMultiSelectBox extends Nette\Forms\Controls\MultiSelectBox implements Nette\Application\UI\ISignalReceiver |
| 25 | +{ |
| 26 | + use NasExt\Forms\DependentTrait; |
| 27 | + |
| 28 | + /** @var string */ |
| 29 | + const SIGNAL_NAME = DependentSelectBox::SIGNAL_NAME; |
| 30 | + |
| 31 | + |
| 32 | + /** |
| 33 | + * @param string $label |
| 34 | + * @param array<int, Nette\Forms\IControl> $parents |
| 35 | + */ |
| 36 | + public function __construct($label, array $parents) |
| 37 | + { |
| 38 | + $this->parents = $parents; |
| 39 | + parent::__construct($label); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * @throws $value |
| 45 | + * @param bool |
| 46 | + * @return self |
| 47 | + */ |
| 48 | + public function setDisabled($value = true) |
| 49 | + { |
| 50 | + if (is_array($value)) { |
| 51 | + throw new Nette\InvalidArgumentException('NasExt\\Forms\\Controls\\DependentMultiSelectBox not supported disabled items!'); |
| 52 | + } |
| 53 | + |
| 54 | + return parent::setDisabled($value); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * @param string $signal |
| 60 | + * @return void |
| 61 | + */ |
| 62 | + public function signalReceived($signal) |
| 63 | + { |
| 64 | + $presenter = $this->lookup('Nette\\Application\\UI\\Presenter'); |
| 65 | + |
| 66 | + if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) { |
| 67 | + $parentsNames = []; |
| 68 | + foreach ($this->parents as $parent) {bdump($presenter->getParameters()); |
| 69 | + $value = $presenter->getParameter($this->getNormalizeName($parent)); |
| 70 | + $parent->setValue($value); |
| 71 | + |
| 72 | + $parentsNames[$parent->getName()] = $parent->getValue(); |
| 73 | + } |
| 74 | + |
| 75 | + $data = $this->getDependentData([$parentsNames]); |
| 76 | + $presenter->payload->dependentselectbox = [ |
| 77 | + 'id' => $this->getHtmlId(), |
| 78 | + 'items' => $data->getPreparedItems(!is_array($this->disabled) ?: $this->disabled), |
| 79 | + 'value' => $data->getValue(), |
| 80 | + 'prompt' => false, |
| 81 | + 'disabledWhenEmpty' => $this->disabledWhenEmpty, |
| 82 | + ]; |
| 83 | + |
| 84 | + $presenter->sendPayload(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + /** |
| 90 | + * @return void |
| 91 | + */ |
| 92 | + private function tryLoadItems() |
| 93 | + { |
| 94 | + if ($this->parents === array_filter($this->parents, function ($p) {return !$p->hasErrors();})) { |
| 95 | + $parentsValues = []; |
| 96 | + foreach ($this->parents as $parent) { |
| 97 | + $parentsValues[$parent->getName()] = $parent->getValue(); |
| 98 | + } |
| 99 | + |
| 100 | + $data = $this->getDependentData([$parentsValues]); |
| 101 | + $items = $data->getItems(); |
| 102 | + |
| 103 | + if ($this->getForm()->isSubmitted()) { |
| 104 | + $this->setValue($this->value); |
| 105 | + |
| 106 | + } elseif ($this->tempValue !== null) { |
| 107 | + $this->setValue($this->tempValue); |
| 108 | + |
| 109 | + } else { |
| 110 | + $this->setValue($data->getValue()); |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + $this->loadHttpData(); |
| 115 | + $this->setItems($items); |
| 116 | + |
| 117 | + if (count($items) === 0) { |
| 118 | + if ($this->disabledWhenEmpty === true && !$this->isDisabled()) { |
| 119 | + $this->setDisabled(); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments