forked from foowie/dependentselectbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormControlDependencyHelper.php
217 lines (184 loc) · 6.19 KB
/
FormControlDependencyHelper.php
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* @author Daniel Robenek
* @license MIT
*/
namespace DependentSelectBox;
use Nette\Forms\Container;
use Nette\Forms\Controls\BaseControl as FormControl;
use Nette\Forms\Controls\SubmitButton;
use Nette\Object;
use \InvalidArgumentException;
use Nette\InvalidStateException;
class FormControlDependencyHelper extends Object {
// <editor-fold defaultstate="collapsed" desc="variables">
/** Dont use directly, button allready created and position was defined in other class */
const POSITION_UNDEFINED = 0;
/** Put button on current position */
const POSITION_DEFAULT = 1;
/** Put button before given control */
const POSITION_BEFORE_CONTROL = 2;
/** Put button after given control */
const POSITION_AFTER_CONTROL = 3;
/** @var string Suffix for button name and html class */
public static $buttonSuffix = "_submit";
/** @var POSITION_UNDEFINED|POSITION_DEFAULT|POSITION_BEFORE_CONTROL|POSITION_AFTER_CONTROL Default position for buttons */
public static $defaultButtonPosition = self::POSITION_AFTER_CONTROL;
/** @var FormControl */
public $control;
/** @var String Html class of control*/
protected $controlClass;
/** @var int Button-s position */
protected $buttonPosition;
/** @var SubmitButton Created SubmitButton */
protected $button = null;
/** @var String SubmitButton-s label */
protected $buttonText = "Reload";
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="constructor">
/**
*
* @param FormControl $control Component to attach button
* @param string $controlClass Html class for that component
*/
function __construct(FormControl $control, $controlClass = "dependentControl") {
$this->control = $control;
$this->controlClass = $controlClass;
$this->buttonPosition = self::$defaultButtonPosition;
if($this->control->lookup($this->getContainerClass(), false) === null)
throw new InvalidArgumentException("Components should be assigned to FormContainer !");
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="getters & setters & add & remove">
/**
* Is there any button with same name ?
* @return boolean
*/
public function isAnyButtonAttached() {
$form = $this->control->lookup($this->getContainerClass());
$buttonName = $this->formatButtonName($this->control->getName());
$button = $form->getComponent($buttonName, false);
return $button !== null;
}
/**
* Return button with same name
* @param boolean $need Throw exception when not exist?
* @return SubmitButton
*/
public function getAnyAttachedButton($need = false) {
$form = $this->control->lookup($this->getContainerClass());
$buttonName = $this->formatButtonName($this->control->getName());
$button = $form->getComponent($buttonName, $need);
return $button;
}
/**
* Add callback which is called when linked button is submitted
* @param callback $callback
* 'public function methodName(SubmittButton $button)'
*/
public function addOnChangeCallback($callback) {
$this->createButton();
if(!is_callable($callback))
throw new InvalidArgumentException("Not callable !");
$this->button->onClick[] = $callback;
}
public function getControl() {
return $this->control;
}
public function getButton($need = false) {
if($need == true)
$this->createButton();
return $this->button;
}
public function getButtonText() {
return $this->buttonText;
}
public function getButtonPosition() {
return $this->buttonPosition;
}
public function setButtonText($buttonText) {
$this->buttonText = $buttonText;
return $this;
}
public function setButtonPosition($buttonPosition) {
$this->buttonPosition = $buttonPosition;
return $this;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="tools & helpers">
/**
* Create button if no button is attached to control
* @return Was button created or not?
*/
public function createButton() {
if($this->button !== null)
return false;
if($this->isAnyButtonAttached()) {
$this->button = $this->getAnyAttachedButton(true);
$this->buttonText = $this->button->getLabel();
$this->buttonPosition = self::POSITION_UNDEFINED;
return false;
}
// add $this->controlClass to $this->control if not added yet
$this->updateControlClass();
// create button
if($this->buttonText === null)
throw new InvalidArgumentException("Null caption of button is crappy *** !!! (ajax not working properly)");
$container = $this->control->lookup($this->getContainerClass());
$this->button = new SubmitButton($this->buttonText);
$this->button->setValidationScope(false);
$this->button->getControlPrototype()->class($this->controlClass.self::$buttonSuffix);
$buttonName = $this->formatButtonName($this->control->getName());
switch($this->buttonPosition) {
case(self::POSITION_DEFAULT):
$container->addComponent($this->button, $buttonName);
break;
case(self::POSITION_BEFORE_CONTROL):
$container->addComponent($this->button, $buttonName, $this->control->getName());
break;
case(self::POSITION_AFTER_CONTROL): { // Uhm :(
$findName = $this->control->getName();
$nextName = null;
$found = false;
$components = $container->getComponents(true);
foreach($components as $name => $component) {
if($found === true) {
$nextName = $name;
break;
}
if($name == $findName)
$found = true;
}
if($nextName === null)
$container->addComponent($this->button, $buttonName);
else
$container->addComponent($this->button, $buttonName, $nextName);
} break;
default:
throw new InvalidArgumentException("Ivalid position value !");
}
return true;
}
/**
* Update html class of control
*/
protected function updateControlClass() {
if($this->controlClass !== null) {
$classes = explode(" ", $this->control->getControlPrototype()->class);
if(!in_array($this->controlClass, $classes))
$this->control->getControlPrototype()->class($this->controlClass);
}
}
/**
* Format name of button
* @param string $componentName Name of component
* @return string
*/
protected function formatButtonName($componentName) {
return $componentName.self::$buttonSuffix;
}
public function getContainerClass() {
return NETTE_PACKAGE == "PHP 5.2" ? "FormContainer" : "Nette\Forms\Container";
}
// </editor-fold>
}