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