1: <?php
2:
3: namespace Zippy\Html;
4:
5: use Zippy\Interfaces\EventReceiver;
6: use Zippy\WebApplication;
7:
8: /**
9: * Компонент реализующий блок страницы с собственным HTML шаблоном
10: * для отображении на странице как обычного компонента.
11: * Позволяет отображать на разных страницах типовые элементы
12: * с тем же содержанием. Напрмер меню, формы логина и т.д.
13: * Как правило используется блочный тэг типа DIV
14: *
15: */
16: abstract class PageFragment extends HtmlContainer implements EventReceiver
17: {
18: /**
19: * @see HtmlComponent
20: */
21: public function Render() {
22: // $template =
23: $htmltag = $this->getTag();
24:
25: $template = WebApplication::getApplication()->getTemplate(get_class($this));
26: $dom = (new \DOMWrap\Document())->html($template);
27:
28: $body=$dom->find('body')->first()->html();
29: $htmltag->html($body);
30: // $htmltag->follow($body);
31: // $htmltag->destroy();
32:
33:
34: $this->beforeRender();
35:
36: parent::RenderImpl();
37: $this->afterRender();
38: }
39:
40: /**
41: * Вызывается страницей владельцем при обработке HTTP запроса
42: *
43: */
44: public function RequestHandle() {
45: $this->beforeRequest();
46: parent::RequestHandle();
47: $this->afterRequest();
48: }
49:
50: /**
51: * Вызывается перед обработкой HTTP запроса
52: */
53: public function beforeRequest() {
54:
55: }
56:
57: /**
58: * Вызывается после обработки HTTP запроса
59: */
60: public function afterRequest() {
61:
62: }
63:
64: /**
65: * Возвращает страницу-владельца
66: * @return WebPage
67: */
68: protected function getOwnerPage() {
69: $owner = $this->getOwner();
70: do {
71: if ($owner instanceof \Zippy\Html\WebPage) {
72: return $owner;
73: }
74: $owner = $owner->getOwner();
75: } while ($owner != null);
76:
77: return null;
78: }
79:
80: /**
81: * Вызывается перед обработкой HTTP запроса страницы
82: */
83: public function beforeRequestPage() {
84:
85: }
86:
87: /**
88: * вызывается после обработки HTTP запроса страницы
89: *
90: */
91: public function afterRequestPage() {
92:
93: }
94:
95: protected function onAdded() {
96: $page = $this->getOwnerPage();
97: if ($page instanceof \Zippy\Html\WebPage) {
98: $page->addBeforeRequestEvent($this, 'beforeRequestPage');
99: $page->addAfterRequestEvent($this, 'afterRequestPage');
100: }
101: }
102: final public function RequestMethod($method) {
103: $p = WebApplication::$app->getRequest()->request_params[$method];
104: $post=null;
105: if($_SERVER["REQUEST_METHOD"]=='POST') {
106:
107: if(count($_POST)>0) {
108: $post = $_POST;
109: } else {
110: $post = file_get_contents('php://input');
111: }
112: }
113: $answer = $this->{$method}($p, $post);
114: if(strlen($answer)) {
115: WebApplication::$app->getResponse()->addAjaxResponse($answer) ;
116: }
117:
118: }
119: }
120: