1: <?php
2:
3: namespace Zippy\Html\Form;
4:
5: use Zippy\WebApplication;
6: use Zippy\Interfaces\Binding;
7: use Zippy\Interfaces\ChangeListener;
8: use Zippy\Interfaces\Requestable;
9: use Zippy\Interfaces\EventReceiver;
10: use Zippy\Event;
11:
12: /**
13: * Компонент тэга &lt;input type=&quot;checkbox&quot;&gt;
14: */
15: class CheckBox extends HtmlFormDataElement implements ChangeListener, Requestable
16: {
17: private $event;
18:
19: /**
20: * Конструктор
21: * @param mixed ID
22: * @param Значение елеента или поле привязанного объекта
23: */
24: public function __construct($id, $value = false) {
25: parent::__construct($id);
26: $this->setValue($value);
27: }
28:
29: /**
30: * @see HtmlComponent
31: */
32: public function RenderImpl() {
33:
34: if ($this->getValue() == true || $this->getValue() == 1) {
35: $this->setAttribute("checked", "On");
36: } else {
37: $this->setAttribute("checked", null);
38: }
39:
40: $this->setAttribute("name", $this->id);
41: // if set event
42: if ($this->event != null) {
43: $formid = $this->getFormOwner()->id;
44: // $this->attributes["onclick"]="javascript:{ $('#".$formattr["id"]."_hf').val('submit1') ; $('#".$formattr["id"]."').submit();}";
45:
46: $url = $this->owner->getURLNode() . '::' . $this->id;
47: $url = substr($url, 2 + strpos($url, 'q='));
48: if ($this->event->isajax == false) {
49:
50: $this->setAttribute("onchange", "javascript:{ $('#" . $formid . "_q').attr('value','" . $url . "');$('#" . $formid . "_s').trigger('click');}");
51: } else {
52: $_BASEURL = WebApplication::$app->getResponse()->getHostUrl();
53: $this->setAttribute("onchange", " $('#" . $formid . "_q').attr('value','" . $url . "'); submitForm('{$formid}','{$_BASEURL}/?ajax=true');");
54: }
55: }
56: }
57:
58: /**
59: * @see SubmitDataRequest
60: */
61: public function getRequestData() {
62: $this->setValue(isset($_REQUEST[$this->id]));
63:
64: }
65:
66: /**
67: * @see Requestable
68: */
69: public function RequestHandle() {
70: $this->OnEvent();
71: }
72:
73: /**
74: * @see ChangeListener
75: */
76: public function onChange(EventReceiver $receiver, $handler, $ajax = false) {
77:
78: $this->event = new Event($receiver, $handler);
79: $this->event->isajax = $ajax;
80: }
81:
82: /**
83: * @see ChangeListener
84: */
85: public function OnEvent() {
86: if ($this->event != null) {
87: $this->event->onEvent($this);
88: }
89: }
90:
91: /**
92: * Устанавливает checkbox
93: * @param boolean
94: */
95: public function setChecked($checked) {
96: $checked = $checked == 1 ? true : $checked;
97: $checked = $checked === 'true' ? true : $checked;
98: $this->setValue($checked);
99: }
100:
101: /**
102: * Установлен ли checkbox
103: */
104: public function isChecked() {
105: return $this->getValue() === true;
106: }
107:
108: public function clean() {
109: $this->setChecked(false);
110: }
111: }
112: