19b83c77f90b69991f616afe0c6f322e935c6276
[core.git] / inc / classes / main / template / image / class_ImageTemplateEngine.php
1 <?php
2 /**
3  * The own template engine for loading caching and sending out images
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
25         /**
26          * Main nodes in the XML tree ('image' is ignored)
27          */
28         private $mainNodes = array(
29                 'base',
30                 'type',
31                 'resolution',
32                 'background-color',
33                 'foreground-color',
34                 'image-string'
35         );
36
37         /**
38          * Sub nodes in the XML tree
39          */
40         private $subNodes = array(
41                 'name',
42                 'string-name',
43                 'x',
44                 'y',
45                 'font-size',
46                 'width',
47                 'height',
48                 'red',
49                 'green',
50                 'blue',
51                 'text'
52         );
53
54         /**
55          * Image instance
56          */
57         private $imageInstance = null;
58
59         /**
60          * Current main node
61          */
62         private $currMainNode = '';
63
64         /**
65          * Protected constructor
66          *
67          * @return      void
68          */
69         protected function __construct () {
70                 // Call parent constructor
71                 parent::__construct(__CLASS__);
72         }
73
74         /**
75          * Creates an instance of the class TemplateEngine and prepares it for usage
76          *
77          * @param       $appInstance    A manageable application
78          * @return      $tplInstance    An instance of TemplateEngine
79          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
80          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
81          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
82          *                                                                                      directory or not found
83          * @throws      BasePathReadProtectedException  If $templateBasePath is
84          *                                                                                      read-protected
85          */
86         public final static function createImageTemplateEngine (ManageableApplication $appInstance) {
87                 // Get a new instance
88                 $templateInstance = new ImageTemplateEngine();
89
90                 // Get language and file I/O instances from application
91                 $langInstance = $appInstance->getLanguageInstance();
92                 $ioInstance = $appInstance->getFileIoInstance();
93
94                 // Determine base path
95                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
96
97                 // Is the base path valid?
98                 if (empty($templateBasePath)) {
99                         // Base path is empty
100                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
101                 } elseif (!is_string($templateBasePath)) {
102                         // Is not a string
103                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
104                 } elseif (!is_dir($templateBasePath)) {
105                         // Is not a path
106                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
107                 } elseif (!is_readable($templateBasePath)) {
108                         // Is not readable
109                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
110                 }
111
112                 // Get configuration instance
113                 $configInstance = FrameworkConfiguration::getInstance();
114
115                 // Set the base path
116                 $templateInstance->setTemplateBasePath($templateBasePath);
117
118                 // Set the language and IO instances
119                 $templateInstance->setLanguageInstance($langInstance);
120                 $templateInstance->setFileIoInstance($ioInstance);
121
122                 // Set template extensions
123                 $templateInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
124                 $templateInstance->setCodeTemplateExtension($configInstance->getConfigEntry('code_template_extension'));
125
126                 // Absolute output path for compiled templates
127                 $templateInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
128
129                 // Return the prepared instance
130                 return $templateInstance;
131         }
132
133         /**
134          * Getter for current main node
135          *
136          * @return      $currMainNode   Current main node
137          */
138         public final function getCurrMainNode () {
139                 return $this->currMainNode;
140         }
141
142         /**
143          * Getter for main node array
144          *
145          * @return      $mainNodes      Array with valid main node names
146          */
147         public final function getMainNodes () {
148                 return $this->mainNodes;
149         }
150
151         /**
152          * Getter for sub node array
153          *
154          * @return      $subNodes       Array with valid sub node names
155          */
156         public final function getSubNodes () {
157                 return $this->subNodes;
158         }
159
160         /**
161          * Handles the start element of an XML resource
162          *
163          * @param       $resource               XML parser resource (currently ignored)
164          * @param       $element                The element we shall handle
165          * @param       $attributes             All attributes
166          * @return      void
167          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
168          */
169         public function startElement ($resource, $element, array $attributes) {
170                 // Initial method name which will never be called...
171                 $methodName = 'initImage';
172
173                 // Make the element name lower-case
174                 $element = strtolower($element);
175
176                 // Is the element a main node?
177                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
178                 if (in_array($element, $this->mainNodes)) {
179                         // Okay, main node found!
180                         $methodName = 'setImage' . $this->convertToClassName($element);
181                 } elseif (in_array($element, $this->subNodes)) {
182                         // Sub node found
183                         $methodName = 'setImageProperty' . $this->convertToClassName($element);
184                 } elseif ($element != 'image') {
185                         // Invalid node name found
186                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
187                 }
188
189                 // Call method
190                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
191                 call_user_func_array(array($this, $methodName), $attributes);
192         }
193
194         /**
195          * Ends the main or sub node by sending out the gathered data
196          *
197          * @param       $resource       An XML resource pointer (currently ignored)
198          * @param       $nodeName       Name of the node we want to finish
199          * @return      void
200          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
201          */
202         public function endElement ($resource, $nodeName) {
203                 // Make all lower-case
204                 $nodeName = strtolower($nodeName);
205
206                 // Does this match with current main node?
207                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
208                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
209                         // Did not match!
210                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
211                 } elseif (in_array($nodeName, $this->getSubNodes())) {
212                         // Silently ignore sub nodes
213                         return;
214                 }
215
216                 // Construct method name
217                 $methodName = 'finish' . $this->convertToClassName($nodeName);
218
219                 // Call the corresponding method
220                 call_user_func_array(array($this->imageInstance, $methodName), array());
221         }
222
223         /**
224          * Currently not used
225          *
226          * @param       $resource               XML parser resource (currently ignored)
227          * @param       $characters             Characters to handle
228          * @return      void
229          * @todo        Find something usefull with this!
230          */
231         public function characterHandler ($resource, $characters) {
232                 // Trim all spaces away
233                 $characters = trim($characters);
234
235                 // Is this string empty?
236                 if (empty($characters)) {
237                         // Then skip it silently
238                         return false;
239                 } // END - if
240
241                 // Unfinished work!
242                 $this->partialStub("Handling extra characters is not yet supported!");
243         }
244
245         /**
246          * Intializes the image
247          *
248          * @return      void
249          * @todo        Add cache creation here
250          */
251         private function initImage () {
252                 // Unfinished work!
253         }
254
255         /**
256          * Set the image type
257          *
258          * @param       $imageType      Code fragment or direct value holding the image type
259          * @return      void
260          */
261         private function setImageType ($imageType) {
262                 // Set group to general
263                 $this->setVariableGroup('general');
264
265                 // Try to compile it first to get the value from variable stack
266                 $imageType = $this->compileRawCode($imageType);
267
268                 // Now make a class name of it
269                 $className = $this->convertToClassName($imageType.'_image');
270
271                 // And try to initiate it
272                 $this->imageInstance = ObjectFactory::createObjectByName($className, array($this));
273
274                 // Set current main node to type
275                 $this->currMainNode = 'type';
276         }
277
278         /**
279          * "Setter" for resolution, we first need to collect the resolution from the
280          * sub-nodes. So first, this method will prepare an array for it
281          *
282          * @return      void
283          */
284         private function setImageResolution () {
285                 // Call the image class
286                 $this->imageInstance->initResolution();
287
288                 // Current main node is resolution
289                 $this->currMainNode = 'resolution';
290         }
291
292         /**
293          * "Setter" for base information. For more details see above method!
294          *
295          * @return      void
296          * @see         ImageTemplateEngine::setImageResolution
297          */
298         private function setImageBase () {
299                 // Call the image class
300                 $this->imageInstance->initBase();
301
302                 // Current main node is resolution
303                 $this->currMainNode = 'base';
304         }
305
306         /**
307          * "Setter" for background-color. For more details see above method!
308          *
309          * @return      void
310          * @see         ImageTemplateEngine::setImageResolution
311          */
312         private function setImageBackgroundColor () {
313                 // Call the image class
314                 $this->imageInstance->initBackgroundColor();
315
316                 // Current main node is background-color
317                 $this->currMainNode = 'background-color';
318         }
319
320         /**
321          * "Setter" for foreground-color. For more details see above method!
322          *
323          * @return      void
324          * @see         ImageTemplateEngine::setImageResolution
325          */
326         private function setImageForegroundColor () {
327                 // Call the image class
328                 $this->imageInstance->initForegroundColor();
329
330                 // Current main node is foreground-color
331                 $this->currMainNode = 'foreground-color';
332         }
333
334         /**
335          * "Setter" for image-string. For more details see above method!
336          *
337          * @param       $groupable      Wether this image string is groupable
338          * @return      void
339          * @see         ImageTemplateEngine::setImageResolution
340          */
341         private function setImageImageString ($groupable = 'single') {
342                 // Call the image class
343                 $this->imageInstance->initImageString($groupable);
344
345                 // Current main node is foreground-color
346                 $this->currMainNode = 'image-string';
347         }
348
349         /**
350          * Setter for image name
351          *
352          * @param       $imageName      Name of the image
353          * @return      void
354          */
355         private function setImagePropertyName ($imageName) {
356                 // Call the image class
357                 $this->imageInstance->setImageName($imageName);
358         }
359
360         /**
361          * Setter for image width
362          *
363          * @param       $width  Width of the image or variable
364          * @return      void
365          */
366         private function setImagePropertyWidth ($width) {
367                 // Call the image class
368                 $this->imageInstance->setWidth($width);
369         }
370
371         /**
372          * Setter for image height
373          *
374          * @param       $height Height of the image or variable
375          * @return      void
376          */
377         private function setImagePropertyHeight ($height) {
378                 // Call the image class
379                 $this->imageInstance->setHeight($height);
380         }
381
382         /**
383          * Setter for image red color
384          *
385          * @param       $red    Red color value
386          * @return      void
387          */
388         private function setImagePropertyRed ($red) {
389                 // Call the image class
390                 $this->imageInstance->setRed($red);
391         }
392
393         /**
394          * Setter for image green color
395          *
396          * @param       $green  Green color value
397          * @return      void
398          */
399         private function setImagePropertyGreen ($green) {
400                 // Call the image class
401                 $this->imageInstance->setGreen($green);
402         }
403
404         /**
405          * Setter for image blue color
406          *
407          * @param       $blue   Blue color value
408          * @return      void
409          */
410         private function setImagePropertyBlue ($blue) {
411                 // Call the image class
412                 $this->imageInstance->setBlue($blue);
413         }
414
415         /**
416          * Setter for string name (identifier)
417          *
418          * @param       $stringName             String name (identifier)
419          * @return      void
420          */
421         private function setImagePropertyStringName ($stringName) {
422                 // Call the image class
423                 $this->imageInstance->setStringName($stringName);
424         }
425
426         /**
427          * Setter for font size
428          *
429          * @param       $fontSize       Size of the font
430          * @return      void
431          */
432         private function setImagePropertyFontSize ($fontSize) {
433                 // Call the image class
434                 $this->imageInstance->setFontSize($fontSize);
435         }
436
437         /**
438          * Setter for image string
439          *
440          * @param       $imageString    Image string to set
441          * @return      void
442          */
443         private function setImagePropertyText ($imageString) {
444                 // Call the image class
445                 $this->imageInstance->setString($imageString);
446         }
447
448         /**
449          * Setter for X coordinate
450          *
451          * @param       $x      X coordinate
452          * @return      void
453          */
454         private function setImagePropertyX ($x) {
455                 // Call the image class
456                 $this->imageInstance->setX($x);
457         }
458
459         /**
460          * Setter for Y coordinate
461          *
462          * @param       $y      Y coordinate
463          * @return      void
464          */
465         private function setImagePropertyY ($y) {
466                 // Call the image class
467                 $this->imageInstance->setY($y);
468         }
469
470         /**
471          * Getter for image cache file (FQFN)
472          *
473          * @return      $fqfn   Full-qualified file name of the image cache
474          */
475         public function getImageCacheFqfn () {
476                 // Get the FQFN ready
477                 $fqfn = sprintf("%s%s%s/%s.%s",
478                         $this->getConfigInstance()->getConfigEntry('base_path'),
479                         $this->getGenericBasePath(),
480                         'images/_cache',
481                         md5(
482                                 $this->imageInstance->getImageName() . ':' . $this->__toString() . ':' . $this->imageInstance->__toString()
483                         ),
484                         $this->imageInstance->getImageType()
485                 );
486
487                 // Return it
488                 return $fqfn;
489         }
490
491         /**
492          * Outputs the image to the world
493          *
494          * @param       $responseInstance       An instance of a Responseable class
495          * @return      void
496          */
497         public function transferToResponse (Responseable $responseInstance) {
498                 // Set the image instance
499                 $responseInstance->setImageInstance($this->imageInstance);
500         }
501
502         /**
503          * Load a specified image template into the engine
504          *
505          * @param       $template       The image template we shall load which is
506          *                                              located in 'image' by default
507          * @return      void
508          */
509         public function loadImageTemplate ($template) {
510                 // Set template type
511                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('image_template_type'));
512
513                 // Load the special template
514                 $this->loadTemplate($template);
515         }
516 }
517
518 // [EOF]
519 ?>