Ongoing refactoring - renamed a variable.
[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     // var $mAllDays       = true;
48     var $cClassName    = "Calendar";
49
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');
54     }
55     
56     function setHighlight($highlight) {
57         if ($highlight && $highlight != 'time')
58           $this->highlight = $highlight;
59     }
60
61     function setLocalization($i18n) {
62       global $user;
63       
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;
71         }
72       }
73       $this->weekStartDay = $user->week_start;
74     }
75
76     function setStyle($style) { $this->mStyle = $style; }
77     function setCellStyle($style) { $this->mCellStyle = $style; }
78     function setACellStyle($style) { $this->mACellStyle = $style; }
79     function setLinkStyle($style) { $this->mLinkStyle = $style; }
80
81     function setShowHolidays($value) {
82       $this->showHolidays = $value;
83     }
84
85     /**
86      * @return void
87      * @param date
88      * @desc Enter description here...
89      */
90     function toString($date="") {
91       global $i18n;
92         
93       $indate = $this->value;
94       if (!$indate) $indate = strftime(DB_DATEFORMAT);
95
96       if (!$this->isRenderable()) return "";
97
98       //current year and month
99       if ( strlen ( $indate ) > 0 ) {
100         $indateObj = new DateAndTime(DB_DATEFORMAT, $indate);
101         $thismonth = $indateObj->getMonth();
102         $thisyear = $indateObj->getYear();
103       } else {
104         $thismonth = date("m");
105         $thisyear = date("Y");
106       }
107
108       // next date, month, year
109       $next = mktime ( 2, 0, 0, $thismonth + 1, 1, $thisyear );
110       $nextyear = date ( "Y", $next );
111       $nextmonth = date ( "m", $next );
112       $nextdate = strftime (DB_DATEFORMAT, $next );
113
114       // prev date, month, year
115       $prev = mktime ( 2, 0, 0, $thismonth - 1, 1, $thisyear );
116       $prevyear = date ( "Y", $prev );
117       $prevmonth = date ( "m", $prev );
118       $prevdate = strftime(DB_DATEFORMAT, $prev );
119
120       $str = $this->_genStyles();
121
122       $str .= '<table cellpadding="0" cellspacing="0" border="0" width="100%">
123           <tr><td align="center"><div class="CalendarHeader">'.
124           //'<a href="?date='.$prevyear.'">&lt;&lt;</a> '.
125           '<a href="?date='.$prevdate.'" tabindex="-1">&lt;&lt;&lt;</a>  '.
126           $this->mMonthNames[$thismonth-1].'&nbsp;'.$thisyear.
127           '  <a href="?date='.$nextdate.'" tabindex="-1">&gt;&gt;&gt;</a>'.
128           //' <a href="?date='.$nextyear.'">&gt;&gt;</a>'.
129           '</div></td></tr>
130           </table>';
131
132       $str .= '<center>
133           <table border="0" cellpadding="1" cellspacing="1" width="100%">
134           <tr>';
135
136       $str .= "<tr>";
137
138       $weekend_start = 6 - $this->weekStartDay;      // Saturday by default.
139       $weekend_end = (7 - $this->weekStartDay) % 7;  // Sunday by default.
140       if (defined('WEEKEND_START_DAY')) {
141         $weekend_start = (7 + WEEKEND_START_DAY - $this->weekStartDay) % 7;
142         $weekend_end = (7 + WEEKEND_START_DAY + 1 - $this->weekStartDay) % 7;
143       } 
144
145       for ( $i=0; $i<7; $i++ ) {
146         $weekdayNameIdx = ($i + $this->weekStartDay) % 7;
147         if ($i==$weekend_start || $i==$weekend_end) {
148           $str .= '<td class="CalendarDayHeaderWeekend">'.$this->mWeekDayShortNames[$weekdayNameIdx].'</td>';
149         } else {
150           $str .= '<td class="CalendarDayHeader">'.$this->mWeekDayShortNames[$weekdayNameIdx].'</td>';
151         }
152       }
153
154       $str .= "</tr>\n";
155
156       list($wkstart,$monthstart,$monthend,$start_date) = $this->_getWeekDayBefore( $thisyear, $thismonth );
157
158       $active_dates = $this->_getActiveDates($monthstart, $monthend);
159
160       for ( $i = $wkstart; $i<=$monthend;  $i=mktime(0,0,0,$thismonth,$start_date+=7,$thisyear) ) {
161         $str .= "<TR>\n";
162           for ( $j = 0; $j < 7; $j++ ) {
163             $date = mktime(0,0,0,$thismonth,$start_date+$j,$thisyear);
164             if (($date >= $monthstart) && ($date <= $monthend)) {
165
166             $stl_cell = "";
167             $stl_link = "";
168
169             // weekend
170             if ($j==$weekend_start || $j==$weekend_end) {
171               $stl_cell = ' class="CalendarDayWeekend"';
172               $stl_link = ' class="CalendarLinkWeekend"';
173             } else {
174               $stl_cell = ' class="CalendarDay"';
175             }
176
177               // holidays
178               if ($this->showHolidays) {
179               foreach ($this->holidays as $day) {
180                 if($day == $date) {
181                   $stl_cell = ' class="CalendarDayHoliday"';
182                   $stl_link = ' class="CalendarLinkHoliday"';
183                 }
184               }
185             }
186
187             // selected day
188             if ( $indate == strftime(DB_DATEFORMAT, $date))
189               $stl_cell = ' class="CalendarDaySelected"';
190
191
192             $str .= '<td'.$stl_cell.'>';
193
194             // Entries exist.
195             if($active_dates) {
196               if( in_array(strftime(DB_DATEFORMAT, $date), $active_dates) )
197                 $stl_link = ' class="CalendarLinkRecordsExist"';
198             }
199
200             $str .= "<a".$stl_link." href=\"?".$this->controlName."=".strftime(DB_DATEFORMAT, $date)."\" tabindex=\"-1\">".date("d",$date)."</a>";
201
202             $str .= "</TD>";
203           }
204           else {
205             $str .= "<TD>&nbsp;</TD>\n";
206           }
207         }
208         $str .= "</TR>\n";
209       }
210
211       $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";
212       $str .= "</table>\n";
213
214       $str .= "<input type=\"hidden\" name=\"$this->controlName\" value=\"$indate\">\n";
215
216       // Add script to adjust today link to match browser today, as PHP may run in a different timezone.
217       $str .= "<script>\n";
218       $str .= "function adjustToday() {\n";
219       $str .= "  var browser_today = new Date();\n";
220       $str .= "  document.getElementById('today_link').href = '?$this->controlName='+browser_today.strftime('".DB_DATEFORMAT."');\n";
221       $str .= "}\n";
222       $str .= "adjustToday();\n";
223       $str .= "</script>\n";
224       
225       return $str;
226     }
227
228     function toStringControl() {
229         return $this->toString();
230     }
231
232     function _getWeekDayBefore($year, $month) {
233       $weekday = date ( "w", mktime ( 2, 0, 0, $month, 1 - $this->weekStartDay, $year ) );
234       return array(
235         mktime ( 0, 0, 0, $month, 1 - $weekday, $year ),
236         mktime ( 0, 0, 0, $month, 1, $year ),
237       mktime ( 0, 0, 0, $month + 1, 0, $year ),
238       (1 - $weekday)
239       );
240     }
241
242     function _genStyles() {
243       $str = "<style>\n";
244       $str .= ".CalendarHeader {". $this->mHeader ."}\n";
245       $str .= ".CalendarDay {". $this->mDayCell  ."}\n";
246       $str .= ".CalendarDaySelected {". $this->mDaySelected  ."}\n";
247       $str .= ".CalendarDayWeekend {". $this->mDayWeekend ."}\n";
248       $str .= ".CalendarDayHoliday {". $this->mDayHoliday ."}\n";
249       $str .= ".CalendarDayHeader {". $this->mDayHeader ."}\n";
250       $str .= ".CalendarDayHeaderWeekend {". $this->mDayHeaderWeekend ."}\n";
251       
252       $str .= ".CalendarLinkWeekend {color: #999999;}\n";
253       $str .= ".CalendarLinkHoliday {color: #999999;}\n";
254       $str .= ".CalendarLinkRecordsExist {color: #FF0000;}\n";
255         $str .= "</style>\n";
256         return $str;
257     }
258     
259     // _getActiveDates returns an array of dates, for which entries exist for user.
260     // Type of entries (time or expenses) is determined by $this->highlight value.
261     function _getActiveDates($start, $end) {
262       
263       global $user;
264       $user_id = $user->getActiveUser();
265       
266       $table = ($this->highlight == 'expenses') ? 'tt_expense_items' : 'tt_log';
267       
268       $mdb2 = getConnection();
269
270       $start_date = date("Y-m-d", $start);
271       $end_date = date("Y-m-d", $end);
272       $sql = "SELECT date FROM $table WHERE date >= '$start_date' AND date <= '$end_date' AND user_id = $user_id AND status = 1";
273       $res = $mdb2->query($sql);
274       if (!is_a($res, 'PEAR_Error')) {
275         while ($row = $res->fetchRow()) {
276           $out[] = date('Y-m-d', strtotime($row['date']));
277         }
278         return @$out;
279       }
280       else
281         return false;
282     }
283 }