2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('form.FormElement');
30 import('DateAndTime');
32 class Calendar extends FormElement {
33 var $holidays = array();
34 var $showHolidays = true;
35 var $weekStartDay = 0;
37 var $mHeader = "padding: 5px; font-size: 8pt; color: #333333; background-color: #d9d9d9;";
38 var $mDayCell = "padding: 5px; border: 1px solid silver; font-size: 8pt; color: #333333; background-color: #ffffff;";
39 var $mDaySelected = "padding: 5px; border: 1px solid silver; font-size: 8pt; color: #666666; background-color: #a6ccf7;";
40 var $mDayWeekend = "padding: 5px; border: 1px solid silver; font-size: 8pt; color: #666666; background-color: #f7f7f7;";
41 var $mDayHoliday = "padding: 5px; border: 1px solid silver; font-size: 8pt; color: #666666; background-color: #f7f7f7;";
42 var $mDayHeader = "padding: 5px; border: 1px solid white; font-size: 8pt; color: #333333;";
43 var $mDayHeaderWeekend = "padding: 5px; border: 1px solid white; font-size: 8pt; color: #999999;";
45 var $controlName = "";
46 var $highlight = "time"; // Determines what type of active days to highlight ("time" or "expenses").
47 // var $mAllDays = true;
48 var $class = "Calendar";
50 function __construct($name) {
51 $this->controlName = $name;
52 $this->mMonthNames = array('January','February','March','April','May','June','July','August','September','October','November','December');
53 $this->mWeekDayShortNames = array('Su','Mo','Tu','We','Th','Fr','Sa');
56 function setHighlight($highlight) {
57 if ($highlight && $highlight != 'time')
58 $this->highlight = $highlight;
61 function setLocalization($i18n) {
64 FormElement::setLocalization($i18n);
65 $this->mMonthNames = $i18n->monthNames;
66 $this->mWeekDayShortNames = $i18n->weekdayShortNames;
67 if (is_array($i18n->holidays)) {
68 foreach ($i18n->holidays as $fday) {
69 $date_a = explode("/",$fday); // format mm/dd
70 $this->holidays[] = mktime(0,0,0, $date_a[0], $date_a[1], date("Y"));// + 7200;
73 $this->weekStartDay = $user->week_start;
76 function setStyle($style) { $this->style = $style; }
77 function setCellStyle($style) { $this->mCellStyle = $style; }
78 function setACellStyle($style) { $this->mACellStyle = $style; }
79 function setLinkStyle($style) { $this->mLinkStyle = $style; }
81 function setShowHolidays($value) {
82 $this->showHolidays = $value;
88 * @desc Enter description here...
90 function toString($date="") {
93 $indate = $this->value;
94 if (!$indate) $indate = strftime(DB_DATEFORMAT);
96 //current year and month
97 if ( strlen ( $indate ) > 0 ) {
98 $indateObj = new DateAndTime(DB_DATEFORMAT, $indate);
99 $thismonth = $indateObj->getMonth();
100 $thisyear = $indateObj->getYear();
102 $thismonth = date("m");
103 $thisyear = date("Y");
106 // next date, month, year
107 $next = mktime ( 2, 0, 0, $thismonth + 1, 1, $thisyear );
108 $nextyear = date ( "Y", $next );
109 $nextmonth = date ( "m", $next );
110 $nextdate = strftime (DB_DATEFORMAT, $next );
112 // prev date, month, year
113 $prev = mktime ( 2, 0, 0, $thismonth - 1, 1, $thisyear );
114 $prevyear = date ( "Y", $prev );
115 $prevmonth = date ( "m", $prev );
116 $prevdate = strftime(DB_DATEFORMAT, $prev );
118 $str = $this->_genStyles();
120 $str .= '<table cellpadding="0" cellspacing="0" border="0" width="100%">
121 <tr><td align="center"><div class="CalendarHeader">'.
122 //'<a href="?date='.$prevyear.'"><<</a> '.
123 '<a href="?date='.$prevdate.'" tabindex="-1"><<<</a> '.
124 $this->mMonthNames[$thismonth-1].' '.$thisyear.
125 ' <a href="?date='.$nextdate.'" tabindex="-1">>>></a>'.
126 //' <a href="?date='.$nextyear.'">>></a>'.
131 <table border="0" cellpadding="1" cellspacing="1" width="100%">
136 $weekend_start = 6 - $this->weekStartDay; // Saturday by default.
137 $weekend_end = (7 - $this->weekStartDay) % 7; // Sunday by default.
138 if (defined('WEEKEND_START_DAY')) {
139 $weekend_start = (7 + WEEKEND_START_DAY - $this->weekStartDay) % 7;
140 $weekend_end = (7 + WEEKEND_START_DAY + 1 - $this->weekStartDay) % 7;
143 for ( $i=0; $i<7; $i++ ) {
144 $weekdayNameIdx = ($i + $this->weekStartDay) % 7;
145 if ($i==$weekend_start || $i==$weekend_end) {
146 $str .= '<td class="CalendarDayHeaderWeekend">'.$this->mWeekDayShortNames[$weekdayNameIdx].'</td>';
148 $str .= '<td class="CalendarDayHeader">'.$this->mWeekDayShortNames[$weekdayNameIdx].'</td>';
154 list($wkstart,$monthstart,$monthend,$start_date) = $this->_getWeekDayBefore( $thisyear, $thismonth );
156 $active_dates = $this->_getActiveDates($monthstart, $monthend);
158 for ( $i = $wkstart; $i<=$monthend; $i=mktime(0,0,0,$thismonth,$start_date+=7,$thisyear) ) {
160 for ( $j = 0; $j < 7; $j++ ) {
161 $date = mktime(0,0,0,$thismonth,$start_date+$j,$thisyear);
162 if (($date >= $monthstart) && ($date <= $monthend)) {
168 if ($j==$weekend_start || $j==$weekend_end) {
169 $stl_cell = ' class="CalendarDayWeekend"';
170 $stl_link = ' class="CalendarLinkWeekend"';
172 $stl_cell = ' class="CalendarDay"';
176 if ($this->showHolidays) {
177 foreach ($this->holidays as $day) {
179 $stl_cell = ' class="CalendarDayHoliday"';
180 $stl_link = ' class="CalendarLinkHoliday"';
186 if ( $indate == strftime(DB_DATEFORMAT, $date))
187 $stl_cell = ' class="CalendarDaySelected"';
190 $str .= '<td'.$stl_cell.'>';
194 if( in_array(strftime(DB_DATEFORMAT, $date), $active_dates) )
195 $stl_link = ' class="CalendarLinkRecordsExist"';
198 $str .= "<a".$stl_link." href=\"?".$this->controlName."=".strftime(DB_DATEFORMAT, $date)."\" tabindex=\"-1\">".date("d",$date)."</a>";
203 $str .= "<TD> </TD>\n";
209 $str .= "<tr><td colspan=\"7\" align=\"center\"><a id=\"today_link\" href=\"?".$this->controlName."=".strftime(DB_DATEFORMAT)."\" tabindex=\"-1\">".$i18n->getKey('label.today')."</a></td></tr>\n";
210 $str .= "</table>\n";
212 $str .= "<input type=\"hidden\" name=\"$this->controlName\" value=\"$indate\">\n";
214 // Add script to adjust today link to match browser today, as PHP may run in a different timezone.
215 $str .= "<script>\n";
216 $str .= "function adjustToday() {\n";
217 $str .= " var browser_today = new Date();\n";
218 $str .= " document.getElementById('today_link').href = '?$this->controlName='+browser_today.strftime('".DB_DATEFORMAT."');\n";
220 $str .= "adjustToday();\n";
221 $str .= "</script>\n";
226 function toStringControl() {
227 return $this->toString();
230 function _getWeekDayBefore($year, $month) {
231 $weekday = date ( "w", mktime ( 2, 0, 0, $month, 1 - $this->weekStartDay, $year ) );
233 mktime ( 0, 0, 0, $month, 1 - $weekday, $year ),
234 mktime ( 0, 0, 0, $month, 1, $year ),
235 mktime ( 0, 0, 0, $month + 1, 0, $year ),
240 function _genStyles() {
242 $str .= ".CalendarHeader {". $this->mHeader ."}\n";
243 $str .= ".CalendarDay {". $this->mDayCell ."}\n";
244 $str .= ".CalendarDaySelected {". $this->mDaySelected ."}\n";
245 $str .= ".CalendarDayWeekend {". $this->mDayWeekend ."}\n";
246 $str .= ".CalendarDayHoliday {". $this->mDayHoliday ."}\n";
247 $str .= ".CalendarDayHeader {". $this->mDayHeader ."}\n";
248 $str .= ".CalendarDayHeaderWeekend {". $this->mDayHeaderWeekend ."}\n";
250 $str .= ".CalendarLinkWeekend {color: #999999;}\n";
251 $str .= ".CalendarLinkHoliday {color: #999999;}\n";
252 $str .= ".CalendarLinkRecordsExist {color: #FF0000;}\n";
253 $str .= "</style>\n";
257 // _getActiveDates returns an array of dates, for which entries exist for user.
258 // Type of entries (time or expenses) is determined by $this->highlight value.
259 function _getActiveDates($start, $end) {
262 $user_id = $user->getActiveUser();
264 $table = ($this->highlight == 'expenses') ? 'tt_expense_items' : 'tt_log';
266 $mdb2 = getConnection();
268 $start_date = date("Y-m-d", $start);
269 $end_date = date("Y-m-d", $end);
270 $sql = "SELECT date FROM $table WHERE date >= '$start_date' AND date <= '$end_date' AND user_id = $user_id AND status = 1";
271 $res = $mdb2->query($sql);
272 if (!is_a($res, 'PEAR_Error')) {
273 while ($row = $res->fetchRow()) {
274 $out[] = date('Y-m-d', strtotime($row['date']));