cancel.php 2.08 KB
Newer Older
1
2
<?php
/**
3
 * This program is part of Pieforms
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 *  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
 *
19
20
 * @package    pieform
 * @subpackage element
21
22
 * @author     Nigel McNie <nigel@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006 Catalyst IT Ltd http://catalyst.net.nz
24
25
26
27
 *
 */

/**
28
 * Renders a "cancel" button. Custom buttons are rendered nearly the same as
29
30
 * normal submit buttons, only their name is changed (for use by the Pieform
 * class internally).
31
 *
32
 * @param Pieform  $form    The form to render the element for
Nigel McNie's avatar
Nigel McNie committed
33
 * @param array    $element The element to render
34
 * @return string           The HTML for the element
35
 */
Nigel McNie's avatar
Nigel McNie committed
36
function pieform_element_cancel(Pieform $form, $element) {
37
38
39
40
    if (!isset($element['value'])) {
        throw new PieformException('Cancel elements must have a value');
    }

41
42
43
44
    if (isset($element['confirm'])) {
        $element['onclick'] = 'return confirm(' . json_encode($element['confirm']) . ');';
    }

45
    $attributes = $form->element_attributes($element);
46
47
    $attributes = preg_replace('/name="(.*)"/', 'name="cancel_$1"', $attributes);
    $attributes = preg_replace('/id="(.*)"/', 'id="cancel_$1"', $attributes);
48
    return '<input type="submit"'
49
        . $attributes
50
        . ' value="' . Pieform::hsc($element['value']) . '">';
51
52
}

Nigel McNie's avatar
Nigel McNie committed
53
54
55
function pieform_element_cancel_set_attributes($element) {
    $element['cancelelement'] = true;
    return $element;
56
57
}

58
?>