expiry.php 8.5 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>
Richard Mansfield's avatar
Richard Mansfield committed
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
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']) . '"';
Nigel McNie's avatar
Nigel McNie committed
72
    $numberinput .= (isset($element['error']) ? ' class="error"' : '') . ">\n";
Richard Mansfield's avatar
Richard Mansfield committed
73

Nigel McNie's avatar
Nigel McNie committed
74
    $uselect = '<select onchange="' . $name . '_change()" ';
75
    $uselect .= 'name="' . $name . '_units" id="' . $formname . '_' . $name . '_units"' . ' tabindex="' . Pieform::hsc($element['tabindex']) . "\">\n";
Nigel McNie's avatar
Nigel McNie committed
76
77
78
79
80
81
82
83
    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
84
85
86
    }
    $uselect .= "</select>\n";

Nigel McNie's avatar
Nigel McNie committed
87
    // Make sure the input is disabled if "no end date" is selected
Nigel McNie's avatar
Nigel McNie committed
88
    $script = <<<EOJS
Richard Mansfield's avatar
Richard Mansfield committed
89
90
<script type="text/javascript" language="javascript">
function {$name}_change() {
Nigel McNie's avatar
Nigel McNie committed
91
92
    if ($('{$formname}_{$name}_units').value == 'noenddate') {
        $('{$formname}_{$name}').disabled = true;
93
94
    }
    else {
Nigel McNie's avatar
Nigel McNie committed
95
        $('{$formname}_{$name}').disabled = false;
96
    }
Richard Mansfield's avatar
Richard Mansfield committed
97
98
99
100
}
</script>
EOJS;

Nigel McNie's avatar
Nigel McNie committed
101
    return $numberinput . $uselect . $script;
102
}/*}}}*/
Richard Mansfield's avatar
Richard Mansfield committed
103

Nigel McNie's avatar
Nigel McNie committed
104
105
106
107
108
109
110
/**
 * 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
 */
111
function pieform_element_expiry_get_value(Pieform $form, $element) {/*{{{*/
Nigel McNie's avatar
Nigel McNie committed
112
113
114
115
116
117
118
119
120
121
122
123
    $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);
124
}/*}}}*/
Nigel McNie's avatar
Nigel McNie committed
125

126
function pieform_element_expiry_i18n() {/*{{{*/
Nigel McNie's avatar
Nigel McNie committed
127
128
129
130
131
132
133
    return array(
        'en.utf8' => array(
            'days'      => 'Days',
            'weeks'     => 'Weeks',
            'months'    => 'Months',
            'years'     => 'Years',
            'noenddate' => 'No end date'
134
135
136
137
138
139
140
        ),
        'de.utf8' => array(
            'days'      => 'Tage',
            'weeks'     => 'Wochen',
            'months'    => 'Monate',
            'years'     => 'Jahre',
            'noenddate' => 'kein Endedatum'
141
142
143
144
145
146
147
148
        ),
        'fr.utf8' => array(
            'days'      => 'Jours',
            'weeks'     => 'Semaines',
            'months'    => 'Mois',
            'years'     => 'Années',
            'noenddate' => 'Pas de date de fin'
        ),
149
150
151
152
153
154
155
        'ja.utf8' => array(
            'days'      => '日',
            'weeks'     => '週',
            'months'    => '月',
            'years'     => '年',
            'noenddate' => '終了日なし'
        ),
156
157
158
159
160
161
162
        'es.utf8' => array(
            'days'      => 'Días',
            'weeks'     => 'Semanas',
            'months'    => 'Meses',
            'years'     => 'Años',
            'noenddate' => 'Sin fecha de finalización'
        ),
163
164
165
166
167
168
169
        'sl.utf8' => array(
            'days'      => 'dni',
            'weeks'     => 'tednov',
            'months'    => 'mesecev',
            'years'     => 'let',
            'noenddate' => 'ni zaključnega datuma'
        ),
170
171
172
173
174
175
176
        'nl.utf8' => array(
            'days'      => 'Dagen',
            'weeks'     => 'Weken',
            'months'    => 'Maanden',
            'years'     => 'Jaren',
            'noenddate' => 'Geen einddatum'
        ),
177
178
179
180
181
182
183
        'cs.utf8' => array(
            'days'      => 'dnů',
            'weeks'     => 'týdnů',
            'months'    => 'měsíců',
            'years'     => 'roků',
            'noenddate' => 'nikdy'
        ),
184

Nigel McNie's avatar
Nigel McNie committed
185
    );
186
}/*}}}*/
Nigel McNie's avatar
Nigel McNie committed
187

188
function pieform_element_expire_get_expiry_units() {/*{{{*/
Nigel McNie's avatar
Nigel McNie committed
189
    return array('days', 'weeks', 'months', 'years', 'noenddate');
190
}/*}}}*/
Richard Mansfield's avatar
Richard Mansfield committed
191

192
function pieform_element_expiry_seconds_in($unit) {/*{{{*/
Richard Mansfield's avatar
Richard Mansfield committed
193
194
    $dayseconds = 60 * 60 * 24;
    switch ($unit) {
Nigel McNie's avatar
Nigel McNie committed
195
196
197
198
199
        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
200
    }
201
}/*}}}*/
Richard Mansfield's avatar
Richard Mansfield committed
202

203
function pieform_element_expiry_get_expiry_from_seconds($seconds) {/*{{{*/
Richard Mansfield's avatar
Richard Mansfield committed
204
205
206
207
208
    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
209
    $yearseconds = pieform_element_expiry_seconds_in('years');
Richard Mansfield's avatar
Richard Mansfield committed
210
211
212
    if ($seconds % $yearseconds == 0 && $seconds > 0) {
        return array('number' => (int) ($seconds / $yearseconds), 'units' => 'years');
    }
Nigel McNie's avatar
Nigel McNie committed
213
    $monthseconds = pieform_element_expiry_seconds_in('months');
Richard Mansfield's avatar
Richard Mansfield committed
214
215
216
    if ($seconds % $monthseconds == 0 && $seconds > 0) {
        return array('number' => (int) ($seconds / $monthseconds), 'units' => 'months');
    }
Nigel McNie's avatar
Nigel McNie committed
217
    $weekseconds = pieform_element_expiry_seconds_in('weeks');
Richard Mansfield's avatar
Richard Mansfield committed
218
219
220
    if ($seconds % $weekseconds == 0 && $seconds > 0) {
        return array('number' => (int) ($seconds / $weekseconds), 'units' => 'weeks');
    }
Nigel McNie's avatar
Nigel McNie committed
221
    $dayseconds = pieform_element_expiry_seconds_in('days');
Richard Mansfield's avatar
Richard Mansfield committed
222
223
224
225
    if ($seconds % $dayseconds == 0) {
        return array('number' => (int) ($seconds / $dayseconds), 'units' => 'days');
    }
    return null;
226
}/*}}}*/
Richard Mansfield's avatar
Richard Mansfield committed
227
228

?>