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->getValueField($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->getValueField($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
112 * @param $extraKey Extra value instance key used if registryKey is null
114 * @throws NullPointerException If recovery of requested value instance failed
116 public function prefetchValueInstance ($registryKey, $extraKey = null) {
117 // Get the required instance
118 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
120 // Is the instance valid?
121 if (is_null($this->valueInstance)) {
122 // Try to create it "from scratch", by first init extra instance
123 $extraInstance = null;
125 // Shall we get an extra instance?
126 if (!is_null($extraKey)) {
127 // Get the extra instance.
128 $extraInstance = Registry::getRegistry()->getInstance($extraKey);
131 // Get the requested instance
133 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
135 } catch (FrameworkException $e) {
136 // Okay, nothing found so throw a null pointer exception here
137 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
143 * Getter for direct field values
145 * @param $fieldName Name of the field we shall fetch
146 * @return $fieldValue Value from field
148 public function getValueField ($fieldName) {
149 // Get the field value
150 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
157 * Getter for value instance
159 * @return $valueInstance Instance of the class holding our values
161 public final function getValueInstance () {
162 return $this->valueInstance;