Commit 7538d3c9 authored by Nigel McNie's avatar Nigel McNie Committed by Nigel McNie
Browse files

Changed most forms to be pieforms 0.2.0 compliant. A _huge_ change! It's

likely some stuff still doesn't work properly...
parent 1f2db621
......@@ -81,6 +81,8 @@ foreach ( $element_list as $element => $type ) {
}
if ($element == 'country') {
$elements[$element]['options'] = getoptions_country();
// @todo configure default country somehow...
$elements[$element]['defaultvalue'] = 'nz';
}
if (isset($profilefields[$element])) {
......@@ -101,7 +103,7 @@ $profileform = pieform(array(
'name' => 'profileform',
'plugintype' => 'artefact',
'pluginname' => 'internal',
'ajaxpost' => true,
'jsform' => true,
'method' => 'post',
'elements' => $elements,
));
......@@ -118,7 +120,7 @@ function profileform_validate(Pieform $form, $values) {
}
}
function profileform_submit($values, Pieform $form) {
function profileform_submit(Pieform $form, $values) {
global $SESSION;
global $USER;
global $element_list;
......
......@@ -8,6 +8,7 @@
<div class="content">
<div class="box-cnrs"><span class="cnr-tl"><span class="cnr-tr"><span class="cnr-bl"><span class="cnr-br">
<div class="maincontent">
<div class="fr"><a href="profileicons.php">{str tag="editprofileicons" section="artefact.internal"} &raquo;</a></div>
<h2>{str section="artefact.internal" tag="profile"}</h2>
{$profileform}
......
......@@ -170,10 +170,6 @@ function auth_setup () {
}
auth_draw_login_page(get_string('sessiontimedout'));
// The auth_draw_login_page function may authenticate a user if a login
// request was sent at the same time that the "timed out" message is to
// be displayed.
//return $USER;
}
else {
// There is no session, so we check to see if one needs to be started.
......@@ -463,13 +459,14 @@ function auth_get_login_form() {
}
$form = array(
'name' => 'login',
'method' => 'post',
'action' => $action,
'plugintype' => 'auth',
'pluginname' => 'internal',
'elements' => $elements,
'iscancellable' => false
'name' => 'login',
'method' => 'post',
'action' => $action,
'plugintype' => 'auth',
'pluginname' => 'internal',
'elements' => $elements,
'dieaftersubmit' => false,
'iscancellable' => false
);
return $form;
......@@ -480,8 +477,8 @@ function auth_get_login_form() {
* javascript is used to detect whether cookies are enabled, and not show the
* login form if they are not.
*
* @param string $form A rendered login form
* @return string The form with extra javascript added for cookie detection
* @param string $form A rendered login form
* @return string The form with extra javascript added for cookie detection
* @private
*/
function get_login_form_js($form) {
......@@ -510,7 +507,7 @@ EOF;
* @param array $values The submitted values
* @access private
*/
function login_submit($values) {
function login_submit(Pieform $form, $values) {
global $SESSION, $USER;
$username = $values['login_username'];
......
......@@ -143,7 +143,10 @@ class Session {
$result = '<div id="messages">';
if (isset($_SESSION['messages'])) {
foreach ($_SESSION['messages'] as $data) {
$result .= '<div class="' . $data['type'] . '">' . $data['msg'] . '</div>';
// @todo probably should be done with MochiKit.Signal
$result .= '<div class="' . $data['type'] . '">';
$result .= '<div class="fr"><a href="" onclick="removeElement(this.parentNode.parentNode);return false;">[X]</a></div>';
$result .= $data['msg'] . '</div>';
}
$_SESSION['messages'] = array();
}
......
......@@ -45,7 +45,7 @@ $contactform = pieform(array(
'name' => 'contactus',
'method' => 'post',
'action' => '',
'ajaxpost' => true,
'jsform' => true,
'elements' => array(
'name' => array(
'type' => 'text',
......@@ -89,7 +89,7 @@ $contactform = pieform(array(
)
));
function contactus_submit($values, Pieform $form) {
function contactus_submit(Pieform $form, $values) {
$data = new StdClass;
$data->fromname = $values['name'];
$data->fromaddress = $values['email'];
......
......@@ -44,7 +44,7 @@ $creategroup = pieform(array(
'type' => 'wysiwyg',
'title' => get_string('groupdescription'),
'rows' => 10,
'cols' => 80,
'cols' => 70,
),
'members' => array(
'type' => 'userlist',
......@@ -69,10 +69,10 @@ function creategroup_validate(Pieform $form, $values) {
}
function creategroup_cancel_submit() {
redirect('./');
redirect('contacts/groups/');
}
function creategroup_submit($values) {
function creategroup_submit(Pieform $form, $values) {
global $USER;
global $SESSION;
......@@ -108,7 +108,7 @@ function creategroup_submit($values) {
db_commit();
redirect('./');
redirect('contacts/groups/');
}
$smarty = smarty();
......
......@@ -36,7 +36,7 @@ $id = param_integer('id',null);
$group_data = get_record('usr_group', 'id', $id, 'owner', $USER->get('id'));
if (!$group_data) {
$SESSION->add_error_msg(get_string('canteditdontown'));
redirect('./');
redirect('contacts/groups/');
}
$group_members = get_column('usr_group_member', 'member', 'grp', $group_data->id);
......@@ -55,7 +55,7 @@ $editgroup = pieform(array(
'type' => 'wysiwyg',
'title' => get_string('groupdescription'),
'rows' => 10,
'cols' => 80,
'cols' => 70,
'defaultvalue' => $group_data->description,
),
'members' => array(
......@@ -90,15 +90,15 @@ function editgroup_validate(Pieform $form, $values) {
if (!$id) {
$SESSION->add_error_msg(get_string('canteditdontown'));
redirect('./');
redirect('contacts/groups/');
}
}
function editgroup_cancel_submit() {
redirect('./');
redirect('contacts/groups/');
}
function editgroup_submit($values) {
function editgroup_submit(Pieform $form, $values) {
global $USER;
global $SESSION;
......@@ -135,7 +135,7 @@ function editgroup_submit($values) {
db_commit();
redirect('./');
redirect('contacts/groups/');
}
$smarty = smarty();
......
......@@ -14,17 +14,68 @@ function get_string(s) {
return str;
}
function global_error_handler(data) {}
function globalErrorHandler(data) {
if (data.returnCode == 3) {
// Logged out!
}
}
// Form related functions
var oldValue = null;
function formStartProcessing(form, btn) {
processingStart();
var button = $(btn);
if (button) {
oldValue = button.value;
button.value = get_string('processingform') + ' ...';
button.disabled = "disabled";
button.style.borderWidth = '1px';
button.blur();
}
}
function formStopProcessing(form, btn) {
processingStop();
var button = $(btn);
if (button) {
button.value = oldValue;
button.disabled = null;
button.style.borderWidth = '2px';
}
}
function formError(form, data) {
var errMsg = DIV({'id': 'messages'}, makeMessage(data.message.message, 'error'));
swapDOM('messages', errMsg);
scrollTo(0, 0);
}
function formSuccess(form, data) {
var yayMsg = DIV({'id': 'messages'}, makeMessage(data.message, 'ok'));
swapDOM('messages', yayMsg);
scrollTo(0, 0);
}
function formGlobalError(form, data) {
globalErrorHandler(data);
}
// End form related functions
// Message related functions
function makeMessage(message, type) {
var a = A({'href': ''}, '[X]');
connect(a, 'onclick', function(e) {
removeElement(a.parentNode.parentNode);
e.stop();
});
return DIV({'class': type}, DIV({'class': 'fr'}, a), message);
}
// Appends a status message to the end of elemid
/* Appends a status message to the end of elemid */
function displayMessage(message, type) {
// ensure we have type 'ok', 'error', or 'info' (the default)
if (!type || (type != 'ok' && type != 'error')) {
type = 'info';
}
var message = DIV({'class':type},message, ' ', A({'style': 'cursor: pointer;', 'onclick':'removeElement(this.parentNode)'},'[X]'));
var message = makeMessage(message, type);
appendChildNodes('messages', message);
// callLater(2, function() {
......@@ -33,7 +84,7 @@ function displayMessage(message, type) {
// });
}
// display a nice little loading notification
/* Display a nice little loading notification */
function processingStart(msg) {
if (!msg) {
msg = get_string('loading');
......@@ -43,13 +94,14 @@ function processingStart(msg) {
$('loading_box'),
DIV(msg)
);
$('loading_box').style.display = 'block';
showElement('loading_box');
}
// hide the loading notification
/* Hide the loading notification */
function processingStop() {
$('loading_box').style.display = 'none';
hideElement('loading_box');
}
// End message related functions
// Function to post a data object to a json script.
function sendjsonrequest(script, data, successcallback, errorcallback) {
......@@ -75,7 +127,7 @@ function sendjsonrequest(script, data, successcallback, errorcallback) {
errtype = 'error';
}
else {
global_error_handler(data);
globalErrorHandler(data);
}
if (errtype) {
displayMessage(data.message,errtype);
......@@ -84,7 +136,7 @@ function sendjsonrequest(script, data, successcallback, errorcallback) {
}
},
function () {
displayMessage(get_string('unknownerror'),'error');
displayMessage(get_string('unknownerror'), 'error');
errorcallback();
processingStop();
});
......@@ -110,7 +162,7 @@ function newfilename(oldname, fileexistsfunc) {
return newname;
}
// Autofocus the first element with a class of 'autofocus' on page load
// Autofocus the first element with a class of 'autofocus' on page load (@todo: move this to pieforms.js)
// Also, connect input elements with the 'emptyonfocus' class to work properly
addLoadEvent(function() {
var element = getFirstElementByTagAndClassName(null, 'autofocus', document.body)
......@@ -121,9 +173,13 @@ addLoadEvent(function() {
forEach(getElementsByTagAndClassName('input', 'emptyonfocus'), function(elem) {
connect(elem, 'onfocus', function(e) { elem.value = ''; e.stop(); });
if (elem.form) {
connect(elem.form, 'onsubmit', function(e) { elem.value = ''; });
}
});
});
// Contextual help
var ctxHelp = new Array();
var ctxHelp_selected;
var container;
......@@ -207,30 +263,32 @@ function contextualHelpOpen(helpName, content) {
appendChildNodes($(helpName + '_container'), help);
}
// End contextual help
// this function gets the cookie, if it exists
function getCookie( name ) {
// Cookie related functions
/* this function gets the cookie, if it exists */
function getCookie(name) {
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if (
( !start ) &&
( name != document.cookie.substring( 0, name.length ) )
(!start) &&
(name != document.cookie.substring(0, name.length))
) {
return null;
}
if ( start == -1 ) {
if (start == -1) {
return null;
}
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) {
if (end == -1) {
end = document.cookie.length;
}
return unescape( document.cookie.substring( len, end ) );
return unescape(document.cookie.substring( len, end ));
}
function clearCookie( name ) {
......@@ -250,8 +308,7 @@ function setCookie( name, value, expires, path, domain, secure )
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
if (expires) {
expires = expires * 1000;
}
......@@ -263,9 +320,10 @@ function setCookie( name, value, expires, path, domain, secure )
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
// End cookie related functions
function toggleChecked(c) {
var e = getElementsByTagAndClassName(null,c);
var e = getElementsByTagAndClassName(null, c);
if (e) {
for (cb in e) {
if (e[cb].checked == true) {
......
......@@ -37,6 +37,7 @@ $string['update'] = 'Update';
$string['change'] = 'Change';
$string['go'] = 'Go';
$string['loading'] = 'Loading ...';
$string['errorprocessingform'] = 'There was an error with submitting this form. Please check the marked fields and try again.';
$string['no'] = 'no';
$string['yes'] = 'yes';
......
......@@ -180,21 +180,22 @@ function log_message ($message, $loglevel, $escape, $backtrace, $file=null, $lin
$method = 'add_error_msg';
}
foreach ($loglines as $line) {
if ($escape) {
$line = htmlspecialchars($line, ENT_COMPAT, 'UTF-8');
$line = str_replace(' ', '&nbsp; ', $line);
}
$line = '<div style="font-family: monospace;">' . $prefix . $line . "</div>\n";
if (is_a($SESSION, 'Session')) {
$SESSION->$method($line, false);
}
else if (!function_exists('get_config') || get_config('installed')) {
// Don't output when we are not installed, since this will cause the
// redirect to the install page to fail.
echo $line;
}
$message = implode("\n", $loglines);
if ($escape) {
$message = htmlspecialchars($message, ENT_COMPAT, 'UTF-8');
$message = str_replace(' ', '&nbsp; ', $message);
}
$message = nl2br($message);
$message = '<div style="font-family: monospace;">' . $prefix . $message . "</div>\n";
if (is_a($SESSION, 'Session')) {
$SESSION->$method($message, false);
}
else if (!function_exists('get_config') || get_config('installed')) {
// Don't output when we are not installed, since this will cause the
// redirect to the install page to fail.
echo $message;
}
if ($backtrace && $htmlbacktrace) {
if (is_a($SESSION, 'Session')) {
$SESSION->add_info_msg($htmlbacktrace, false);
......
......@@ -31,7 +31,7 @@
* @param Pieform $form The form to render the element for
* @return string The HTML for the element
*/
function pieform_render_cancelbackcreate($element, Pieform $form) {
function pieform_element_cancelbackcreate(Pieform $form, $element) {
$form->include_plugin('element', 'submit');
$form->include_plugin('element', 'cancel');
$cancelelement = $element;
......@@ -42,12 +42,12 @@ function pieform_render_cancelbackcreate($element, Pieform $form) {
$backelement['value'] = $element['value'][1];
$submitelement = $element;
$submitelement['value'] = $element['value'][2];
return pieform_render_cancel($cancelelement, $form) . ' ' . pieform_render_submit($backelement, $form)
. ' ' . pieform_render_submit($submitelement, $form);
return pieform_element_cancel($form, $cancelelement) . ' ' . pieform_element_submit($form, $backelement)
. ' ' . pieform_element_submit($form, $submitelement);
}
function pieform_render_cancelbacksubmit_set_attributes($element) {
$element['ajaxmessages'] = true;
function pieform_element_cancelbacksubmit_set_attributes($element) {
$element['submitelement'] = true;
return $element;
}
......
......@@ -33,7 +33,7 @@ defined('INTERNAL') || die();
* @param Pieform $form The form to render the element for
* @return string The HTML for the element
*/
function pieform_render_emaillist($element, Pieform $form) {
function pieform_element_emaillist(Pieform $form, $element) {
$smarty = smarty();
$smarty->left_delimiter = '{{';
......@@ -67,10 +67,10 @@ function pieform_render_emaillist($element, Pieform $form) {
return $smarty->fetch('form/emaillist.tpl');
}
function pieform_get_value_emaillist($element, Pieform $form) {
function pieform_element_emaillist_get_value(Pieform $form, $element) {
$name = $element['name'];
$global = ($form->get_method() == 'get') ? $_GET : $_POST;
$global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
if (!isset($global[$name . '_valid']) || !is_array($global[$name . '_valid'])) {
return null;
......@@ -88,56 +88,4 @@ function pieform_get_value_emaillist($element, Pieform $form) {
return $value;
}
function pieform_get_value_js_emaillist($element, Pieform $form) {
$formname = $form->get_name();
$name = $element['name'];
return <<<EOF
var valid_list = document.forms['$formname'].elements['{$name}_valid\[\]'];
var invalid_list = document.forms['$formname'].elements['{$name}_invalid\[\]'];
data['{$name}_valid\[\]'] = new Array();
data['{$name}_invalid\[\]'] = new Array();
if (valid_list) {
if (valid_list.length) {
for (var i = 0; i < valid_list.length; i++) {
if (valid_list[i].value) {
data['{$name}_valid\[\]'].push(valid_list[i].value);
}
}
}
else {
data['{$name}_valid\[\]'].push(valid_list.value);
}
}
if (invalid_list) {
if (invalid_list.length) {
for (var i = 0; i < invalid_list.length; i++) {
if (invalid_list[i].value) {
data['{$name}_invalid\[\]'].push(invalid_list[i].value);
}
}
}
else {
data['{$name}_invalid\[\]'].push(invalid_list.value);
}
}
var emailselected = document.forms['$formname'].elements['${name}_selected'];
if ( emailselected ) {
if (!emailselected.length) {
emailselected = [emailselected];
}
emailselected = filter(function(elem) { return elem.checked; }, emailselected);
if (emailselected && emailselected[0]) {
data['{$name}_selected'] = emailselected[0].value;
}
}
EOF;
}
?>
......@@ -34,7 +34,7 @@ defined('INTERNAL') || die();
* @param Pieform $form The form to render the element for
* @return string The HTML for the element
*/
function pieform_render_userlist($element, Pieform $form) {
function pieform_element_userlist(Pieform $form, $element) {
$smarty = smarty();
$smarty->left_delimiter = '{{';
......@@ -65,10 +65,10 @@ function pieform_render_userlist($element, Pieform $form) {
return $smarty->fetch('form/userlist.tpl');
}
function pieform_get_value_userlist($element, Pieform $form) {
function pieform_element_userlist_get_value(Pieform $form, $element) {
$name = $element['name'];
$global = ($form->get_method() == 'get') ? $_GET : $_POST;
$global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
if (isset($global[$name])) {
$value = $global[$name];
......@@ -87,7 +87,7 @@ function pieform_get_value_userlist($element, Pieform $form) {
return null;
}
function form_is_empty_userlist($value, $element) {
function form_element_userlist_rule_required($value, $element) {
if (is_array($value) && count($value)) {
return false;
}
......@@ -95,7 +95,7 @@ function form_is_empty_userlist($value, $element) {
return true;
}
function form_render_userlist_set_attributes($element) {
function form_element_userlist_set_attributes($element) {
// By default, use the filter select box
if (!isset($element['filter'])) {
$element['filter'] = true;
......
......@@ -31,7 +31,7 @@
* @param Pieform $form The form to render the element for
* @return string The HTML for the element
*/
function pieform_render_viewacl($element, Pieform $form) {
function pieform_element_viewacl(Pieform $form, $element) {
// @todo addressbook stuff...
$smarty = smarty();
$smarty->left_delimiter = '{{';
......
......@@ -28,11 +28,13 @@
* Renders a textarea, but with extra javascript to turn it into a wysiwyg
* textarea.
*
* @todo support resizable.
*
* @param array $element The element to render
* @param Pieform $form The form to render the element for
* @return string The HTML for the element
*/
function pieform_render_wysiwyg($element, Pieform $form) {
function pieform_element_wysiwyg(Pieform $form, $element) {
global $USER;
if ($USER->get_account_preference('wysiwyg')) {
$element['class'] = isset($element['class']) && $element['class'] !== '' ? $element['class'] . ' wysiwyg' : 'wysiwyg';
......@@ -67,15 +69,11 @@ function pieform_render_wysiwyg($element, Pieform $form) {
. '>' . Pieform::hsc($form->get_value($element)) . '</textarea>';
}