| 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: | |
| 14: | |
| 15: | class CheckBox extends HtmlFormDataElement implements ChangeListener, Requestable |
| 16: | { |
| 17: | private $event; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | public function __construct($id, $value = false) { |
| 25: | parent::__construct($id); |
| 26: | $this->setValue($value); |
| 27: | } |
| 28: | |
| 29: | |
| 30: | |
| 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: | |
| 42: | if ($this->event != null) { |
| 43: | $formid = $this->getFormOwner()->id; |
| 44: | |
| 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: | |
| 60: | |
| 61: | public function getRequestData() { |
| 62: | $this->setValue(isset($_REQUEST[$this->id])); |
| 63: | |
| 64: | } |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | public function RequestHandle() { |
| 70: | $this->OnEvent(); |
| 71: | } |
| 72: | |
| 73: | |
| 74: | |
| 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: | |
| 84: | |
| 85: | public function OnEvent() { |
| 86: | if ($this->event != null) { |
| 87: | $this->event->onEvent($this); |
| 88: | } |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 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: | |
| 103: | |
| 104: | public function isChecked() { |
| 105: | return $this->getValue() === true; |
| 106: | } |
| 107: | |
| 108: | public function clean() { |
| 109: | $this->setChecked(false); |
| 110: | } |
| 111: | } |
| 112: | |