'added styling for textarea in adminsetting pages'
[moodle.git] / lib / formslib.php
CommitLineData
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.
11if (FALSE===strstr(ini_get('include_path'), $CFG->libdir.'/pear' )){
12 ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
13}
14require_once 'HTML/QuickForm.php';
15require_once 'HTML/QuickForm/DHTMLRulesTableless.php';
16require_once 'HTML/QuickForm/Renderer/Tableless.php';
17
18class moodleform extends HTML_QuickForm_DHTMLRulesTableless{
19 /**
20 * Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless
21 * @param string $formName Form's name.
22 * @param string $method (optional)Form's method defaults to 'POST'
23 * @param string $action (optional)Form's action
24 * @param string $target (optional)Form's target defaults to none
25 * @param mixed $attributes (optional)Extra attributes for <form> tag
26 * @param bool $trackSubmit (optional)Whether to track if the form was submitted by adding a special hidden field
27 * @access public
28 */
29 function moodleform($formName='', $method='post', $action='', $target='', $attributes=null){
30 global $CFG;
31 //we need to override the constructor, we don't call the parent constructor
32 //at all because it strips slashes depending on the magic quotes setting
33 //whereas Moodle already has added slashes whether magic quotes is on or not.
34
35 //also added check for sesskey and added sesskey to all forms
36 //and told class to ask Moodle for the max upload file size
37 HTML_Common::HTML_Common($attributes);
38 $method = (strtoupper($method) == 'GET') ? 'get' : 'post';
39 $action = ($action == '') ? $_SERVER['PHP_SELF'] : $action;
40 $target = empty($target) ? array() : array('target' => $target);
41 //no 'name' atttribute for form in xhtml strict :
42 $attributes = array('action'=>$action, 'method'=>$method, 'id'=>$formName) + $target;
43 $this->updateAttributes($attributes);
44 //check for sesskey for this form
45 //if it is not present then we don't accept any input
46 if (isset($_REQUEST['_qf__' . $formName])) {
47 $this->_submitValues = $this->_recursiveFilter('stripslashes', 'get' == $method? $_GET: $_POST);
48 foreach ($_FILES as $keyFirst => $valFirst) {
49 foreach ($valFirst as $keySecond => $valSecond) {
50 if ('name' == $keySecond) {
51 $this->_submitFiles[$keyFirst][$keySecond] = $this->_recursiveFilter('stripslashes', $valSecond);
52 } else {
53 $this->_submitFiles[$keyFirst][$keySecond] = $valSecond;
54 }
55 }
56 }
57
58 $this->_flagSubmitted = count($this->_submitValues) > 0 || count($this->_submitFiles) > 0;
59 }
60
61 //check sesskey
62 if ($this->_flagSubmitted){
63 confirm_sesskey($this->_submitValues['_qf__' . $formName]);
64 }
65 unset($this->_submitValues['_qf__' . $formName]);
66 //add sesskey to all forms
67 $this->addElement('hidden', '_qf__' . $formName, sesskey());
68
69 if (preg_match('/^([0-9]+)([a-zA-Z]*)$/', get_max_upload_file_size(), $matches)) {
70 // see http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
71 switch (strtoupper($matches['2'])) {
72 case 'G':
73 $this->_maxFileSize = $matches['1'] * 1073741824;
74 break;
75 case 'M':
76 $this->_maxFileSize = $matches['1'] * 1048576;
77 break;
78 case 'K':
79 $this->_maxFileSize = $matches['1'] * 1024;
80 break;
81 default:
82 $this->_maxFileSize = $matches['1'];
83 }
84 }
85 //this is custom stuff for Moodle :
86 $oldclass= $this->getAttribute('class');
87 if (!empty($oldclass)){
88 $this->updateAttributes(array('class'=>$oldclass.' mform'));
89 }else {
90 $this->updateAttributes(array('class'=>'mform'));
91 }
92 $this->_helpImageURL="$CFG->wwwroot/lib/form/req.gif";
93 $this->_reqHTML =
94 helpbutton('requiredelement', get_string('requiredelement', 'form'),'moodle',
95 true, false, '', true, '<img alt="'.get_string('requiredelement', 'form').'" src="'.
96 $this->_helpImageURL.'" />');
97 $this->setRequiredNote(get_string('denotesreq', 'form', $this->getReqHTML()));
98 }
99 function getReqHTML(){
100 return $this->_reqHTML;
101 }
102 /**
103 * Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless
104 * @param array $buttons An associative array representing help button to attach to
105 * to the form. keys of array correspond to names of elements in form.
106 *
107 * @access public
108 */
109 function add_help_buttons($buttons, $suppresscheck=false){
110
111 foreach ($this->_elements as $no => $element){
112 if (array_key_exists($element->getName(), $buttons)){
113
114 //dynamically create a property so we don't have to modify element
115 //class :
116 $this->_elements[$no]->setHelpButton($buttons[$element->getName()]);
117 unset($buttons[$element->getName()]);
118 }
119
120 }
121 if (count($buttons)&& !$suppresscheck){
122 print_error('nonexistentformelements', 'form', '', join(', ', array_keys($buttons)));
123 }
124 }
125 /**
126 * Applies a data filter for the given field(s)
127 * We can use any PARAM_
128 *
129 * @param mixed $element Form element name or array of such names
130 * @param mixed $filter Callback, either function name or array(&$object, 'method') or PARAM_ integer
131 * @since 2.0
132 * @access public
133 */
134 function applyFilter($element, $filter){
135 if (is_numeric($filter)){
136 $filter=create_function('$value', "clean_param(\$value, $filter);");
137 }
138 parent::applyFilter($element, $filter);
139 }
140 function exportValue($element, $addslashes=true){
141 $unfiltered=parent::exportValue($element);
142 if ($addslashes){
143 return HTML_QuickForm::_recursiveFilter('addslashes',$unfiltered);
144 } else {
145 return $unfiltered;
146 }
147 }
148 function exportValues($elementList, $addslashes=true){
149 $unfiltered=parent::exportValues($elementList);
150 if ($addslashes){
151 return HTML_QuickForm::_recursiveFilter('addslashes',$unfiltered);
152 } else {
153 return $unfiltered;
154 }
155 }
156}
157
158/**
159 * A renderer for moodleform that only uses XHTML and CSS and no
160 * table tags, extends PEAR class HTML_QuickForm_Renderer_Tableless
161 *
162 * Stylesheet is part of standard theme and should be automatically included.
163 *
164 * @author Jamie Pratt <me@jamiep.org>
165 * @license gpl license
166 */
167class moodleform_renderer extends HTML_QuickForm_Renderer_Tableless{
168
169 /**
170 * Element template array
171 * @var array
172 * @access private
173 */
174 var $_elementTemplates;
175 var $_htmleditors=array();
176 function moodleform_renderer(){
177 $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 -->\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></div><br />",
178 'wide'=>"\n\t\t<div class=\"qfrow\"><label class=\"qflabel\">{label}{help}<!-- BEGIN required -->{req}<!-- END required --></label><br /><div class=\"qfelementwide<!-- BEGIN error --> error<!-- END error -->\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</span></div><br />");
179
180 parent::HTML_QuickForm_Renderer_Tableless();
181 }
182 function startForm(&$form){
183 $this->_reqHTML=$form->getReqHTML();
184 $this->_elementTemplates=str_replace('{req}', $this->_reqHTML, $this->_elementTemplates);
185 parent::startForm($form);
186 }
187 function startGroup(&$group, $required, $error){
188 if (method_exists($group, 'getElementTemplateType')){
189 $html = $this->_elementTemplates[$element->getElementTemplateType()];
190 }else{
191 $html = $this->_elementTemplates['default'];
192
193 }
194 if (method_exists($group, 'getHelpButton')){
195 $html =str_replace('{help}', $group->getHelpButton(), $html);
196 }else{
197 $html =str_replace('{help}', '', $html);
198
199 }
200 $this->_templates[$group->getName()]=$html;
201 // Fix for bug in tableless quickforms that didn't allow you to stop a
202 // fieldset before a group of elements.
203 // if the element name indicates the end of a fieldset, close the fieldset
204 if ( in_array($group->getName(), $this->_stopFieldsetElements)
205 && $this->_fieldsetsOpen > 0
206 ) {
207 $this->_html .= $this->_closeFieldsetTemplate;
208 $this->_fieldsetsOpen--;
209 }
210 parent::startGroup($group, $required, $error);
211 }
212 function renderElement(&$element, $required, $error){
213 if (method_exists($element, 'getElementTemplateType')){
214 $html = $this->_elementTemplates[$element->getElementTemplateType()];
215 }else{
216 $html = $this->_elementTemplates['default'];
217
218 }
219 if (method_exists($element, 'getHelpButton')){
220 $html=str_replace('{help}', $element->getHelpButton(), $html);
221 }else{
222 $html=str_replace('{help}', '', $html);
223
224 }
225 $this->_templates[$element->getName()]=$html;
226
227 parent::renderElement($element, $required, $error);
228 }
229
230
231}
232
233class moodleform_filter{
234 var $paramtype;
235 var $default;
236 function moodleform_filter($paramtype, $default){
237 $this->paramtype=$paramtype;
238 $this->default=$default;
239 }
240 function required_param($value){
241 if (isset($value)) {
242 $param = $value;
243 } else {
244 error('A required parameter was missing');
245 }
246
247 return $this->clean_param($param);
248 }
249 function optional_param($value){
250 if (!isset($value)) {
251 return $this->default;
252 }
253 return $this->clean_param($value);
254 }
255 function clean_param($value){
256 //clean param expects vars with slashes
257 $value=HTML_QuickForm::_recursiveFilter('addslashes', $value);
258 $value=clean_param($value, $this->paramtype);
259 return HTML_QuickForm::_recursiveFilter('stripslashes', $value);
260 }
261}
262
263$GLOBALS['_HTML_QuickForm_default_renderer']=& new moodleform_renderer();
264
265moodleform::registerElementType('checkbox', "$CFG->libdir/form/checkbox.php", 'moodleform_checkbox');
266moodleform::registerElementType('file', "$CFG->libdir/form/file.php", 'moodleform_file');
267moodleform::registerElementType('group', "$CFG->libdir/form/group.php", 'moodleform_group');
268moodleform::registerElementType('password', "$CFG->libdir/form/password.php", 'moodleform_password');
269moodleform::registerElementType('radio', "$CFG->libdir/form/radio.php", 'moodleform_radio');
270moodleform::registerElementType('select', "$CFG->libdir/form/select.php", 'moodleform_select');
271moodleform::registerElementType('text', "$CFG->libdir/form/text.php", 'moodleform_text');
272moodleform::registerElementType('textarea', "$CFG->libdir/form/textarea.php", 'moodleform_textarea');
273moodleform::registerElementType('date_selector', "$CFG->libdir/form/dateselector.php", 'moodleform_date_selector');
274moodleform::registerElementType('htmleditor', "$CFG->libdir/form/htmleditor.php", 'moodleform_htmleditor');
275
276if ($CFG->debug >= DEBUG_ALL){
277 PEAR::setErrorHandling(PEAR_ERROR_PRINT);
278}
279?>