3 * A generic helper class with generic methods
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
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 * Extra instance to the class which provides field values
33 private $extraInstance = NULL;
36 * Rendered content created by the helper class
38 private $content = '';
43 private $groups = array();
46 * Array with sub group
48 private $subGroups = array();
51 * Previously opened group
53 private $previousGroupId = '';
56 * Previously opened sub group
58 private $previousSubGroupId = '';
61 * Total counter for groups and sub groups
63 private $totalCounter = 0;
65 // Exception constants
66 const EXCEPTION_GROUP_NOT_OPENED = 0x1e3;
67 const EXCEPTION_GROUP_ALREADY_FOUND = 0x1e4;
68 const EXCEPTION_SUB_GROUP_ALREADY_FOUND = 0x1e5;
69 const EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED = 0x1e6;
70 const EXCEPTION_NO_PREVIOUS_GROUP_OPENED = 0x1e7;
73 * Protected constructor
75 * @param $className Real name of the class
78 protected function __construct ($className) {
79 // Call parent constructor
80 parent::__construct($className);
84 * Adds content directly
86 * @param $newContent New content to add
89 protected final function addContent ($newContent) {
90 $this->content .= (string) trim($newContent) . PHP_EOL;
94 * Add header content to the helper
96 * @param $content Content to to the base
99 protected function addHeaderContent ($content) {
100 // Add the header content
101 $this->groups['header']['content'] = (string) trim($content);
105 * Add footer content to the helper
107 * @param $content Content to to the base
110 protected function addFooterContent ($content) {
111 // Add the footer content
112 $this->groups['footer']['content'] = (string) trim($content);
116 * Adds content to the previously opened group or sub group. If a sub group
117 * was found it will be taken. If no group/sub group is opened at the moment
118 * the code will be passed to addContent().
120 * @param $newContent New content to add
123 protected final function addContentToPreviousGroup ($newContent) {
124 // Check for sub/group
125 if ($this->ifSubGroupOpenedPreviously()) {
127 $subGroupId = $this->getPreviousSubGroupId();
130 $this->subGroups[$subGroupId]['content'] .= $newContent;
131 } elseif ($this->ifGroupOpenedPreviously()) {
133 $groupId = $this->getPreviousGroupId();
136 $this->groups[$groupId]['content'] .= $newContent;
139 $this->addContent($newContent);
146 * @return $content The rendered content by this helper
148 protected final function getContent () {
149 return $this->content;
153 * Assigns a field from the value instance with a template variable
155 * @param $fieldName Name of the field to assign
158 public function assignField ($fieldName) {
159 // Get the value from value instance
160 $fieldValue = $this->getValueField($fieldName);
162 // Assign it with a template variable
163 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
167 * Assigns a field from the value instance with a template variable but
168 * parses its content through a given filter method of the value instance
170 * @param $fieldName Name of the field to assign
171 * @param $filterMethod Method name to call of the value instance
173 * @todo Rewrite this method using a helper class for filtering data
175 public function assignFieldWithFilter ($fieldName, $filterMethod) {
177 $fieldValue = $this->getValueField($fieldName);
178 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'='.$fieldValue);
180 // Now filter it through the value through the filter method
181 $filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue));
183 // Assign it with a template variable
184 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
188 * Pre-fetches field default values from the given registry key instance into this class
190 * @param $registryKey Registry key which holds an object with values
191 * @param $extraKey Extra value instance key used if registryKey is null
193 * @throws NullPointerException If recovery of requested value instance failed
195 public function prefetchValueInstance ($registryKey, $extraKey = NULL) {
196 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('O:'.$registryKey.'/'.$extraKey);
198 // Get the required instance
199 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
200 } catch (NullPointerException $e) {
201 // Not set in registry
202 // @TODO Try to log it here
203 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($registryKey.'=NULL!');
206 // Shall we get an extra instance?
207 if (!is_null($extraKey)) {
209 // Get the extra instance.
210 $this->extraInstance = Registry::getRegistry()->getInstance($extraKey);
211 } catch (NullPointerException $e) {
213 $this->extraInstance = ObjectFactory::createObjectByConfiguredName($extraKey . '_class', array($this->valueInstance));
215 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($extraKey.'='.$this->extraInstance.' - EXTRA!');
218 // Is the value instance valid?
219 if (is_null($this->valueInstance)) {
221 // Get the requested instance
222 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance));
223 } catch (FrameworkException $e) {
224 // Okay, nothing found so throw a null pointer exception here
225 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
231 * Opens a helper group with given group id and content or throws an
232 * exception if that group is already found regardless if it is open or
235 * @param $groupId Group id to open
236 * @param $content Initial content to add to the group
237 * @param $tag HTML tag used to open this group
239 * @throws HelperGroupAlreadyCreatedException If the group was already created before
241 protected function openGroupByIdContent ($groupId, $content, $tag) {
242 //* DEBUG: */ echo "OPEN:groupId={$groupId},content=<pre>".htmlentities($content)."</pre>\n";
243 // Is the group already there?
244 if (isset($this->groups[$groupId])) {
245 // Then throw an exception here
246 throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND);
250 $this->totalCounter++;
252 // Add the group to the stack
253 $this->groups[$this->totalCounter] = $groupId;
254 $this->groups[$groupId]['opened'] = true;
255 $this->groups[$groupId]['content'] = sprintf(
256 "<!-- group %s opened (length: %s, tag: %s) //-->%s\n",
262 $this->groups[$groupId]['tag'] = $tag;
264 // Mark this group as previously opened
265 $this->setPreviousGroupId($groupId);
269 * Closes the previously opened group by added given content to it or
270 * throws an exception if no previous group was opened
272 * @param $content Content for previously opened group, or empty to use tag of opener
274 * @throws HelperNoPreviousOpenedGroupException If no previously opened group was found
276 public function closePreviousGroupByContent ($content = '') {
277 // Check if any sub group was opened before
278 if ($this->ifSubGroupOpenedPreviously()) {
279 // Close it automatically
280 $this->closePreviousSubGroupByContent();
283 // Check if any group was opened before
284 if ($this->ifGroupOpenedPreviously() === false) {
285 // Then throw an exception
286 throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
289 // Get previous group
290 $groupId = $this->getPreviousGroupId();
292 // Is the content empty?
293 if ((empty($content)) && (!empty($this->groups[$groupId]['tag']))) {
294 // Get it from opener
296 "<!-- group %s auto-closed //--></%s>",
298 $this->groups[$groupId]['tag']
302 // Add content to it and mark it as closed
303 $this->groups[$groupId]['content'] .= sprintf(
304 "<!-- group %s closed (length: %s, tag: %s) //-->%s\n",
307 $this->groups[$groupId]['tag'],
310 $this->groups[$groupId]['opened'] = false;
312 // Mark previous group as closed
313 $this->setPreviousGroupId('');
314 //* DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
318 * Opens a helper sub group with given group id and content or throws an
319 * exception if that sub group is already found regardless if it is open or
322 * @param $subGroupId Sub group id to open
323 * @param $content Initial content to add to the sub group
324 * @param $tag HTML tag used to open this group
326 * @throws HelperSubGroupAlreadyCreatedException If the sub group was already created before
328 protected function openSubGroupByIdContent ($subGroupId, $content, $tag) {
329 //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."<br />\n";
330 // Is the group already there?
331 if (isset($this->subGroups[$subGroupId])) {
332 // Then throw an exception here
333 throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND);
337 $this->totalCounter++;
339 // Add the group to the stack
340 $this->subGroups[$this->totalCounter] = $subGroupId;
341 $this->subGroups[$subGroupId]['opened'] = true;
342 $this->subGroups[$subGroupId]['content'] = sprintf("<!-- sub-group %s opened (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $tag, $content);
343 $this->subGroups[$subGroupId]['tag'] = $tag;
345 // Mark this group as previously opened
346 $this->setPreviousSubGroupId($subGroupId);
350 * Closes the previously opened sub group by added given content to it or
351 * throws an exception if no previous sub group was opened
353 * @param $content Content for previously opened sub group, or leave empty to use div/span of openener
355 * @throws HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
357 public function closePreviousSubGroupByContent ($content = '') {
358 // Check if any sub group was opened before
359 if ($this->ifSubGroupOpenedPreviously() === false) {
360 // Then throw an exception
361 throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
364 // Get previous sub group
365 $subGroupId = $this->getPreviousSubGroupId();
367 // Is the content empty?
368 if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) {
369 // Get it from opener
370 $content = sprintf("<!-- sub-group %s auto-closed //--></%s>", $subGroupId, $this->subGroups[$subGroupId]['tag']);
373 // Add content to it and mark it as closed
374 $this->subGroups[$subGroupId]['content'] .= sprintf("<!-- sub-group %s closed (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
375 $this->subGroups[$subGroupId]['opened'] = false;
377 // Mark previous sub group as closed
378 $this->setPreviousSubGroupId('');
379 //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
383 * Renders all group and sub group in their order
385 * @return $content Rendered HTML content
387 public function renderContent () {
388 // Initialize content
391 // Is header content there?
392 if (isset($this->groups['header'])) {
394 $content .= $this->groups['header']['content'] . PHP_EOL;
398 $content .= $this->getContent();
400 // Now "walk" through all groups and sub-groups
401 for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
402 // Is this a sub/group and is it closed?
403 if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) {
404 // Then add it's content
405 $groupContent = trim($this->groups[$this->groups[$idx]]['content']);
406 //* DEBUG: */ echo "group={$this->groups[$idx]},content=<pre>".htmlentities($groupContent)."</pre><br />\n";
407 $content .= $groupContent;
408 } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) {
409 // Then add it's content
410 $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content'];
411 //* DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=<pre>".htmlentities($subGroupContent)."</pre><br />\n";
412 $content .= trim($subGroupContent);
414 // Something went wrong
415 $this->debugInstance(__METHOD__."(): Something unexpected happened here.");
419 // Is footer content there?
420 if (isset($this->groups['footer'])) {
422 $content .= $this->groups['footer']['content'] . PHP_EOL;
426 //* DEBUG: */ echo "content=<pre>".htmlentities($content)."</pre> (".strlen($content).")<br />\n";
431 * Checks whether the specified group is opened
433 * @param $groupId Id of group to check
434 * @return $isOpened Whether the specified group is open
436 protected function ifGroupIsOpened ($groupId) {
437 // Is the group open?
438 $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === true));
445 * Getter for direct field values
447 * @param $fieldName Name of the field we shall fetch
448 * @return $fieldValue Value from field
449 * @throws NullPointerException Thrown if $valueInstance is null
451 public function getValueField ($fieldName) {
452 // The $valueInstance attribute should not be null!
453 if (is_null($this->getValueInstance())) {
454 // Throws an exception here
455 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
458 // Get the field value
459 $fieldValue = $this->getValueInstance()->getField($fieldName);
460 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).')');
463 if ((is_null($fieldValue)) && (!is_null($this->extraInstance))) {
464 // So try the extra instance
465 $fieldValue = $this->extraInstance->getField($fieldName);
473 * Getter for value instance
475 * @return $valueInstance Instance of the class holding our values
477 public final function getValueInstance () {
478 return $this->valueInstance;
482 * Check whether a group was opened previously
484 * @return $groupOpened Whether any group was opened before
486 protected final function ifGroupOpenedPreviously () {
487 $groupOpened = (!empty($this->previousGroupId));
492 * Check whether a group was opened previously
494 * @return $subGroupOpened Whether any group was opened before
496 protected final function ifSubGroupOpenedPreviously () {
497 $subGroupOpened = (!empty($this->previousSubGroupId));
498 return $subGroupOpened;
502 * Getter for previous group id
504 * @return $previousGroupId Id of previously opened group
506 protected final function getPreviousGroupId () {
507 return $this->previousGroupId;
511 * Setter for previous group id
513 * @param $previousGroupId Id of previously opened group
516 protected final function setPreviousGroupId ($previousGroupId) {
517 $this->previousGroupId = (string) $previousGroupId;
521 * Getter for previous sub group id
523 * @return $previousSubGroupId Id of previously opened sub group
525 protected final function getPreviousSubGroupId () {
526 return $this->previousSubGroupId;
530 * Setter for previous sub group id
532 * @param $previousSubGroupId Id of previously opened sub group
535 protected final function setPreviousSubGroupId ($previousSubGroupId) {
536 $this->previousSubGroupId = (string) $previousSubGroupId;