1: <?php
2:
3: namespace Zippy\Html\Form;
4:
5: use Zippy\WebApplication;
6: use Zippy\Html\HtmlContainer;
7: use Zippy\Interfaces\SubmitDataRequest;
8: use Zippy\Interfaces\Requestable;
9: use Zippy\Interfaces\EventReceiver;
10: use Zippy\Event;
11:
12: /**
13: * Класс компонента HTML формы
14: */
15: class Form extends HtmlContainer
16: {
17: private $event;
18:
19: /**
20: * Конструктор
21: * @param string ШВ
22: * @param string Тип запроса get или post
23: */
24: public function __construct($id, $method = "post") {
25: parent::__construct($id);
26: $this->setAttribute("method", $method);
27: }
28:
29: /**
30: * @see HtmlComponent
31: */
32: protected function beforeRender() {
33:
34:
35: $url = $this->owner->getURLNode();
36:
37:
38: $this->setAttribute("id", $this->id);
39: $this->setAttribute("name", $this->id);
40:
41: $url = substr($url, strpos($url, 'index.php?q='));
42: $url = str_replace('index.php?q=', '', $url) ;
43:
44: $HtmlTag = $this->getTag();
45: $HtmlTag->appendWith("<input type=\"hidden\" name=\"q\" id=\"{$this->id}_q\" value=\"" . $url . "::" . $this->id . "\" >");
46: $HtmlTag->appendWith("<input type=\"hidden\" name=\"{$this->id}_q\" value=\"\" >");
47: $HtmlTag->appendWith("<input type=\"submit\" name=\"{$this->id}_s\" id=\"{$this->id}_s\" style=\"display:none\" >");
48: }
49:
50: /**
51: * @see Requestable
52: */
53: public function RequestHandle() {
54:
55: if (isset($_REQUEST[$this->id . '_q'])) {
56: $allchild = $this->getChildComponents(true);
57: foreach ($allchild as $component) {
58: if ($component instanceof SubmitDataRequest) {
59: //проверяем что элемент не находится в невидимой панели
60: if ($this->checkInvisibleOwner($component) == false) {
61: if ($component->isVisible()) {
62: $component->getRequestData();
63: }
64: }
65: }
66: }
67: $clist = explode("::", $_REQUEST["q"]);
68: if ($clist[count($clist) - 1] == $this->id) {
69: $this->onEvent();
70: }
71:
72: }
73: parent::RequestHandle();
74: }
75:
76: /**
77: * Устанавливает обработчик события при отправке формы
78: * @param EventReceiver Объект метод которого является обработчиком события
79: * @param string Имя метода - обработчика
80: */
81: public function onSubmit(EventReceiver $receiver, $handler) {
82: $this->event = new Event($receiver, $handler);
83: }
84:
85: /**
86: * Вызывает обработчик события отправки формы
87: */
88: public function onEvent() {
89: if ($this->event instanceof Event) {
90: $this->event->onEvent($this);
91: }
92: }
93:
94: private function checkInvisibleOwner($component) {
95: $owner = $component->getOwner();
96: do {
97: if ($owner->isVisible() == false) {
98: return true;
99: }
100: $owner = $owner->getOwner();
101: } while ($owner != null);
102: return false;
103: }
104:
105: /**
106: * Очистка элементов формы
107: *
108: */
109: public function clean() {
110: $allchild = $this->getChildComponents(true);
111: foreach ($allchild as $component) {
112:
113: if ($component instanceof SubmitDataRequest) {
114: $component->clean();
115: }
116:
117:
118: }
119: }
120:
121: }
122: