1<?php
2/**
3 * The Horde_Core_Ui_JsCalendar:: class generates the necessary javascript
4 * code to allow the javascript calendar widget to be displayed on the page.
5 *
6 * Copyright 2009-2017 Horde LLC (http://www.horde.org/)
7 *
8 * See the enclosed file COPYING for license information (LGPL). If you
9 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
10 *
11 * @author   Michael Slusarz <slusarz@horde.org>
12 * @category Horde
13 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
14 * @package  Core
15 */
16class Horde_Core_Ui_JsCalendar
17{
18    /**
19     * Make sure init() is only run once.
20     *
21     * @var boolean
22     */
23    protected static $_initRun = false;
24
25    /**
26     * Output the necessary javascript code to allow display of the calendar
27     * widget.
28     *
29     * @param array $params  Configuration parameters for the widget:
30     * <pre>
31     * 'click_month' - (boolean) If true, the month is clickable.
32     *                 DEFAULT: false
33     * 'click_week' - (boolean) If true, will display a clickable week.
34     *                DEFAULT: false
35     * 'click_year' - (boolean) If true, the year is clickable.
36     *                DEFAULT: false
37     * 'full_weekdays' - (boolean) Add full weekday localized list to
38     *                   javascript object.
39     *                   DEFAULT: false
40     * 'short_weekdays' - (boolean) Display only the first letter of
41     *                    weekdays?
42     *                    DEFAULT: false
43     * </pre>
44     */
45    public static function init(array $params = array())
46    {
47        if (self::$_initRun) {
48            return;
49        }
50        self::$_initRun = true;
51
52        $params = array_merge(array(
53            'click_month' => false,
54            'click_week' => false,
55            'click_year' => false,
56            'full_weekdays' => false,
57            'short_weekdays' => false
58        ), $params);
59
60        $weekdays = self::weekdays();
61        if ($params['short_weekdays']) {
62            foreach ($weekdays as &$day) {
63                $day = Horde_String::substr($day, 0, 1);
64            }
65        }
66
67        $js = array(
68            '-Horde_Calendar.click_month' => intval($params['click_month']),
69            '-Horde_Calendar.click_week' => intval($params['click_week']),
70            '-Horde_Calendar.click_year' => intval($params['click_year']),
71            '-Horde_Calendar.firstDayOfWeek' => intval($GLOBALS['prefs']->getValue('first_week_day')),
72            'Horde_Calendar.months' => self::months(),
73            'Horde_Calendar.weekdays' => $weekdays
74        );
75        if ($params['full_weekdays']) {
76            $js['Horde_Calendar.fullweekdays'] = self::fullWeekdays();
77        }
78
79        $page_output = $GLOBALS['injector']->getInstance('Horde_PageOutput');
80        $page_output->addScriptFile('calendar.js', 'horde');
81        $page_output->addInlineJsVars($js);
82    }
83
84    /**
85     * Return the list of localized abbreviated weekdays.
86     *
87     * @return array  Abbreviated weekdays.
88     */
89    public static function weekdays()
90    {
91        return array(
92            Horde_Core_Translation::t("Su"),
93            Horde_Core_Translation::t("Mo"),
94            Horde_Core_Translation::t("Tu"),
95            Horde_Core_Translation::t("We"),
96            Horde_Core_Translation::t("Th"),
97            Horde_Core_Translation::t("Fr"),
98            Horde_Core_Translation::t("Sa")
99        );
100    }
101
102    /**
103     * Return the list of localized full weekday names.
104     *
105     * @return array  Full weekday names.
106     */
107    public static function fullWeekdays()
108    {
109        return array(
110            Horde_Core_Translation::t("Sunday"),
111            Horde_Core_Translation::t("Monday"),
112            Horde_Core_Translation::t("Tuesday"),
113            Horde_Core_Translation::t("Wednesday"),
114            Horde_Core_Translation::t("Thursday"),
115            Horde_Core_Translation::t("Friday"),
116            Horde_Core_Translation::t("Saturday"),
117        );
118    }
119
120    /**
121     * Return the localized list of months.
122     *
123     * @return array  Month list.
124     */
125    public static function months()
126    {
127        return array(
128            Horde_Core_Translation::t("January"),
129            Horde_Core_Translation::t("February"),
130            Horde_Core_Translation::t("March"),
131            Horde_Core_Translation::t("April"),
132            Horde_Core_Translation::t("May"),
133            Horde_Core_Translation::t("June"),
134            Horde_Core_Translation::t("July"),
135            Horde_Core_Translation::t("August"),
136            Horde_Core_Translation::t("September"),
137            Horde_Core_Translation::t("October"),
138            Horde_Core_Translation::t("November"),
139            Horde_Core_Translation::t("December")
140        );
141    }
142
143}
144