3 namespace CoreFramework\Helper;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Generic\FrameworkInterface;
8 use CoreFramework\Object\BaseFrameworkSystem;
9 use CoreFramework\Registry\Generic\Registry;
12 * A generic helper class with generic methods
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class BaseHelper extends BaseFrameworkSystem {
35 * Instance to the class which provides field values
37 private $valueInstance = NULL;
40 * Extra instance to the class which provides field values
42 private $extraInstance = NULL;
45 * Rendered content created by the helper class
47 private $content = '';
52 private $groups = array();
55 * Array with sub group
57 private $subGroups = array();
60 * Previously opened group
62 private $previousGroupId = '';
65 * Previously opened sub group
67 private $previousSubGroupId = '';
70 * Total counter for groups and sub groups
72 private $totalCounter = 0;
74 // Exception constants
75 const EXCEPTION_GROUP_NOT_OPENED = 0x1e3;
76 const EXCEPTION_GROUP_ALREADY_FOUND = 0x1e4;
77 const EXCEPTION_SUB_GROUP_ALREADY_FOUND = 0x1e5;
78 const EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED = 0x1e6;
79 const EXCEPTION_NO_PREVIOUS_GROUP_OPENED = 0x1e7;
82 * Protected constructor
84 * @param $className Real name of the class
87 protected function __construct ($className) {
88 // Call parent constructor
89 parent::__construct($className);
93 * Adds content directly
95 * @param $newContent New content to add
98 protected final function addContent ($newContent) {
99 $this->content .= (string) trim($newContent) . PHP_EOL;
103 * Add header content to the helper
105 * @param $content Content to to the base
108 protected function addHeaderContent ($content) {
109 // Add the header content
110 $this->groups['header']['content'] = (string) trim($content);
114 * Add footer content to the helper
116 * @param $content Content to to the base
119 protected function addFooterContent ($content) {
120 // Add the footer content
121 $this->groups['footer']['content'] = (string) trim($content);
125 * Adds content to the previously opened group or sub group. If a sub group
126 * was found it will be taken. If no group/sub group is opened at the moment
127 * the code will be passed to addContent().
129 * @param $newContent New content to add
132 protected final function addContentToPreviousGroup ($newContent) {
133 // Check for sub/group
134 if ($this->ifSubGroupOpenedPreviously()) {
136 $subGroupId = $this->getPreviousSubGroupId();
139 $this->subGroups[$subGroupId]['content'] .= $newContent;
140 } elseif ($this->ifGroupOpenedPreviously()) {
142 $groupId = $this->getPreviousGroupId();
145 $this->groups[$groupId]['content'] .= $newContent;
148 $this->addContent($newContent);
155 * @return $content The rendered content by this helper
157 protected final function getContent () {
158 return $this->content;
162 * Public setter for extra instance
164 * @param $extraInstance An extra instance of FrameworkInterface to set
167 public final function setExtraInstance (FrameworkInterface $extraInstance) {
168 $this->extraInstance = $extraInstance;
172 * Assigns a field from the value instance with a template variable
174 * @param $fieldName Name of the field to assign
177 public function assignField ($fieldName) {
178 // Get the value from value instance
179 $fieldValue = $this->getValueField($fieldName);
181 // Assign it with a template variable
182 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
186 * Assigns a field from the value instance with a template variable but
187 * parses its content through a given filter method of the value instance
189 * @param $fieldName Name of the field to assign
190 * @param $filterMethod Method name to call of the value instance
192 * @todo Rewrite this method using a helper class for filtering data
194 public function assignFieldWithFilter ($fieldName, $filterMethod) {
196 $fieldValue = $this->getValueField($fieldName);
197 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'='.$fieldValue);
199 // Now filter it through the value through the filter method
200 $filteredValue = call_user_func_array(array($this, 'doFilter' . self::convertToClassName($filterMethod)), array($fieldValue));
202 // Assign it with a template variable
203 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
207 * Pre-fetches field default values from the given registry key instance into this class
209 * @param $registryKey Registry key which holds an object with values
210 * @param $extraKey Extra value instance key used if registryKey is null
212 * @throws NullPointerException If recovery of requested value instance failed
214 public function prefetchValueInstance ($registryKey, $extraKey = NULL) {
215 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('O:'.$registryKey.'/'.$extraKey);
217 // Get the required instance
218 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
219 } catch (NullPointerException $e) {
220 // Not set in registry
221 // @TODO Try to log it here
222 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($registryKey.'=NULL!');
225 // Shall we get an extra instance?
226 if (!is_null($extraKey)) {
228 // Get the extra instance.
229 $this->extraInstance = Registry::getRegistry()->getInstance($extraKey);
230 } catch (NullPointerException $e) {
232 $this->extraInstance = ObjectFactory::createObjectByConfiguredName($extraKey . '_class', array($this->valueInstance));
234 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($extraKey.'='.$this->extraInstance.' - EXTRA!');
237 // Is the value instance valid?
238 if (is_null($this->valueInstance)) {
239 // Get the requested instance
240 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance));
245 * Opens a helper group with given group id and content or throws an
246 * exception if that group is already found regardless if it is open or
249 * @param $groupId Group id to open
250 * @param $content Initial content to add to the group
251 * @param $tag HTML tag used to open this group
253 * @throws HelperGroupAlreadyCreatedException If the group was already created before
255 protected function openGroupByIdContent ($groupId, $content, $tag) {
256 //* DEBUG: */ echo "OPEN:groupId={$groupId},content=<pre>".htmlentities($content)."</pre>\n";
257 // Is the group already there?
258 if (isset($this->groups[$groupId])) {
259 // Then throw an exception here
260 throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND);
264 $this->totalCounter++;
266 // Add the group to the stack
267 $this->groups[$this->totalCounter] = $groupId;
268 $this->groups[$groupId]['opened'] = TRUE;
269 $this->groups[$groupId]['content'] = sprintf(
270 '<!-- group %s opened (length: %s, tag: %s) //-->%s' . PHP_EOL,
276 $this->groups[$groupId]['tag'] = $tag;
278 // Mark this group as previously opened
279 $this->setPreviousGroupId($groupId);
283 * Closes the previously opened group by added given content to it or
284 * throws an exception if no previous group was opened
286 * @param $content Content for previously opened group, or empty to use tag of opener
288 * @throws HelperNoPreviousOpenedGroupException If no previously opened group was found
290 public function closePreviousGroupByContent ($content = '') {
291 // Check if any sub group was opened before
292 if ($this->ifSubGroupOpenedPreviously()) {
293 // Close it automatically
294 $this->closePreviousSubGroupByContent();
297 // Check if any group was opened before
298 if ($this->ifGroupOpenedPreviously() === FALSE) {
299 // Then throw an exception
300 throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
303 // Get previous group
304 $groupId = $this->getPreviousGroupId();
306 // Is the content empty?
307 if ((empty($content)) && (!empty($this->groups[$groupId]['tag']))) {
308 // Get it from opener
310 "<!-- group %s auto-closed //--></%s>",
312 $this->groups[$groupId]['tag']
316 // Add content to it and mark it as closed
317 $this->groups[$groupId]['content'] .= sprintf(
318 "<!-- group %s closed (length: %s, tag: %s) //-->%s\n",
321 $this->groups[$groupId]['tag'],
324 $this->groups[$groupId]['opened'] = FALSE;
326 // Mark previous group as closed
327 $this->setPreviousGroupId('');
328 //* DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
332 * Opens a helper sub group with given group id and content or throws an
333 * exception if that sub group is already found regardless if it is open or
336 * @param $subGroupId Sub group id to open
337 * @param $content Initial content to add to the sub group
338 * @param $tag HTML tag used to open this group
340 * @throws HelperSubGroupAlreadyCreatedException If the sub group was already created before
342 protected function openSubGroupByIdContent ($subGroupId, $content, $tag) {
343 //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."<br />\n";
344 // Is the group already there?
345 if (isset($this->subGroups[$subGroupId])) {
346 // Then throw an exception here
347 throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND);
351 $this->totalCounter++;
353 // Add the group to the stack
354 $this->subGroups[$this->totalCounter] = $subGroupId;
355 $this->subGroups[$subGroupId]['opened'] = TRUE;
356 $this->subGroups[$subGroupId]['content'] = sprintf("<!-- sub-group %s opened (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $tag, $content);
357 $this->subGroups[$subGroupId]['tag'] = $tag;
359 // Mark this group as previously opened
360 $this->setPreviousSubGroupId($subGroupId);
364 * Closes the previously opened sub group by added given content to it or
365 * throws an exception if no previous sub group was opened
367 * @param $content Content for previously opened sub group, or leave empty to use div/span of openener
369 * @throws HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
371 public function closePreviousSubGroupByContent ($content = '') {
372 // Check if any sub group was opened before
373 if ($this->ifSubGroupOpenedPreviously() === FALSE) {
374 // Then throw an exception
375 throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
378 // Get previous sub group
379 $subGroupId = $this->getPreviousSubGroupId();
381 // Is the content empty?
382 if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) {
383 // Get it from opener
384 $content = sprintf('<!-- sub-group %s auto-closed //--></%s>', $subGroupId, $this->subGroups[$subGroupId]['tag']);
387 // Add content to it and mark it as closed
388 $this->subGroups[$subGroupId]['content'] .= sprintf('<!-- sub-group %s closed (length: %s, tag: %s) //-->%s' . PHP_EOL, $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
389 $this->subGroups[$subGroupId]['opened'] = FALSE
392 // Mark previous sub group as closed
393 $this->setPreviousSubGroupId('');
394 //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
398 * Renders all group and sub group in their order
400 * @return $content Rendered HTML content
402 public function renderContent () {
403 // Initialize content
406 // Is header content there?
407 if (isset($this->groups['header'])) {
409 $content .= $this->groups['header']['content'] . PHP_EOL;
413 $content .= $this->getContent();
415 // Now "walk" through all groups and sub-groups
416 for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
417 // Is this a sub/group and is it closed?
418 if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === FALSE)) {
419 // Then add it's content
420 $groupContent = trim($this->groups[$this->groups[$idx]]['content']);
421 //* DEBUG: */ echo "group={$this->groups[$idx]},content=<pre>".htmlentities($groupContent)."</pre><br />\n";
422 $content .= $groupContent;
423 } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === FALSE)) {
424 // Then add it's content
425 $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content'];
426 //* DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=<pre>".htmlentities($subGroupContent)."</pre><br />\n";
427 $content .= trim($subGroupContent);
429 // Something went wrong
430 $this->debugInstance(__METHOD__ . '(): Something unexpected happened here.');
434 // Is footer content there?
435 if (isset($this->groups['footer'])) {
437 $content .= $this->groups['footer']['content'] . PHP_EOL;
441 //* DEBUG: */ echo "content=<pre>".htmlentities($content)."</pre> (".strlen($content).")<br />\n";
446 * Checks whether the specified group is opened
448 * @param $groupId Id of group to check
449 * @return $isOpened Whether the specified group is open
451 protected function ifGroupIsOpened ($groupId) {
452 // Is the group open?
453 $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === TRUE));
460 * Getter for direct field values
462 * @param $fieldName Name of the field we shall fetch
463 * @return $fieldValue Value from field
464 * @throws NullPointerException Thrown if $valueInstance is null
466 public function getValueField ($fieldName) {
470 // The $valueInstance attribute should not be null!
471 if (is_null($this->getValueInstance())) {
472 // Throws an exception here
473 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
477 if ($this->getValueInstance()->isFieldSet($fieldName)) {
478 // Get the field value
479 $fieldValue = $this->getValueInstance()->getField($fieldName);
480 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Value instance!');
481 } elseif ((!is_null($this->extraInstance)) && ($this->extraInstance->isFieldSet($fieldName))) {
482 // So try the extra instance
483 $fieldValue = $this->extraInstance->getField($fieldName);
484 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Extra instance!');
487 $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] fieldName=' . $fieldName . ' is not set! - @TODO');
495 * Getter for value instance
497 * @return $valueInstance Instance of the class holding our values
499 public final function getValueInstance () {
500 return $this->valueInstance;
504 * Check whether a group was opened previously
506 * @return $groupOpened Whether any group was opened before
508 protected final function ifGroupOpenedPreviously () {
509 $groupOpened = (!empty($this->previousGroupId));
514 * Check whether a group was opened previously
516 * @return $subGroupOpened Whether any group was opened before
518 protected final function ifSubGroupOpenedPreviously () {
519 $subGroupOpened = (!empty($this->previousSubGroupId));
520 return $subGroupOpened;
524 * Getter for previous group id
526 * @return $previousGroupId Id of previously opened group
528 protected final function getPreviousGroupId () {
529 return $this->previousGroupId;
533 * Setter for previous group id
535 * @param $previousGroupId Id of previously opened group
538 protected final function setPreviousGroupId ($previousGroupId) {
539 $this->previousGroupId = (string) $previousGroupId;
543 * Getter for previous sub group id
545 * @return $previousSubGroupId Id of previously opened sub group
547 protected final function getPreviousSubGroupId () {
548 return $this->previousSubGroupId;
552 * Setter for previous sub group id
554 * @param $previousSubGroupId Id of previously opened sub group
557 protected final function setPreviousSubGroupId ($previousSubGroupId) {
558 $this->previousSubGroupId = (string) $previousSubGroupId;