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 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 * 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);
82 * Adds content directly
84 * @param $newContent New content to add
87 protected final function addContent ($newContent) {
88 $this->content .= (string) trim($newContent) . "\n";
92 * Add header content to the helper
94 * @param $content Content to to the base
97 protected function addHeaderContent ($content) {
98 // Add the header content
99 $this->groups['header']['content'] = (string) trim($content);
103 * Add footer content to the helper
105 * @param $content Content to to the base
108 protected function addFooterContent ($content) {
109 // Add the footer content
110 $this->groups['footer']['content'] = (string) trim($content);
114 * Adds content to the previously opened group or sub group. If a sub group
115 * was found it will be taken. If no group/sub group is opened at the moment
116 * the code will be passed to addContent().
118 * @param $newContent New content to add
121 protected final function addContentToPreviousGroup ($newContent) {
122 // Check for sub/group
123 if ($this->ifSubGroupOpenedPreviously()) {
125 $subGroupId = $this->getPreviousSubGroupId();
128 $this->subGroups[$subGroupId]['content'] .= $newContent;
129 } elseif ($this->ifGroupOpenedPreviously()) {
131 $groupId = $this->getPreviousGroupId();
134 $this->groups[$groupId]['content'] .= $newContent;
137 $this->addContent($newContent);
144 * @return $content The rendered content by this helper
146 protected final function getContent () {
147 return $this->content;
151 * Assigns a field from the value instance with a template variable
153 * @param $fieldName Name of the field to assign
156 public function assignField ($fieldName) {
157 // Get the value from value instance
158 $fieldValue = $this->getValueField($fieldName);
160 // Assign it with a template variable
161 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
165 * Assigns a field from the value instance with a template variable but
166 * parses its content through a given filter method of the value instance
168 * @param $fieldName Name of the field to assign
169 * @param $filterMethod Method name to call of the value instance
171 * @todo Rewrite this method using a helper class for filtering data
173 public function assignFieldWithFilter ($fieldName, $filterMethod) {
175 $fieldValue = $this->getValueField($fieldName);
177 // Now filter it through the value through the filter method
178 $filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue));
180 // Assign it with a template variable
181 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
185 * Pre-fetches field default values from the given registry key instance into this class
187 * @param $registryKey Registry key which holds an object with values
188 * @param $extraKey Extra value instance key used if registryKey is null
190 * @throws NullPointerException If recovery of requested value instance failed
192 public function prefetchValueInstance ($registryKey, $extraKey = null) {
194 // Get the required instance
195 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
196 } catch (NullPointerException $e) {
197 // Not set in registry
198 // @TODO Try to log it here
201 // Is the value instance valid?
202 if (is_null($this->valueInstance)) {
203 // Try to create it "from scratch", by first init extra instance
204 $extraInstance = null;
206 // Shall we get an extra instance?
207 if (!is_null($extraKey)) {
208 // Get the extra instance.
209 $extraInstance = Registry::getRegistry()->getInstance($extraKey);
213 // Get the requested instance
214 $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(
248 "<!-- group %s opened (length: %s, tag: %s) //-->%s\n",
254 $this->groups[$groupId]['tag'] = $tag;
256 // Mark this group as previously opened
257 $this->setPreviousGroupId($groupId);
261 * Closes the previously opened group by added given content to it or
262 * throws an exception if no previous group was opened
264 * @param $content Content for previously opened group, or empty to use tag of opener
266 * @throws HelperNoPreviousOpenedGroupException If no previously opened group was found
268 public function closePreviousGroupByContent ($content = '') {
269 // Check if any sub group was opened before
270 if ($this->ifSubGroupOpenedPreviously()) {
271 // Close it automatically
272 $this->closePreviousSubGroupByContent();
275 // Check if any group was opened before
276 if ($this->ifGroupOpenedPreviously() === false) {
277 // Then throw an exception
278 throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
281 // Get previous group
282 $groupId = $this->getPreviousGroupId();
284 // Is the content empty?
285 if ((empty($content)) && (!empty($this->groups[$groupId]['tag']))) {
286 // Get it from opener
288 "<!-- group %s auto-closed //--></%s>",
290 $this->groups[$groupId]['tag']
294 // Add content to it and mark it as closed
295 $this->groups[$groupId]['content'] .= sprintf(
296 "<!-- group %s closed (length: %s, tag: %s) //-->%s\n",
299 $this->groups[$groupId]['tag'],
302 $this->groups[$groupId]['opened'] = false;
304 // Mark previous group as closed
305 $this->setPreviousGroupId('');
306 //* DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
310 * Opens a helper sub group with given group id and content or throws an
311 * exception if that sub group is already found regardless if it is open or
314 * @param $subGroupId Sub group id to open
315 * @param $content Initial content to add to the sub group
316 * @param $tag HTML tag used to open this group
318 * @throws HelperSubGroupAlreadyCreatedException If the sub group was already created before
320 protected function openSubGroupByIdContent ($subGroupId, $content, $tag) {
321 //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."<br />\n";
322 // Is the group already there?
323 if (isset($this->subGroups[$subGroupId])) {
324 // Then throw an exception here
325 throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND);
329 $this->totalCounter++;
331 // Add the group to the stack
332 $this->subGroups[$this->totalCounter] = $subGroupId;
333 $this->subGroups[$subGroupId]['opened'] = true;
334 $this->subGroups[$subGroupId]['content'] = sprintf("<!-- sub-group %s opened (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $tag, $content);
335 $this->subGroups[$subGroupId]['tag'] = $tag;
337 // Mark this group as previously opened
338 $this->setPreviousSubGroupId($subGroupId);
342 * Closes the previously opened sub group by added given content to it or
343 * throws an exception if no previous sub group was opened
345 * @param $content Content for previously opened sub group, or leave empty to use div/span of openener
347 * @throws HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
349 public function closePreviousSubGroupByContent ($content = '') {
350 // Check if any sub group was opened before
351 if ($this->ifSubGroupOpenedPreviously() === false) {
352 // Then throw an exception
353 throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
356 // Get previous sub group
357 $subGroupId = $this->getPreviousSubGroupId();
359 // Is the content empty?
360 if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) {
361 // Get it from opener
362 $content = sprintf("<!-- sub-group %s auto-closed //--></%s>", $subGroupId, $this->subGroups[$subGroupId]['tag']);
365 // Add content to it and mark it as closed
366 $this->subGroups[$subGroupId]['content'] .= sprintf("<!-- sub-group %s closed (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
367 $this->subGroups[$subGroupId]['opened'] = false;
369 // Mark previous sub group as closed
370 $this->setPreviousSubGroupId('');
371 //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
375 * Renders all group and sub group in their order
377 * @return $content Rendered HTML content
379 public function renderContent () {
380 // Initialize content
383 // Is header content there?
384 if (isset($this->groups['header'])) {
386 $content .= $this->groups['header']['content'] . "\n";
390 $content .= $this->getContent();
392 // Now "walk" through all groups and sub-groups
393 for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
394 // Is this a group and is it closed?
395 if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) {
396 // Then add it's content
397 $groupContent = trim($this->groups[$this->groups[$idx]]['content']);
398 //* DEBUG: */ echo "group={$this->groups[$idx]},content=<pre>".htmlentities($groupContent)."</pre><br />\n";
399 $content .= $groupContent;
400 } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) {
401 // Then add it's content
402 $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content'];
403 //* DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=<pre>".htmlentities($subGroupContent)."</pre><br />\n";
404 $content .= trim($subGroupContent);
406 // Something went wrong
407 $this->debugInstance(__METHOD__."(): Something unexpected happened here.");
411 // Is footer content there?
412 if (isset($this->groups['footer'])) {
414 $content .= $this->groups['footer']['content'] . "\n";
418 //* DEBUG: */ echo "content=<pre>".htmlentities($content)."</pre> (".strlen($content).")<br />\n";
423 * Checks wether the specified group is opened
425 * @param $groupId Id of group to check
426 * @return $isOpened Wether the specified group is open
428 protected function ifGroupIsOpened ($groupId) {
429 // Is the group open?
430 $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === true));
437 * Getter for direct field values
439 * @param $fieldName Name of the field we shall fetch
440 * @return $fieldValue Value from field
442 public function getValueField ($fieldName) {
443 // Construct the eval() command
444 $eval = sprintf("\$fieldValue = \$this->valueInstance->getField('%s');",
448 // Get the field value
456 * Getter for value instance
458 * @return $valueInstance Instance of the class holding our values
460 public final function getValueInstance () {
461 return $this->valueInstance;
465 * Check wether a group was opened previously
467 * @return $groupOpened Wether any group was opened before
469 protected final function ifGroupOpenedPreviously () {
470 $groupOpened = (!empty($this->previousGroupId));
475 * Check wether a group was opened previously
477 * @return $subGroupOpened Wether any group was opened before
479 protected final function ifSubGroupOpenedPreviously () {
480 $subGroupOpened = (!empty($this->previousSubGroupId));
481 return $subGroupOpened;
485 * Getter for previous group id
487 * @return $previousGroupId Id of previously opened group
489 protected final function getPreviousGroupId () {
490 return $this->previousGroupId;
494 * Setter for previous group id
496 * @param $previousGroupId Id of previously opened group
499 protected final function setPreviousGroupId ($previousGroupId) {
500 $this->previousGroupId = (string) $previousGroupId;
504 * Getter for previous sub group id
506 * @return $previousSubGroupId Id of previously opened sub group
508 protected final function getPreviousSubGroupId () {
509 return $this->previousSubGroupId;
513 * Setter for previous sub group id
515 * @param $previousSubGroupId Id of previously opened sub group
518 protected final function setPreviousSubGroupId ($previousSubGroupId) {
519 $this->previousSubGroupId = (string) $previousSubGroupId;