0e901a8d6ebf5151e888f84cef3794b0605e926f
[timetracker.git] / WEB-INF / lib / Period.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 define('INTERVAL_THIS_DAY', 1);
30 define('INTERVAL_THIS_WEEK', 2);
31 define('INTERVAL_THIS_MONTH', 3);
32 define('INTERVAL_THIS_YEAR', 4);
33 define('INTERVAL_ALL_TIME', 5);
34 define('INTERVAL_LAST_WEEK', 6);
35 define('INTERVAL_LAST_MONTH', 7);
36 define('INTERVAL_LAST_DAY', 8);
37
38 /*
39 // Definitions for refactored code. TODO: uncomment when done.
40 define('INTERVAL_ALL_TIME', 0);
41 define('INTERVAL_CURRENT_YEAR', 10);
42 define('INTERVAL_PREVIOUS_YEAR', 14);
43 define('INTERVAL_SELECTED_YEAR', 18);
44 define('INTERVAL_CURRENT_MONTH', 20);
45 define('INTERVAL_PREVIOUS_MONTH', 24);
46 define('INTERVAL_SELECTED_MONTH', 28);
47 define('INTERVAL_CURRENT_WEEK', 30);
48 define('INTERVAL_PREVIOUS_WEEK', 34);
49 define('INTERVAL_SELECTED_WEEK', 38);
50 define('INTERVAL_CURRENT_DAY', 40);
51 define('INTERVAL_PREVIOUS_DAY', 44);
52 define('INTERVAL_SELECTED_DAY', 48);
53 */
54
55 // TODO: Refactoring is needed for this class. Probably by refactoring DateAndTime first, as Period is
56 // basically a collection of 2 DateAndTime instances.
57 //
58 // Second problem is that "today" is (most likely?) server today, so reports may give incorrect dates
59 // for browser users in different time zones. Verify and fix this.
60 class Period {
61   var $startDate; // DateAndTime object.
62   var $endDate;   // DateAndTime object.
63
64   function __construct($period_type = 0, $date_point = null) {
65
66     global $user;
67
68     if (!$date_point || !($date_point instanceof DateAndTime))
69       $date_point = new DateAndTime(); // Represents current date. TODO: verify this is needed, as this is server time, not browser today.
70
71     // TODO: refactoring ongoing down from here. Make code nicer, etc.
72     $weekStartDay = $user->week_start;
73
74                 $this->startDate = new DateAndTime();
75                 $this->startDate->setFormat($date_point->getFormat());
76                 $this->endDate = new DateAndTime();
77                 $this->endDate->setFormat($date_point->getFormat());
78                 $t_arr = localtime($date_point->getTimestamp());
79                 $t_arr[5] = $t_arr[5] + 1900;
80
81                 if ($t_arr[6] < $weekStartDay) {
82                   $startWeekBias = $weekStartDay - 7;
83                 } else {
84                   $startWeekBias = $weekStartDay;
85                 }
86
87                 switch ($period_type) {
88                         case INTERVAL_THIS_DAY:
89                             $this->startDate->setTimestamp($date_point->getTimestamp());
90                             $this->endDate->setTimestamp($date_point->getTimestamp());
91                         break;
92
93                         case INTERVAL_LAST_DAY:
94                             $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-1,$t_arr[5]));
95                             $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-1,$t_arr[5]));
96                         break;
97
98                         case INTERVAL_THIS_WEEK:
99                           $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+$startWeekBias,$t_arr[5]));
100                                 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+6+$startWeekBias,$t_arr[5]));
101                         break;
102                         case INTERVAL_LAST_WEEK:
103                                 $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-7+$startWeekBias,$t_arr[5]));
104                                 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-1+$startWeekBias,$t_arr[5]));
105                         break;
106                         case INTERVAL_THIS_MONTH:
107                                 $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,1,$t_arr[5]));
108                                 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+2,0,$t_arr[5]));
109                         break;
110                         case INTERVAL_LAST_MONTH:
111                                 $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4],1,$t_arr[5]));
112                                 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,0,$t_arr[5]));
113                         break;
114
115                         case INTERVAL_THIS_YEAR:
116                                 $this->startDate->setTimestamp(mktime(0, 0, 0, 1, 1, $t_arr[5]));
117                                 $this->endDate->setTimestamp(mktime(0, 0, 0, 12, 31, $t_arr[5]));
118                         break;
119                 }
120         }
121
122         function setPeriod($b_date, $e_date) {
123                 $this->startDate = $b_date;
124                 $this->endDate = $e_date;
125         }
126
127         // return date string
128         function getStartDate($format="") {
129                 return $this->startDate->toString($format);
130         }
131
132         // return date string
133         function getEndDate($format="") {
134                 return $this->endDate->toString($format);
135         }
136 }