3 * A generic helper class with generic methods
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class BaseHelper extends BaseFrameworkSystem {
26 * Instance to the class which provides field values
28 private $valueInstance = null;
31 * Rendered content created by the helper class
33 private $content = "";
38 private $groups = array();
41 * Array with sub group
43 private $subGroups = array();
46 * Previously opened group
48 private $previousGroupId = "";
51 * Previously opened sub group
53 private $previousSubGroupId = "";
56 * Total counter for groups and sub groups
58 private $totalCounter = 0;
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;
71 * Protected constructor
73 * @param $className Real name of the class
76 protected function __construct ($className) {
77 // Call parent constructor
78 parent::__construct($className);
81 $this->removeNumberFormaters();
82 $this->removeSystemArray();
88 * @param $newContent New content to add
91 protected final function addContent ($newContent) {
92 $this->content .= (string) trim($newContent) . "\r\n";
98 * @return $content The rendered content by this helper
100 protected final function getContent () {
101 return $this->content;
105 * Assigns a field from the value instance with a template variable
107 * @param $fieldName Name of the field to assign
110 public function assignField ($fieldName) {
111 // Get the value from value instance
112 $fieldValue = $this->getValueField($fieldName);
114 // Assign it with a template variable
115 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
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
122 * @param $fieldName Name of the field to assign
123 * @param $filterMethod Method name to call of the value instance
125 * @todo Rewrite this method using a helper class for filtering data
127 public function assignFieldWithFilter ($fieldName, $filterMethod) {
129 $fieldValue = $this->getValueField($fieldName);
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));
134 // Assign it with a template variable
135 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
139 * Pre-fetches field default values from the given registry key instance into this class
141 * @param $registryKey Registry key which holds an object with values
142 * @param $extraKey Extra value instance key used if registryKey is null
144 * @throws NullPointerException If recovery of requested value instance failed
146 public function prefetchValueInstance ($registryKey, $extraKey = null) {
147 // Get the required instance
148 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
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;
155 // Shall we get an extra instance?
156 if (!is_null($extraKey)) {
157 // Get the extra instance.
158 $extraInstance = Registry::getRegistry()->getInstance($extraKey);
161 // Get the requested instance
163 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
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);
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
177 * @param $groupId Group id to open
178 * @param $content Initial content to add to the group
180 * @throws HelperGroupAlreadyCreatedException If the group was already created before
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);
192 $this->totalCounter++;
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";
199 // Mark this group as previously opened
200 $this->setPreviousGroupId($groupId);
204 * Closes the previously opened group by added given content to it or
205 * throws an exception if no previous group was opened
207 * @param $content Content for previously opened grouop
209 * @throws HelperNoPreviousOpenedGroupException If no previously opened group was found
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);
218 // Get previous group
219 $groupId = $this->getPreviousGroupId();
221 // Add content to it and mark it as closed
222 $this->groups[$groupId]['content'] .= $content."\n";
223 $this->groups[$groupId]['opened'] = false;
225 // Mark previous group as closed
226 $this->setPreviousGroupId("");
227 //* DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
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
235 * @param $subGroupId Sub group id to open
236 * @param $content Initial content to add to the sub group
238 * @throws HelperSubGroupAlreadyCreatedException If the sub group was already created before
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);
249 $this->totalCounter++;
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";
256 // Mark this group as previously opened
257 $this->setPreviousSubGroupId($subGroupId);
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
264 * @param $content Content for previously opened sub grouop
266 * @throws HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
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);
275 // Get previous sub group
276 $subGroupId = $this->getPreviousSubGroupId();
278 // Add content to it and mark it as closed
279 $this->subGroups[$subGroupId]['content'] .= $content."\n";
280 $this->subGroups[$subGroupId]['opened'] = false;
282 // Mark previous sub group as closed
283 $this->setPreviousSubGroupId("");
284 //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
288 * Renders all group and sub group in their order
290 * @return $content Rendered HTML content
292 public function renderContent () {
294 $content = $this->getContent();
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'];
306 // Something went wrong
307 die("GROUP/SUB GROUP ERROR: {$idx}");
317 * Getter for direct field values
319 * @param $fieldName Name of the field we shall fetch
320 * @return $fieldValue Value from field
322 public function getValueField ($fieldName) {
323 // Get the field value
324 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
331 * Getter for value instance
333 * @return $valueInstance Instance of the class holding our values
335 public final function getValueInstance () {
336 return $this->valueInstance;
340 * Check wether a group was opened previously
342 * @return $groupOpened Wether any group was opened before
344 protected final function ifGroupOpenedPreviously () {
345 $groupOpened = (!empty($this->previousGroupId));
350 * Check wether a group was opened previously
352 * @return $subGroupOpened Wether any group was opened before
354 protected final function ifSubGroupOpenedPreviously () {
355 $subGroupOpened = (!empty($this->previousSubGroupId));
356 return $subGroupOpened;
360 * Getter for previous group id
362 * @return $previousGroupId Id of previously opened group
364 protected final function getPreviousGroupId () {
365 return $this->previousGroupId;
369 * Setter for previous group id
371 * @param $previousGroupId Id of previously opened group
374 protected final function setPreviousGroupId ($previousGroupId) {
375 $this->previousGroupId = (string) $previousGroupId;
379 * Getter for previous sub group id
381 * @return $previousSubGroupId Id of previously opened sub group
383 protected final function getPreviousSubGroupId () {
384 return $this->previousSubGroupId;
388 * Setter for previous sub group id
390 * @param $previousSubGroupId Id of previously opened sub group
393 protected final function setPreviousSubGroupId ($previousSubGroupId) {
394 $this->previousSubGroupId = (string) $previousSubGroupId;