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