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