expiry.php 7.12 KB
Newer Older
Richard Mansfield's avatar
Richard Mansfield committed
1
2
<?php
/**
3
4
 * Pieforms: Advanced web forms made easy
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
Richard Mansfield's avatar
Richard Mansfield committed
5
 *
6
7
8
9
 * 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.
Richard Mansfield's avatar
Richard Mansfield committed
10
 *
11
12
13
14
 * 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.
Richard Mansfield's avatar
Richard Mansfield committed
15
 *
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Richard Mansfield's avatar
Richard Mansfield committed
18
 *
19
20
21
 * @package    pieform
 * @subpackage element
 * @author     Richard Mansfield <richard.mansfield@catalyst.net.nz>
22
23
 * @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.
Richard Mansfield's avatar
Richard Mansfield committed
24
25
26
27
28
29
30
 *
 */

/**
 * Provides a duration chooser, with a text box for a number and a
 * select box to choose the units, in days, weeks, months, years, or 'no end date'.
 *
Nigel McNie's avatar
Nigel McNie committed
31
32
33
 * @param Pieform $form    The form to render the element for
 * @param array   $element The element to render
 * @return string          The HTML for the element
Richard Mansfield's avatar
Richard Mansfield committed
34
 */
35
function pieform_element_expiry(Pieform $form, $element) {/*{{{*/
Nigel McNie's avatar
Nigel McNie committed
36
    $formname = $form->get_name();
Richard Mansfield's avatar
Richard Mansfield committed
37
    $result = '';
38
    $name = Pieform::hsc($element['name']);
Richard Mansfield's avatar
Richard Mansfield committed
39
40
41
42
    if (!isset($element['defaultvalue'])) {
        $element['defaultvalue'] = null;
    }

Nigel McNie's avatar
Nigel McNie committed
43
    $global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
Richard Mansfield's avatar
Richard Mansfield committed
44

Nigel McNie's avatar
Nigel McNie committed
45
    // Get the value of the element for rendering.
Richard Mansfield's avatar
Richard Mansfield committed
46
47
    if (isset($element['value'])) {
        $seconds = $element['value'];
Nigel McNie's avatar
Nigel McNie committed
48
        $values = pieform_element_expiry_get_expiry_from_seconds($element['value']);
Richard Mansfield's avatar
Richard Mansfield committed
49
    }
50
51
    else if ($form->is_submitted()
             && isset($global[$element['name']])
Richard Mansfield's avatar
Richard Mansfield committed
52
             && isset($global[$element['name'] . '_units'])) {
Nigel McNie's avatar
Nigel McNie committed
53
        $values = array('number' => $global[$element['name']],
Richard Mansfield's avatar
Richard Mansfield committed
54
                        'units'  => $global[$element['name'] . '_units']);
Nigel McNie's avatar
Nigel McNie committed
55
        $seconds = $values['number'] * pieform_element_expiry_seconds_in($values['units']);
Richard Mansfield's avatar
Richard Mansfield committed
56
57
58
    }
    else if (isset($element['defaultvalue'])) {
        $seconds = $element['defaultvalue'];
Nigel McNie's avatar
Nigel McNie committed
59
        $values = pieform_element_expiry_get_expiry_from_seconds($seconds);
Richard Mansfield's avatar
Richard Mansfield committed
60
61
62
63
64
65
    }
    else {
        $values = array('number' => '', 'units' => 'noenddate');
        $seconds = null;
    }

Nigel McNie's avatar
Nigel McNie committed
66
    // @todo probably create with an actual input element, as tabindex doesn't work here for one thing
67
    // Same with the select. And do the events using mochikit signal instead of dom events
Nigel McNie's avatar
Nigel McNie committed
68
    $numberinput = '<input';
Nigel McNie's avatar
Nigel McNie committed
69
70
    $numberinput .= ($values['units'] == 'noenddate' && empty($element['rules']['required'])) ? ' disabled="disabled"' : '';
    $numberinput .= ' type="text" size="4" name="' . $name . '"';
71
    $numberinput .= ' id="' . $formname . '_' . $name . '" value="' . Pieform::hsc($values['number']) . '" tabindex="' . Pieform::hsc($element['tabindex']) . '"';
72
    if (isset($element['description'])) {
73
        $numberinput .= ' aria-describedby="' . $form->element_descriptors($element) . '"';
74
    }
Nigel McNie's avatar
Nigel McNie committed
75
    $numberinput .= (isset($element['error']) ? ' class="error"' : '') . ">\n";
Naomi Guyer's avatar
Naomi Guyer committed
76
    $uselect = '<label for="' . $formname . '_' . $name . '_units" class="accessible-hidden sr-only">' . get_string('units', 'mahara') . '</label>';
77
    $uselect .= '<select onchange="' . $name . '_change()" ';
78
79
    $uselect .= 'name="' . $name . '_units" id="' . $formname . '_' . $name . '_units"' . ' tabindex="' . Pieform::hsc($element['tabindex']) . '"';
    if (isset($element['description'])) {
80
        $uselect .= ' aria-describedby="' . $form->element_descriptors($element) . '"';
81
82
    }
    $uselect .= ">\n";
Nigel McNie's avatar
Nigel McNie committed
83
84
85
86
87
88
89
90
    foreach (pieform_element_expire_get_expiry_units() as $u) {
        // Don't allow 'no end date' if the element is required
        if ($u == 'noenddate' && !empty($element['rules']['required'])) {
            continue;
        }

        $uselect .= "\t<option value=\"$u\"" . (($values['units'] == $u) ? ' selected="selected"' : '') . '>'
            . $form->i18n('element', 'expiry', $u, $element) . "</option>\n";
Richard Mansfield's avatar
Richard Mansfield committed
91
92
93
    }
    $uselect .= "</select>\n";

Nigel McNie's avatar
Nigel McNie committed
94
    // Make sure the input is disabled if "no end date" is selected
Nigel McNie's avatar
Nigel McNie committed
95
    $script = <<<EOJS
96
<script type="application/javascript" language="javascript">
Richard Mansfield's avatar
Richard Mansfield committed
97
function {$name}_change() {
Nigel McNie's avatar
Nigel McNie committed
98
99
    if ($('{$formname}_{$name}_units').value == 'noenddate') {
        $('{$formname}_{$name}').disabled = true;
100
101
    }
    else {
Nigel McNie's avatar
Nigel McNie committed
102
        $('{$formname}_{$name}').disabled = false;
103
    }
Richard Mansfield's avatar
Richard Mansfield committed
104
105
106
107
}
</script>
EOJS;

Nigel McNie's avatar
Nigel McNie committed
108
    return $numberinput . $uselect . $script;
109
}/*}}}*/
Richard Mansfield's avatar
Richard Mansfield committed
110

Nigel McNie's avatar
Nigel McNie committed
111
112
113
114
115
116
117
/**
 * Gets the value of the expiry element and converts it to a time in seconds.
 *
 * @param Pieform $form    The form the element is attached to
 * @param array   $element The element to get the value for
 * @return int             The number of seconds until expiry
 */
118
function pieform_element_expiry_get_value(Pieform $form, $element) {/*{{{*/
Nigel McNie's avatar
Nigel McNie committed
119
120
121
122
123
124
125
126
127
128
129
130
    $name = $element['name'];
    $global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
    $unit = $global[$name . '_units'];
    if ($unit == 'noenddate') {
        return null;
    }
    $allunits = pieform_element_expire_get_expiry_units();
    $number = $global[$name];
    if (!in_array($unit,$allunits) || $number < 0) {
        return null;
    }
    return $number * pieform_element_expiry_seconds_in($unit);
131
}/*}}}*/
Nigel McNie's avatar
Nigel McNie committed
132

133
function pieform_element_expire_get_expiry_units() {/*{{{*/
Nigel McNie's avatar
Nigel McNie committed
134
    return array('days', 'weeks', 'months', 'years', 'noenddate');
135
}/*}}}*/
Richard Mansfield's avatar
Richard Mansfield committed
136

137
function pieform_element_expiry_seconds_in($unit) {/*{{{*/
Richard Mansfield's avatar
Richard Mansfield committed
138
139
    $dayseconds = 60 * 60 * 24;
    switch ($unit) {
Nigel McNie's avatar
Nigel McNie committed
140
141
142
143
144
        case 'days'   : return $dayseconds;
        case 'weeks'  : return $dayseconds * 7;
        case 'months' : return $dayseconds * 30;
        case 'years'  : return $dayseconds * 365;
        default       : return null;
Richard Mansfield's avatar
Richard Mansfield committed
145
    }
146
}/*}}}*/
Richard Mansfield's avatar
Richard Mansfield committed
147

148
function pieform_element_expiry_get_expiry_from_seconds($seconds) {/*{{{*/
Richard Mansfield's avatar
Richard Mansfield committed
149
150
151
152
153
    if ($seconds == null) {
        return array('number' => '', 'units' => 'noenddate');
    }
    // This needs work to produce sensible values; at the moment it will convert
    // 60 days into 2 months; 70 days into 7 weeks, etc.
Nigel McNie's avatar
Nigel McNie committed
154
    $yearseconds = pieform_element_expiry_seconds_in('years');
Richard Mansfield's avatar
Richard Mansfield committed
155
156
157
    if ($seconds % $yearseconds == 0 && $seconds > 0) {
        return array('number' => (int) ($seconds / $yearseconds), 'units' => 'years');
    }
Nigel McNie's avatar
Nigel McNie committed
158
    $monthseconds = pieform_element_expiry_seconds_in('months');
Richard Mansfield's avatar
Richard Mansfield committed
159
160
161
    if ($seconds % $monthseconds == 0 && $seconds > 0) {
        return array('number' => (int) ($seconds / $monthseconds), 'units' => 'months');
    }
Nigel McNie's avatar
Nigel McNie committed
162
    $weekseconds = pieform_element_expiry_seconds_in('weeks');
Richard Mansfield's avatar
Richard Mansfield committed
163
164
165
    if ($seconds % $weekseconds == 0 && $seconds > 0) {
        return array('number' => (int) ($seconds / $weekseconds), 'units' => 'weeks');
    }
Nigel McNie's avatar
Nigel McNie committed
166
    $dayseconds = pieform_element_expiry_seconds_in('days');
Richard Mansfield's avatar
Richard Mansfield committed
167
168
169
170
    if ($seconds % $dayseconds == 0) {
        return array('number' => (int) ($seconds / $dayseconds), 'units' => 'days');
    }
    return null;
171
}/*}}}*/