Brushed up theme oceanblue.
[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
864cc1de 18if ($CFG->debug >= DEBUG_ALL){
19 PEAR::setErrorHandling(PEAR_ERROR_PRINT);
20}
21
da6f8763 22class 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 {
94 $this->updateAttributes(array('class'=>'mform'));
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 */
113 function add_help_buttons($buttons, $suppresscheck=false){
114
115 foreach ($this->_elements as $no => $element){
116 if (array_key_exists($element->getName(), $buttons)){
117
118 //dynamically create a property so we don't have to modify element
119 //class :
120 $this->_elements[$no]->setHelpButton($buttons[$element->getName()]);
121 unset($buttons[$element->getName()]);
122 }
123
124 }
125 if (count($buttons)&& !$suppresscheck){
126 print_error('nonexistentformelements', 'form', '', join(', ', array_keys($buttons)));
127 }
128 }
129 /**
130 * Applies a data filter for the given field(s)
131 * We can use any PARAM_
132 *
133 * @param mixed $element Form element name or array of such names
134 * @param mixed $filter Callback, either function name or array(&$object, 'method') or PARAM_ integer
135 * @since 2.0
136 * @access public
137 */
138 function applyFilter($element, $filter){
139 if (is_numeric($filter)){
140 $filter=create_function('$value', "clean_param(\$value, $filter);");
141 }
142 parent::applyFilter($element, $filter);
143 }
144 function exportValue($element, $addslashes=true){
145 $unfiltered=parent::exportValue($element);
146 if ($addslashes){
147 return HTML_QuickForm::_recursiveFilter('addslashes',$unfiltered);
148 } else {
149 return $unfiltered;
150 }
151 }
152 function exportValues($elementList, $addslashes=true){
153 $unfiltered=parent::exportValues($elementList);
154 if ($addslashes){
155 return HTML_QuickForm::_recursiveFilter('addslashes',$unfiltered);
156 } else {
157 return $unfiltered;
158 }
159 }
160}
161
162/**
163 * A renderer for moodleform that only uses XHTML and CSS and no
164 * table tags, extends PEAR class HTML_QuickForm_Renderer_Tableless
165 *
166 * Stylesheet is part of standard theme and should be automatically included.
167 *
168 * @author Jamie Pratt <me@jamiep.org>
169 * @license gpl license
170 */
171class moodleform_renderer extends HTML_QuickForm_Renderer_Tableless{
172
173 /**
174 * Element template array
175 * @var array
176 * @access private
177 */
178 var $_elementTemplates;
179 var $_htmleditors=array();
180 function moodleform_renderer(){
864cc1de 181 $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>",
182 '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>");
da6f8763 183
184 parent::HTML_QuickForm_Renderer_Tableless();
185 }
186 function startForm(&$form){
187 $this->_reqHTML=$form->getReqHTML();
188 $this->_elementTemplates=str_replace('{req}', $this->_reqHTML, $this->_elementTemplates);
189 parent::startForm($form);
190 }
191 function startGroup(&$group, $required, $error){
192 if (method_exists($group, 'getElementTemplateType')){
193 $html = $this->_elementTemplates[$element->getElementTemplateType()];
194 }else{
195 $html = $this->_elementTemplates['default'];
196
197 }
198 if (method_exists($group, 'getHelpButton')){
199 $html =str_replace('{help}', $group->getHelpButton(), $html);
200 }else{
201 $html =str_replace('{help}', '', $html);
202
203 }
204 $this->_templates[$group->getName()]=$html;
205 // Fix for bug in tableless quickforms that didn't allow you to stop a
206 // fieldset before a group of elements.
207 // if the element name indicates the end of a fieldset, close the fieldset
208 if ( in_array($group->getName(), $this->_stopFieldsetElements)
209 && $this->_fieldsetsOpen > 0
210 ) {
211 $this->_html .= $this->_closeFieldsetTemplate;
212 $this->_fieldsetsOpen--;
213 }
214 parent::startGroup($group, $required, $error);
215 }
216 function renderElement(&$element, $required, $error){
217 if (method_exists($element, 'getElementTemplateType')){
218 $html = $this->_elementTemplates[$element->getElementTemplateType()];
219 }else{
220 $html = $this->_elementTemplates['default'];
221
222 }
223 if (method_exists($element, 'getHelpButton')){
224 $html=str_replace('{help}', $element->getHelpButton(), $html);
225 }else{
226 $html=str_replace('{help}', '', $html);
227
228 }
229 $this->_templates[$element->getName()]=$html;
230
231 parent::renderElement($element, $required, $error);
232 }
233
234
235}
236
237class moodleform_filter{
238 var $paramtype;
239 var $default;
240 function moodleform_filter($paramtype, $default){
241 $this->paramtype=$paramtype;
242 $this->default=$default;
243 }
244 function required_param($value){
245 if (isset($value)) {
246 $param = $value;
247 } else {
248 error('A required parameter was missing');
249 }
250
251 return $this->clean_param($param);
252 }
253 function optional_param($value){
254 if (!isset($value)) {
255 return $this->default;
256 }
257 return $this->clean_param($value);
258 }
259 function clean_param($value){
260 //clean param expects vars with slashes
261 $value=HTML_QuickForm::_recursiveFilter('addslashes', $value);
262 $value=clean_param($value, $this->paramtype);
263 return HTML_QuickForm::_recursiveFilter('stripslashes', $value);
264 }
265}
266
267$GLOBALS['_HTML_QuickForm_default_renderer']=& new moodleform_renderer();
268
269moodleform::registerElementType('checkbox', "$CFG->libdir/form/checkbox.php", 'moodleform_checkbox');
270moodleform::registerElementType('file', "$CFG->libdir/form/file.php", 'moodleform_file');
271moodleform::registerElementType('group', "$CFG->libdir/form/group.php", 'moodleform_group');
272moodleform::registerElementType('password', "$CFG->libdir/form/password.php", 'moodleform_password');
273moodleform::registerElementType('radio', "$CFG->libdir/form/radio.php", 'moodleform_radio');
274moodleform::registerElementType('select', "$CFG->libdir/form/select.php", 'moodleform_select');
275moodleform::registerElementType('text', "$CFG->libdir/form/text.php", 'moodleform_text');
276moodleform::registerElementType('textarea', "$CFG->libdir/form/textarea.php", 'moodleform_textarea');
277moodleform::registerElementType('date_selector', "$CFG->libdir/form/dateselector.php", 'moodleform_date_selector');
278moodleform::registerElementType('htmleditor', "$CFG->libdir/form/htmleditor.php", 'moodleform_htmleditor');
279
864cc1de 280
da6f8763 281?>