root/trunk/includes/smarty_plugins/function.calendar.php

Revision 1, 8.3 KB (checked in by anonymous, 7 years ago)

initial import

Line 
1<?php
2/****************************************************** Open .node ***
3 * Description:   
4 * Status:        Stable.
5 * Author:        Alexandre Dath <alexandre@dotnode.com>
6 * $Id$
7 *
8 * Copyright (C) 2005 Alexandre Dath <alexandre@dotnode.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 ******************** http://opensource.ikse.net/projects/dotnode ***/
24
25/* vim: set ts=4 sw=4: */
26// +----------------------------------------------------------------------+
27// | Copyright (C) 2002-2003 Michael Yoon                                 |
28// +----------------------------------------------------------------------+
29// | This program is free software; you can redistribute it and/or        |
30// | modify it under the terms of the GNU General Public License          |
31// | as published by the Free Software Foundation; either version 2       |
32// | of the License, or (at your option) any later version.               |
33// |                                                                      |
34// | This program is distributed in the hope that it will be useful,      |
35// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
36// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         |
37// | GNU General Public License for more details.                         |
38// |                                                                      |
39// | You should have received a copy of the GNU General Public License    |
40// | along with this program; if not, write to the Free Software          |
41// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA            |
42// | 02111-1307, USA.                                                     |
43// +----------------------------------------------------------------------+
44// | Authors: Michael Yoon <michael@yoon.org>                             |
45// |          Andrew Nagy  <andrew@webitecture.org>                       |
46// +----------------------------------------------------------------------+
47//
48// $Id$
49
50require_once 'Smarty.class.php';
51
52/*
53 * Smarty plugin
54 * -------------------------------------------------------------
55 * File:     function.calendar.php
56 * Type:     function
57 * Name:     calendar
58 * Purpose:  outputs a calendar
59 * -------------------------------------------------------------
60 */
61function smarty_function_calendar($params, &$smarty)
62{
63  // Start input validation
64  if (empty($params['url_format'])) {
65    trigger_error('mandatory param is empty: url_format', E_USER_ERROR);
66  }
67
68  if (empty($params['year'])) {
69    trigger_error('mandatory param is empty: year', E_USER_ERROR);
70  }
71
72  if (empty($params['month'])) {
73    trigger_error('mandatory param is empty: month', E_USER_ERROR);
74  }
75
76  // The 'day' param is optional.
77
78  // End input validation
79
80  $url_format = $params['url_format'];
81  $year = $params['year'];
82  $month = $params['month'];
83  $day = $params['day'];
84
85  if ($day != '') {
86    $selected_date = mktime(0, 0, 0, $month, $day, $year);
87  }
88
89  $prev_month_end = mktime(0, 0, 0, $month, 0, $year);
90  $next_month_begin = mktime(0, 0, 0, $month + 1, 1, $year);
91
92  $month_name = strftime('%B', mktime(0, 0, 0, $month, 1, $year));
93  $prev_month_abbrev = strftime('%b', $prev_month_end);
94  $prev_month_end_info = getdate($prev_month_end);
95  $prev_month = $prev_month_end_info['mon'];
96  $prev_month_year = $prev_month_end_info['year'];
97  $prev_month_url = strftime("$url_format", $prev_month_end);
98
99  $next_month_abbrev = strftime('%b', $next_month_begin);
100  $next_month_url = strftime("$url_format", $next_month_begin);
101
102  // TODO: make "week starts on" configurable: Monday vs. Sunday
103  $day_of_week_abbrevs = array();
104  for ($i = 0; $i < 7; $i++) {
105    $day_of_week_abbrevs[] =
106      smarty_function_calendar__day_of_week_abbrev($i);
107  }
108
109  // Build a two-dimensional array of UNIX timestamps.
110  $calendar = array();
111
112  // Start the first row with the final day(s) of the previous month.
113  $week = array();
114  $month_begin = mktime(0, 0, 0, $month, 1, $year);
115  $month_begin_day_of_week = strftime('%w', $month_begin);
116  $days_in_prev_month =
117    smarty_function_calendar__days_in_month($prev_month, $prev_month_year);
118  for ($day_of_week = 0;
119       $day_of_week < $month_begin_day_of_week;
120       $day_of_week++) {
121    $day = $days_in_prev_month - $month_begin_day_of_week + $day_of_week;
122    $week[] =
123      mktime(0, 0, 0, $month - 1, $day, $year);
124  }
125
126  // Fill in the days of the selected month.
127  $days_in_month =
128    smarty_function_calendar__days_in_month($month, $year);
129  for ($i = 1; $i <= $days_in_month; $i++) {
130    if ($day_of_week == 7) {
131      $calendar[] = $week;
132
133      // re-initialize $day_of_week and $week
134      $day_of_week = 0;
135      unset($week);
136      $week = array();
137    }
138    $week[] = mktime(0, 0, 0, $month, $i, $year);
139    $day_of_week++;
140  }
141
142  // Fill out the last row with the first day(s) of the next month.
143  for ($i = 1; $day_of_week < 7; $i++, $day_of_week++) {
144    $week[] = mktime(0, 0, 0, $month + 1, $i, $year);
145  }
146  $calendar[] = $week;
147
148  // Generate the URL for today, which will be null if $selected_date is
149  // today.
150  $today = getdate();
151  $today_date =
152    mktime(0, 0, 0, $today['mon'], $today['mday'], $today['year']);
153  if ($selected_date == $today_date) {
154    $today_url = '';
155  } else {
156    $today_url = strftime($url_format, $today_date);
157  }
158
159  /*
160  $calendar_smarty = new Smarty;
161
162  // Copy the configuration of the enclosing Smarty template.
163  $calendar_smarty->template_dir = $smarty->template_dir;
164  $calendar_smarty->compile_dir = $smarty->compile_dir;
165  $calendar_smarty->config_dir = $smarty->config_dir;
166  $calendar_smarty->plugins_dir = $smarty->plugins_dir;
167  $calendar_smarty->debugging = $smarty->debugging;
168  $calendar_smarty->compile_check = $smarty->compile_check;
169  $calendar_smarty->force_compile = $smarty->force_compile;
170  $calendar_smarty->cache_dir = $smarty->cache_dir;
171  $calendar_smarty->cache_dir = $smarty->cache_dir;
172  $calendar_smarty->cache_lifetime = $smarty->cache_lifetime;
173  */
174  $smarty->assign('url_format', $url_format);
175  $smarty->assign('month_name', $month_name);
176  $smarty->assign('selected_date', $selected_date);
177  $smarty->assign('month', $month);
178  $smarty->assign('year', $year);
179  $smarty->assign('prev_month_end', $prev_month_end);
180  $smarty->assign('prev_month_abbrev', $prev_month_abbrev);
181  $smarty->assign('next_month_begin', $next_month_begin);
182  $smarty->assign('next_month_abbrev', $next_month_abbrev);
183  $smarty->assign('day_of_week_abbrevs', $day_of_week_abbrevs);
184  $smarty->assign('calendar', $calendar);
185  $smarty->assign('today_url', $today_url);
186
187  $smarty->display('calendar:month');
188}
189
190/* Helper functions for the plugin that replicate a subset of what
191 * Date_Calc does (as well as MCAL); required for the plugin to work
192 * without Date_Calc or MCAL installed.
193 *
194 * TODO: make it possible to use Date_Calc or MCAL if installed
195 */
196
197function smarty_function_calendar__is_leap_year($year)
198{
199  return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0);
200}
201
202function smarty_function_calendar__days_in_month($month, $year)
203{
204  switch ($month) {
205  case 1:
206  case 3:
207  case 5:
208  case 7:
209  case 8:
210  case 10:
211  case 12:
212  case 0: // == 12
213    return 31;
214
215  case 4:
216  case 6:
217  case 9:
218  case 11:
219    return 30;
220
221  case 2:
222    return smarty_function_calendar__is_leap_year($year) ? 29 : 28;
223
224  default:
225    assert(false);
226  }
227}
228
229/**
230 * @param int $day_of_week Sunday is 0, Monday is 1, etc.
231 * @return string
232 */
233function smarty_function_calendar__day_of_week_abbrev($day_of_week)
234{
235  // January 2, 2000 is an arbitrary Sunday that serves as the basis for
236  // using strftime() to get the localized name (or abbreviation) of the
237  // specified day of week.
238  $day = 2 + $day_of_week;
239  $timestamp = mktime(0, 0, 0, 1, $day, 2000);
240
241  return strftime('%a', $timestamp);
242}
243?>
Note: See TracBrowser for help on using the browser.