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