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