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 * Instance to the class which provides field values
28 private $valueInstance = null;
31 * Rendered content created by the helper class
33 private $content = "";
36 * Wether the group is opened or not
38 private $groupOpened = false;
41 * Wether the sub group is opened or not
43 private $subGroupOpened = false;
48 private $groupName = "";
51 * Name of the sub group
53 private $subGroupId = "";
56 * Previously opened group
58 private $previousGroupId = "";
61 * Previously opened sub group
63 private $previousSubGroupId = "";
65 // Exception constants
66 const EXCEPTION_XML_PARSER_ERROR = 0x1e0;
67 const EXCEPTION_XML_NODE_UNKNOWN = 0x1e1;
68 const EXCEPTION_XML_NODE_MISMATCH = 0x1e2;
69 const EXCEPTION_GROUP_NOT_OPENED = 0x1e3;
72 * Protected constructor
74 * @param $className Real name of the class
77 protected function __construct ($className) {
78 // Call parent constructor
79 parent::__construct($className);
82 $this->removeNumberFormaters();
83 $this->removeSystemArray();
89 * @param $newContent New content to add
92 protected final function addContent ($newContent) {
93 $this->content .= (string) trim($newContent) . "\r\n";
99 * @return $content The rendered content by this helper
101 protected final function getContent () {
102 return $this->content;
106 * Assigns a field from the value instance with a template variable
108 * @param $fieldName Name of the field to assign
111 public function assignField ($fieldName) {
112 // Get the value from value instance
113 $fieldValue = $this->getValueField($fieldName);
115 // Assign it with a template variable
116 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
120 * Assigns a field from the value instance with a template variable but
121 * parses its content through a given filter method of the value instance
123 * @param $fieldName Name of the field to assign
124 * @param $filterMethod Method name to call of the value instance
126 * @todo Rewrite this method using a helper class for filtering data
128 public function assignFieldWithFilter ($fieldName, $filterMethod) {
130 $fieldValue = $this->getValueField($fieldName);
132 // Now filter it through the value through the filter method
133 $filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue));
135 // Assign it with a template variable
136 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
140 * Pre-fetches field default values from the given registry key instance into this class
142 * @param $registryKey Registry key which holds an object with values
143 * @param $extraKey Extra value instance key used if registryKey is null
145 * @throws NullPointerException If recovery of requested value instance failed
147 public function prefetchValueInstance ($registryKey, $extraKey = null) {
148 // Get the required instance
149 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
151 // Is the instance valid?
152 if (is_null($this->valueInstance)) {
153 // Try to create it "from scratch", by first init extra instance
154 $extraInstance = null;
156 // Shall we get an extra instance?
157 if (!is_null($extraKey)) {
158 // Get the extra instance.
159 $extraInstance = Registry::getRegistry()->getInstance($extraKey);
162 // Get the requested instance
164 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
166 } catch (FrameworkException $e) {
167 // Okay, nothing found so throw a null pointer exception here
168 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
174 * Getter for direct field values
176 * @param $fieldName Name of the field we shall fetch
177 * @return $fieldValue Value from field
179 public function getValueField ($fieldName) {
180 // Get the field value
181 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
188 * Getter for value instance
190 * @return $valueInstance Instance of the class holding our values
192 public final function getValueInstance () {
193 return $this->valueInstance;
197 * Check wether a group was opened previously
199 * @return $groupOpened Wether any group was opened before
201 protected final function ifGroupOpenedPreviously () {
202 $groupOpened = (!empty($this->previousGroupId));
207 * Check wether a group was opened previously
209 * @return $subGroupOpened Wether any group was opened before
211 protected final function ifSubGroupOpenedPreviously () {
212 $subGroupOpened = (!empty($this->previousSubGroupId));
213 return $subGroupOpened;
217 * Getter for previous group id
219 * @return $previousGroupId Id of previously opened group
221 protected final function getPreviousGroupId () {
222 return $this->previousGroupId;
226 * Setter for previous group id
228 * @param $previousGroupId Id of previously opened group
231 protected final function setPreviousGroupId ($previousGroupId) {
232 $this->previousGroupId = (string) $previousGroupId;
236 * Getter for previous sub group id
238 * @return $previousSubGroupId Id of previously opened sub group
240 protected final function getPreviousSubGroupId () {
241 return $this->previousSubGroupId;
245 * Setter for previous sub group id
247 * @param $previousSubGroupId Id of previously opened sub group
250 protected final function setPreviousSubGroupId ($previousSubGroupId) {
251 $this->previousSubGroupId = (string) $previousSubGroupId;