3 * A generic helper class with generic methods
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
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 * Rendered content created by the helper class
28 private $content = "";
31 * Instance to the class which provides field values
33 private $valueInstance = null;
35 // Exception constants
36 const EXCEPTION_XML_PARSER_ERROR = 0x1e0;
37 const EXCEPTION_XML_NODE_UNKNOWN = 0x1e1;
38 const EXCEPTION_XML_NODE_MISMATCH = 0x1e2;
41 * Protected constructor
43 * @param $className Real name of the class
46 protected function __construct ($className) {
47 // Call parent constructor
48 parent::__construct($className);
51 $this->removeNumberFormaters();
52 $this->removeSystemArray();
58 * @param $newContent New content to add
61 protected final function addContent ($newContent) {
62 $this->content .= (string) trim($newContent)."\r\n";
68 * @return $content The rendered content by this helper
70 protected final function getContent () {
71 return $this->content;
75 * Assigns a field from the value instance with a template variable
77 * @param $fieldName Name of the field to assign
80 public function assignField ($fieldName) {
81 // Get the value from value instance
82 $fieldValue = $this->getField($fieldName);
84 // Assign it with a template variable
85 $this->getTemplateInstance()->assignVariable("block_" . $fieldName, $fieldValue);
89 * Assigns a field from the value instance with a template variable but
90 * parses its content through a given filter method of the value instance
92 * @param $fieldName Name of the field to assign
93 * @param $filterMethod Method name to call of the value instance
95 * @todo Rewrite this method using a helper class for filtering data
97 public function assignFieldWithFilter ($fieldName, $filterMethod) {
99 $fieldValue = $this->getField($fieldName);
101 // Now filter it through the value through the filter method
102 $filteredValue = call_user_func_array(array($this, "doFilter" . ucfirst($filterMethod)), array($fieldValue));
104 // Assign it with a template variable
105 $this->getTemplateInstance()->assignVariable("block_" . $fieldName, $filteredValue);
109 * Pre-fetches field default values from the given registry key instance into this class
111 * @param $registryKey Registry key which holds an object with values
113 * @throws NullPointerException If an instance from registry is null
115 public function prefetchValueInstance ($registryKey) {
116 // Get the required instance
117 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
119 // Is the instance valid?
120 if (is_null($this->valueInstance)) {
121 // Throw an exception
122 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
127 * Getter for direct field values
129 * @param $fieldName Name of the field we shall fetch
130 * @return $fieldValue Value from field
132 public function getField ($fieldName) {
133 // Get the field value
134 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));