Conflicting getField() in BaseHelper vs. BaseFrameworkSystem fixed
[shipsimu.git] / inc / classes / main / helper / class_BaseHelper.php
1 <?php
2 /**
3  * A generic helper class with generic methods
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class BaseHelper extends BaseFrameworkSystem {
25         /**
26          * Rendered content created by the helper class
27          */
28         private $content = "";
29
30         /**
31          * Instance to the class which provides field values
32          */
33         private $valueInstance = null;
34
35         // Exception constants
36         const EXCEPTION_XML_PARSER_ERROR  = 0x1e0;
37         const EXCEPTION_XML_NODE_UNKNOWN  = 0x1e1;
38         const EXCEPTION_XML_NODE_MISMATCH = 0x1e2;
39
40         /**
41          * Protected constructor
42          *
43          * @param       $className      Real name of the class
44          * @return      void
45          */
46         protected function __construct ($className) {
47                 // Call parent constructor
48                 parent::__construct($className);
49
50                 // Clean up a little
51                 $this->removeNumberFormaters();
52                 $this->removeSystemArray();
53         }
54
55         /**
56          * Add content
57          *
58          * @param       $newContent             New content to add
59          * @return      void
60          */
61         protected final function addContent ($newContent) {
62                 $this->content .= (string) trim($newContent)."\r\n";
63         }
64
65         /**
66          * Getter for content
67          *
68          * @return      $content        The rendered content by this helper
69          */
70         protected final function getContent () {
71                 return $this->content;
72         }
73
74         /**
75          *  Assigns a field from the value instance with a template variable
76          *
77          * @param       $fieldName      Name of the field to assign
78          * @return      void
79          */
80         public function assignField ($fieldName) {
81                 // Get the value from value instance
82                 $fieldValue = $this->getValueField($fieldName);
83
84                 // Assign it with a template variable
85                 $this->getTemplateInstance()->assignVariable("block_" . $fieldName, $fieldValue);
86         }
87
88         /**
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
91          *
92          * @param       $fieldName              Name of the field to assign
93          * @param       $filterMethod   Method name to call of the value instance
94          * @return      void
95          * @todo        Rewrite this method using a helper class for filtering data
96          */
97         public function assignFieldWithFilter ($fieldName, $filterMethod) {
98                 // Get the value
99                 $fieldValue = $this->getValueField($fieldName);
100
101                 // Now filter it through the value through the filter method
102                 $filteredValue = call_user_func_array(array($this, "doFilter" . ucfirst($filterMethod)), array($fieldValue));
103
104                 // Assign it with a template variable
105                 $this->getTemplateInstance()->assignVariable("block_" . $fieldName, $filteredValue);
106         }
107
108         /**
109          * Pre-fetches field default values from the given registry key instance into this class
110          *
111          * @param       $registryKey    Registry key which holds an object with values
112          * @param       $extraKey               Extra value instance key used if registryKey is null
113          * @return      void
114          * @throws      NullPointerException    If recovery of requested value instance failed
115          */
116         public function prefetchValueInstance ($registryKey, $extraKey = null) {
117                 // Get the required instance
118                 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
119
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;
124
125                         // Shall we get an extra instance?
126                         if (!is_null($extraKey)) {
127                                 // Get the extra instance.
128                                 $extraInstance = Registry::getRegistry()->getInstance($extraKey);
129                         } // END - if
130
131                         // Get the requested instance
132                         try {
133                                 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
134
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);
138                         }
139                 } // END - if
140         }
141
142         /**
143          * Getter for direct field values
144          *
145          * @param       $fieldName              Name of the field we shall fetch
146          * @return      $fieldValue             Value from field
147          */
148         public function getValueField ($fieldName) {
149                 // Get the field value
150                 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
151
152                 // Return it
153                 return $fieldValue;
154         }
155
156         /**
157          * Getter for value instance
158          *
159          * @return      $valueInstance  Instance of the class holding our values
160          */
161         public final function getValueInstance () {
162                 return $this->valueInstance;
163         }
164 }
165
166 // [EOF]
167 ?>