]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/helper/class_BaseHelper.php
ad0c6ca6d42179c529a133336103271039b7c6b2
[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          * Wether the group is opened or not
37          */
38         private $groupOpened = false;
39
40         /**
41          * Wether the sub group is opened or not
42          */
43         private $subGroupOpened = false;
44
45         /**
46          * Name of the group
47          */
48         private $groupName = "";
49
50         /**
51          * Name of the sub group
52          */
53         private $subGroupId = "";
54
55         /**
56          * Previously opened group
57          */
58         private $previousGroupId = "";
59
60         /**
61          * Previously opened sub group
62          */
63         private $previousSubGroupId = "";
64
65         // Exception constants
66         const EXCEPTION_XML_PARSER_ERROR  = 0x1e0;
67         const EXCEPTION_XML_NODE_UNKNOWN  = 0x1e1;
68         const EXCEPTION_XML_NODE_MISMATCH = 0x1e2;
69         const EXCEPTION_GROUP_NOT_OPENED  = 0x1e3;
70
71         /**
72          * Protected constructor
73          *
74          * @param       $className      Real name of the class
75          * @return      void
76          */
77         protected function __construct ($className) {
78                 // Call parent constructor
79                 parent::__construct($className);
80
81                 // Clean up a little
82                 $this->removeNumberFormaters();
83                 $this->removeSystemArray();
84         }
85
86         /**
87          * Add content
88          *
89          * @param       $newContent             New content to add
90          * @return      void
91          */
92         protected final function addContent ($newContent) {
93                 $this->content .= (string) trim($newContent) . "\r\n";
94         }
95
96         /**
97          * Getter for content
98          *
99          * @return      $content        The rendered content by this helper
100          */
101         protected final function getContent () {
102                 return $this->content;
103         }
104
105         /**
106          *  Assigns a field from the value instance with a template variable
107          *
108          * @param       $fieldName      Name of the field to assign
109          * @return      void
110          */
111         public function assignField ($fieldName) {
112                 // Get the value from value instance
113                 $fieldValue = $this->getValueField($fieldName);
114
115                 // Assign it with a template variable
116                 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
117         }
118
119         /**
120          * Assigns a field from the value instance with a template variable but
121          * parses its content through a given filter method of the value instance
122          *
123          * @param       $fieldName              Name of the field to assign
124          * @param       $filterMethod   Method name to call of the value instance
125          * @return      void
126          * @todo        Rewrite this method using a helper class for filtering data
127          */
128         public function assignFieldWithFilter ($fieldName, $filterMethod) {
129                 // Get the value
130                 $fieldValue = $this->getValueField($fieldName);
131
132                 // Now filter it through the value through the filter method
133                 $filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue));
134
135                 // Assign it with a template variable
136                 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
137         }
138
139         /**
140          * Pre-fetches field default values from the given registry key instance into this class
141          *
142          * @param       $registryKey    Registry key which holds an object with values
143          * @param       $extraKey               Extra value instance key used if registryKey is null
144          * @return      void
145          * @throws      NullPointerException    If recovery of requested value instance failed
146          */
147         public function prefetchValueInstance ($registryKey, $extraKey = null) {
148                 // Get the required instance
149                 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
150
151                 // Is the instance valid?
152                 if (is_null($this->valueInstance)) {
153                         // Try to create it "from scratch", by first init extra instance
154                         $extraInstance = null;
155
156                         // Shall we get an extra instance?
157                         if (!is_null($extraKey)) {
158                                 // Get the extra instance.
159                                 $extraInstance = Registry::getRegistry()->getInstance($extraKey);
160                         } // END - if
161
162                         // Get the requested instance
163                         try {
164                                 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
165
166                         } catch (FrameworkException $e) {
167                                 // Okay, nothing found so throw a null pointer exception here
168                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
169                         }
170                 } // END - if
171         }
172
173         /**
174          * Getter for direct field values
175          *
176          * @param       $fieldName              Name of the field we shall fetch
177          * @return      $fieldValue             Value from field
178          */
179         public function getValueField ($fieldName) {
180                 // Get the field value
181                 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
182
183                 // Return it
184                 return $fieldValue;
185         }
186
187         /**
188          * Getter for value instance
189          *
190          * @return      $valueInstance  Instance of the class holding our values
191          */
192         public final function getValueInstance () {
193                 return $this->valueInstance;
194         }
195
196         /**
197          * Check wether a group was opened previously
198          *
199          * @return      $groupOpened    Wether any group was opened before
200          */
201         protected final function ifGroupOpenedPreviously () {
202                 $groupOpened = (!empty($this->previousGroupId));
203                 return $groupOpened;
204         }
205
206         /**
207          * Check wether a group was opened previously
208          *
209          * @return      $subGroupOpened         Wether any group was opened before
210          */
211         protected final function ifSubGroupOpenedPreviously () {
212                 $subGroupOpened = (!empty($this->previousSubGroupId));
213                 return $subGroupOpened;
214         }
215
216         /**
217          * Getter for previous group id
218          *
219          * @return      $previousGroupId        Id of previously opened group
220          */
221         protected final function getPreviousGroupId () {
222                 return $this->previousGroupId;
223         }
224
225         /**
226          * Setter for previous group id
227          *
228          * @param       $previousGroupId        Id of previously opened group
229          * @return      void
230          */
231         protected final function setPreviousGroupId ($previousGroupId) {
232                 $this->previousGroupId = (string) $previousGroupId;
233         }
234
235         /**
236          * Getter for previous sub group id
237          *
238          * @return      $previousSubGroupId     Id of previously opened sub group
239          */
240         protected final function getPreviousSubGroupId () {
241                 return $this->previousSubGroupId;
242         }
243
244         /**
245          * Setter for previous sub group id
246          *
247          * @param       $previousSubGroupId     Id of previously opened sub group
248          * @return      void
249          */
250         protected final function setPreviousSubGroupId ($previousSubGroupId) {
251                 $this->previousSubGroupId = (string) $previousSubGroupId;
252         }
253 }
254
255 // [EOF]
256 ?>