1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Forms\Controls;
9:
10: use Nette;
11: use Nette\Forms\IControl;
12: use Nette\Utils\Html;
13: use Nette\Forms\Form;
14:
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
37: abstract class BaseControl extends Nette\ComponentModel\Component implements IControl
38: {
39:
40: public static $idMask = 'frm-%s';
41:
42:
43: public $caption;
44:
45:
46: protected $value;
47:
48:
49: protected $control;
50:
51:
52: protected $label;
53:
54:
55: private $errors = array();
56:
57:
58: protected $disabled = FALSE;
59:
60:
61: private $omitted = FALSE;
62:
63:
64: private $rules;
65:
66:
67: private $translator = TRUE;
68:
69:
70: private $options = array();
71:
72:
73: 74: 75:
76: public function __construct($caption = NULL)
77: {
78: $this->monitor('Nette\Forms\Form');
79: parent::__construct();
80: $this->control = Html::el('input', array('type' => NULL, 'name' => NULL));
81: $this->label = Html::el('label');
82: $this->caption = $caption;
83: $this->rules = new Nette\Forms\Rules($this);
84: $this->setValue(NULL);
85: }
86:
87:
88: 89: 90: 91: 92:
93: protected function attached($form)
94: {
95: if (!$this->isDisabled() && $form instanceof Form && $form->isAnchored() && $form->isSubmitted()) {
96: $this->loadHttpData();
97: }
98: }
99:
100:
101: 102: 103: 104: 105:
106: public function getForm($need = TRUE)
107: {
108: return $this->lookup('Nette\Forms\Form', $need);
109: }
110:
111:
112: 113: 114: 115:
116: public function loadHttpData()
117: {
118: $this->setValue($this->getHttpData(Form::DATA_TEXT));
119: }
120:
121:
122: 123: 124: 125:
126: public function getHttpData($type, $htmlTail = NULL)
127: {
128: return $this->getForm()->getHttpData($type, $this->getHtmlName() . $htmlTail);
129: }
130:
131:
132: 133: 134: 135:
136: public function getHtmlName()
137: {
138: return Nette\Forms\Helpers::generateHtmlName($this->lookupPath('Nette\Forms\Form'));
139: }
140:
141:
142:
143:
144:
145: 146: 147: 148:
149: public function setValue($value)
150: {
151: $this->value = $value;
152: return $this;
153: }
154:
155:
156: 157: 158: 159:
160: public function getValue()
161: {
162: return $this->value;
163: }
164:
165:
166: 167: 168: 169:
170: public function isFilled()
171: {
172: $value = $this->getValue();
173: return $value !== NULL && $value !== array() && $value !== '';
174: }
175:
176:
177: 178: 179: 180:
181: public function setDefaultValue($value)
182: {
183: $form = $this->getForm(FALSE);
184: if ($this->isDisabled() || !$form || !$form->isAnchored() || !$form->isSubmitted()) {
185: $this->setValue($value);
186: }
187: return $this;
188: }
189:
190:
191: 192: 193: 194: 195:
196: public function setDisabled($value = TRUE)
197: {
198: if ($this->disabled = (bool) $value) {
199: $this->omitted = TRUE;
200: $this->setValue(NULL);
201: }
202: return $this;
203: }
204:
205:
206: 207: 208: 209:
210: public function isDisabled()
211: {
212: return $this->disabled === TRUE;
213: }
214:
215:
216: 217: 218: 219: 220:
221: public function setOmitted($value = TRUE)
222: {
223: $this->omitted = (bool) $value;
224: return $this;
225: }
226:
227:
228: 229: 230: 231:
232: public function isOmitted()
233: {
234: return $this->omitted;
235: }
236:
237:
238:
239:
240:
241: 242: 243: 244:
245: public function getControl()
246: {
247: $this->setOption('rendered', TRUE);
248: $el = clone $this->control;
249: return $el->addAttributes(array(
250: 'name' => $this->getHtmlName(),
251: 'id' => $this->getHtmlId(),
252: 'required' => $this->isRequired(),
253: 'disabled' => $this->isDisabled(),
254: 'data-nette-rules' => Nette\Forms\Helpers::exportRules($this->rules) ?: NULL,
255: ));
256: }
257:
258:
259: 260: 261: 262: 263:
264: public function getLabel($caption = NULL)
265: {
266: $label = clone $this->label;
267: $label->for = $this->getHtmlId();
268: $label->setText($this->translate($caption === NULL ? $this->caption : $caption));
269: return $label;
270: }
271:
272:
273: 274: 275: 276:
277: public function getControlPrototype()
278: {
279: return $this->control;
280: }
281:
282:
283: 284: 285: 286:
287: public function getLabelPrototype()
288: {
289: return $this->label;
290: }
291:
292:
293: 294: 295: 296: 297:
298: public function setHtmlId($id)
299: {
300: $this->control->id = $id;
301: return $this;
302: }
303:
304:
305: 306: 307: 308:
309: public function getHtmlId()
310: {
311: if (!isset($this->control->id)) {
312: $this->control->id = sprintf(self::$idMask, $this->lookupPath());
313: }
314: return $this->control->id;
315: }
316:
317:
318: 319: 320: 321: 322: 323:
324: public function setAttribute($name, $value = TRUE)
325: {
326: $this->control->$name = $value;
327: return $this;
328: }
329:
330:
331:
332:
333:
334: 335: 336: 337:
338: public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
339: {
340: $this->translator = $translator;
341: return $this;
342: }
343:
344:
345: 346: 347: 348:
349: public function getTranslator()
350: {
351: if ($this->translator === TRUE) {
352: return $this->getForm(FALSE) ? $this->getForm()->getTranslator() : NULL;
353: }
354: return $this->translator;
355: }
356:
357:
358: 359: 360: 361: 362: 363:
364: public function translate($value, $count = NULL)
365: {
366: if ($translator = $this->getTranslator()) {
367: $tmp = is_array($value) ? array(& $value) : array(array(& $value));
368: foreach ($tmp[0] as & $v) {
369: if ($v != NULL && !$v instanceof Html) {
370: $v = $translator->translate($v, $count);
371: }
372: }
373: }
374: return $value;
375: }
376:
377:
378:
379:
380:
381: 382: 383: 384: 385: 386: 387:
388: public function addRule($validator, $message = NULL, $arg = NULL)
389: {
390: $this->rules->addRule($validator, $message, $arg);
391: return $this;
392: }
393:
394:
395: 396: 397: 398: 399: 400:
401: public function addCondition($validator, $value = NULL)
402: {
403: return $this->rules->addCondition($validator, $value);
404: }
405:
406:
407: 408: 409: 410: 411: 412: 413:
414: public function addConditionOn(IControl $control, $validator, $value = NULL)
415: {
416: return $this->rules->addConditionOn($control, $validator, $value);
417: }
418:
419:
420: 421: 422:
423: public function getRules()
424: {
425: return $this->rules;
426: }
427:
428:
429: 430: 431: 432: 433:
434: public function setRequired($value = TRUE)
435: {
436: $this->rules->setRequired($value);
437: return $this;
438: }
439:
440:
441: 442: 443: 444:
445: public function isRequired()
446: {
447: return $this->rules->isRequired();
448: }
449:
450:
451: 452: 453: 454:
455: public function validate()
456: {
457: if ($this->isDisabled()) {
458: return;
459: }
460: $this->cleanErrors();
461: $this->rules->validate();
462: }
463:
464:
465: 466: 467: 468: 469:
470: public function addError($message)
471: {
472: $this->errors[] = $message;
473: }
474:
475:
476: 477: 478: 479:
480: public function getError()
481: {
482: return $this->errors ? implode(' ', array_unique($this->errors)) : NULL;
483: }
484:
485:
486: 487: 488: 489:
490: public function getErrors()
491: {
492: return array_unique($this->errors);
493: }
494:
495:
496: 497: 498:
499: public function hasErrors()
500: {
501: return (bool) $this->errors;
502: }
503:
504:
505: 506: 507:
508: public function cleanErrors()
509: {
510: $this->errors = array();
511: }
512:
513:
514:
515: protected static function exportRules($rules)
516: {
517: trigger_error(__METHOD__ . '() is deprecated; use Nette\Forms\Helpers::exportRules() instead.', E_USER_DEPRECATED);
518: return Nette\Forms\Helpers::exportRules($rules);
519: }
520:
521:
522:
523:
524:
525: 526: 527: 528:
529: public function setOption($key, $value)
530: {
531: if ($value === NULL) {
532: unset($this->options[$key]);
533: } else {
534: $this->options[$key] = $value;
535: }
536: return $this;
537: }
538:
539:
540: 541: 542: 543:
544: public function getOption($key, $default = NULL)
545: {
546: return isset($this->options[$key]) ? $this->options[$key] : $default;
547: }
548:
549:
550: 551: 552: 553:
554: public function getOptions()
555: {
556: return $this->options;
557: }
558:
559: }
560: