Commit 63316dcc authored by Robert Lyon's avatar Robert Lyon Committed by Bob Lyon
Browse files

Bug 1680640: Allow multiple submit buttons from one pieform element



Calling it multisubmit where we pass in an arrays of options/values so
that it sets up a bunch of 'submit' buttons that all flow thru the
same [formname]_submit function and be handled there.

This allows the submit buttons to 'live together' on the page and in
the code.

behatnotneeded

Change-Id: I16864d46ff3ea6bc0cb89e7769ba4caa5c20e53e
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent ac7d4ab4
......@@ -124,19 +124,15 @@ $invited = get_column('usr_institution_request', 'institution', 'usr', $USER->id
if (!empty($invited)) {
$elements = array();
foreach ($invited as $i) {
$elements[] = array(
'type' => 'submit',
'name' => '_confirminvite_' . $i,
$elements[$i] = array(
'type' => 'multisubmit',
'name' => 'invite_' . $i,
'options' => array('confirm', 'decline'),
'primarychoice' => 'confirm',
'title' => get_string('youhavebeeninvitedtojoin', 'mahara',
$institutions[$i]->displayname),
'class' => 'btn-default',
'value' => get_string('joininstitution')
);
$elements[] = array(
'type' => 'submit',
'name' => '_declineinvite_' . $i,
'class' => 'btn-default',
'value' => get_string('decline')
'value' => array(get_string('joininstitution'), get_string('decline'))
);
unset($institutions[$i]);
}
......@@ -147,33 +143,34 @@ if (!empty($invited)) {
'pluginname' => 'account',
'elements' => $elements
));
} else {
}
else {
$invitedform = null;
}
function confirminvite_submit(Pieform $form, $values) {
global $USER;
foreach ($values as $k => $v) {
if (preg_match('/^\_confirminvite\_([a-z0-9]+)$/', $k, $m)) {
if (preg_match('/^invite\_([a-z0-9]+)$/', $k, $m)) {
$institution = $m[1];
if (count_records('usr_institution_request', 'usr', $USER->id,
'institution', $institution, 'confirmedinstitution', 1)) {
$USER->join_institution($institution);
if ($v == 'confirm') {
if (count_records('usr_institution_request', 'usr', $USER->id,
'institution', $institution, 'confirmedinstitution', 1)) {
$USER->join_institution($institution);
break;
}
}
else if ($v == 'decline') {
delete_records('usr_institution_request', 'usr', $USER->id,
'institution', $institution, 'confirmedinstitution', 1);
break;
}
}
if (preg_match('/^\_declineinvite\_([a-z0-9]+)$/', $k, $m)) {
$institution = $m[1];
delete_records('usr_institution_request', 'usr', $USER->id,
'institution', $institution, 'confirmedinstitution', 1);
break;
}
}
redirect(get_config('wwwroot') . 'account/institutions.php');
}
foreach ($institutions as $k => $i) {
if ($i->name == 'mahara' || !$i->registerallowed) {
unset($institutions[$k]);
......
<?php
/**
* Pieforms: Advanced web forms made easy
* Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* @package pieform
* @subpackage element
* @author Nigel McNie <nigel@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
/**
* Renders a set of submit buttons. Useful if you have multiple choices for a decision that go
* to different form update places.
*
* @param Pieform $form The form to render the element for
* @param array $element The element to render
* @return string The HTML for the element
*/
function pieform_element_multisubmit(Pieform $form, $element) {
if (!isset($element['value']) || !is_array($element['value']) || count($element['value']) != 2) {
throw new PieformException('The multisubmit element "' . $element['name']
. '" must have a two element array for its value otherwise use "submit" element');
}
$form->include_plugin('element', 'button');
$form->include_plugin('element', 'submit');
// first try for string indices
$plugins = array('button');
$elems = '';
foreach ($element['options'] as $key => $value) {
if (!is_numeric($key) && in_array($key, $plugins)) {
$function = 'pieform_element_' . $key;
}
else {
// default to 'submit' element
$function = 'pieform_element_submit';
}
if (function_exists($function)) {
$item = $element;
$item['class'] = isset($element['class']) ? $element['class'] . ' ' . $key : $key;
// A primary choice will give the submit option the btn-primary class
if (isset($element['primarychoice']) && $element['primarychoice'] === $value) {
$item['class'] .= ' btn-primary';
}
$item['usebuttontag'] = ($key == 'button') ? true : false;
$item['value'] = $element['value'][$key];
$item['name'] = $element['name'] . '[' . $key .']';
$item['option'] = $element['options'][$key];
if (isset($element['confirm']) && isset($element['confirm'][$key])) {
$item['confirm'] = $element['confirm'][$key];
}
else {
unset($item['confirm']);
}
$elems .= $function($form, $item);
$elems .= ' ';
}
}
if (!empty($elems)) {
return $elems;
}
}
function pieform_element_multisubmit_set_attributes($element) {
$element['submitelement'] = true;
return $element;
}
function pieform_element_multisubmit_get_value(Pieform $form, $element) {
if (!empty($_POST[$element['name']])) {
return $element['options'][key($_POST[$element['name']])];
}
else if (is_array($element['value'])) {
return $element['value'][0];
}
else {
return $element['value'];
}
}
Supports Markdown
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