calendar::set_option()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * File: calendar.php | (c) dynarch.com 2004
5
 *                         Distributed as part of "The Coolest DHTML Calendar"
6
 *                         under the same terms.
7
 *                         -----------------------------------------------------------------
8
 *                         This file implements a simple PHP wrapper for the calendar.  It
9
 *                         allows you to easily include all the calendar files and setup the
10
 *                         calendar by instantiating and calling a PHP object.
11
 */
12
define('NEWLINE', "\n");
13
14
/**
15
 * DHTML_Calendar
16
 *
17
 * @author    John
18
 * @copyright Copyright (c) 2009
19
 */
20
class calendar
21
{
22
    public $calendar_lib_path;
23
    public $calendar_file;
24
    public $calendar_lang_file;
25
    public $calendar_setup_file;
26
    public $calendar_theme_file;
27
    public $calendar_options;
28
29
    /**
30
     * DHTML_Calendar::DHTML_Calendar()
31
     *
32
     * @param string $calendar_lib_path
33
     * @param string $lang
34
     * @param string $theme
35
     * @param mixed  $stripped
36
     * @param array  $calendar_options
37
     * @param array  $calendar_field_attributes
38
     */
39
    public function __construct(
40
        string $calendar_lib_path = '',
41
        string $lang = 'en',
42
        string $theme = 'calendar-win2k-1',
43
        $stripped = false,
44
        array $calendar_options = [],
45
        array $calendar_field_attributes = []
46
    ) {
47
        $this->set_option('date', '');
48
        $this->set_option('ifFormat', '%m/%d/%Y %H:%M');
49
        $this->set_option('daFormat', '%m/%d/%Y %H:%M');
50
        $this->set_option('firstDay', 1); // show Monday first
51
        $this->set_option('showOthers', true);
52
53
        if ($stripped) {
54
            $this->calendar_file       = 'calendar_stripped.js';
55
            $this->calendar_setup_file = 'calendar-setup_stripped.js';
56
        } else {
57
            $this->calendar_file       = 'calendar.js';
58
            $this->calendar_setup_file = 'calendar-setup.js';
59
        }
60
61
        $lang                      = file_exists(XOOPS_ROOT_PATH . 'modules/xhelp/assets/js/calendar/lang/calendar-' . _LANGCODE . '.js') ? _LANGCODE : 'en';
62
        $this->calendar_lang_file  = 'lang/calendar-' . $lang . '.js';
63
        $this->calendar_lib_path   = '/modules/xhelp/assets/js/calendar/';
64
        $this->calendar_theme_file = 'calendar-blue.css';
65
        $this->calendar_theme_url  = 'modules/xhelp/assets/js/calendar/css/';
0 ignored issues
show
Bug Best Practice introduced by
The property calendar_theme_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
66
    }
67
68
    /**
69
     * @param $name
70
     * @param $value
71
     */
72
    public function set_option($name, $value)
73
    {
74
        $this->calendar_options[$name] = $value;
75
    }
76
77
    /**
78
     * DHTML_Calendar::load_files()
79
     */
80
    public function load_files()
81
    {
82
        $this->get_load_files_code();
83
    }
84
85
    /**
86
     * DHTML_Calendar::get_load_files_code()
87
     */
88
    public function get_load_files_code()
89
    {
90
        if (isset($GLOBALS['xo_Theme'])) {
91
            $GLOBALS['xo_Theme']->addStylesheet($this->calendar_theme_url . $this->calendar_theme_file);
92
            $GLOBALS['xo_Theme']->addScript($this->calendar_lib_path . $this->calendar_file);
93
            $GLOBALS['xo_Theme']->addScript($this->calendar_lib_path . $this->calendar_lang_file);
94
            $GLOBALS['xo_Theme']->addScript($this->calendar_lib_path . $this->calendar_setup_file);
95
        } else {
96
            $ret = '<link rel="stylesheet" type="text/css" media="all" href="' . XOOPS_URL . '/' . $this->calendar_theme_url . $this->calendar_theme_file . '">';
97
            $ret .= '<script type="text/javascript" src="' . XOOPS_URL . '/' . $this->calendar_lib_path . $this->calendar_file . '"></script>';
98
            $ret .= '<script type="text/javascript" src="' . XOOPS_URL . '/' . $this->calendar_lib_path . $this->calendar_lang_file . '"></script>';
99
            $ret .= '<script type="text/javascript" src="' . XOOPS_URL . '/' . $this->calendar_lib_path . $this->calendar_setup_file . '"></script>';
100
            echo $ret;
101
        }
102
    }
103
104
    /**
105
     * DHTML_Calendar::_make_calendar()
106
     *
107
     * @param array $other_options
108
     * @return string
109
     */
110
    public function _make_calendar(array $other_options = []): string
111
    {
112
        $js_options = $this->_make_js_hash(array_merge($this->calendar_options, $other_options));
113
        $code       = ('<script type="text/javascript">Calendar.setup({' . $js_options . '});</script>');
114
115
        return $code;
116
    }
117
118
    /**
119
     * DHTML_Calendar::make_input_field()
120
     *
121
     * @param array $cal_options
122
     * @param array $field_attributes
123
     * @param mixed $show
124
     * @return string
125
     */
126
    public function make_input_field(array $cal_options = [], array $field_attributes = [], $show = true): string
127
    {
128
        $id      = $this->_gen_id();
129
        $attrstr = $this->_make_html_attr(array_merge($field_attributes, ['id' => $this->_field_id($id), 'type' => 'text']));
130
        $data    = '<input ' . $attrstr . '>';
131
        $data    .= '<a href="#" id="' . $this->_trigger_id($id) . '">' . '&nbsp;<img src="' . XOOPS_URL . '/' . $this->calendar_lib_path . 'img.png" style="vertical-align: middle; border: 0px;" alt=""></a>&nbsp;';
132
        $options = array_merge(
133
            $cal_options,
134
            [
135
                'inputField' => $this->_field_id($id),
136
                'button'     => $this->_trigger_id($id),
137
            ]
138
        );
139
        $data    .= $this->_make_calendar($options);
140
        if ($show) {
141
            echo $data;
142
143
            return '';
144
        }
145
146
        return $data;
147
    }
148
149
    // / PRIVATE SECTION
150
151
    /**
152
     * @param $id
153
     * @return string
154
     */
155
    public function _field_id($id): string
156
    {
157
        return 'f-calendar-field-' . $id;
158
    }
159
160
    /**
161
     * @param $id
162
     * @return string
163
     */
164
    public function _trigger_id($id): string
165
    {
166
        return 'f-calendar-trigger-' . $id;
167
    }
168
169
    /**
170
     * @return int
171
     */
172
    public function _gen_id(): int
173
    {
174
        static $id = 0;
175
176
        return ++$id;
177
    }
178
179
    /**
180
     * @param $array
181
     * @return string
182
     */
183
    public function _make_js_hash($array): string
184
    {
185
        $jstr = '';
186
        //        reset($array);
187
        //        while (list($key, $val) = each($array)) {
188
        foreach ($array as $key => $val) {
189
            if (is_bool($val)) {
190
                $val = $val ? 'true' : 'false';
191
            } elseif (!is_numeric($val)) {
192
                $val = '"' . $val . '"';
193
            }
194
            if ($jstr) {
195
                $jstr .= ',';
196
            }
197
            $jstr .= '"' . $key . '":' . $val;
198
        }
199
200
        return $jstr;
201
    }
202
203
    /**
204
     * @param $array
205
     * @return string
206
     */
207
    public function _make_html_attr($array): string
208
    {
209
        $attrstr = '';
210
        //        reset($array);
211
        //        while (list($key, $val) = each($array)) {
212
        foreach ($array as $key => $val) {
213
            $attrstr .= $key . '="' . $val . '" ';
214
        }
215
216
        return $attrstr;
217
    }
218
}
219