3 namespace CoreFramework\Helper;
5 // Import framework stuff
6 use CoreFramework\Generic\FrameworkInterface;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Registry\Generic\Registry;
11 * A generic helper class with generic methods
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class BaseHelper extends BaseFrameworkSystem {
34 * Instance to the class which provides field values
36 private $valueInstance = NULL;
39 * Extra instance to the class which provides field values
41 private $extraInstance = NULL;
44 * Rendered content created by the helper class
46 private $content = '';
51 private $groups = array();
54 * Array with sub group
56 private $subGroups = array();
59 * Previously opened group
61 private $previousGroupId = '';
64 * Previously opened sub group
66 private $previousSubGroupId = '';
69 * Total counter for groups and sub groups
71 private $totalCounter = 0;
73 // Exception constants
74 const EXCEPTION_GROUP_NOT_OPENED = 0x1e3;
75 const EXCEPTION_GROUP_ALREADY_FOUND = 0x1e4;
76 const EXCEPTION_SUB_GROUP_ALREADY_FOUND = 0x1e5;
77 const EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED = 0x1e6;
78 const EXCEPTION_NO_PREVIOUS_GROUP_OPENED = 0x1e7;
81 * Protected constructor
83 * @param $className Real name of the class
86 protected function __construct ($className) {
87 // Call parent constructor
88 parent::__construct($className);
92 * Adds content directly
94 * @param $newContent New content to add
97 protected final function addContent ($newContent) {
98 $this->content .= (string) trim($newContent) . PHP_EOL;
102 * Add header content to the helper
104 * @param $content Content to to the base
107 protected function addHeaderContent ($content) {
108 // Add the header content
109 $this->groups['header']['content'] = (string) trim($content);
113 * Add footer content to the helper
115 * @param $content Content to to the base
118 protected function addFooterContent ($content) {
119 // Add the footer content
120 $this->groups['footer']['content'] = (string) trim($content);
124 * Adds content to the previously opened group or sub group. If a sub group
125 * was found it will be taken. If no group/sub group is opened at the moment
126 * the code will be passed to addContent().
128 * @param $newContent New content to add
131 protected final function addContentToPreviousGroup ($newContent) {
132 // Check for sub/group
133 if ($this->ifSubGroupOpenedPreviously()) {
135 $subGroupId = $this->getPreviousSubGroupId();
138 $this->subGroups[$subGroupId]['content'] .= $newContent;
139 } elseif ($this->ifGroupOpenedPreviously()) {
141 $groupId = $this->getPreviousGroupId();
144 $this->groups[$groupId]['content'] .= $newContent;
147 $this->addContent($newContent);
154 * @return $content The rendered content by this helper
156 protected final function getContent () {
157 return $this->content;
161 * Public setter for extra instance
163 * @param $extraInstance An extra instance of FrameworkInterface to set
166 public final function setExtraInstance (FrameworkInterface $extraInstance) {
167 $this->extraInstance = $extraInstance;
171 * Assigns a field from the value instance with a template variable
173 * @param $fieldName Name of the field to assign
176 public function assignField ($fieldName) {
177 // Get the value from value instance
178 $fieldValue = $this->getValueField($fieldName);
180 // Assign it with a template variable
181 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
185 * Assigns a field from the value instance with a template variable but
186 * parses its content through a given filter method of the value instance
188 * @param $fieldName Name of the field to assign
189 * @param $filterMethod Method name to call of the value instance
191 * @todo Rewrite this method using a helper class for filtering data
193 public function assignFieldWithFilter ($fieldName, $filterMethod) {
195 $fieldValue = $this->getValueField($fieldName);
196 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'='.$fieldValue);
198 // Now filter it through the value through the filter method
199 $filteredValue = call_user_func_array(array($this, 'doFilter' . self::convertToClassName($filterMethod)), array($fieldValue));
201 // Assign it with a template variable
202 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
206 * Pre-fetches field default values from the given registry key instance into this class
208 * @param $registryKey Registry key which holds an object with values
209 * @param $extraKey Extra value instance key used if registryKey is null
211 * @throws NullPointerException If recovery of requested value instance failed
213 public function prefetchValueInstance ($registryKey, $extraKey = NULL) {
214 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('O:'.$registryKey.'/'.$extraKey);
216 // Get the required instance
217 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
218 } catch (NullPointerException $e) {
219 // Not set in registry
220 // @TODO Try to log it here
221 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($registryKey.'=NULL!');
224 // Shall we get an extra instance?
225 if (!is_null($extraKey)) {
227 // Get the extra instance.
228 $this->extraInstance = Registry::getRegistry()->getInstance($extraKey);
229 } catch (NullPointerException $e) {
231 $this->extraInstance = ObjectFactory::createObjectByConfiguredName($extraKey . '_class', array($this->valueInstance));
233 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($extraKey.'='.$this->extraInstance.' - EXTRA!');
236 // Is the value instance valid?
237 if (is_null($this->valueInstance)) {
238 // Get the requested instance
239 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance));
244 * Opens a helper group with given group id and content or throws an
245 * exception if that group is already found regardless if it is open or
248 * @param $groupId Group id to open
249 * @param $content Initial content to add to the group
250 * @param $tag HTML tag used to open this group
252 * @throws HelperGroupAlreadyCreatedException If the group was already created before
254 protected function openGroupByIdContent ($groupId, $content, $tag) {
255 //* DEBUG: */ echo "OPEN:groupId={$groupId},content=<pre>".htmlentities($content)."</pre>\n";
256 // Is the group already there?
257 if (isset($this->groups[$groupId])) {
258 // Then throw an exception here
259 throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND);
263 $this->totalCounter++;
265 // Add the group to the stack
266 $this->groups[$this->totalCounter] = $groupId;
267 $this->groups[$groupId]['opened'] = TRUE;
268 $this->groups[$groupId]['content'] = sprintf(
269 '<!-- group %s opened (length: %s, tag: %s) //-->%s' . PHP_EOL,
275 $this->groups[$groupId]['tag'] = $tag;
277 // Mark this group as previously opened
278 $this->setPreviousGroupId($groupId);
282 * Closes the previously opened group by added given content to it or
283 * throws an exception if no previous group was opened
285 * @param $content Content for previously opened group, or empty to use tag of opener
287 * @throws HelperNoPreviousOpenedGroupException If no previously opened group was found
289 public function closePreviousGroupByContent ($content = '') {
290 // Check if any sub group was opened before
291 if ($this->ifSubGroupOpenedPreviously()) {
292 // Close it automatically
293 $this->closePreviousSubGroupByContent();
296 // Check if any group was opened before
297 if ($this->ifGroupOpenedPreviously() === FALSE) {
298 // Then throw an exception
299 throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
302 // Get previous group
303 $groupId = $this->getPreviousGroupId();
305 // Is the content empty?
306 if ((empty($content)) && (!empty($this->groups[$groupId]['tag']))) {
307 // Get it from opener
309 "<!-- group %s auto-closed //--></%s>",
311 $this->groups[$groupId]['tag']
315 // Add content to it and mark it as closed
316 $this->groups[$groupId]['content'] .= sprintf(
317 "<!-- group %s closed (length: %s, tag: %s) //-->%s\n",
320 $this->groups[$groupId]['tag'],
323 $this->groups[$groupId]['opened'] = FALSE;
325 // Mark previous group as closed
326 $this->setPreviousGroupId('');
327 //* DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
331 * Opens a helper sub group with given group id and content or throws an
332 * exception if that sub group is already found regardless if it is open or
335 * @param $subGroupId Sub group id to open
336 * @param $content Initial content to add to the sub group
337 * @param $tag HTML tag used to open this group
339 * @throws HelperSubGroupAlreadyCreatedException If the sub group was already created before
341 protected function openSubGroupByIdContent ($subGroupId, $content, $tag) {
342 //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."<br />\n";
343 // Is the group already there?
344 if (isset($this->subGroups[$subGroupId])) {
345 // Then throw an exception here
346 throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND);
350 $this->totalCounter++;
352 // Add the group to the stack
353 $this->subGroups[$this->totalCounter] = $subGroupId;
354 $this->subGroups[$subGroupId]['opened'] = TRUE;
355 $this->subGroups[$subGroupId]['content'] = sprintf("<!-- sub-group %s opened (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $tag, $content);
356 $this->subGroups[$subGroupId]['tag'] = $tag;
358 // Mark this group as previously opened
359 $this->setPreviousSubGroupId($subGroupId);
363 * Closes the previously opened sub group by added given content to it or
364 * throws an exception if no previous sub group was opened
366 * @param $content Content for previously opened sub group, or leave empty to use div/span of openener
368 * @throws HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
370 public function closePreviousSubGroupByContent ($content = '') {
371 // Check if any sub group was opened before
372 if ($this->ifSubGroupOpenedPreviously() === FALSE) {
373 // Then throw an exception
374 throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
377 // Get previous sub group
378 $subGroupId = $this->getPreviousSubGroupId();
380 // Is the content empty?
381 if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) {
382 // Get it from opener
383 $content = sprintf('<!-- sub-group %s auto-closed //--></%s>', $subGroupId, $this->subGroups[$subGroupId]['tag']);
386 // Add content to it and mark it as closed
387 $this->subGroups[$subGroupId]['content'] .= sprintf('<!-- sub-group %s closed (length: %s, tag: %s) //-->%s' . PHP_EOL, $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
388 $this->subGroups[$subGroupId]['opened'] = FALSE
391 // Mark previous sub group as closed
392 $this->setPreviousSubGroupId('');
393 //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
397 * Renders all group and sub group in their order
399 * @return $content Rendered HTML content
401 public function renderContent () {
402 // Initialize content
405 // Is header content there?
406 if (isset($this->groups['header'])) {
408 $content .= $this->groups['header']['content'] . PHP_EOL;
412 $content .= $this->getContent();
414 // Now "walk" through all groups and sub-groups
415 for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
416 // Is this a sub/group and is it closed?
417 if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === FALSE)) {
418 // Then add it's content
419 $groupContent = trim($this->groups[$this->groups[$idx]]['content']);
420 //* DEBUG: */ echo "group={$this->groups[$idx]},content=<pre>".htmlentities($groupContent)."</pre><br />\n";
421 $content .= $groupContent;
422 } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === FALSE)) {
423 // Then add it's content
424 $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content'];
425 //* DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=<pre>".htmlentities($subGroupContent)."</pre><br />\n";
426 $content .= trim($subGroupContent);
428 // Something went wrong
429 $this->debugInstance(__METHOD__ . '(): Something unexpected happened here.');
433 // Is footer content there?
434 if (isset($this->groups['footer'])) {
436 $content .= $this->groups['footer']['content'] . PHP_EOL;
440 //* DEBUG: */ echo "content=<pre>".htmlentities($content)."</pre> (".strlen($content).")<br />\n";
445 * Checks whether the specified group is opened
447 * @param $groupId Id of group to check
448 * @return $isOpened Whether the specified group is open
450 protected function ifGroupIsOpened ($groupId) {
451 // Is the group open?
452 $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === TRUE));
459 * Getter for direct field values
461 * @param $fieldName Name of the field we shall fetch
462 * @return $fieldValue Value from field
463 * @throws NullPointerException Thrown if $valueInstance is null
465 public function getValueField ($fieldName) {
469 // The $valueInstance attribute should not be null!
470 if (is_null($this->getValueInstance())) {
471 // Throws an exception here
472 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
476 if ($this->getValueInstance()->isFieldSet($fieldName)) {
477 // Get the field value
478 $fieldValue = $this->getValueInstance()->getField($fieldName);
479 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Value instance!');
480 } elseif ((!is_null($this->extraInstance)) && ($this->extraInstance->isFieldSet($fieldName))) {
481 // So try the extra instance
482 $fieldValue = $this->extraInstance->getField($fieldName);
483 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Extra instance!');
486 $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] fieldName=' . $fieldName . ' is not set! - @TODO');
494 * Getter for value instance
496 * @return $valueInstance Instance of the class holding our values
498 public final function getValueInstance () {
499 return $this->valueInstance;
503 * Check whether a group was opened previously
505 * @return $groupOpened Whether any group was opened before
507 protected final function ifGroupOpenedPreviously () {
508 $groupOpened = (!empty($this->previousGroupId));
513 * Check whether a group was opened previously
515 * @return $subGroupOpened Whether any group was opened before
517 protected final function ifSubGroupOpenedPreviously () {
518 $subGroupOpened = (!empty($this->previousSubGroupId));
519 return $subGroupOpened;
523 * Getter for previous group id
525 * @return $previousGroupId Id of previously opened group
527 protected final function getPreviousGroupId () {
528 return $this->previousGroupId;
532 * Setter for previous group id
534 * @param $previousGroupId Id of previously opened group
537 protected final function setPreviousGroupId ($previousGroupId) {
538 $this->previousGroupId = (string) $previousGroupId;
542 * Getter for previous sub group id
544 * @return $previousSubGroupId Id of previously opened sub group
546 protected final function getPreviousSubGroupId () {
547 return $this->previousSubGroupId;
551 * Setter for previous sub group id
553 * @param $previousSubGroupId Id of previously opened sub group
556 protected final function setPreviousSubGroupId ($previousSubGroupId) {
557 $this->previousSubGroupId = (string) $previousSubGroupId;