root/trunk/src/main/php/net/stubbles/websites/memphis/stubMemphisProcessor.php

Revision 1547, 9.5 kB (checked in by mikey, 1 month ago)

refactoring #139, part 6: moved net::stubbles::util::validators to net::stubbles::ipo::request::validators

Line 
1 <?php
2 /**
3  * Processor for memphis pages.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  websites_memphis
8  */
9 stubClassLoader::load('net::stubbles::ipo::request::stubRequestPrefixDecorator',
10                       'net::stubbles::ipo::request::filter::stubRegexFilterDecorator',
11                       'net::stubbles::ipo::request::filter::stubStringFilter',
12                       'net::stubbles::ipo::request::validator::stubPreSelectValidator',
13                       'net::stubbles::lang::stubMode',
14                       'net::stubbles::lang::stubRegistry',
15                       'net::stubbles::websites::cache::stubCachableProcessor',
16                       'net::stubbles::websites::processors::stubAbstractProcessor',
17                       'net::stubbles::websites::processors::stubPageBasedProcessor',
18                       'net::stubbles::websites::memphis::stubMemphisConfig',
19                       'net::stubbles::websites::memphis::stubMemphisPatTemplate'
20 );
21 /**
22  * Processor for memphis pages.
23  *
24  * @package     stubbles
25  * @subpackage  websites_memphis
26  */
27 class stubMemphisProcessor extends stubAbstractProcessor implements stubCachableProcessor, stubPageBasedProcessor
28 {
29     /**
30      * instance of patTemplate
31      *
32      * @var  stubMemphisTemplate
33      */
34     protected $template;
35     /**
36      * configuration
37      *
38      * @var  stubMemphisConfig
39      */
40     protected $config;
41     /**
42      * context
43      *
44      * @var  array<string,mixed>
45      */
46     protected $context = array('part'       => null,
47                                'page'       => null,
48                                'pageName'   => '',
49                                'frameId'    => ''
50                          );
51     /**
52      * decorated request
53      *
54      * @var  stubRequestPrefixDecorator
55      */
56     protected $prefixRequest;
57
58     /**
59      * optional template method to do some constructor work in derived classes
60      */
61     protected function doConstruct()
62     {
63         $this->prefixRequest = new stubRequestPrefixDecorator($this->request, '');
64         $this->config        = $this->createConfig();
65     }
66
67     /**
68      * helper method to create the config object
69      *
70      * @return  stubMemphisConfig
71      */
72     // @codeCoverageIgnoreStart
73     protected function createConfig()
74     {
75         return new stubMemphisConfig();
76     }
77     // @codeCoverageIgnoreEnd
78
79     /**
80      * selects the page to display with help of the page factory
81      *
82      * @param  stubPageFactory  $pageFactory
83      */
84     public function selectPage(stubPageFactory $pageFactory)
85     {
86         $this->context['pageName'] = $pageFactory->getPageName($this->request);
87         $this->context['page']     = $pageFactory->getPage($this->context['pageName']);
88         $this->session->putValue('net.stubbles.websites.lastPage', $this->context['pageName']);
89         $this->context['frameId']  = $this->getFrameId();
90     }
91
92     /**
93      * helper method to get the name of the frame to use
94      *
95      * @return  string
96      */
97     protected function getFrameId()
98     {
99         if ($this->context['page']->hasProperty('frame_fixed') === true && $this->context['page']->getProperty('frame_fixed') === true) {
100             return $this->context['page']->getProperty('frame');
101         }
102
103         if ($this->request->hasValue('frame') === true) {
104             $frame = $this->request->getValidatedValue(new stubPreSelectValidator(array_keys($this->config->getFrames())), 'frame');
105         } elseif ($this->session->hasValue('net.stubbles.websites.memphis.frame') === true) {
106             $frame = $this->session->getValue('net.stubbles.websites.memphis.frame');
107         } else {
108             $frame = $this->context['page']->getProperty('frame');
109         }
110
111         if (null == $frame) {
112             return 'default';
113         }
114
115         return $frame;
116     }
117
118     /**
119      * adds the cache variables for the current request and returns whether
120      * response is cachable or not
121      *
122      * @param   stubWebsiteCache  $cache
123      * @return  bool
124      */
125     public function addCacheVars(stubWebsiteCache $cache)
126     {
127         $cache->addCacheVar('page', $this->context['pageName']);
128         $cache->addCacheVar('frame', $this->context['frameId']);
129         $cache->addCacheVar('variant', $this->session->getValue('net.stubbles.websites.variantmanager.variant.name', ''));
130         foreach ($this->config->getParts() as $part) {
131             $this->context['part'] = $part;
132             foreach ($this->config->getDefaultElements($part) as $defaultElement) {
133                 $this->prefixRequest->setPrefix($defaultElement->getName());
134                 $defaultElement->init($this->prefixRequest, $this->session, $this->response, $this->context);
135                 if ($defaultElement->isAvailable() === true) {
136                     if ($defaultElement->isCachable() === false) {
137                         return false;
138                     }
139
140                     $cache->addCacheVars($defaultElement->getCacheVars());
141                     $cache->addUsedFiles($defaultElement->getUsedFiles());
142                 }
143             }
144
145             foreach ($this->context['page']->getElements() as $name => $element) {
146                 $this->prefixRequest->setPrefix($name);
147                 $element->init($this->prefixRequest, $this->session, $this->response, $this->context);
148                 if ($element->isAvailable() === true) {
149                     if ($element->isCachable() === false) {
150                         return false;
151                     }
152
153                     $cache->addCacheVars($element->getCacheVars());
154                     $cache->addUsedFiles($element->getUsedFiles());
155                 }
156             }
157         }
158
159         return true;
160     }
161
162     /**
163      * returns the name of the current page
164      *
165      * @return  string
166      */
167     public function getPageName()
168     {
169         return $this->context['pageName'];
170     }
171
172     /**
173      * processes the request
174      */
175     public function process()
176     {
177         $this->template            = $this->createTemplate();
178         $this->context['template'] = $this->template;
179         $this->template->readTemplatesFromInput($this->config->getFrame($this->context['frameId']));
180         $this->setTemplateVars();
181         foreach ($this->config->getParts() as $part) {
182             $content               = '';
183             $this->context['part'] = $part;
184             foreach ($this->config->getDefaultElements($part) as $defaultElement) {
185                 $this->prefixRequest->setPrefix($defaultElement->getName());
186                 $defaultElement->init($this->prefixRequest, $this->session, $this->response, $this->context);
187                 if ($defaultElement->isAvailable() === true) {
188                     $content .= $defaultElement->process();
189                     if ($this->prefixRequest->isCancelled() === true) {
190                         return;
191                     }
192                 }
193             }
194
195             foreach ($this->context['page']->getElements() as $name => $element) {
196                 $this->prefixRequest->setPrefix($name);
197                 $element->init($this->prefixRequest, $this->session, $this->response, $this->context);
198                 if ($element->isAvailable() === true) {
199                     $content .= $element->process();
200                     if ($this->prefixRequest->isCancelled() === true) {
201                         return;
202                     }
203                 }
204             }
205
206             $this->template->addGlobalVar($part, $content);
207         }
208
209         $this->response->write($this->template->getParsedTemplate('frame'));
210     }
211
212     /**
213      * helper method to create the template
214      *
215      * @return  stubMemphisTemplate
216      */
217     // @codeCoverageIgnoreStart
218     protected function createTemplate()
219     {
220         $template = new stubMemphisPatTemplate(stubRegistry::getConfig(stubMemphisTemplate::REGISTRY_KEY_DIR, stubConfig::getPagePath() . '/../templates'));
221         if (stubMode::$CURRENT->isCacheEnabled() === true) {
222             $template->enableCache();
223         }
224
225         return $template;
226     }
227     // @codeCoverageIgnoreEnd
228
229     /**
230      * helper method to set the template vars
231      */
232     protected function setTemplateVars()
233     {
234         $this->template->addGlobalVar('UCUO_FRAME', $this->context['frameId']);
235         $this->template->addGlobalVar('PAGE_TITLE', htmlentities($this->context['page']->getProperty('title')));
236         $this->template->addGlobalVar('PAGE_NAME', $this->context['pageName']);
237         $this->template->addGlobalVar('VARIANT', $this->session->getValue('net.stubbles.websites.variantmanager.variant.name', ''));
238         $this->template->addGlobalVar('VARIANT_ALIAS', $this->session->getValue('net.stubbles.websites.variantmanager.variant.alias', ''));
239
240         $regexDecorator = new stubRegexFilterDecorator(new stubStringFilter(), '/\/.*(?=\?)|\/.*/');
241         $requestUri = $this->request->getFilteredValue($regexDecorator, 'REQUEST_URI', stubRequest::SOURCE_HEADER);
242         $serviceUrl = $requestUri . '?processor=jsonrpc';
243         $this->template->addGlobalVar('SERVICE_URL', $serviceUrl);
244
245         $this->template->addGlobalVar('SID', '$SID');
246         $this->template->addGlobalVar('SESSION_NAME', '$SESSION_NAME');
247         $this->template->addGlobalVar('SESSION_ID', '$SESSION_ID');
248         // add meta information to the page
249         foreach ($this->config->getMetaTags() as $key => $value) {
250             $this->template->addVar('frame', 'META_' . $key, htmlentities($value));
251         }
252
253         $sslMode = 'no';
254         if ($this->isSSL() === true) {
255             $sslMode = 'yes';
256         }
257
258         $this->template->addGlobalVar('SSL_MODE', $sslMode);
259     }
260 }
261 ?>
Note: See TracBrowser for help on using the browser.