Commit 8fc1a77d authored by Martyn Smith's avatar Martyn Smith Committed by Martyn Smith
Browse files
parents 1af8345a b69a7bda
......@@ -95,6 +95,7 @@ class Form {
private $data = array();
private $renderer = 'div';
private $fileupload = false;
private $submitted = false;
private $iscancellable = true;
public static function process($data) {
......@@ -191,6 +192,7 @@ class Form {
// Check if the form was submitted
$global = ($this->method == 'get') ? $_GET: $_POST;
if (isset($global['form_' . $this->name] )) {
$this->submitted = true;
// Check if the form has been cancelled
if ($this->iscancellable) {
foreach ($global as $key => $value) {
......@@ -244,6 +246,10 @@ class Form {
return $this->renderer;
}
function is_submitted() {
return $this->submitted;
}
/**
* Builds the HTML for the form
*
......@@ -326,17 +332,16 @@ class Form {
*/
private function get_submitted_values() {
$result = array();
foreach ($this->get_elements() as $element) {
$elementnames[] = $element['name'];
}
$global = ($this->method == 'get') ? $_GET : $_POST;
foreach ($elementnames as $name) {
if (isset($global[$name])) {
$result[$name] = $global[$name];
foreach ($this->get_elements() as $element) {
if (isset($global[$element['name']])) {
$result[$element['name']] = $global[$element['name']];
}
else if ($element['type'] == 'file' && isset($_FILES[$element['name']])) {
$result[$element['name']] = $_FILES[$element['name']];
}
else {
$result[$name] = null;
$result[$element['name']] = null;
}
}
return $result;
......@@ -524,6 +529,4 @@ function form_render_element($element, $form) {
}
function hsc ($text) { return htmlspecialchars($text, ENT_COMPAT, 'UTF-8'); }
?>
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage form
* @author Nigel McNie <nigel@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
/**
* Provides a basic text field input.
*/
function form_render_checkbox($element, $form) {
$checked = false;
if (!empty($element['value'])) {
$checked = true;
}
if ($form->get_value($element)) {
$checked = true;
}
else if (!$form->is_submitted() && !empty($element['checked'])) {
$checked = true;
}
return '<input type="checkbox"'
. Form::element_attributes($element)
. ($checked ? ' checked="checked"' : '')
. '>';
}
?>
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage form
* @author Nigel McNie <nigel@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
/**
* Renders a set of radio buttons for a form
*
* @param array $element The element to render
* @param Form $form The form the element is being rendered on
*/
function form_render_radio($element, Form $form) {
if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) {
log_warn('Radio elements should have at least one option');
}
$result = '';
$form_value = $form->get_value($element);
$id = $element['id'];
foreach ($element['options'] as $value => $text) {
$uid = $id . substr(md5(microtime()), 0, 4);
$element['id'] = $uid;
$result .= '<input type="radio"'
. Form::element_attributes($element)
. ' value="' . hsc($value) . '"'
. (($form_value == $value) ? ' checked="checked"' : '')
. "> <label for=\"$uid\">" . hsc($text) . "</label>\n";
}
return $result;
}
?>
......@@ -34,7 +34,7 @@ function form_render_select($element, $form) {
. Form::element_attributes($element)
. (!empty($element['multiple']) ? ' multiple="multiple"' : '')
. ">\n";
if (!is_array($element['options']) || count($element['options']) < 1) {
if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) {
$result .= "\t<option></option>\n";
log_warn('Select elements should have at least one option');
}
......
......@@ -27,7 +27,7 @@
defined('INTERNAL') || die();
function form_rule_required($field) {
if ($field == '') {
if ($field == '' || is_array($field) && !empty($field['error'])) {
return get_string('This field is required');
}
}
......
......@@ -568,6 +568,16 @@ function handle_event($event) {
}
}
/**
* Returns a string, HTML escaped
*
* @param string $text The text to escape
* @return string The text, HTML escaped
*/
function hsc ($text) {
return htmlspecialchars($text, ENT_COMPAT, 'UTF-8');
}
/**
* Used by XMLDB
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment