summaryrefslogtreecommitdiff
path: root/liberty_plugins/data.calendar.php
blob: 7ee934a75e217ae75e9960d5147df6a72a5a9078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
 * @version  $Revision: 1.4 $
 * @package  liberty
 * @subpackage plugins_data
 */
// +----------------------------------------------------------------------+
// | Copyright (c) 2004, bitweaver.org
// +----------------------------------------------------------------------+
// | All Rights Reserved. See below for details and a complete list of authors.
// | Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
// |
// | For comments, please use phpdocu.sourceforge.net documentation standards!!!
// | -> see http://phpdocu.sourceforge.net/
// +----------------------------------------------------------------------+
// | Author (TikiWiki): Luis Argerich <lrargerich@users.sourceforge.net>
// | Reworked for Bitweaver (& Undoubtedly Screwed-Up)
// | by: StarRider <starrrider@users.sourceforge.net>
// +----------------------------------------------------------------------+
// $Id: data.calendar.php,v 1.4 2009/10/01 14:16:58 wjames5 Exp $

/**
 * definitions
 */
define( 'PLUGIN_GUID_DATACALENDAR', 'datacalendar' );
global $gLibertySystem;
$pluginParams = array (
	'tag' => 'CALENDAR',
	'auto_activate' => TRUE,
	'requires_pair' => FALSE,
	'load_function' => 'data_calendar',
	'title' => 'Calendar',
	'help_page' => 'DataPluginCalendar',
	'description' => tra("Displays a mini calendar that links to the calendar package."),	
	'syntax' => " {CALENDAR} ",
	'plugin_type' => DATA_PLUGIN
);
$gLibertySystem->registerPlugin( PLUGIN_GUID_DATACALENDAR, $pluginParams );
$gLibertySystem->registerDataTag( $pluginParams['tag'], PLUGIN_GUID_DATACALENDAR );

function data_calendar( $data, $params ) {
	 global $gBitSmarty, $gBitSystem;

	 if ($gBitSystem->isPackageActive('calendar')) {
		 $offset = $gBitSystem->get_display_offset();
		 $bitDate = new BitDate($offset);	
		 
		 $time = $bitDate->getUTCTime();
		 $date = $bitDate->getDate($time, true);
		 
		 $month = $date['mon'];
		 $year = $date['year'];
		 $month_day = $date['mday'];
		 $month_name = $date['month'];
		 
		 // reset time so we can make today look different in template with compare
		 $time = $bitDate->mktime(0, 0, 0, $month, $month_day, $year);
		 
		 $last_time = $bitDate->mktime(0, 0, 0, $month, 0, $year);
		 $next_time = $bitDate->mktime(0, 0, 0, $month + 1, 1, $year);
		 $last = $bitDate->getDate($last_time);
		 $next = $bitDate->getDate($next_time);
		 
		 $days = array();
		 for ($i = 2; $i < 9; $i++) {
			 // Start from known sunday.
			 $timestamp = $bitDate->mktime(0, 0, 0, 1, $i, 2000);
			 $days[] = $bitDate->strftime('%a', $timestamp);
		 }
		 
		 // Build a two-dimensional array of UNIX timestamps.
		 $calendar = array();
		 
		 // Start with last days of previous month.
		 $week = array();
		 $month_begin = $bitDate->mktime(0, 0, 0, $month, 1, $year);
		 $month_begin_dow = strftime('%w', $month_begin);
		 
		 $days_last_month = $bitDate->daysInMonth($last['month'], $last['year']);
		 for ($dow = 0;
			  $dow < $month_begin_dow;
			  $dow++) {
			 $day = $days_last_month - $month_begin_dow + $dow;
			 $d['time'] = $bitDate->mktime(0, 0, 0, $month - 1, $day, $year);
			 $d['dim'] = true;
			 $week[] = $d;
		 }
		 
		 // Do this month
		 $days_in_month = $bitDate->daysInMonth($month, $year);
		 for ($i = 1; $i <= $days_in_month; $i++) {
			 if ($dow == 7) {
				 $calendar[] = $week;
				 
				 // Done with row
				 $dow = 0;
				 unset($week);
				 $week = array();
			 }
			 $d['time'] = $bitDate->mktime(0, 0, 0, $month, $i, $year);
			 $d['dim'] = false;
			 // Flag today
			 if ($i == $month_day) {
				 $d['today'] = true;
			 }
			 $week[] = $d;
			 unset($d['today']);
			 $dow++;
		}
		 
		 // Do the last month.
		 for ($i = 1; $dow < 7; $i++, $dow++) {
			 $d['time'] = $bitDate->mktime(0, 0, 0, $month + 1, $i, $year);
			 $d['dim'] = true;
			 $week[] = $d;
		 }
		 $calendar[] = $week;

		 $gBitSmarty->assign('minical', true);
		 $gBitSmarty->assign('month_name', $month_name);
		 $gBitSmarty->assign('month', $month);
		 $gBitSmarty->assign('year', $year);
		 $gBitSmarty->assign('last_month', $last_time);
		 $gBitSmarty->assign('next_month', $next_time);
		 $gBitSmarty->assign('dow_abbrevs', $days);
		 $gBitSmarty->assign('calendar', $calendar);
		 $gBitSmarty->assign('today', $time);

		 // Assign a base url
		 if (empty($params['events'])) {
			 $pBaseUrl = CALENDAR_PKG_URL.'index.php';
		 }
		 else {
			 $pBaseUrl = EVENTS_PKG_URL.'calendar.php';
		 }
		 $gBitSmarty->assign('baseCalendarUrl', $pBaseUrl);

		 return $gBitSmarty->fetch('bitpackage:calendar/minical.tpl');
	 }
	 
	 return '<div class="error">Calendar Package Not Active</div>';
}
?>