expiry.php 7.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
38
39
40
41
42
    $result = '';
    $name = $element['name'];
    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
    }
Nigel McNie's avatar
Nigel McNie committed
50
    else if (isset($global[$element['name']])
Richard Mansfield's avatar
Richard Mansfield committed
51
             && isset($global[$element['name'] . '_units'])) {
Nigel McNie's avatar
Nigel McNie committed
52
        $values = array('number' => $global[$element['name']],
Richard Mansfield's avatar
Richard Mansfield committed
53
                        'units'  => $global[$element['name'] . '_units']);
Nigel McNie's avatar
Nigel McNie committed
54
        $seconds = $values['number'] * pieform_element_expiry_seconds_in($values['units']);
Richard Mansfield's avatar
Richard Mansfield committed
55
56
57
    }
    else if (isset($element['defaultvalue'])) {
        $seconds = $element['defaultvalue'];
Nigel McNie's avatar
Nigel McNie committed
58
        $values = pieform_element_expiry_get_expiry_from_seconds($seconds);
Richard Mansfield's avatar
Richard Mansfield committed
59
60
61
62
63
64
    }
    else {
        $values = array('number' => '', 'units' => 'noenddate');
        $seconds = null;
    }

Nigel McNie's avatar
Nigel McNie committed
65
    // @todo probably create with an actual input element, as tabindex doesn't work here for one thing
66
    // Same with the select. And do the events using mochikit signal instead of dom events
Nigel McNie's avatar
Nigel McNie committed
67
    $numberinput = '<input';
Nigel McNie's avatar
Nigel McNie committed
68
69
70
71
    $numberinput .= ($values['units'] == 'noenddate' && empty($element['rules']['required'])) ? ' disabled="disabled"' : '';
    $numberinput .= ' type="text" size="4" name="' . $name . '"';
    $numberinput .= ' id="' . $formname . '_' . $name . '" value="' . $values['number'] . '" tabindex="' . $element['tabindex'] . '"';
    $numberinput .= (isset($element['error']) ? ' class="error"' : '') . ">\n";
Richard Mansfield's avatar
Richard Mansfield committed
72

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

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

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

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

125
function pieform_element_expiry_i18n() {/*{{{*/
Nigel McNie's avatar
Nigel McNie committed
126
127
128
129
130
131
132
    return array(
        'en.utf8' => array(
            'days'      => 'Days',
            'weeks'     => 'Weeks',
            'months'    => 'Months',
            'years'     => 'Years',
            'noenddate' => 'No end date'
133
134
135
136
137
138
139
        ),
        'de.utf8' => array(
            'days'      => 'Tage',
            'weeks'     => 'Wochen',
            'months'    => 'Monate',
            'years'     => 'Jahre',
            'noenddate' => 'kein Endedatum'
140
141
142
143
144
145
146
147
        ),
        'fr.utf8' => array(
            'days'      => 'Jours',
            'weeks'     => 'Semaines',
            'months'    => 'Mois',
            'years'     => 'Années',
            'noenddate' => 'Pas de date de fin'
        ),
148
149
150
151
152
153
154
        'ja.utf8' => array(
            'days'      => '日',
            'weeks'     => '週',
            'months'    => '月',
            'years'     => '年',
            'noenddate' => '終了日なし'
        ),
Nigel McNie's avatar
Nigel McNie committed
155
    );
156
}/*}}}*/
Nigel McNie's avatar
Nigel McNie committed
157

158
function pieform_element_expire_get_expiry_units() {/*{{{*/
Nigel McNie's avatar
Nigel McNie committed
159
    return array('days', 'weeks', 'months', 'years', 'noenddate');
160
}/*}}}*/
Richard Mansfield's avatar
Richard Mansfield committed
161

162
function pieform_element_expiry_seconds_in($unit) {/*{{{*/
Richard Mansfield's avatar
Richard Mansfield committed
163
164
    $dayseconds = 60 * 60 * 24;
    switch ($unit) {
Nigel McNie's avatar
Nigel McNie committed
165
166
167
168
169
        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
170
    }
171
}/*}}}*/
Richard Mansfield's avatar
Richard Mansfield committed
172

173
function pieform_element_expiry_get_expiry_from_seconds($seconds) {/*{{{*/
Richard Mansfield's avatar
Richard Mansfield committed
174
175
176
177
178
    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
179
    $yearseconds = pieform_element_expiry_seconds_in('years');
Richard Mansfield's avatar
Richard Mansfield committed
180
181
182
    if ($seconds % $yearseconds == 0 && $seconds > 0) {
        return array('number' => (int) ($seconds / $yearseconds), 'units' => 'years');
    }
Nigel McNie's avatar
Nigel McNie committed
183
    $monthseconds = pieform_element_expiry_seconds_in('months');
Richard Mansfield's avatar
Richard Mansfield committed
184
185
186
    if ($seconds % $monthseconds == 0 && $seconds > 0) {
        return array('number' => (int) ($seconds / $monthseconds), 'units' => 'months');
    }
Nigel McNie's avatar
Nigel McNie committed
187
    $weekseconds = pieform_element_expiry_seconds_in('weeks');
Richard Mansfield's avatar
Richard Mansfield committed
188
189
190
    if ($seconds % $weekseconds == 0 && $seconds > 0) {
        return array('number' => (int) ($seconds / $weekseconds), 'units' => 'weeks');
    }
Nigel McNie's avatar
Nigel McNie committed
191
    $dayseconds = pieform_element_expiry_seconds_in('days');
Richard Mansfield's avatar
Richard Mansfield committed
192
193
194
195
    if ($seconds % $dayseconds == 0) {
        return array('number' => (int) ($seconds / $dayseconds), 'units' => 'days');
    }
    return null;
196
}/*}}}*/
Richard Mansfield's avatar
Richard Mansfield committed
197
198

?>