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