6c4a42231e364ebcc11e42891f7020f722e53d09
[timetracker.git] / WEB-INF / lib / form / Calendar.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 import('form.FormElement');
30 import('DateAndTime');
31
32 class Calendar extends FormElement {
33   var $holidays = array();
34   var $showHolidays = true;
35   var $weekStartDay = 0;
36   
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;";
44
45     var $controlName = "";
46     var $highlight = "time"; // Determines what type of active days to highlight ("time" or "expenses"). 
47
48     function __construct($name) {
49       $this->class = 'Calendar';
50       $this->controlName = $name; // TODO: why controlName? Other classes have "name".
51       $this->mMonthNames = array('January','February','March','April','May','June','July','August','September','October','November','December');
52       $this->mWeekDayShortNames = array('Su','Mo','Tu','We','Th','Fr','Sa');
53     }
54
55     function setHighlight($highlight) {
56         if ($highlight && $highlight != 'time')
57           $this->highlight = $highlight;
58     }
59
60     function localize() {
61       global $user;
62       global $i18n;
63       
64       $this->mMonthNames = $i18n->monthNames;
65       $this->mWeekDayShortNames = $i18n->weekdayShortNames;
66       if (is_array($i18n->holidays)) {
67         foreach ($i18n->holidays as $fday) {
68           $date_a = explode("/",$fday); // format mm/dd
69           $this->holidays[] = mktime(0,0,0, $date_a[0], $date_a[1], date("Y"));// + 7200;
70         }
71       }
72       $this->weekStartDay = $user->week_start;
73     }
74
75     function setStyle($style) { $this->style = $style; }
76     function setCellStyle($style) { $this->mCellStyle = $style; }
77     function setACellStyle($style) { $this->mACellStyle = $style; }
78     function setLinkStyle($style) { $this->mLinkStyle = $style; }
79
80     function setShowHolidays($value) {
81       $this->showHolidays = $value;
82     }
83
84     /**
85      * @return void
86      * @param date
87      * @desc Enter description here...
88      */
89     function toString($date="") {
90       global $i18n;
91         
92       $indate = $this->value;
93       if (!$indate) $indate = strftime(DB_DATEFORMAT);
94
95       //current year and month
96       if ( strlen ( $indate ) > 0 ) {
97         $indateObj = new DateAndTime(DB_DATEFORMAT, $indate);
98         $thismonth = $indateObj->getMonth();
99         $thisyear = $indateObj->getYear();
100       } else {
101         $thismonth = date("m");
102         $thisyear = date("Y");
103       }
104
105       // next date, month, year
106       $next = mktime ( 2, 0, 0, $thismonth + 1, 1, $thisyear );
107       $nextyear = date ( "Y", $next );
108       $nextmonth = date ( "m", $next );
109       $nextdate = strftime (DB_DATEFORMAT, $next );
110
111       // prev date, month, year
112       $prev = mktime ( 2, 0, 0, $thismonth - 1, 1, $thisyear );
113       $prevyear = date ( "Y", $prev );
114       $prevmonth = date ( "m", $prev );
115       $prevdate = strftime(DB_DATEFORMAT, $prev );
116
117       $str = $this->_genStyles();
118
119       $str .= '<table cellpadding="0" cellspacing="0" border="0" width="100%">
120           <tr><td align="center"><div class="CalendarHeader">'.
121           //'<a href="?date='.$prevyear.'">&lt;&lt;</a> '.
122           '<a href="?date='.$prevdate.'" tabindex="-1">&lt;&lt;&lt;</a>  '.
123           $this->mMonthNames[$thismonth-1].'&nbsp;'.$thisyear.
124           '  <a href="?date='.$nextdate.'" tabindex="-1">&gt;&gt;&gt;</a>'.
125           //' <a href="?date='.$nextyear.'">&gt;&gt;</a>'.
126           '</div></td></tr>
127           </table>';
128
129       $str .= '<center>
130           <table border="0" cellpadding="1" cellspacing="1" width="100%">
131           <tr>';
132
133       $str .= "<tr>";
134
135       $weekend_start = 6 - $this->weekStartDay;      // Saturday by default.
136       $weekend_end = (7 - $this->weekStartDay) % 7;  // Sunday by default.
137       if (defined('WEEKEND_START_DAY')) {
138         $weekend_start = (7 + WEEKEND_START_DAY - $this->weekStartDay) % 7;
139         $weekend_end = (7 + WEEKEND_START_DAY + 1 - $this->weekStartDay) % 7;
140       } 
141
142       for ( $i=0; $i<7; $i++ ) {
143         $weekdayNameIdx = ($i + $this->weekStartDay) % 7;
144         if ($i==$weekend_start || $i==$weekend_end) {
145           $str .= '<td class="CalendarDayHeaderWeekend">'.$this->mWeekDayShortNames[$weekdayNameIdx].'</td>';
146         } else {
147           $str .= '<td class="CalendarDayHeader">'.$this->mWeekDayShortNames[$weekdayNameIdx].'</td>';
148         }
149       }
150
151       $str .= "</tr>\n";
152
153       list($wkstart,$monthstart,$monthend,$start_date) = $this->_getWeekDayBefore( $thisyear, $thismonth );
154
155       $active_dates = $this->_getActiveDates($monthstart, $monthend);
156
157       for ( $i = $wkstart; $i<=$monthend;  $i=mktime(0,0,0,$thismonth,$start_date+=7,$thisyear) ) {
158         $str .= "<TR>\n";
159           for ( $j = 0; $j < 7; $j++ ) {
160             $date = mktime(0,0,0,$thismonth,$start_date+$j,$thisyear);
161             if (($date >= $monthstart) && ($date <= $monthend)) {
162
163             $stl_cell = "";
164             $stl_link = "";
165
166             // weekend
167             if ($j==$weekend_start || $j==$weekend_end) {
168               $stl_cell = ' class="CalendarDayWeekend"';
169               $stl_link = ' class="CalendarLinkWeekend"';
170             } else {
171               $stl_cell = ' class="CalendarDay"';
172             }
173
174               // holidays
175               //if ($this->showHolidays) {
176               global $user;
177               if ($user->show_holidays) {
178               foreach ($this->holidays as $day) {
179                 if($day == $date) {
180                   $stl_cell = ' class="CalendarDayHoliday"';
181                   $stl_link = ' class="CalendarLinkHoliday"';
182                 }
183               }
184             }
185
186             // selected day
187             if ( $indate == strftime(DB_DATEFORMAT, $date))
188               $stl_cell = ' class="CalendarDaySelected"';
189
190
191             $str .= '<td'.$stl_cell.'>';
192
193             // Entries exist.
194             if($active_dates) {
195               if( in_array(strftime(DB_DATEFORMAT, $date), $active_dates) )
196                 $stl_link = ' class="CalendarLinkRecordsExist"';
197             }
198
199             $str .= "<a".$stl_link." href=\"?".$this->controlName."=".strftime(DB_DATEFORMAT, $date)."\" tabindex=\"-1\">".date("d",$date)."</a>";
200
201             $str .= "</TD>";
202           }
203           else {
204             $str .= "<TD>&nbsp;</TD>\n";
205           }
206         }
207         $str .= "</TR>\n";
208       }
209
210       $str .= "<tr><td colspan=\"7\" align=\"center\"><a id=\"today_link\" href=\"?".$this->controlName."=".strftime(DB_DATEFORMAT)."\" tabindex=\"-1\">".$i18n->get('label.today')."</a></td></tr>\n";
211       $str .= "</table>\n";
212
213       $str .= "<input type=\"hidden\" name=\"$this->controlName\" value=\"$indate\">\n";
214
215       // Add script to adjust today link to match browser today, as PHP may run in a different timezone.
216       $str .= "<script>\n";
217       $str .= "function adjustToday() {\n";
218       $str .= "  var browser_today = new Date();\n";
219       $str .= "  document.getElementById('today_link').href = '?$this->controlName='+browser_today.strftime('".DB_DATEFORMAT."');\n";
220       $str .= "}\n";
221       $str .= "adjustToday();\n";
222       $str .= "</script>\n";
223       
224       return $str;
225     }
226
227     function getHtml() {
228         return $this->toString();
229     }
230
231     function _getWeekDayBefore($year, $month) {
232       $weekday = date ( "w", mktime ( 2, 0, 0, $month, 1 - $this->weekStartDay, $year ) );
233       return array(
234         mktime ( 0, 0, 0, $month, 1 - $weekday, $year ),
235         mktime ( 0, 0, 0, $month, 1, $year ),
236       mktime ( 0, 0, 0, $month + 1, 0, $year ),
237       (1 - $weekday)
238       );
239     }
240
241     function _genStyles() {
242       $str = "<style>\n";
243       $str .= ".CalendarHeader {". $this->mHeader ."}\n";
244       $str .= ".CalendarDay {". $this->mDayCell  ."}\n";
245       $str .= ".CalendarDaySelected {". $this->mDaySelected  ."}\n";
246       $str .= ".CalendarDayWeekend {". $this->mDayWeekend ."}\n";
247       $str .= ".CalendarDayHoliday {". $this->mDayHoliday ."}\n";
248       $str .= ".CalendarDayHeader {". $this->mDayHeader ."}\n";
249       $str .= ".CalendarDayHeaderWeekend {". $this->mDayHeaderWeekend ."}\n";
250       
251       $str .= ".CalendarLinkWeekend {color: #999999;}\n";
252       $str .= ".CalendarLinkHoliday {color: #999999;}\n";
253       $str .= ".CalendarLinkRecordsExist {color: #FF0000;}\n";
254         $str .= "</style>\n";
255         return $str;
256     }
257     
258     // _getActiveDates returns an array of dates, for which entries exist for user.
259     // Type of entries (time or expenses) is determined by $this->highlight value.
260     function _getActiveDates($start, $end) {
261       
262       global $user;
263       $user_id = $user->getUser();
264       
265       $table = ($this->highlight == 'expenses') ? 'tt_expense_items' : 'tt_log';
266       
267       $mdb2 = getConnection();
268
269       $start_date = date("Y-m-d", $start);
270       $end_date = date("Y-m-d", $end);
271       $sql = "SELECT date FROM $table WHERE date >= '$start_date' AND date <= '$end_date' AND user_id = $user_id AND status = 1";
272       $res = $mdb2->query($sql);
273       if (!is_a($res, 'PEAR_Error')) {
274         while ($row = $res->fetchRow()) {
275           $out[] = date('Y-m-d', strtotime($row['date']));
276         }
277         return @$out;
278       }
279       else
280         return false;
281     }
282 }