Code syncronized with shipsimu code base
[mailer.git] / inc / classes / main / helper / class_BaseHelper.php
1 <?php
2 /**
3  * A generic helper class with generic methods
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
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 BaseHelper extends BaseFrameworkSystem {
25         /**
26          * Instance to the class which provides field values
27          */
28         private $valueInstance = null;
29
30         /**
31          * Rendered content created by the helper class
32          */
33         private $content = "";
34
35         /**
36          * Array with groups
37          */
38         private $groups = array();
39
40         /**
41          * Array with sub group
42          */
43         private $subGroups = array();
44
45         /**
46          * Previously opened group
47          */
48         private $previousGroupId = "";
49
50         /**
51          * Previously opened sub group
52          */
53         private $previousSubGroupId = "";
54
55         /**
56          * Total counter for groups and sub groups
57          */
58         private $totalCounter = 0;
59
60         // Exception constants
61         const EXCEPTION_XML_PARSER_ERROR             = 0x1e0;
62         const EXCEPTION_XML_NODE_UNKNOWN             = 0x1e1;
63         const EXCEPTION_XML_NODE_MISMATCH            = 0x1e2;
64         const EXCEPTION_GROUP_NOT_OPENED             = 0x1e3;
65         const EXCEPTION_GROUP_ALREADY_FOUND          = 0x1e4;
66         const EXCEPTION_SUB_GROUP_ALREADY_FOUND      = 0x1e5;
67         const EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED = 0x1e6;
68         const EXCEPTION_NO_PREVIOUS_GROUP_OPENED     = 0x1e7;
69
70         /**
71          * Protected constructor
72          *
73          * @param       $className      Real name of the class
74          * @return      void
75          */
76         protected function __construct ($className) {
77                 // Call parent constructor
78                 parent::__construct($className);
79
80                 // Clean up a little
81                 $this->removeNumberFormaters();
82                 $this->removeSystemArray();
83         }
84
85         /**
86          * Adds content directly
87          *
88          * @param       $newContent             New content to add
89          * @return      void
90          */
91         protected final function addContent ($newContent) {
92                 $this->content .= (string) trim($newContent)."\n";
93         }
94
95         /**
96          * Add header content to the helper
97          *
98          * @param       $content        Content to to the base
99          * @return      void
100          */
101         protected function addHeaderContent ($content) {
102                 // Add the header content
103                 $this->groups['header']['content'] = (string) trim($content);
104         }
105
106         /**
107          * Add footer content to the helper
108          *
109          * @param       $content        Content to to the base
110          * @return      void
111          */
112         protected function addFooterContent ($content) {
113                 // Add the footer content
114                 $this->groups['footer']['content'] = (string) trim($content);
115         }
116
117         /**
118          * Adds content to the previously opened group or sub group. If a sub group
119          * was found it will be taken. If no group/sub group is opened at the moment
120          * the code will be passed to addContent().
121          *
122          * @param       $newContent             New content to add
123          * @return      void
124          */
125         protected final function addContentToPreviousGroup ($newContent) {
126                 // Check for sub/group
127                 if ($this->ifSubGroupOpenedPreviously()) {
128                         // Get sub group id
129                         $subGroupId = $this->getPreviousSubGroupId();
130
131                         // Add the content
132                         $this->subGroups[$subGroupId]['content'] .= $newContent;
133                 } elseif ($this->ifGroupOpenedPreviously()) {
134                         // Get group id
135                         $groupId = $this->getPreviousGroupId();
136
137                         // Add the content
138                         $this->groups[$groupId]['content'] .= $newContent;
139                 } else {
140                         // Add it directly
141                         $this->addContent($newContent);
142                 }
143         }
144
145         /**
146          * Getter for content
147          *
148          * @return      $content        The rendered content by this helper
149          */
150         protected final function getContent () {
151                 return $this->content;
152         }
153
154         /**
155          *  Assigns a field from the value instance with a template variable
156          *
157          * @param       $fieldName      Name of the field to assign
158          * @return      void
159          */
160         public function assignField ($fieldName) {
161                 // Get the value from value instance
162                 $fieldValue = $this->getValueField($fieldName);
163
164                 // Assign it with a template variable
165                 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
166         }
167
168         /**
169          * Assigns a field from the value instance with a template variable but
170          * parses its content through a given filter method of the value instance
171          *
172          * @param       $fieldName              Name of the field to assign
173          * @param       $filterMethod   Method name to call of the value instance
174          * @return      void
175          * @todo        Rewrite this method using a helper class for filtering data
176          */
177         public function assignFieldWithFilter ($fieldName, $filterMethod) {
178                 // Get the value
179                 $fieldValue = $this->getValueField($fieldName);
180
181                 // Now filter it through the value through the filter method
182                 $filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue));
183
184                 // Assign it with a template variable
185                 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
186         }
187
188         /**
189          * Pre-fetches field default values from the given registry key instance into this class
190          *
191          * @param       $registryKey    Registry key which holds an object with values
192          * @param       $extraKey               Extra value instance key used if registryKey is null
193          * @return      void
194          * @throws      NullPointerException    If recovery of requested value instance failed
195          */
196         public function prefetchValueInstance ($registryKey, $extraKey = null) {
197                 // Get the required instance
198                 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
199
200                 // Is the value instance valid?
201                 if (is_null($this->valueInstance)) {
202                         // Try to create it "from scratch", by first init extra instance
203                         $extraInstance = null;
204
205                         // Shall we get an extra instance?
206                         if (!is_null($extraKey)) {
207                                 // Get the extra instance.
208                                 $extraInstance = Registry::getRegistry()->getInstance($extraKey);
209                         } // END - if
210
211                         // Get the requested instance
212                         try {
213                                 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
214
215                         } catch (FrameworkException $e) {
216                                 // Okay, nothing found so throw a null pointer exception here
217                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
218                         }
219                 } // END - if
220         }
221
222         /**
223          * Opens a helper group with given group id and content or throws an
224          * exception if that group is already found regardless if it is open or
225          * closed.
226          *
227          * @param       $groupId        Group id to open
228          * @param       $content        Initial content to add to the group
229          * @return      void
230          * @throws      HelperGroupAlreadyCreatedException      If the group was already created before
231          */
232         protected function openGroupByIdContent ($groupId, $content) {
233                 //* DEBUG: */ echo "OPEN:groupId={$groupId}<br />\n";
234                 // Is the group already there?
235                 if (isset($this->groups[$groupId])) {
236                         // Then throw an exception here
237                         throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND);
238                 } // END - if
239
240                 // Count one up
241                 $this->totalCounter++;
242
243                 // Add the group to the stack
244                 $this->groups[$this->totalCounter] = $groupId;
245                 $this->groups[$groupId]['opened']  = true;
246                 $this->groups[$groupId]['content'] = $content."\n";
247
248                 // Mark this group as previously opened
249                 $this->setPreviousGroupId($groupId);
250         }
251
252         /**
253          * Closes the previously opened group by added given content to it or
254          * throws an exception if no previous group was opened
255          *
256          * @param       $content        Content for previously opened grouop
257          * @return      void
258          * @throws      HelperNoPreviousOpenedGroupException    If no previously opened group was found
259          */
260         public function closePreviousGroupByContent ($content) {
261                 // Check if any group was opened before
262                 if (!$this->ifGroupOpenedPreviously()) {
263                         // Then throw an exception
264                         throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
265                 } // END - if
266
267                 // Get previous group
268                 $groupId = $this->getPreviousGroupId();
269
270                 // Add content to it and mark it as closed
271                 $this->groups[$groupId]['content'] .= $content."\n";
272                 $this->groups[$groupId]['opened'] = false;
273
274                 // Mark previous group as closed
275                 $this->setPreviousGroupId("");
276                 //* DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
277         }
278
279         /**
280          * Opens a helper sub group with given group id and content or throws an
281          * exception if that sub group is already found regardless if it is open or
282          * closed.
283          *
284          * @param       $subGroupId             Sub group id to open
285          * @param       $content                Initial content to add to the sub group
286          * @return      void
287          * @throws      HelperSubGroupAlreadyCreatedException   If the sub group was already created before
288          */
289         protected function openSubGroupByIdContent ($subGroupId, $content) {
290                 //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."<br />\n";
291                 // Is the group already there?
292                 if (isset($this->subGroups[$subGroupId])) {
293                         // Then throw an exception here
294                         throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND);
295                 } // END - if
296
297                 // Count one up
298                 $this->totalCounter++;
299
300                 // Add the group to the stack
301                 $this->subGroups[$this->totalCounter] = $subGroupId;
302                 $this->subGroups[$subGroupId]['opened']  = true;
303                 $this->subGroups[$subGroupId]['content'] = $content."\n";
304
305                 // Mark this group as previously opened
306                 $this->setPreviousSubGroupId($subGroupId);
307         }
308
309         /**
310          * Closes the previously opened sub group by added given content to it or
311          * throws an exception if no previous sub group was opened
312          *
313          * @param       $content        Content for previously opened sub grouop
314          * @return      void
315          * @throws      HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
316          */
317         public function closePreviousSubGroupByContent ($content) {
318                 // Check if any sub group was opened before
319                 if (!$this->ifSubGroupOpenedPreviously()) {
320                         // Then throw an exception
321                         throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
322                 } // END - if
323
324                 // Get previous sub group
325                 $subGroupId = $this->getPreviousSubGroupId();
326
327                 // Add content to it and mark it as closed
328                 $this->subGroups[$subGroupId]['content'] .= $content."\n";
329                 $this->subGroups[$subGroupId]['opened'] = false;
330
331                 // Mark previous sub group as closed
332                 $this->setPreviousSubGroupId("");
333                 //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
334         }
335
336         /**
337          * Renders all group and sub group in their order
338          *
339          * @return      $content        Rendered HTML content
340          */
341         public function renderContent () {
342                 // Initialize content
343                 $content = "";
344
345                 // Is header content there?
346                 if (isset($this->groups['header'])) {
347                         // Then add it
348                         $content .= $this->groups['header']['content']."\n";
349                 } // END - if
350
351                 // Initiate content
352                 $content .= $this->getContent();
353
354                 // Now "walk" through all groups and sub-groups
355                 for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
356                         // Is this a group and is it closed?
357                         if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) {
358                                 // Then add it's content
359                                 $groupContent = $this->groups[$this->groups[$idx]]['content'];
360                                 //* DEBUG: */ echo "group={$this->groups[$idx]},content=<pre>".htmlentities($groupContent)."</pre><br />\n";
361                                 $content .= $groupContent;
362                         } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) {
363                                 // Then add it's content
364                                 $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content'];
365                                 //* DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=<pre>".htmlentities($subGroupContent)."</pre><br />\n";
366                                 $content .= $subGroupContent;
367                         } else {
368                                 // Something went wrong
369                                 $this->debugInstance();
370                         }
371
372                 } // END - for
373
374                 // Is footer content there?
375                 if (isset($this->groups['footer'])) {
376                         // Then add it
377                         $content .= $this->groups['footer']['content']."\n";
378                 } // END - if
379
380                 // Return it
381                 //* DEBUG: */ echo "content=<pre>".htmlentities($content)."</pre> (".strlen($content).")<br />\n";
382                 return $content;
383         }
384
385         /**
386          * Checks wether the specified group is opened
387          *
388          * @param       $groupId        Id of group to check
389          * @return      $isOpened       Wether the specified group is open
390          */
391         protected function ifGroupIsOpened ($groupId) {
392                 // Is the group open?
393                 $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === true));
394
395                 // Return status
396                 return $isOpened;
397         }
398
399         /**
400          * Getter for direct field values
401          *
402          * @param       $fieldName              Name of the field we shall fetch
403          * @return      $fieldValue             Value from field
404          */
405         public function getValueField ($fieldName) {
406                 // Get the field value
407                 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
408
409                 // Return it
410                 return $fieldValue;
411         }
412
413         /**
414          * Getter for value instance
415          *
416          * @return      $valueInstance  Instance of the class holding our values
417          */
418         public final function getValueInstance () {
419                 return $this->valueInstance;
420         }
421
422         /**
423          * Check wether a group was opened previously
424          *
425          * @return      $groupOpened    Wether any group was opened before
426          */
427         protected final function ifGroupOpenedPreviously () {
428                 $groupOpened = (!empty($this->previousGroupId));
429                 return $groupOpened;
430         }
431
432         /**
433          * Check wether a group was opened previously
434          *
435          * @return      $subGroupOpened         Wether any group was opened before
436          */
437         protected final function ifSubGroupOpenedPreviously () {
438                 $subGroupOpened = (!empty($this->previousSubGroupId));
439                 return $subGroupOpened;
440         }
441
442         /**
443          * Getter for previous group id
444          *
445          * @return      $previousGroupId        Id of previously opened group
446          */
447         protected final function getPreviousGroupId () {
448                 return $this->previousGroupId;
449         }
450
451         /**
452          * Setter for previous group id
453          *
454          * @param       $previousGroupId        Id of previously opened group
455          * @return      void
456          */
457         protected final function setPreviousGroupId ($previousGroupId) {
458                 $this->previousGroupId = (string) $previousGroupId;
459         }
460
461         /**
462          * Getter for previous sub group id
463          *
464          * @return      $previousSubGroupId             Id of previously opened sub group
465          */
466         protected final function getPreviousSubGroupId () {
467                 return $this->previousSubGroupId;
468         }
469
470         /**
471          * Setter for previous sub group id
472          *
473          * @param       $previousSubGroupId             Id of previously opened sub group
474          * @return      void
475          */
476         protected final function setPreviousSubGroupId ($previousSubGroupId) {
477                 $this->previousSubGroupId = (string) $previousSubGroupId;
478         }
479 }
480
481 // [EOF]
482 ?>