wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.DefaultControlRenderer.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.DefaultControlRenderer.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/core/class.WCMFException.php");
20 require_once(BASE."wcmf/lib/util/class.InifileParser.php");
21 require_once(BASE."wcmf/lib/util/class.URIUtil.php");
22 require_once(BASE."wcmf/lib/presentation/class.View.php");
23 require_once(BASE."wcmf/lib/presentation/class.Controller.php");
24 require_once(BASE."wcmf/lib/presentation/class.InternalLink.php");
25 
26 /**
27  * @class DefaultControlRenderer
28  * @ingroup Presentation
29  * @brief DefaultControlRenderer is responsible for rendering html input controls.
30  * Each control is defined in a smarty template, which is configured by the appropriate entry
31  * in the configuration section 'htmlform' (e.g. the text input control has the entry 'text').
32  * The templates get a default set of variables assigned. Additional variables, needed only for
33  * certain controls, are assigned in the appropriate configure method (e.g. 'configure_text').
34  * which may be overridden by subclasses.
35  *
36  * New controls may be defined by defining the template, putting it into the configuration
37  * section 'htmlform' and maybe implementing a configure method in a subclass of DefaultControlRenderer.
38  * If a subclass is needed, the key 'ControlRenderer' in the configuration section 'implementation'
39  * must point to it (don't forget to give the class definition in the 'classmapping' section).
40  *
41  * @author ingo herwig <ingo@wemove.com>
42  */
44 {
47  var $colorCodeAdded = 0;
48  var $view = null;
49 
50  /**
51  * Get the maximum file size for uploads.
52  * Returns the value given in config key 'maxFileSize' in section 'htmlform', default: 200000
53  * @return The maximum file size in bytes
54  */
55  function getMaxFileSize()
56  {
57  $MAX_FILE_SIZE = 200000;
58 
59  // try to get default max file size
60  $parser = InifileParser::getInstance();
61  if (($maxFileSize = $parser->getValue('maxFileSize', 'htmlform')) === false)
62  $maxFileSize = $MAX_FILE_SIZE;
63  return $maxFileSize;
64  }
65  /**
66  * Render a HTML control of given type using the appropriate smarty template (defined in the
67  * config section 'htmlform'). The given parameters will be passed to the view. If additional
68  * parameters are needed an configure method must be implemented (name: configure_type)
69  * @param type The type of the input control (the template is selected based on the type)
70  * @param enabled Indicates wether the input control should be enabled or not
71  * @param name The name of the input control (usually the name attribute in the control tag)
72  * @param value The value to display (this may be an array if multiple values are possible e.g. select control)
73  * @param error The input validation error if existing
74  * @param attributes An attribute string to be placed in the input control tag (defining its appearance)
75  * @param listMap An assoziative array defining the possible values for list controls
76  * (e.g. select control) see FormUtil::getListMap
77  * @param inputType The original control definition string
78  * @return The HMTL definition string of the input control
79  */
80  function renderControl($type, $enabled, $name, $value, $error, $attributes, $listMap, $inputType)
81  {
82  $parser = InifileParser::getInstance();
83 
84  // build input control
85  if ($view == null)
86  $view = new View();
87  else
88  $view->clear_all_assign();
89 
90  // set default view parameters
91  $view->setup();
92  $view->assign('enabled', $enabled);
93  $view->assign('name', $name);
94  $view->assign('value', $value);
95  $view->assign('error', $error);
96  $view->assign('attributes', $attributes);
97  $view->assign('listMap', $listMap);
98  $view->assign('inputType', $inputType);
99 
100  // split attributes into array and assign it
101  $attributeList = array();
102  $attributeParts = preg_split("/[\s,;]+/", $attributes);
103  foreach($attributeParts as $attribute)
104  {
105  if (strlen($attribute) > 0)
106  {
107  list($key, $value) = preg_split("/[=:]+/", $attribute);
108  $key = trim(stripslashes($key));
109  $value = trim(stripslashes($value));
110  $attributeList[$key] = $value;
111  }
112  }
113  $view->assign('attributeList', $attributeList);
114 
115  // set additional view parameters if needed
116  $configureFunction = "configure_".$type;
117  if (method_exists($this, $configureFunction))
118  $this->$configureFunction($view);
119 
120  if ($viewTpl = $parser->getValue($type, 'htmlform') === false)
121  WCMFException::throwEx("Unknown input control '".$type."'", __FILE__, __LINE__);
122 
123  $htmlString = $view->fetch(BASE.$parser->getValue($type, 'htmlform'));
124  return $htmlString;
125  }
126  /**
127  * Set additional parameters to the select view
128  * @param view A reference to the control view
129  */
131  {
132  // see if we have an async select box
133  $inputType = $view->get_template_vars('inputType');
134  if (preg_match("/^select.*?#async\:(.+)$/", $inputType, $matches))
135  {
136  $list = $matches[1];
137  // get the entity type to list and an optional filter
138  $parts = split('\|', $list);
139  $entityType = array_shift($parts);
140  $filter = join('', $parts);
141  $view->assign('entityType', $entityType);
142  $view->assign('filter', $filter);
143  $view->assign('obfuscator', Obfuscator::getInstance());
144 
145  // get the translated value
146  $listMap = $view->get_template_vars('listMap');
147  $value = $view->get_template_vars('value');
148  $view->assign('translatedValue', $listMap[$value]);
149 
150  $view->assign('isAsync', true);
151  }
152  else
153  $view->assign('isAsync', false);
154  }
155  /**
156  * Set additional parameters to the file control view
157  * @param view A reference to the control view
158  */
160  {
161  $view->assign('maxFileSize', DefaultControlRenderer::getMaxFileSize());
162  }
163  /**
164  * Set additional parameters to the fileex control view
165  * @param view A reference to the control view
166  */
168  {
169  $parser = InifileParser::getInstance();
170  $view->assign('maxFileSize', DefaultControlRenderer::getMaxFileSize());
171  $view->assign('fieldDelimiter', FormUtil::getInputFieldDelimiter());
172  $view->assign('uploadDir', $parser->getValue('uploadDir', 'media'));
173  }
174  /**
175  * Set additional parameters to the filebrowser view
176  * @param view A reference to the control view
177  */
179  {
180  $view->assign('resourceBrowserCodeAdded', $this->resourceBrowserCodeAdded);
181  $view->assign('directory', dirname($view->get_template_vars('value')));
182  $this->resourceBrowserCodeAdded = 1;
183  }
184  /**
185  * Set additional parameters to the linkbrowser view
186  * @param view A reference to the control view
187  */
189  {
190  $value = $view->get_template_vars('value');
191  if (InternalLink::isLink($value))
192  $view->assign('isExternal', false);
193  else
194  $view->assign('isExternal', true);
195  $view->assign('resourceBrowserCodeAdded', $this->resourceBrowserCodeAdded);
196  $this->resourceBrowserCodeAdded = 1;
197  }
198  /**
199  * Set additional parameters to the fckeditor view
200  * @param view A reference to the control view
201  */
203  {
204  $parser = InifileParser::getInstance();
205  if (($libDir = $parser->getValue('libDir', 'cms')) === false)
206  WCMFException::throwEx("No library path 'libDir' defined in ini section 'cms'.", __FILE__, __LINE__);
207  $libDir .= '3rdparty/fckeditor/';
208  $view->assign('libDir', $libDir);
209  $view->assign('appDir', UriUtil::getProtocolStr().$_SERVER['HTTP_HOST'].str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']));
210  $view->assign('FCKeditorCodeAdded', $this->FCKeditorCodeAdded);
211 
212  // unescape html special chars
213  $trans = get_html_translation_table(HTML_ENTITIES);
214  $trans = array_flip($trans);
215  $value = strtr($view->get_template_vars('value'), $trans);
216  $view->assign('value', preg_replace("/[\n\r]/", "", $value));
217  $this->FCKeditorCodeAdded = 1;
218  }
219  /**
220  * Set additional parameters to the color view
221  * @param view A reference to the control view
222  */
224  {
225  $parser = InifileParser::getInstance();
226  if (($libDir = $parser->getValue('libDir', 'cms')) === false)
227  WCMFException::throwEx("No library path 'libDir' defined in ini section 'cms'.", __FILE__, __LINE__);
228  $libDir .= '3rdparty/colorpicker/';
229  $view->assign('libDir', $libDir);
230  $view->assign('colorCodeAdded', $this->colorCodeAdded);
231  $this->colorCodeAdded = 1;
232  }
233 }
234 ?>
renderControl($type, $enabled, $name, $value, $error, $attributes, $listMap, $inputType)
throwEx($message, $file='', $line='')
DefaultControlRenderer is responsible for rendering html input controls. Each control is defined in a...
static getInputFieldDelimiter()
View is used by Controller to handle the view presentation in MVC pattern. View is a subclass of Smar...
Definition: class.View.php:31