Added this day and last day to report interval options.
[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 class Period {
39   var $startDate;
40   var $endDate;
41
42   function __construct($period_type = 0, $date_point = null) {
43
44     global $user;
45
46     if (!$date_point || !($date_point instanceof DateAndTime))
47       $date_point = new DateAndTime();
48
49     // TODO: refactoring ongoing down from here. Make code nicer, etc.
50     $weekStartDay = $user->week_start;
51
52                 $date_begin = new DateAndTime();
53                 $date_begin->setFormat($date_point->getFormat());
54                 $date_end = new DateAndTime();
55                 $date_end->setFormat($date_point->getFormat());
56                 $t_arr = localtime($date_point->getTimestamp());
57                 $t_arr[5] = $t_arr[5] + 1900;
58
59                 if ($t_arr[6] < $weekStartDay) {
60                   $startWeekBias = $weekStartDay - 7;
61                 } else {
62                   $startWeekBias = $weekStartDay;
63                 }
64
65                 switch ($period_type) {
66                         case INTERVAL_THIS_DAY:
67                             $date_begin->setTimestamp($date_point->getTimestamp());
68                             $date_end->setTimestamp($date_point->getTimestamp());
69                         break;
70
71                         case INTERVAL_LAST_DAY:
72                             $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-1,$t_arr[5]));
73                             $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-1,$t_arr[5]));
74                         break;
75
76                         case INTERVAL_THIS_WEEK:
77                           $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+$startWeekBias,$t_arr[5]));
78                                 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+6+$startWeekBias,$t_arr[5]));
79                         break;
80                         case INTERVAL_LAST_WEEK:
81                                 $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-7+$startWeekBias,$t_arr[5]));
82                                 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-1+$startWeekBias,$t_arr[5]));
83                         break;
84                         case INTERVAL_THIS_MONTH:
85                                 $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4]+1,1,$t_arr[5]));
86                                 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+2,0,$t_arr[5]));
87                         break;
88                         case INTERVAL_LAST_MONTH:
89                                 $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4],1,$t_arr[5]));
90                                 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+1,0,$t_arr[5]));
91                         break;
92
93                         case INTERVAL_THIS_YEAR:
94                                 $date_begin->setTimestamp(mktime(0, 0, 0, 1, 1, $t_arr[5]));
95                                 $date_end->setTimestamp(mktime(0, 0, 0, 12, 31, $t_arr[5]));
96                         break;
97                 }
98                 $this->startDate        = &$date_begin;
99                 $this->endDate          = &$date_end;
100         }
101
102         /**
103          * Return all days by period
104          *
105          * @return array
106          */
107         function getAllDays() {
108                 $ret_array = array();
109                 if ($this->startDate->before($this->endDate)) {
110                         $d = $this->getBegin();
111                         while ($d->before($this->getEnd())) {
112                                 array_push($ret_array, $d);
113                                 $d = $d->nextDate();
114                         }
115                         array_push($ret_array, $d);
116                 } else {
117                         array_push($ret_array, $this->startDate);
118                 }
119                 return $ret_array;
120         }
121
122         function setPeriod($b_date, $e_date) {
123                 $this->startDate = $b_date;
124                 $this->endDate = $e_date;
125         }
126
127         // return date object
128         function getBegin() {
129                 return $this->startDate;
130         }
131
132         // return date object
133         function getEnd() {
134                 return $this->endDate;
135         }
136
137         // return date string
138         function getBeginDate($format="") {
139                 return $this->startDate->toString($format);
140         }
141
142         // return date string
143         function getEndDate($format="") {
144                 return $this->endDate->toString($format);
145         }
146
147         function getArray($format="") {
148                 $result = array();
149                 $d = $this->getBegin();
150                 while ($d->before($this->getEnd())) {
151                         $result[] = $d->toString($format);
152                         $d = $d->nextDate();
153                 }
154                 return $result;
155         }
156 }