-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from aleswita/master
Bug fix of passing array parameter to signal link
- Loading branch information
Showing
19 changed files
with
1,609 additions
and
1,560 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,124 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the NasExt extensions of Nette Framework | ||
* | ||
* Copyright (c) 2013 Dusan Hudak (http://dusan-hudak.com) | ||
* | ||
* For the full copyright and license information, please view | ||
* the file license.md that was distributed with this source code. | ||
*/ | ||
|
||
namespace NasExt\Forms\Controls; | ||
|
||
use NasExt; | ||
use Nette; | ||
|
||
|
||
/** | ||
* @author Jáchym Toušek | ||
* @author Dusan Hudak | ||
* @author Ales Wita | ||
* @license MIT | ||
*/ | ||
class DependentMultiSelectBox extends Nette\Forms\Controls\MultiSelectBox implements Nette\Application\UI\ISignalReceiver | ||
{ | ||
use NasExt\Forms\DependentTrait; | ||
|
||
/** @var string */ | ||
const SIGNAL_NAME = DependentSelectBox::SIGNAL_NAME; | ||
|
||
|
||
/** | ||
* @param string | ||
* @param array[Nette\Forms\IControl] | ||
*/ | ||
public function __construct($label, array $parents) | ||
{ | ||
$this->parents = $parents; | ||
parent::__construct($label); | ||
} | ||
|
||
|
||
/** | ||
* @throws | ||
* @param bool | ||
* @return self | ||
*/ | ||
public function setDisabled($value = true) | ||
{ | ||
if (is_array($value)) { | ||
throw new Nette\InvalidArgumentException('NasExt\\Forms\\Controls\\DependentMultiSelectBox not supported disabled items!'); | ||
} | ||
return parent::setDisabled($value); | ||
} | ||
|
||
|
||
/** | ||
* @param string | ||
* @return void | ||
*/ | ||
public function signalReceived($signal) | ||
{ | ||
$presenter = $this->lookup('Nette\\Application\\UI\\Presenter'); | ||
|
||
if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) { | ||
$parentsNames = []; | ||
foreach ($this->parents as $parent) { | ||
$value = $presenter->getParameter($this->getNormalizeName($parent)); | ||
$parent->setValue($value); | ||
|
||
$parentsNames[$parent->getName()] = $parent->getValue(); | ||
} | ||
|
||
$data = $this->getDependentData([$parentsNames]); | ||
$presenter->payload->dependentselectbox = [ | ||
'id' => $this->getHtmlId(), | ||
'items' => $data->getPreparedItems(!is_array($this->disabled) ?: $this->disabled), | ||
'value' => $data->getValue(), | ||
'prompt' => false, | ||
'disabledWhenEmpty' => $this->disabledWhenEmpty, | ||
]; | ||
$presenter->sendPayload(); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @return void | ||
*/ | ||
private function tryLoadItems() | ||
{ | ||
if ($this->parents === array_filter($this->parents, function ($p) {return !$p->hasErrors();})) { | ||
$parentsValues = []; | ||
foreach ($this->parents as $parent) { | ||
$parentsValues[$parent->getName()] = $parent->getValue(); | ||
} | ||
|
||
$data = $this->getDependentData([$parentsValues]); | ||
$items = $data->getItems(); | ||
|
||
|
||
if ($this->getForm()->isSubmitted()) { | ||
$this->setValue($this->value); | ||
|
||
} elseif ($this->tempValue !== null) { | ||
$this->setValue($this->tempValue); | ||
|
||
} else { | ||
$this->setValue($data->getValue()); | ||
} | ||
|
||
|
||
$this->loadHttpData(); | ||
$this->setItems($items); | ||
|
||
if (count($items) === 0) { | ||
if ($this->disabledWhenEmpty === true && !$this->isDisabled()) { | ||
$this->setDisabled(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
<?php | ||
|
||
/** | ||
* This file is part of the NasExt extensions of Nette Framework | ||
* | ||
* Copyright (c) 2013 Dusan Hudak (http://dusan-hudak.com) | ||
* | ||
* For the full copyright and license information, please view | ||
* the file license.md that was distributed with this source code. | ||
*/ | ||
|
||
namespace NasExt\Forms\Controls; | ||
|
||
use NasExt; | ||
use Nette; | ||
|
||
|
||
/** | ||
* @author Jáchym Toušek | ||
* @author Dusan Hudak | ||
* @author Ales Wita | ||
* @license MIT | ||
*/ | ||
class DependentMultiSelectBox extends Nette\Forms\Controls\MultiSelectBox implements Nette\Application\UI\ISignalReceiver | ||
{ | ||
use NasExt\Forms\DependentTrait; | ||
|
||
/** @var string */ | ||
const SIGNAL_NAME = DependentSelectBox::SIGNAL_NAME; | ||
|
||
|
||
/** | ||
* @param string $label | ||
* @param array<int, Nette\Forms\IControl> $parents | ||
*/ | ||
public function __construct($label, array $parents) | ||
{ | ||
$this->parents = $parents; | ||
parent::__construct($label); | ||
} | ||
|
||
|
||
/** | ||
* @throws $value | ||
* @param bool | ||
* @return self | ||
*/ | ||
public function setDisabled($value = true) | ||
{ | ||
if (is_array($value)) { | ||
throw new Nette\InvalidArgumentException('NasExt\\Forms\\Controls\\DependentMultiSelectBox not supported disabled items!'); | ||
} | ||
|
||
return parent::setDisabled($value); | ||
} | ||
|
||
|
||
/** | ||
* @param string $signal | ||
* @return void | ||
*/ | ||
public function signalReceived($signal) | ||
{ | ||
$presenter = $this->lookup('Nette\\Application\\UI\\Presenter'); | ||
|
||
if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) { | ||
$parentsNames = []; | ||
foreach ($this->parents as $parent) {bdump($presenter->getParameters()); | ||
$value = $presenter->getParameter($this->getNormalizeName($parent)); | ||
$parent->setValue($value); | ||
|
||
$parentsNames[$parent->getName()] = $parent->getValue(); | ||
} | ||
|
||
$data = $this->getDependentData([$parentsNames]); | ||
$presenter->payload->dependentselectbox = [ | ||
'id' => $this->getHtmlId(), | ||
'items' => $data->getPreparedItems(!is_array($this->disabled) ?: $this->disabled), | ||
'value' => $data->getValue(), | ||
'prompt' => false, | ||
'disabledWhenEmpty' => $this->disabledWhenEmpty, | ||
]; | ||
|
||
$presenter->sendPayload(); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @return void | ||
*/ | ||
private function tryLoadItems() | ||
{ | ||
if ($this->parents === array_filter($this->parents, function ($p) {return !$p->hasErrors();})) { | ||
$parentsValues = []; | ||
foreach ($this->parents as $parent) { | ||
$parentsValues[$parent->getName()] = $parent->getValue(); | ||
} | ||
|
||
$data = $this->getDependentData([$parentsValues]); | ||
$items = $data->getItems(); | ||
|
||
if ($this->getForm()->isSubmitted()) { | ||
$this->setValue($this->value); | ||
|
||
} elseif ($this->tempValue !== null) { | ||
$this->setValue($this->tempValue); | ||
|
||
} else { | ||
$this->setValue($data->getValue()); | ||
} | ||
|
||
|
||
$this->loadHttpData(); | ||
$this->setItems($items); | ||
|
||
if (count($items) === 0) { | ||
if ($this->disabledWhenEmpty === true && !$this->isDisabled()) { | ||
$this->setDisabled(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.