1 <?php
2 3 4 5 6 7 8
9
10 11 12 13
14 abstract class TbInput extends CInputWidget
15 {
16
17 const TYPE_CHECKBOX = 'checkbox';
18 const TYPE_CHECKBOXLIST = 'checkboxlist';
19 const TYPE_CHECKBOXLIST_INLINE = 'checkboxlist_inline';
20 const TYPE_DROPDOWN = 'dropdownlist';
21 const TYPE_FILE = 'filefield';
22 const TYPE_PASSWORD = 'password';
23 const TYPE_RADIO = 'radiobutton';
24 const TYPE_RADIOLIST = 'radiobuttonlist';
25 const TYPE_RADIOLIST_INLINE = 'radiobuttonlist_inline';
26 const TYPE_TEXTAREA = 'textarea';
27 const TYPE_TEXT = 'textfield';
28 const TYPE_CAPTCHA = 'captcha';
29 const TYPE_UNEDITABLE = 'uneditable';
30
31 32 33
34 public $form;
35 36 37
38 public $label;
39 40 41 42 43
44 public $type;
45 46 47
48 public $data = array();
49 50 51
52 public $prependText;
53 54 55
56 public $appendText;
57 58 59
60 public $hintText;
61 62 63
64 public $labelOptions = array();
65 66 67
68 public $prependOptions = array();
69 70 71
72 public $appendOptions = array();
73 74 75
76 public $hintOptions = array();
77 78 79
80 public $errorOptions = array();
81 82 83
84 public $captchaOptions = array();
85
86 87 88 89
90 public function init()
91 {
92 if (!isset($this->form))
93 throw new CException(__CLASS__.': Failed to initialize widget! Form is not set.');
94
95 if (!isset($this->model))
96 throw new CException(__CLASS__.': Failed to initialize widget! Model is not set.');
97
98 if (!isset($this->type))
99 throw new CException(__CLASS__.': Failed to initialize widget! Input type is not set.');
100
101 if ($this->type === self::TYPE_UNEDITABLE)
102 {
103 if (isset($this->htmlOptions['class']))
104 $this->htmlOptions['class'] .= ' uneditable-input';
105 else
106 $this->htmlOptions['class'] = 'uneditable-input';
107 }
108
109 $this->processHtmlOptions();
110 }
111
112 113 114
115 protected function processHtmlOptions()
116 {
117 if (isset($this->htmlOptions['prepend']))
118 {
119 $this->prependText = $this->htmlOptions['prepend'];
120 unset($this->htmlOptions['prepend']);
121 }
122
123 if (isset($this->htmlOptions['append']))
124 {
125 $this->appendText = $this->htmlOptions['append'];
126 unset($this->htmlOptions['append']);
127 }
128
129 if (isset($this->htmlOptions['hint']))
130 {
131 $this->hintText = $this->htmlOptions['hint'];
132 unset($this->htmlOptions['hint']);
133 }
134
135 if (isset($this->htmlOptions['labelOptions']))
136 {
137 $this->labelOptions = $this->htmlOptions['labelOptions'];
138 unset($this->htmlOptions['labelOptions']);
139 }
140
141 if (isset($this->htmlOptions['prependOptions']))
142 {
143 $this->prependOptions = $this->htmlOptions['prependOptions'];
144 unset($this->htmlOptions['prependOptions']);
145 }
146
147 if (isset($this->htmlOptions['appendOptions']))
148 {
149 $this->appendOptions = $this->htmlOptions['appendOptions'];
150 unset($this->htmlOptions['appendOptions']);
151 }
152
153 if (isset($this->htmlOptions['hintOptions']))
154 {
155 $this->hintOptions = $this->htmlOptions['hintOptions'];
156 unset($this->htmlOptions['hintOptions']);
157
158 }
159
160 if (isset($this->htmlOptions['errorOptions']))
161 {
162 $this->errorOptions = $this->htmlOptions['errorOptions'];
163 unset($this->htmlOptions['errorOptions']);
164 }
165
166 if (isset($this->htmlOptions['captchaOptions']))
167 {
168 $this->captchaOptions = $this->htmlOptions['captchaOptions'];
169 unset($this->htmlOptions['captchaOptions']);
170 }
171 }
172
173 174 175 176
177 public function run()
178 {
179 switch ($this->type)
180 {
181 case self::TYPE_CHECKBOX:
182 $this->checkBox();
183 break;
184
185 case self::TYPE_CHECKBOXLIST:
186 $this->checkBoxList();
187 break;
188
189 case self::TYPE_CHECKBOXLIST_INLINE:
190 $this->checkBoxListInline();
191 break;
192
193 case self::TYPE_DROPDOWN:
194 $this->dropDownList();
195 break;
196
197 case self::TYPE_FILE:
198 $this->fileField();
199 break;
200
201 case self::TYPE_PASSWORD:
202 $this->passwordField();
203 break;
204
205 case self::TYPE_RADIO:
206 $this->radioButton();
207 break;
208
209 case self::TYPE_RADIOLIST:
210 $this->radioButtonList();
211 break;
212
213 case self::TYPE_RADIOLIST_INLINE:
214 $this->radioButtonListInline();
215 break;
216
217 case self::TYPE_TEXTAREA:
218 $this->textArea();
219 break;
220
221 case self::TYPE_TEXT:
222 $this->textField();
223 break;
224
225 case self::TYPE_CAPTCHA:
226 $this->captcha();
227 break;
228
229 case self::TYPE_UNEDITABLE:
230 $this->uneditableField();
231 break;
232
233 default:
234 throw new CException(__CLASS__.': Failed to run widget! Type is invalid.');
235 }
236 }
237
238 239 240 241
242 protected function getLabel()
243 {
244 if ($this->label !== false && !in_array($this->type, array('checkbox', 'radio')) && $this->hasModel())
245 return $this->form->labelEx($this->model, $this->attribute, $this->labelOptions);
246 else if ($this->label !== null)
247 return $this->label;
248 else
249 return '';
250 }
251
252 253 254 255
256 protected function getPrepend()
257 {
258 if ($this->hasAddOn())
259 {
260 $htmlOptions = $this->prependOptions;
261
262 if (isset($htmlOptions['class']))
263 $htmlOptions['class'] .= ' add-on';
264 else
265 $htmlOptions['class'] = 'add-on';
266
267 ob_start();
268 echo '<div class="'.$this->getAddonCssClass().'">';
269 if (isset($this->prependText))
270 echo CHtml::tag('span', $htmlOptions, $this->prependText);
271
272 return ob_get_clean();
273 }
274 else
275 return '';
276 }
277
278 279 280 281
282 protected function getAppend()
283 {
284 if ($this->hasAddOn())
285 {
286 $htmlOptions = $this->appendOptions;
287
288 if (isset($htmlOptions['class']))
289 $htmlOptions['class'] .= ' add-on';
290 else
291 $htmlOptions['class'] = 'add-on';
292
293 ob_start();
294 if (isset($this->appendText))
295 echo CHtml::tag('span', $htmlOptions, $this->appendText);
296
297 echo '</div>';
298 return ob_get_clean();
299 }
300 else
301 return '';
302 }
303
304 305 306 307 308
309 protected function getAttributeId($attribute)
310 {
311 return isset($this->htmlOptions['id'])
312 ? $this->htmlOptions['id']
313 : CHtml::getIdByName(CHtml::resolveName($this->model, $attribute));
314 }
315
316 317 318 319
320 protected function getError()
321 {
322 return $this->form->error($this->model, $this->attribute, $this->errorOptions);
323 }
324
325 326 327 328
329 protected function getHint()
330 {
331 if (isset($this->hintText))
332 {
333 $htmlOptions = $this->hintOptions;
334
335 if (isset($htmlOptions['class']))
336 $htmlOptions['class'] .= ' help-block';
337 else
338 $htmlOptions['class'] = 'help-block';
339
340 return CHtml::tag('p', $htmlOptions, $this->hintText);
341 }
342 else
343 return '';
344 }
345
346 347 348 349
350 protected function getContainerCssClass()
351 {
352 $attribute = $this->attribute;
353 return $this->model->hasErrors(CHtml::resolveName($this->model, $attribute)) ? CHtml::$errorCss : '';
354 }
355
356 357 358 359
360 protected function getAddonCssClass()
361 {
362 $classes = array();
363 if (isset($this->prependText))
364 $classes[] = 'input-prepend';
365 if (isset($this->appendText))
366 $classes[] = 'input-append';
367
368 return implode(' ', $classes);
369 }
370
371 372 373 374
375 protected function hasAddOn()
376 {
377 return isset($this->prependText) || isset($this->appendText);
378 }
379
380 381 382 383 384
385 abstract protected function checkBox();
386
387 388 389 390 391
392 abstract protected function checkBoxList();
393
394 395 396 397 398
399 abstract protected function checkBoxListInline();
400
401 402 403 404 405
406 abstract protected function dropDownList();
407
408 409 410 411 412
413 abstract protected function fileField();
414
415 416 417 418 419
420 abstract protected function passwordField();
421
422 423 424 425 426
427 abstract protected function radioButton();
428
429 430 431 432 433
434 abstract protected function radioButtonList();
435
436 437 438 439 440
441 abstract protected function radioButtonListInline();
442
443 444 445 446 447
448 abstract protected function textArea();
449
450 451 452 453 454
455 abstract protected function textField();
456
457 458 459 460 461
462 abstract protected function captcha();
463
464 465 466 467 468
469 abstract protected function uneditableField();
470 }
471