3 * A generic helper class with generic methods
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007 - 2009 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();
86 * Adds content directly
88 * @param $newContent New content to add
91 protected final function addContent ($newContent) {
92 $this->content .= (string) trim($newContent)."\n";
96 * Add header content to the helper
98 * @param $content Content to to the base
101 protected function addHeaderContent ($content) {
102 // Add the header content
103 $this->groups['header']['content'] = (string) trim($content);
107 * Add footer content to the helper
109 * @param $content Content to to the base
112 protected function addFooterContent ($content) {
113 // Add the footer content
114 $this->groups['footer']['content'] = (string) trim($content);
118 * Adds content to the previously opened group or sub group. If a sub group
119 * was found it will be taken. If no group/sub group is opened at the moment
120 * the code will be passed to addContent().
122 * @param $newContent New content to add
125 protected final function addContentToPreviousGroup ($newContent) {
126 // Check for sub/group
127 if ($this->ifSubGroupOpenedPreviously()) {
129 $subGroupId = $this->getPreviousSubGroupId();
132 $this->subGroups[$subGroupId]['content'] .= $newContent;
133 } elseif ($this->ifGroupOpenedPreviously()) {
135 $groupId = $this->getPreviousGroupId();
138 $this->groups[$groupId]['content'] .= $newContent;
141 $this->addContent($newContent);
148 * @return $content The rendered content by this helper
150 protected final function getContent () {
151 return $this->content;
155 * Assigns a field from the value instance with a template variable
157 * @param $fieldName Name of the field to assign
160 public function assignField ($fieldName) {
161 // Get the value from value instance
162 $fieldValue = $this->getValueField($fieldName);
164 // Assign it with a template variable
165 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
169 * Assigns a field from the value instance with a template variable but
170 * parses its content through a given filter method of the value instance
172 * @param $fieldName Name of the field to assign
173 * @param $filterMethod Method name to call of the value instance
175 * @todo Rewrite this method using a helper class for filtering data
177 public function assignFieldWithFilter ($fieldName, $filterMethod) {
179 $fieldValue = $this->getValueField($fieldName);
181 // Now filter it through the value through the filter method
182 $filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue));
184 // Assign it with a template variable
185 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
189 * Pre-fetches field default values from the given registry key instance into this class
191 * @param $registryKey Registry key which holds an object with values
192 * @param $extraKey Extra value instance key used if registryKey is null
194 * @throws NullPointerException If recovery of requested value instance failed
196 public function prefetchValueInstance ($registryKey, $extraKey = null) {
197 // Get the required instance
198 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
200 // Is the value instance valid?
201 if (is_null($this->valueInstance)) {
202 // Try to create it "from scratch", by first init extra instance
203 $extraInstance = null;
205 // Shall we get an extra instance?
206 if (!is_null($extraKey)) {
207 // Get the extra instance.
208 $extraInstance = Registry::getRegistry()->getInstance($extraKey);
211 // Get the requested instance
213 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
215 } catch (FrameworkException $e) {
216 // Okay, nothing found so throw a null pointer exception here
217 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
223 * Opens a helper group with given group id and content or throws an
224 * exception if that group is already found regardless if it is open or
227 * @param $groupId Group id to open
228 * @param $content Initial content to add to the group
229 * @param $tag HTML tag used to open this group
231 * @throws HelperGroupAlreadyCreatedException If the group was already created before
233 protected function openGroupByIdContent ($groupId, $content, $tag) {
234 //* DEBUG: */ echo "OPEN:groupId={$groupId},content=<pre>".htmlentities($content)."</pre>\n";
235 // Is the group already there?
236 if (isset($this->groups[$groupId])) {
237 // Then throw an exception here
238 throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND);
242 $this->totalCounter++;
244 // Add the group to the stack
245 $this->groups[$this->totalCounter] = $groupId;
246 $this->groups[$groupId]['opened'] = true;
247 $this->groups[$groupId]['content'] = sprintf("<!-- group %s opened (length: %s, tag: %s) //-->%s\n", $groupId, strlen($content), $tag, $content);
248 $this->groups[$groupId]['tag'] = $tag;
250 // Mark this group as previously opened
251 $this->setPreviousGroupId($groupId);
255 * Closes the previously opened group by added given content to it or
256 * throws an exception if no previous group was opened
258 * @param $content Content for previously opened group, or empty to use tag of opener
260 * @throws HelperNoPreviousOpenedGroupException If no previously opened group was found
262 public function closePreviousGroupByContent ($content = "") {
263 // Check if any sub group was opened before
264 if ($this->ifSubGroupOpenedPreviously()) {
265 // Close it automatically
266 $this->closePreviousSubGroupByContent();
269 // Check if any group was opened before
270 if ($this->ifGroupOpenedPreviously() === false) {
271 // Then throw an exception
272 throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
275 // Get previous group
276 $groupId = $this->getPreviousGroupId();
278 // Is the content empty?
279 if ((empty($content)) && (!empty($this->groups[$groupId]['tag']))) {
280 // Get it from opener
281 $content = sprintf("<!-- group %s auto-closed //--></%s>", $groupId, $this->groups[$groupId]['tag']);
284 // Add content to it and mark it as closed
285 $this->groups[$groupId]['content'] .= sprintf("<!-- group %s closed (length: %s, tag: %s) //-->%s\n", $groupId, strlen($content), $this->groups[$groupId]['tag'], $content);
286 $this->groups[$groupId]['opened'] = false;
288 // Mark previous group as closed
289 $this->setPreviousGroupId("");
290 //* DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
294 * Opens a helper sub group with given group id and content or throws an
295 * exception if that sub group is already found regardless if it is open or
298 * @param $subGroupId Sub group id to open
299 * @param $content Initial content to add to the sub group
300 * @param $tag HTML tag used to open this group
302 * @throws HelperSubGroupAlreadyCreatedException If the sub group was already created before
304 protected function openSubGroupByIdContent ($subGroupId, $content, $tag) {
305 //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."<br />\n";
306 // Is the group already there?
307 if (isset($this->subGroups[$subGroupId])) {
308 // Then throw an exception here
309 throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND);
313 $this->totalCounter++;
315 // Add the group to the stack
316 $this->subGroups[$this->totalCounter] = $subGroupId;
317 $this->subGroups[$subGroupId]['opened'] = true;
318 $this->subGroups[$subGroupId]['content'] = sprintf("<!-- sub-group %s opened (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $tag, $content);
319 $this->subGroups[$subGroupId]['tag'] = $tag;
321 // Mark this group as previously opened
322 $this->setPreviousSubGroupId($subGroupId);
326 * Closes the previously opened sub group by added given content to it or
327 * throws an exception if no previous sub group was opened
329 * @param $content Content for previously opened sub group, or leave empty to use div/span of openener
331 * @throws HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
333 public function closePreviousSubGroupByContent ($content = "") {
334 // Check if any sub group was opened before
335 if ($this->ifSubGroupOpenedPreviously() === false) {
336 // Then throw an exception
337 throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
340 // Get previous sub group
341 $subGroupId = $this->getPreviousSubGroupId();
343 // Is the content empty?
344 if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) {
345 // Get it from opener
346 $content = sprintf("<!-- sub-group %s auto-closed //--></%s>", $subGroupId, $this->subGroups[$subGroupId]['tag']);
349 // Add content to it and mark it as closed
350 $this->subGroups[$subGroupId]['content'] .= sprintf("<!-- sub-group %s closed (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
351 $this->subGroups[$subGroupId]['opened'] = false;
353 // Mark previous sub group as closed
354 $this->setPreviousSubGroupId("");
355 //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
359 * Renders all group and sub group in their order
361 * @return $content Rendered HTML content
363 public function renderContent () {
364 // Initialize content
367 // Is header content there?
368 if (isset($this->groups['header'])) {
370 $content .= $this->groups['header']['content']."\n";
374 $content .= $this->getContent();
376 // Now "walk" through all groups and sub-groups
377 for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
378 // Is this a group and is it closed?
379 if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) {
380 // Then add it's content
381 $groupContent = trim($this->groups[$this->groups[$idx]]['content']);
382 //* DEBUG: */ echo "group={$this->groups[$idx]},content=<pre>".htmlentities($groupContent)."</pre><br />\n";
383 $content .= $groupContent;
384 } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) {
385 // Then add it's content
386 $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content'];
387 //* DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=<pre>".htmlentities($subGroupContent)."</pre><br />\n";
388 $content .= trim($subGroupContent);
390 // Something went wrong
391 $this->debugInstance(__METHOD__."(): Something unexpected happened here.");
395 // Is footer content there?
396 if (isset($this->groups['footer'])) {
398 $content .= $this->groups['footer']['content']."\n";
402 //* DEBUG: */ echo "content=<pre>".htmlentities($content)."</pre> (".strlen($content).")<br />\n";
407 * Checks wether the specified group is opened
409 * @param $groupId Id of group to check
410 * @return $isOpened Wether the specified group is open
412 protected function ifGroupIsOpened ($groupId) {
413 // Is the group open?
414 $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === true));
421 * Getter for direct field values
423 * @param $fieldName Name of the field we shall fetch
424 * @return $fieldValue Value from field
426 public function getValueField ($fieldName) {
427 // Get the field value
428 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
435 * Getter for value instance
437 * @return $valueInstance Instance of the class holding our values
439 public final function getValueInstance () {
440 return $this->valueInstance;
444 * Check wether a group was opened previously
446 * @return $groupOpened Wether any group was opened before
448 protected final function ifGroupOpenedPreviously () {
449 $groupOpened = (!empty($this->previousGroupId));
454 * Check wether a group was opened previously
456 * @return $subGroupOpened Wether any group was opened before
458 protected final function ifSubGroupOpenedPreviously () {
459 $subGroupOpened = (!empty($this->previousSubGroupId));
460 return $subGroupOpened;
464 * Getter for previous group id
466 * @return $previousGroupId Id of previously opened group
468 protected final function getPreviousGroupId () {
469 return $this->previousGroupId;
473 * Setter for previous group id
475 * @param $previousGroupId Id of previously opened group
478 protected final function setPreviousGroupId ($previousGroupId) {
479 $this->previousGroupId = (string) $previousGroupId;
483 * Getter for previous sub group id
485 * @return $previousSubGroupId Id of previously opened sub group
487 protected final function getPreviousSubGroupId () {
488 return $this->previousSubGroupId;
492 * Setter for previous sub group id
494 * @param $previousSubGroupId Id of previously opened sub group
497 protected final function setPreviousSubGroupId ($previousSubGroupId) {
498 $this->previousSubGroupId = (string) $previousSubGroupId;