da6f8763 |
1 | <?php |
2 | /** |
3 | * formslib.php - library of classes for creating forms in Moodle, based on PEAR QuickForms. |
4 | * THIS IS NOT YET PART OF THE MOODLE API, IT IS HERE FOR TESTING ONLY |
5 | * @author Jamie Pratt |
6 | * @version $Id$ |
7 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
8 | */ |
9 | |
10 | //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else. |
11 | if (FALSE===strstr(ini_get('include_path'), $CFG->libdir.'/pear' )){ |
12 | ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path')); |
13 | } |
14 | require_once 'HTML/QuickForm.php'; |
15 | require_once 'HTML/QuickForm/DHTMLRulesTableless.php'; |
16 | require_once 'HTML/QuickForm/Renderer/Tableless.php'; |
17 | |
864cc1de |
18 | if ($CFG->debug >= DEBUG_ALL){ |
19 | PEAR::setErrorHandling(PEAR_ERROR_PRINT); |
20 | } |
21 | |
da6f8763 |
22 | class moodleform extends HTML_QuickForm_DHTMLRulesTableless{ |
23 | /** |
24 | * Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless |
25 | * @param string $formName Form's name. |
26 | * @param string $method (optional)Form's method defaults to 'POST' |
27 | * @param string $action (optional)Form's action |
28 | * @param string $target (optional)Form's target defaults to none |
29 | * @param mixed $attributes (optional)Extra attributes for <form> tag |
30 | * @param bool $trackSubmit (optional)Whether to track if the form was submitted by adding a special hidden field |
31 | * @access public |
32 | */ |
33 | function moodleform($formName='', $method='post', $action='', $target='', $attributes=null){ |
34 | global $CFG; |
35 | //we need to override the constructor, we don't call the parent constructor |
36 | //at all because it strips slashes depending on the magic quotes setting |
37 | //whereas Moodle already has added slashes whether magic quotes is on or not. |
38 | |
39 | //also added check for sesskey and added sesskey to all forms |
40 | //and told class to ask Moodle for the max upload file size |
41 | HTML_Common::HTML_Common($attributes); |
42 | $method = (strtoupper($method) == 'GET') ? 'get' : 'post'; |
43 | $action = ($action == '') ? $_SERVER['PHP_SELF'] : $action; |
44 | $target = empty($target) ? array() : array('target' => $target); |
45 | //no 'name' atttribute for form in xhtml strict : |
46 | $attributes = array('action'=>$action, 'method'=>$method, 'id'=>$formName) + $target; |
47 | $this->updateAttributes($attributes); |
48 | //check for sesskey for this form |
49 | //if it is not present then we don't accept any input |
50 | if (isset($_REQUEST['_qf__' . $formName])) { |
51 | $this->_submitValues = $this->_recursiveFilter('stripslashes', 'get' == $method? $_GET: $_POST); |
52 | foreach ($_FILES as $keyFirst => $valFirst) { |
53 | foreach ($valFirst as $keySecond => $valSecond) { |
54 | if ('name' == $keySecond) { |
55 | $this->_submitFiles[$keyFirst][$keySecond] = $this->_recursiveFilter('stripslashes', $valSecond); |
56 | } else { |
57 | $this->_submitFiles[$keyFirst][$keySecond] = $valSecond; |
58 | } |
59 | } |
60 | } |
61 | |
62 | $this->_flagSubmitted = count($this->_submitValues) > 0 || count($this->_submitFiles) > 0; |
63 | } |
64 | |
65 | //check sesskey |
66 | if ($this->_flagSubmitted){ |
67 | confirm_sesskey($this->_submitValues['_qf__' . $formName]); |
68 | } |
69 | unset($this->_submitValues['_qf__' . $formName]); |
70 | //add sesskey to all forms |
71 | $this->addElement('hidden', '_qf__' . $formName, sesskey()); |
72 | |
73 | if (preg_match('/^([0-9]+)([a-zA-Z]*)$/', get_max_upload_file_size(), $matches)) { |
74 | // see http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes |
75 | switch (strtoupper($matches['2'])) { |
76 | case 'G': |
77 | $this->_maxFileSize = $matches['1'] * 1073741824; |
78 | break; |
79 | case 'M': |
80 | $this->_maxFileSize = $matches['1'] * 1048576; |
81 | break; |
82 | case 'K': |
83 | $this->_maxFileSize = $matches['1'] * 1024; |
84 | break; |
85 | default: |
86 | $this->_maxFileSize = $matches['1']; |
87 | } |
88 | } |
89 | //this is custom stuff for Moodle : |
90 | $oldclass= $this->getAttribute('class'); |
91 | if (!empty($oldclass)){ |
92 | $this->updateAttributes(array('class'=>$oldclass.' mform')); |
93 | }else { |
80f962df |
94 | $this->updateAttributes(array('class'=>'mform')); |
da6f8763 |
95 | } |
96 | $this->_helpImageURL="$CFG->wwwroot/lib/form/req.gif"; |
97 | $this->_reqHTML = |
98 | helpbutton('requiredelement', get_string('requiredelement', 'form'),'moodle', |
99 | true, false, '', true, '<img alt="'.get_string('requiredelement', 'form').'" src="'. |
100 | $this->_helpImageURL.'" />'); |
101 | $this->setRequiredNote(get_string('denotesreq', 'form', $this->getReqHTML())); |
102 | } |
103 | function getReqHTML(){ |
104 | return $this->_reqHTML; |
105 | } |
106 | /** |
107 | * Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless |
108 | * @param array $buttons An associative array representing help button to attach to |
109 | * to the form. keys of array correspond to names of elements in form. |
110 | * |
111 | * @access public |
112 | */ |
42f248e6 |
113 | function setHelpButtons($buttons, $suppresscheck=false){ |
da6f8763 |
114 | |
115 | foreach ($this->_elements as $no => $element){ |
116 | if (array_key_exists($element->getName(), $buttons)){ |
117 | |
80f962df |
118 | if (method_exists($element, 'setHelpButton')){ |
119 | $this->_elements[$no]->setHelpButton($buttons[$element->getName()]); |
120 | }else{ |
121 | $a=new object(); |
122 | $a->name=$element->getName(); |
123 | $a->classname=get_class($element); |
124 | print_error('nomethodforaddinghelpbutton', 'form', '', $a); |
125 | } |
da6f8763 |
126 | unset($buttons[$element->getName()]); |
127 | } |
128 | |
129 | } |
130 | if (count($buttons)&& !$suppresscheck){ |
131 | print_error('nonexistentformelements', 'form', '', join(', ', array_keys($buttons))); |
132 | } |
133 | } |
42f248e6 |
134 | function acceptGet(){ |
135 | $names=func_get_args(); |
136 | foreach ($names as $name){ |
137 | //if no form data is submitted then the page has just beeen loaded |
138 | //so we get the page param value from $_GET |
139 | if (!$this->_flagSubmitted && isset($_GET[$name])){ |
140 | $this->_submitValues[$name]=$_GET[$name]; |
141 | } |
da6f8763 |
142 | } |
da6f8763 |
143 | } |
42f248e6 |
144 | function required_param($element, $paramtype){ |
145 | $value = $this->getSubmitValue($element); |
146 | if (null !== $value) { |
147 | if (false === strpos($element, '[')) { |
148 | return $this->_submitValues[$element] = $this->_clean_param($value, $paramtype); |
149 | } else { |
150 | $idx = "['" . str_replace(array(']', '['), array('', "']['"), $element) . "']"; |
151 | eval("return \$this->_submitValues{$idx} = \$this->_clean_param(\$value, \$paramtype);"); |
152 | } |
153 | }else{ |
154 | //could print name of param but better not to for security? |
155 | print_error('missingrequiredfield', 'error'); |
156 | } |
157 | } |
158 | function optional_param($element, $default, $paramtype){ |
159 | $value = $this->getSubmitValue($element); |
160 | if (null === $value) { |
161 | return $default; |
162 | } |
163 | if (false === strpos($element, '[')) { |
164 | return $this->_submitValues[$element] = $this->_clean_param($value, $paramtype); |
165 | } else { |
166 | $idx = "['" . str_replace(array(']', '['), array('', "']['"), $element) . "']"; |
167 | eval("return \$this->_submitValues{$idx} = \$this->_clean_param(\$value, \$paramtype);"); |
168 | } |
169 | } |
170 | function clean_param($elementList, $paramtype){ |
171 | $value = $this->getSubmitValue($element); |
172 | if (false === strpos($element, '[')) { |
173 | return $this->_submitValues[$element] = $this->_clean_param($value, $paramtype); |
174 | } else { |
175 | $idx = "['" . str_replace(array(']', '['), array('', "']['"), $element) . "']"; |
176 | eval("return \$this->_submitValues{$idx} = \$this->_clean_param(\$value, \$paramtype);"); |
177 | } |
178 | } |
179 | function _clean_param($value, $paramtype){ |
180 | //clean_param function in moodlelib expects vars with slashes |
181 | $value=$this->_recursiveFilter('addslashes', $value); |
182 | $value=clean_param($value, $paramtype); |
183 | return $this->_recursiveFilter('stripslashes', $value); |
184 | } |
185 | |
da6f8763 |
186 | function exportValue($element, $addslashes=true){ |
187 | $unfiltered=parent::exportValue($element); |
188 | if ($addslashes){ |
42f248e6 |
189 | return $this->_recursiveFilter('addslashes',$unfiltered); |
da6f8763 |
190 | } else { |
191 | return $unfiltered; |
192 | } |
193 | } |
42f248e6 |
194 | function exportValues($elementList= null, $addslashes=true){ |
da6f8763 |
195 | $unfiltered=parent::exportValues($elementList); |
196 | if ($addslashes){ |
42f248e6 |
197 | return $this->_recursiveFilter('addslashes',$unfiltered); |
da6f8763 |
198 | } else { |
199 | return $unfiltered; |
200 | } |
201 | } |
202 | } |
203 | |
204 | /** |
205 | * A renderer for moodleform that only uses XHTML and CSS and no |
206 | * table tags, extends PEAR class HTML_QuickForm_Renderer_Tableless |
207 | * |
208 | * Stylesheet is part of standard theme and should be automatically included. |
209 | * |
210 | * @author Jamie Pratt <me@jamiep.org> |
211 | * @license gpl license |
212 | */ |
213 | class moodleform_renderer extends HTML_QuickForm_Renderer_Tableless{ |
214 | |
215 | /** |
216 | * Element template array |
217 | * @var array |
218 | * @access private |
219 | */ |
220 | var $_elementTemplates; |
42f248e6 |
221 | |
222 | // uncomment this and edit formslib.php below for |
223 | // ol li containers for form items. |
224 | |
225 | // /** |
226 | // * Template used when opening a hidden fieldset |
227 | // * (i.e. a fieldset that is opened when there is no header element) |
228 | // * @var string |
229 | // * @access private |
230 | // */ |
231 | // var $_openHiddenFieldsetTemplate = "\n\t<fieldset class=\"hidden\">\n\t\t<ol>"; |
232 | // /** |
233 | // * Header Template string |
234 | // * @var string |
235 | // * @access private |
236 | // */ |
237 | // var $_headerTemplate = |
238 | // "\n\t\t<legend>{header}</legend>\n\t\t<ol>"; |
239 | // /** |
240 | // * Template used when closing a fieldset |
241 | // * @var string |
242 | // * @access private |
243 | // */ |
244 | // var $_closeFieldsetTemplate = "\n\t\t</ol>\n\t</fieldset>"; |
245 | |
da6f8763 |
246 | var $_htmleditors=array(); |
247 | function moodleform_renderer(){ |
42f248e6 |
248 | // switch next two lines for ol li containers for form items. |
249 | // $this->_elementTemplates=array('default'=>"\n\t\t<li class=\"qfrow\"><label class=\"qflabel\">{label}{help}<!-- BEGIN required -->{req}<!-- END required --></label><div class=\"qfelement<!-- BEGIN error --> error<!-- END error --> {type}\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></li>"); |
e249661f |
250 | $this->_elementTemplates=array('default'=>"\n\t\t<div class=\"qfrow\"><label class=\"qflabel\">{label}{help}<!-- BEGIN required -->{req}<!-- END required --></label><div class=\"qfelement<!-- BEGIN error --> error<!-- END error --> {type}\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></div>"); /*, |
251 | will cause problems with client side validation so will leave for now |
252 | 'fieldset'=>"\n\t\t<div class=\"qfrow\"><label class=\"qflabel\">{label}{help}<!-- BEGIN required -->{req}<!-- END required --></label><fieldset class=\"qfelement<!-- BEGIN error --> error<!-- END error --> {type}\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</fieldset></div>");*/ |
da6f8763 |
253 | |
254 | parent::HTML_QuickForm_Renderer_Tableless(); |
255 | } |
256 | function startForm(&$form){ |
257 | $this->_reqHTML=$form->getReqHTML(); |
258 | $this->_elementTemplates=str_replace('{req}', $this->_reqHTML, $this->_elementTemplates); |
259 | parent::startForm($form); |
260 | } |
261 | function startGroup(&$group, $required, $error){ |
262 | if (method_exists($group, 'getElementTemplateType')){ |
e249661f |
263 | $html = $this->_elementTemplates[$group->getElementTemplateType()]; |
da6f8763 |
264 | }else{ |
265 | $html = $this->_elementTemplates['default']; |
266 | |
267 | } |
268 | if (method_exists($group, 'getHelpButton')){ |
269 | $html =str_replace('{help}', $group->getHelpButton(), $html); |
270 | }else{ |
271 | $html =str_replace('{help}', '', $html); |
272 | |
273 | } |
80f962df |
274 | $html =str_replace('{type}', 'group', $html); |
275 | |
da6f8763 |
276 | $this->_templates[$group->getName()]=$html; |
277 | // Fix for bug in tableless quickforms that didn't allow you to stop a |
278 | // fieldset before a group of elements. |
279 | // if the element name indicates the end of a fieldset, close the fieldset |
280 | if ( in_array($group->getName(), $this->_stopFieldsetElements) |
281 | && $this->_fieldsetsOpen > 0 |
282 | ) { |
283 | $this->_html .= $this->_closeFieldsetTemplate; |
284 | $this->_fieldsetsOpen--; |
285 | } |
286 | parent::startGroup($group, $required, $error); |
287 | } |
288 | function renderElement(&$element, $required, $error){ |
289 | if (method_exists($element, 'getElementTemplateType')){ |
290 | $html = $this->_elementTemplates[$element->getElementTemplateType()]; |
291 | }else{ |
292 | $html = $this->_elementTemplates['default']; |
293 | |
294 | } |
80f962df |
295 | $html =str_replace('{type}', $element->getType(), $html); |
da6f8763 |
296 | if (method_exists($element, 'getHelpButton')){ |
297 | $html=str_replace('{help}', $element->getHelpButton(), $html); |
298 | }else{ |
299 | $html=str_replace('{help}', '', $html); |
300 | |
301 | } |
302 | $this->_templates[$element->getName()]=$html; |
303 | |
304 | parent::renderElement($element, $required, $error); |
305 | } |
306 | |
307 | |
308 | } |
309 | |
42f248e6 |
310 | /*class moodleform_filter{ |
da6f8763 |
311 | var $paramtype; |
312 | var $default; |
42f248e6 |
313 | function moodleform_filter($paramtype, $default=NULL){ |
da6f8763 |
314 | $this->paramtype=$paramtype; |
315 | $this->default=$default; |
316 | } |
317 | function required_param($value){ |
318 | if (isset($value)) { |
319 | $param = $value; |
320 | } else { |
321 | error('A required parameter was missing'); |
322 | } |
323 | |
324 | return $this->clean_param($param); |
325 | } |
326 | function optional_param($value){ |
327 | if (!isset($value)) { |
328 | return $this->default; |
329 | } |
330 | return $this->clean_param($value); |
331 | } |
42f248e6 |
332 | |
333 | } */ |
da6f8763 |
334 | |
335 | $GLOBALS['_HTML_QuickForm_default_renderer']=& new moodleform_renderer(); |
336 | |
337 | moodleform::registerElementType('checkbox', "$CFG->libdir/form/checkbox.php", 'moodleform_checkbox'); |
338 | moodleform::registerElementType('file', "$CFG->libdir/form/file.php", 'moodleform_file'); |
339 | moodleform::registerElementType('group', "$CFG->libdir/form/group.php", 'moodleform_group'); |
340 | moodleform::registerElementType('password', "$CFG->libdir/form/password.php", 'moodleform_password'); |
341 | moodleform::registerElementType('radio', "$CFG->libdir/form/radio.php", 'moodleform_radio'); |
342 | moodleform::registerElementType('select', "$CFG->libdir/form/select.php", 'moodleform_select'); |
343 | moodleform::registerElementType('text', "$CFG->libdir/form/text.php", 'moodleform_text'); |
344 | moodleform::registerElementType('textarea', "$CFG->libdir/form/textarea.php", 'moodleform_textarea'); |
345 | moodleform::registerElementType('date_selector', "$CFG->libdir/form/dateselector.php", 'moodleform_date_selector'); |
346 | moodleform::registerElementType('htmleditor', "$CFG->libdir/form/htmleditor.php", 'moodleform_htmleditor'); |
f8857b49 |
347 | moodleform::registerElementType('static', "$CFG->libdir/form/static.php", 'moodleform_static'); |
42f248e6 |
348 | moodleform::registerElementType('hidden', "$CFG->libdir/form/hidden.php", 'moodleform_hidden'); |
da6f8763 |
349 | |
864cc1de |
350 | |
da6f8763 |
351 | ?> |