3 namespace Org\Mxchange\CoreFramework\Helper;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10 use Org\Mxchange\CoreFramework\Registry\Registry;
13 * A generic helper class with generic methods
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class BaseHelper extends BaseFrameworkSystem {
36 * Instance to the class which provides field values
38 private $valueInstance = NULL;
41 * Extra instance to the class which provides field values
43 private $extraInstance = NULL;
46 * Rendered content created by the helper class
48 private $content = '';
53 private $groups = array();
56 * Array with sub group
58 private $subGroups = array();
61 * Previously opened group
63 private $previousGroupId = '';
66 * Previously opened sub group
68 private $previousSubGroupId = '';
71 * Total counter for groups and sub groups
73 private $totalCounter = 0;
75 // Exception constants
76 const EXCEPTION_GROUP_NOT_OPENED = 0x1e3;
77 const EXCEPTION_GROUP_ALREADY_FOUND = 0x1e4;
78 const EXCEPTION_SUB_GROUP_ALREADY_FOUND = 0x1e5;
79 const EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED = 0x1e6;
80 const EXCEPTION_NO_PREVIOUS_GROUP_OPENED = 0x1e7;
83 * Protected constructor
85 * @param $className Real name of the class
88 protected function __construct ($className) {
89 // Call parent constructor
90 parent::__construct($className);
94 * Adds content directly
96 * @param $newContent New content to add
99 protected final function addContent ($newContent) {
100 $this->content .= (string) trim($newContent) . PHP_EOL;
104 * Add header content to the helper
106 * @param $content Content to to the base
109 protected function addHeaderContent ($content) {
110 // Add the header content
111 $this->groups['header']['content'] = (string) trim($content);
115 * Add footer content to the helper
117 * @param $content Content to to the base
120 protected function addFooterContent ($content) {
121 // Add the footer content
122 $this->groups['footer']['content'] = (string) trim($content);
126 * Adds content to the previously opened group or sub group. If a sub group
127 * was found it will be taken. If no group/sub group is opened at the moment
128 * the code will be passed to addContent().
130 * @param $newContent New content to add
133 protected final function addContentToPreviousGroup ($newContent) {
134 // Check for sub/group
135 if ($this->ifSubGroupOpenedPreviously()) {
137 $subGroupId = $this->getPreviousSubGroupId();
140 $this->subGroups[$subGroupId]['content'] .= $newContent;
141 } elseif ($this->ifGroupOpenedPreviously()) {
143 $groupId = $this->getPreviousGroupId();
146 $this->groups[$groupId]['content'] .= $newContent;
149 $this->addContent($newContent);
156 * @return $content The rendered content by this helper
158 protected final function getContent () {
159 return $this->content;
163 * Public setter for extra instance
165 * @param $extraInstance An extra instance of FrameworkInterface to set
168 public final function setExtraInstance (FrameworkInterface $extraInstance) {
169 $this->extraInstance = $extraInstance;
173 * Assigns a field from the value instance with a template variable
175 * @param $fieldName Name of the field to assign
178 public function assignField ($fieldName) {
179 // Get the value from value instance
180 $fieldValue = $this->getValueField($fieldName);
182 // Assign it with a template variable
183 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
187 * Assigns a field from the value instance with a template variable but
188 * parses its content through a given filter method of the value instance
190 * @param $fieldName Name of the field to assign
191 * @param $filterMethod Method name to call of the value instance
193 * @todo Rewrite this method using a helper class for filtering data
195 public function assignFieldWithFilter ($fieldName, $filterMethod) {
197 $fieldValue = $this->getValueField($fieldName);
198 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'='.$fieldValue);
200 // Now filter it through the value through the filter method
201 $filteredValue = call_user_func_array(array($this, 'doFilter' . self::convertToClassName($filterMethod)), array($fieldValue));
203 // Assign it with a template variable
204 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
208 * Pre-fetches field default values from the given registry key instance into this class
210 * @param $registryKey Registry key which holds an object with values
211 * @param $extraKey Extra value instance key used if registryKey is null
213 * @throws NullPointerException If recovery of requested value instance failed
215 public function prefetchValueInstance ($registryKey, $extraKey = NULL) {
216 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('O:'.$registryKey.'/'.$extraKey);
218 // Get the required instance
219 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
220 } catch (NullPointerException $e) {
221 // Not set in registry
222 // @TODO Try to log it here
223 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($registryKey.'=NULL!');
226 // Shall we get an extra instance?
227 if (!is_null($extraKey)) {
229 // Get the extra instance.
230 $this->extraInstance = Registry::getRegistry()->getInstance($extraKey);
231 } catch (NullPointerException $e) {
233 $this->extraInstance = ObjectFactory::createObjectByConfiguredName($extraKey . '_class', array($this->valueInstance));
235 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($extraKey.'='.$this->extraInstance.' - EXTRA!');
238 // Is the value instance valid?
239 if (is_null($this->valueInstance)) {
240 // Get the requested instance
241 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance));
246 * Opens a helper group with given group id and content or throws an
247 * exception if that group is already found regardless if it is open or
250 * @param $groupId Group id to open
251 * @param $content Initial content to add to the group
252 * @param $tag HTML tag used to open this group
254 * @throws HelperGroupAlreadyCreatedException If the group was already created before
256 protected function openGroupByIdContent ($groupId, $content, $tag) {
257 //* DEBUG: */ echo "OPEN:groupId={$groupId},content=<pre>".htmlentities($content)."</pre>\n";
258 // Is the group already there?
259 if (isset($this->groups[$groupId])) {
260 // Then throw an exception here
261 throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND);
265 $this->totalCounter++;
267 // Add the group to the stack
268 $this->groups[$this->totalCounter] = $groupId;
269 $this->groups[$groupId]['opened'] = true;
270 $this->groups[$groupId]['content'] = sprintf(
271 '<!-- group %s opened (length: %s, tag: %s) //-->%s' . PHP_EOL,
277 $this->groups[$groupId]['tag'] = $tag;
279 // Mark this group as previously opened
280 $this->setPreviousGroupId($groupId);
284 * Closes the previously opened group by added given content to it or
285 * throws an exception if no previous group was opened
287 * @param $content Content for previously opened group, or empty to use tag of opener
289 * @throws HelperNoPreviousOpenedGroupException If no previously opened group was found
291 public function closePreviousGroupByContent ($content = '') {
292 // Check if any sub group was opened before
293 if ($this->ifSubGroupOpenedPreviously()) {
294 // Close it automatically
295 $this->closePreviousSubGroupByContent();
298 // Check if any group was opened before
299 if ($this->ifGroupOpenedPreviously() === false) {
300 // Then throw an exception
301 throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
304 // Get previous group
305 $groupId = $this->getPreviousGroupId();
307 // Is the content empty?
308 if ((empty($content)) && (!empty($this->groups[$groupId]['tag']))) {
309 // Get it from opener
311 "<!-- group %s auto-closed //--></%s>",
313 $this->groups[$groupId]['tag']
317 // Add content to it and mark it as closed
318 $this->groups[$groupId]['content'] .= sprintf(
319 "<!-- group %s closed (length: %s, tag: %s) //-->%s\n",
322 $this->groups[$groupId]['tag'],
325 $this->groups[$groupId]['opened'] = false;
327 // Mark previous group as closed
328 $this->setPreviousGroupId('');
329 //* DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
333 * Opens a helper sub group with given group id and content or throws an
334 * exception if that sub group is already found regardless if it is open or
337 * @param $subGroupId Sub group id to open
338 * @param $content Initial content to add to the sub group
339 * @param $tag HTML tag used to open this group
341 * @throws HelperSubGroupAlreadyCreatedException If the sub group was already created before
343 protected function openSubGroupByIdContent ($subGroupId, $content, $tag) {
344 //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."<br />\n";
345 // Is the group already there?
346 if (isset($this->subGroups[$subGroupId])) {
347 // Then throw an exception here
348 throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND);
352 $this->totalCounter++;
354 // Add the group to the stack
355 $this->subGroups[$this->totalCounter] = $subGroupId;
356 $this->subGroups[$subGroupId]['opened'] = true;
357 $this->subGroups[$subGroupId]['content'] = sprintf("<!-- sub-group %s opened (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $tag, $content);
358 $this->subGroups[$subGroupId]['tag'] = $tag;
360 // Mark this group as previously opened
361 $this->setPreviousSubGroupId($subGroupId);
365 * Closes the previously opened sub group by added given content to it or
366 * throws an exception if no previous sub group was opened
368 * @param $content Content for previously opened sub group, or leave empty to use div/span of openener
370 * @throws HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
372 public function closePreviousSubGroupByContent ($content = '') {
373 // Check if any sub group was opened before
374 if ($this->ifSubGroupOpenedPreviously() === false) {
375 // Then throw an exception
376 throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
379 // Get previous sub group
380 $subGroupId = $this->getPreviousSubGroupId();
382 // Is the content empty?
383 if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) {
384 // Get it from opener
385 $content = sprintf('<!-- sub-group %s auto-closed //--></%s>', $subGroupId, $this->subGroups[$subGroupId]['tag']);
388 // Add content to it and mark it as closed
389 $this->subGroups[$subGroupId]['content'] .= sprintf('<!-- sub-group %s closed (length: %s, tag: %s) //-->%s' . PHP_EOL, $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
390 $this->subGroups[$subGroupId]['opened'] = false
393 // Mark previous sub group as closed
394 $this->setPreviousSubGroupId('');
395 //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
399 * Renders all group and sub group in their order
401 * @return $content Rendered HTML content
403 public function renderContent () {
404 // Initialize content
407 // Is header content there?
408 if (isset($this->groups['header'])) {
410 $content .= $this->groups['header']['content'] . PHP_EOL;
414 $content .= $this->getContent();
416 // Now "walk" through all groups and sub-groups
417 for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
418 // Is this a sub/group and is it closed?
419 if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) {
420 // Then add it's content
421 $groupContent = trim($this->groups[$this->groups[$idx]]['content']);
422 //* DEBUG: */ echo "group={$this->groups[$idx]},content=<pre>".htmlentities($groupContent)."</pre><br />\n";
423 $content .= $groupContent;
424 } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) {
425 // Then add it's content
426 $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content'];
427 //* DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=<pre>".htmlentities($subGroupContent)."</pre><br />\n";
428 $content .= trim($subGroupContent);
430 // Something went wrong
431 $this->debugInstance(__METHOD__ . '(): Something unexpected happened here.');
435 // Is footer content there?
436 if (isset($this->groups['footer'])) {
438 $content .= $this->groups['footer']['content'] . PHP_EOL;
442 //* DEBUG: */ echo "content=<pre>".htmlentities($content)."</pre> (".strlen($content).")<br />\n";
447 * Checks whether the specified group is opened
449 * @param $groupId Id of group to check
450 * @return $isOpened Whether the specified group is open
452 protected function ifGroupIsOpened ($groupId) {
453 // Is the group open?
454 $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === true));
461 * Getter for direct field values
463 * @param $fieldName Name of the field we shall fetch
464 * @return $fieldValue Value from field
465 * @throws NullPointerException Thrown if $valueInstance is null
467 public function getValueField ($fieldName) {
471 // The $valueInstance attribute should not be null!
472 if (is_null($this->getValueInstance())) {
473 // Throws an exception here
474 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
478 if ($this->getValueInstance()->isFieldSet($fieldName)) {
479 // Get the field value
480 $fieldValue = $this->getValueInstance()->getField($fieldName);
481 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Value instance!');
482 } elseif ((!is_null($this->extraInstance)) && ($this->extraInstance->isFieldSet($fieldName))) {
483 // So try the extra instance
484 $fieldValue = $this->extraInstance->getField($fieldName);
485 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Extra instance!');
488 $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] fieldName=' . $fieldName . ' is not set! - @TODO');
496 * Getter for value instance
498 * @return $valueInstance Instance of the class holding our values
500 public final function getValueInstance () {
501 return $this->valueInstance;
505 * Check whether a group was opened previously
507 * @return $groupOpened Whether any group was opened before
509 protected final function ifGroupOpenedPreviously () {
510 $groupOpened = (!empty($this->previousGroupId));
515 * Check whether a group was opened previously
517 * @return $subGroupOpened Whether any group was opened before
519 protected final function ifSubGroupOpenedPreviously () {
520 $subGroupOpened = (!empty($this->previousSubGroupId));
521 return $subGroupOpened;
525 * Getter for previous group id
527 * @return $previousGroupId Id of previously opened group
529 protected final function getPreviousGroupId () {
530 return $this->previousGroupId;
534 * Setter for previous group id
536 * @param $previousGroupId Id of previously opened group
539 protected final function setPreviousGroupId ($previousGroupId) {
540 $this->previousGroupId = (string) $previousGroupId;
544 * Getter for previous sub group id
546 * @return $previousSubGroupId Id of previously opened sub group
548 protected final function getPreviousSubGroupId () {
549 return $this->previousSubGroupId;
553 * Setter for previous sub group id
555 * @param $previousSubGroupId Id of previously opened sub group
558 protected final function setPreviousSubGroupId ($previousSubGroupId) {
559 $this->previousSubGroupId = (string) $previousSubGroupId;