calendar.php 6.18 KB
Newer Older
1
2
<?php
/**
3
4
 * Pieforms: Advanced web forms made easy
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
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.
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.
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/>.
18
19
20
21
22
 *
 * @package    pieform
 * @subpackage element
 * @author     Nigel McNie <nigel@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
24
25
26
27
28
29
30
31
32
 *
 */

/**
 * Provides a javascript calendar for inputting a date.
 *
 * General documentation about the calendar is available at
 * http://www.dynarch.com/demos/jscalendar/doc/html/reference.html
 *
Nigel McNie's avatar
Nigel McNie committed
33
 * @param Pieform $form    The form to render the element for
Nigel McNie's avatar
Nigel McNie committed
34
 * @param array   $element The element to render
Nigel McNie's avatar
Nigel McNie committed
35
 * @return string          The HTML for the element
36
 */
37
function pieform_element_calendar(Pieform $form, $element) {/*{{{*/
38
    $id = $form->get_name() . '_' . $element['name'];
Nigel McNie's avatar
Nigel McNie committed
39
40
41
42
43
44
    $value = $form->get_value($element);
    if ($value) {
        $value = Pieform::hsc(strftime($element['caloptions']['ifFormat'], $value));
    }

    // Build the HTML
45
46
    $result = '<input type="text"'
        . $form->element_attributes($element)
Nigel McNie's avatar
Nigel McNie committed
47
        . ' value="' . $value . '">';
48
    if (isset($element['imagefile'])) {
Nigel McNie's avatar
Nigel McNie committed
49
50
        $result .= '<a href="" id="'. $id . '_btn" onclick="return false;" class="pieform-calendar-toggle"'
            . ' tabindex="' . $element['tabindex'] . '">'
51
52
53
            . '<img src="' . $element['imagefile'] . '" alt=""></a>';
    }
    else {
Nigel McNie's avatar
Nigel McNie committed
54
55
        $result .= '<input type="button" id="' . $id . '_btn" onclick="return false;" class="pieform-calendar-toggle"'
            . ' value="..." tabindex="' . $element['tabindex'] . '">';
56
57
    }

Nigel McNie's avatar
Nigel McNie committed
58
    // Build the configuring javascript
59
60
61
62
63
64
65
66
    $options = array_merge($element['caloptions'], array('inputField' => $id, 'button' => $id . '_btn'));

    $encodedoptions = json_encode($options);
    // Some options are callbacks and need their quoting removed
    foreach (array('dateStatusFunc', 'flatCallback', 'onSelect', 'onClose', 'onUpdate') as $function) {
        $encodedoptions = preg_replace('/("' . $function . '"):"([a-zA-Z0-9$]+)"/', '\1:\2', $encodedoptions);
    }
    $result .= '<script type="text/javascript">Calendar.setup(' . $encodedoptions . ');</script>';
Nigel McNie's avatar
Nigel McNie committed
67

68
    return $result;
69
}/*}}}*/
70

Nigel McNie's avatar
Nigel McNie committed
71
72
73
74
75
76
/**
 * Sets default attributes of the calendar element.
 *
 * @param array $element The element to configure
 * @return array         The configured element
 */
77
function pieform_element_calendar_set_attributes($element) {/*{{{*/
Nigel McNie's avatar
Nigel McNie committed
78
    $element['jsroot']   = isset($element['jsroot']) ? $element['jsroot'] : '';
79
80
    $element['language'] = isset($element['language']) ? $element['language'] : 'en';
    $element['theme']    = isset($element['theme']) ? $element['theme'] : 'calendar-win2k-2';
81
82
    $element['caloptions']['ifFormat'] = isset($element['caloptions']['ifFormat']) ? $element['caloptions']['ifFormat'] : '%Y/%m/%d';
    $element['caloptions']['daFormat'] = isset($element['caloptions']['daFormat']) ? $element['caloptions']['daFormat'] : '%Y/%m/%d';
83
    return $element;
84
}/*}}}*/
85

Nigel McNie's avatar
Nigel McNie committed
86
87
88
89
90
91
/**
 * Returns code to go in <head> for the given calendar instance
 *
 * @param array $element The element to get <head> code for
 * @return array         An array of HTML elements to go in the <head>
 */
92
function pieform_element_calendar_get_headdata($element) {/*{{{*/
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    if (isset($element['themefile'])) {
        $themefile = $element['themefile'];
    }
    else if (isset($element['theme'])) {
        $themefile = $element['jsroot'] . $element['theme'] . '.css';
    }
    else {
        throw new PieformException('No theme chosen for calendar "' . $element['name'] . '": please set themefile or theme');
    }
    $libfile   = $element['jsroot'] . 'calendar_stripped.js';
    $langfile  = $element['jsroot'] . 'lang/calendar-' . $element['language'] . '.js';
    $setupfile = $element['jsroot'] . 'calendar-setup_stripped.js';
    $result = array(
        '<link rel="stylesheet" type="text/css" media="all" href="' . $themefile . '">',
        '<script type="text/javascript" src="' . $libfile . '"></script>',
        '<script type="text/javascript" src="' . $langfile . '"></script>',
        '<script type="text/javascript" src="' . $setupfile . '"></script>'
    );
    return $result;
112
}/*}}}*/
113

Nigel McNie's avatar
Nigel McNie committed
114
115
116
117
118
119
120
/**
 * Retrieves the value of the calendar as a unix timestamp
 *
 * @param Pieform $form    The form the element is attached to
 * @param array   $element The element to get the value for
 * @return int             The unix timestamp represented by the calendar
 */
121
function pieform_element_calendar_get_value(Pieform $form, $element) {/*{{{*/
122
    $name = $element['name'];
Nigel McNie's avatar
Nigel McNie committed
123
    $global = ($form->get_property('method') == 'get') ? $_GET : $_POST;
124
125
126
127
128

    if (isset($element['value'])) {
        return $element['value'];
    }

129
    if ($form->is_submitted() && isset($global[$name])) {
130
131
132
133
134
135
136
        if (trim($global[$name]) == '') {
            return null;
        }

        $value = strtotime($global[$name]);

        if ($value === false) {
137
            $form->set_error($name, $form->i18n('element', 'calendar', 'invalidvalue', $element));
138
139
140
141
142
143
144
145
146
147
            return null;
        }
        return $value;
    }

    if (isset($element['defaultvalue'])) {
        return $element['defaultvalue'];
    }

    return null;
148
}/*}}}*/
149

150
151
152
/**
 * i18n for calendar
 */
153
function pieform_element_calendar_i18n() {/*{{{*/
154
155
156
    return array(
        'en.utf8' => array(
            'invalidvalue' => 'Invalid date/time specified'
157
158
159
160
        ),
         'de.utf8' => array(
            'invalidvalue' => 'Datum/Zeit sind falsch festgelegt'
        ),
161
162
163
         'fr.utf8' => array(
            'invalidvalue' => 'Date/Heure indiquée invalide'
        ),
164
    );
165
}/*}}}*/
166

167
?>