Resumed refactoring of the Period class.
[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 class Period {
56   var $startDate;
57   var $endDate;
58
59   function __construct($period_type = 0, $date_point = null) {
60
61     global $user;
62
63     if (!$date_point || !($date_point instanceof DateAndTime))
64       $date_point = new DateAndTime(); // Represents current date. TODO: verify this is needed, as this is server time, not browser today.
65
66     // TODO: refactoring ongoing down from here. Make code nicer, etc.
67     $weekStartDay = $user->week_start;
68
69                 $this->startDate = new DateAndTime();
70                 $this->startDate->setFormat($date_point->getFormat());
71                 $this->endDate = new DateAndTime();
72                 $this->endDate->setFormat($date_point->getFormat());
73                 $t_arr = localtime($date_point->getTimestamp());
74                 $t_arr[5] = $t_arr[5] + 1900;
75
76                 if ($t_arr[6] < $weekStartDay) {
77                   $startWeekBias = $weekStartDay - 7;
78                 } else {
79                   $startWeekBias = $weekStartDay;
80                 }
81
82                 switch ($period_type) {
83                         case INTERVAL_THIS_DAY:
84                             $this->startDate->setTimestamp($date_point->getTimestamp());
85                             $this->endDate->setTimestamp($date_point->getTimestamp());
86                         break;
87
88                         case INTERVAL_LAST_DAY:
89                             $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-1,$t_arr[5]));
90                             $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-1,$t_arr[5]));
91                         break;
92
93                         case INTERVAL_THIS_WEEK:
94                           $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+$startWeekBias,$t_arr[5]));
95                                 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+6+$startWeekBias,$t_arr[5]));
96                         break;
97                         case INTERVAL_LAST_WEEK:
98                                 $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-7+$startWeekBias,$t_arr[5]));
99                                 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-1+$startWeekBias,$t_arr[5]));
100                         break;
101                         case INTERVAL_THIS_MONTH:
102                                 $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,1,$t_arr[5]));
103                                 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+2,0,$t_arr[5]));
104                         break;
105                         case INTERVAL_LAST_MONTH:
106                                 $this->startDate->setTimestamp(mktime(0,0,0,$t_arr[4],1,$t_arr[5]));
107                                 $this->endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,0,$t_arr[5]));
108                         break;
109
110                         case INTERVAL_THIS_YEAR:
111                                 $this->startDate->setTimestamp(mktime(0, 0, 0, 1, 1, $t_arr[5]));
112                                 $this->endDate->setTimestamp(mktime(0, 0, 0, 12, 31, $t_arr[5]));
113                         break;
114                 }
115         }
116
117         /**
118          * Return all days by period
119          *
120          * @return array
121          */
122         function getAllDays() {
123                 $ret_array = array();
124                 if ($this->startDate->before($this->endDate)) {
125                         $d = $this->getBegin();
126                         while ($d->before($this->getEnd())) {
127                                 array_push($ret_array, $d);
128                                 $d = $d->nextDate();
129                         }
130                         array_push($ret_array, $d);
131                 } else {
132                         array_push($ret_array, $this->startDate);
133                 }
134                 return $ret_array;
135         }
136
137         function setPeriod($b_date, $e_date) {
138                 $this->startDate = $b_date;
139                 $this->endDate = $e_date;
140         }
141
142         // return date object
143         function getBegin() {
144                 return $this->startDate;
145         }
146
147         // return date object
148         function getEnd() {
149                 return $this->endDate;
150         }
151
152         // return date string
153         function getBeginDate($format="") {
154                 return $this->startDate->toString($format);
155         }
156
157         // return date string
158         function getEndDate($format="") {
159                 return $this->endDate->toString($format);
160         }
161
162         function getArray($format="") {
163                 $result = array();
164                 $d = $this->getBegin();
165                 while ($d->before($this->getEnd())) {
166                         $result[] = $d->toString($format);
167                         $d = $d->nextDate();
168                 }
169                 return $result;
170         }
171 }