forked from foowie/dependentselectbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonDependentSelectBox.php
More file actions
72 lines (57 loc) · 2.28 KB
/
JsonDependentSelectBox.php
File metadata and controls
72 lines (57 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* @author Daniel Robenek
* @license MIT
*/
namespace DependentSelectBox;
use Nette\Application\Responses\JsonResponse;
use Nette\Forms\Container as FormContainer;
use Nette\Application\UI\Presenter;
use Nette\InvalidStateException;
// \Nette\Forms\FormContainer::extensionMethod("addJsonDependentSelectBox", "DependentSelectBox\JsonDependentSelectBox::formAddJsonDependentSelectBox");
class JsonDependentSelectBox extends DependentSelectBox
{
public static $jsonResoponseItems = array();
public function submitButtonHandler($button) {
parent::submitButtonHandler($button);
if ($this->lookup("\Nette\Application\UI\Presenter")->isAjax())
$this->addJsonResponseItem($this);
}
protected function addJsonResponseItem($selectBox) {
self::$jsonResoponseItems[] = $selectBox;
if($selectBox instanceof DependentSelectBox)
foreach($selectBox->childs as $child)
$child->addJsonResponseItem($child);
}
public static function tryJsonResponse(Presenter $presenter) {
if(empty(self::$jsonResoponseItems))
return;
$payload = array(
"type" => "JsonDependentSelectBoxResponse",
"items" => array()
);
foreach(self::$jsonResoponseItems as $item) {
$payload["items"][$item->getHtmlId()] = array(
"selected" => $item->getValue(),
"items" => $item->getItems()
);
}
$response = new JsonResponse($payload);
$presenter->sendResponse($response);
}
/**
* @deprecated Alias for Container_prototype_addDependentSelectBox
*/
public static function formAddJsonDependentSelectBox($_this, $name, $label, $parents, $dataCallback) {
return self::Container_prototype_addJsonDependentSelectBox($_this, $name, $label, $parents, $dataCallback);
}
public static function Container_prototype_addJsonDependentSelectBox(FormContainer $obj, $name, $label, $parents, $dataCallback) {
return $obj[$name] = new JsonDependentSelectBox($label, $parents, $dataCallback);
}
public static function register($methodName = "addJsonDependentSelectBox") {
if(NETTE_PACKAGE == 'PHP 5.2')
FormContainer::extensionMethod("FormContainer::$methodName", array("JsonDependentSelectBox", "Container_prototype_addJsonDependentSelectBox"));
else
FormContainer::extensionMethod($methodName, "DependentSelectBox\JsonDependentSelectBox::Container_prototype_addJsonDependentSelectBox");
}
}