]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/helper/class_BaseHelper.php
Rendering of helper code improved
[shipsimu.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          * Add content
87          *
88          * @param       $newContent             New content to add
89          * @return      void
90          */
91         protected final function addContent ($newContent) {
92                 $this->content .= (string) trim($newContent) . "\r\n";
93         }
94
95         /**
96          * Getter for content
97          *
98          * @return      $content        The rendered content by this helper
99          */
100         protected final function getContent () {
101                 return $this->content;
102         }
103
104         /**
105          *  Assigns a field from the value instance with a template variable
106          *
107          * @param       $fieldName      Name of the field to assign
108          * @return      void
109          */
110         public function assignField ($fieldName) {
111                 // Get the value from value instance
112                 $fieldValue = $this->getValueField($fieldName);
113
114                 // Assign it with a template variable
115                 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
116         }
117
118         /**
119          * Assigns a field from the value instance with a template variable but
120          * parses its content through a given filter method of the value instance
121          *
122          * @param       $fieldName              Name of the field to assign
123          * @param       $filterMethod   Method name to call of the value instance
124          * @return      void
125          * @todo        Rewrite this method using a helper class for filtering data
126          */
127         public function assignFieldWithFilter ($fieldName, $filterMethod) {
128                 // Get the value
129                 $fieldValue = $this->getValueField($fieldName);
130
131                 // Now filter it through the value through the filter method
132                 $filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue));
133
134                 // Assign it with a template variable
135                 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
136         }
137
138         /**
139          * Pre-fetches field default values from the given registry key instance into this class
140          *
141          * @param       $registryKey    Registry key which holds an object with values
142          * @param       $extraKey               Extra value instance key used if registryKey is null
143          * @return      void
144          * @throws      NullPointerException    If recovery of requested value instance failed
145          */
146         public function prefetchValueInstance ($registryKey, $extraKey = null) {
147                 // Get the required instance
148                 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
149
150                 // Is the value instance valid?
151                 if (is_null($this->valueInstance)) {
152                         // Try to create it "from scratch", by first init extra instance
153                         $extraInstance = null;
154
155                         // Shall we get an extra instance?
156                         if (!is_null($extraKey)) {
157                                 // Get the extra instance.
158                                 $extraInstance = Registry::getRegistry()->getInstance($extraKey);
159                         } // END - if
160
161                         // Get the requested instance
162                         try {
163                                 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
164
165                         } catch (FrameworkException $e) {
166                                 // Okay, nothing found so throw a null pointer exception here
167                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
168                         }
169                 } // END - if
170         }
171
172         /**
173          * Opens a helper group with given group id and content or throws an
174          * exception if that group is already found regardless if it is open or
175          * closed.
176          *
177          * @param       $groupId        Group id to open
178          * @param       $content        Initial content to add to the group
179          * @return      void
180          * @throws      HelperGroupAlreadyCreatedException      If the group was already created before
181          */
182         protected function openGroupByIdContent ($groupId, $content) {
183                 //* DEBUG: */ echo "OPEN:groupId={$groupId}<br />\n";
184                 // Is the group already there?
185                 if (isset($this->groups[$groupId])) {
186                         // Then throw an exception here
187                         $this->debugBackTrace();
188                         throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND);
189                 } // END - if
190
191                 // Count one up
192                 $this->totalCounter++;
193
194                 // Add the group to the stack
195                 $this->groups[$this->totalCounter] = $groupId;
196                 $this->groups[$groupId]['opened']  = true;
197                 $this->groups[$groupId]['content'] = $content."\n";
198
199                 // Mark this group as previously opened
200                 $this->setPreviousGroupId($groupId);
201         }
202
203         /**
204          * Closes the previously opened group by added given content to it or
205          * throws an exception if no previous group was opened
206          *
207          * @param       $content        Content for previously opened grouop
208          * @return      void
209          * @throws      HelperNoPreviousOpenedGroupException    If no previously opened group was found
210          */
211         public function closePreviousGroupByContent ($content) {
212                 // Check if any group was opened before
213                 if (!$this->ifGroupOpenedPreviously()) {
214                         // Then throw an exception
215                         throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
216                 } // END - if
217
218                 // Get previous group
219                 $groupId = $this->getPreviousGroupId();
220
221                 // Add content to it and mark it as closed
222                 $this->groups[$groupId]['content'] .= $content."\n";
223                 $this->groups[$groupId]['opened'] = false;
224
225                 // Mark previous group as closed
226                 $this->setPreviousGroupId("");
227                 //* DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
228         }
229
230         /**
231          * Opens a helper sub group with given group id and content or throws an
232          * exception if that sub group is already found regardless if it is open or
233          * closed.
234          *
235          * @param       $subGroupId             Sub group id to open
236          * @param       $content                Initial content to add to the sub group
237          * @return      void
238          * @throws      HelperSubGroupAlreadyCreatedException   If the sub group was already created before
239          */
240         protected function openSubGroupByIdContent ($subGroupId, $content) {
241                 //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."<br />\n";
242                 // Is the group already there?
243                 if (isset($this->subGroups[$subGroupId])) {
244                         // Then throw an exception here
245                         throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND);
246                 } // END - if
247
248                 // Count one up
249                 $this->totalCounter++;
250
251                 // Add the group to the stack
252                 $this->subGroups[$this->totalCounter] = $subGroupId;
253                 $this->subGroups[$subGroupId]['opened']  = true;
254                 $this->subGroups[$subGroupId]['content'] = $content."\n";
255
256                 // Mark this group as previously opened
257                 $this->setPreviousSubGroupId($subGroupId);
258         }
259
260         /**
261          * Closes the previously opened sub group by added given content to it or
262          * throws an exception if no previous sub group was opened
263          *
264          * @param       $content        Content for previously opened sub grouop
265          * @return      void
266          * @throws      HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
267          */
268         public function closePreviousSubGroupByContent ($content) {
269                 // Check if any sub group was opened before
270                 if (!$this->ifSubGroupOpenedPreviously()) {
271                         // Then throw an exception
272                         throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
273                 } // END - if
274
275                 // Get previous sub group
276                 $subGroupId = $this->getPreviousSubGroupId();
277
278                 // Add content to it and mark it as closed
279                 $this->subGroups[$subGroupId]['content'] .= $content."\n";
280                 $this->subGroups[$subGroupId]['opened'] = false;
281
282                 // Mark previous sub group as closed
283                 $this->setPreviousSubGroupId("");
284                 //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
285         }
286
287         /**
288          * Renders all group and sub group in their order
289          *
290          * @return      $content        Rendered HTML content
291          */
292         public function renderContent () {
293                 // Initiate content
294                 $content = $this->getContent();
295
296                 // Now "walk" through all groups and sub-groups
297                 for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
298                         // Is this a group and is it closed?
299                         if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) {
300                                 // Then add it's content
301                                 $content .= $this->groups[$this->groups[$idx]]['content'];
302                         } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) {
303                                 // Then add it's content
304                                 $content .= $this->subGroups[$this->subGroups[$idx]]['content'];
305                         } else {
306                                 // Something went wrong
307                                 die("GROUP/SUB GROUP ERROR: {$idx}");
308                         }
309
310                 } // END - for
311
312                 // Return it
313                 return $content;
314         }
315
316         /**
317          * Getter for direct field values
318          *
319          * @param       $fieldName              Name of the field we shall fetch
320          * @return      $fieldValue             Value from field
321          */
322         public function getValueField ($fieldName) {
323                 // Get the field value
324                 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
325
326                 // Return it
327                 return $fieldValue;
328         }
329
330         /**
331          * Getter for value instance
332          *
333          * @return      $valueInstance  Instance of the class holding our values
334          */
335         public final function getValueInstance () {
336                 return $this->valueInstance;
337         }
338
339         /**
340          * Check wether a group was opened previously
341          *
342          * @return      $groupOpened    Wether any group was opened before
343          */
344         protected final function ifGroupOpenedPreviously () {
345                 $groupOpened = (!empty($this->previousGroupId));
346                 return $groupOpened;
347         }
348
349         /**
350          * Check wether a group was opened previously
351          *
352          * @return      $subGroupOpened         Wether any group was opened before
353          */
354         protected final function ifSubGroupOpenedPreviously () {
355                 $subGroupOpened = (!empty($this->previousSubGroupId));
356                 return $subGroupOpened;
357         }
358
359         /**
360          * Getter for previous group id
361          *
362          * @return      $previousGroupId        Id of previously opened group
363          */
364         protected final function getPreviousGroupId () {
365                 return $this->previousGroupId;
366         }
367
368         /**
369          * Setter for previous group id
370          *
371          * @param       $previousGroupId        Id of previously opened group
372          * @return      void
373          */
374         protected final function setPreviousGroupId ($previousGroupId) {
375                 $this->previousGroupId = (string) $previousGroupId;
376         }
377
378         /**
379          * Getter for previous sub group id
380          *
381          * @return      $previousSubGroupId             Id of previously opened sub group
382          */
383         protected final function getPreviousSubGroupId () {
384                 return $this->previousSubGroupId;
385         }
386
387         /**
388          * Setter for previous sub group id
389          *
390          * @param       $previousSubGroupId             Id of previously opened sub group
391          * @return      void
392          */
393         protected final function setPreviousSubGroupId ($previousSubGroupId) {
394                 $this->previousSubGroupId = (string) $previousSubGroupId;
395         }
396 }
397
398 // [EOF]
399 ?>