1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace picon;
24:
25: 26: 27: 28: 29: 30:
31: class AjaxRequestTarget implements RequestTarget
32: {
33: private $components = array();
34: private $script = array();
35:
36: 37: 38: 39: 40: 41:
42: public function add(Component &$component)
43: {
44: if($component instanceof WebPage)
45: {
46: throw new \InvalidArgumentException('A page cannot be added the ajax request target');
47: }
48: elseif($component instanceof AbstractRepeater)
49: {
50: throw new \InvalidArgumentException('A repeater cannot be directly added to an ajax request target. Add a parent component instead');
51: }
52: array_push($this->components, $component);
53: }
54:
55: 56: 57: 58:
59: public function executeScript($script)
60: {
61: array_push($this->script, $script);
62: }
63:
64: public function respond(Response $response)
65: {
66: $ajaxResponse = array();
67: $ajaxResponse['components'] = array();
68: $ajaxResponse['header'] = array();
69: $ajaxResponse['script'] = $this->script;
70:
71: $headerResponse = new HeaderResponse($response);
72:
73: foreach($this->components as $component)
74: {
75: $response->clean();
76: $component->beforePageRender();
77: $component->render();
78: $value = $response->getBody();
79: $response->clean();
80: array_push($ajaxResponse['components'], array('id' => $component->getMarkupId(), 'value' => $value));
81:
82: $this->renderComponentHeader($component, $response, $headerResponse);
83: $value = $response->getBody();
84: array_push($ajaxResponse['header'], $value);
85: $response->clean();
86: }
87: FeedbackModel::get()->cleanup();
88: header('Content-Type: application/json');
89: print(json_encode($ajaxResponse));
90: }
91:
92: private function renderComponentHeader(Component $component, Response $response, $headerResponse)
93: {
94: $header = new HeaderContainer(HeaderResolver::HEADER_ID);
95:
96: $page = $component->getPage();
97: $page->addOrReplace($header);
98:
99: PiconApplication::get()->getComponentRenderHeadListener()->onHeadRendering($component, $headerResponse);
100: $page->renderHead($headerResponse);
101:
102: $component->renderHeadContainer($header, $headerResponse);
103: $callback = function(Component &$component) use($headerResponse, $header)
104: {
105: $component->renderHeadContainer($header, $headerResponse);
106: return Component::VISITOR_CONTINUE_TRAVERSAL;
107: };
108: if($component instanceof MarkupContainer)
109: {
110: $component->visitChildren(Component::getIdentifier(), $callback);
111: }
112: }
113: }
114:
115: ?>
116: