use ANSI color codes on shell by default
[bdrem.git] / src / bdrem / Event.php
1 <?php
2 /**
3  * Part of bdrem
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Bdrem
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/bdrem.htm
13  */
14 namespace bdrem;
15
16 /**
17  * Event model with title, type and date.
18  * Contains calculation methods
19  *
20  * @category  Tools
21  * @package   Bdrem
22  * @author    Christian Weiske <cweiske@cweiske.de>
23  * @copyright 2014 Christian Weiske
24  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
25  * @version   Release: @package_version@
26  * @link      http://cweiske.de/bdrem.htm
27  */
28 class Event
29 {
30     /**
31      * Title of the event or name of the person that has the event
32      * @var string
33      */
34     public $title;
35
36     /**
37      * Type of the event, e.g. "birthday"
38      * @var string
39      */
40     public $type;
41
42     /**
43      * Date of the event.
44      * ???? as year is allowed
45      *
46      * @var string YYYY-MM-DD
47      */
48     public $date;
49
50     /**
51      * Reference date against which $age and $days are calculated
52      * (often today)
53      *
54      * @var string YYYY-MM-DD
55      */
56     public $refDate;
57
58     /**
59      * "Localized" $date used for calculations against $refDate.
60      * Month and day are the same as in $date, year is near $refDate's year.
61      *
62      * @var string YYYY-MM-DD
63      */
64     public $localDate;
65
66     /**
67      * Which repetition this is
68      *
69      * @var integer
70      */
71     public $age;
72
73     /**
74      * Number of days until the event (positive) or since the event (negative)
75      *
76      * @var integer
77      */
78     public $days;
79
80
81
82     /**
83      * Set event data
84      *
85      * @param string $title Name of person the event relates to
86      * @param string $type  Type of the event (e.g. "birthday")
87      * @param string $date  Date of the event in format YYYY-MM-DD
88      */
89     public function __construct($title = null, $type = null, $date = null)
90     {
91         $this->title = $title;
92         $this->type  = $type;
93         $this->date  = $date;
94     }
95
96     /**
97      * Checks if the event's date is within the given date.
98      * Also calculates the age and days since the event.
99      *
100      * @param string  $strDate       Date, YYYY-MM-DD
101      * @param integer $nDaysPrevious Include number of days before $strDate
102      * @param integer $nDaysNext     Include number of days after $strDate
103      *
104      * @return boolean True if the event's date is within the given range
105      */
106     public function isWithin($strDate, $nDaysPrevious, $nDaysNext)
107     {
108         $this->refDate = $strDate;
109         list($rYear, $rMonth, $rDay) = explode('-', $strDate);
110         list($eYear, $eMonth, $eDay) = explode('-', $this->date);
111
112         if ($rMonth == $eMonth && $rDay == $eDay) {
113             $this->localDate = $strDate;
114             $this->days = 0;
115             if ($eYear == '????') {
116                 $this->age = null;
117             } else {
118                 $this->age = $rYear - $eYear;
119             }
120             return true;
121         }
122
123         $yearOffset = 0;
124         if ($eMonth < 3 && $rMonth > 10) {
125             $yearOffset = 1;
126         } else if ($eMonth > 10 && $rMonth < 3) {
127             $yearOffset = -1;
128         }
129
130         $this->localDate = ($rYear + $yearOffset) . '-' . $eMonth . '-' . $eDay;
131         $rD = new \DateTime($strDate);
132         $eD = new \DateTime($this->localDate);
133
134         $nDiff = (int) $rD->diff($eD)->format('%r%a');
135
136         $this->days = $nDiff;
137         if ($eYear == '????') {
138             $this->age = null;
139         } else {
140             $this->age = $rYear - $eYear + $yearOffset;
141         }
142
143         if ($nDiff > 0) {
144             return $nDiff <= $nDaysNext;
145         } else {
146             return -$nDiff <= $nDaysPrevious;
147         }
148
149         return false;
150     }
151
152     /**
153      * Compare two events by by their date, then by their title.
154      * Used for sorting
155      *
156      * @param Event $e1 Event #1
157      * @param Event $e2 Event #2
158      *
159      * @return integer x < 0: e1 is less than e2
160      *                 x > 0: e1 is larger than e2
161      */
162     public static function compare(Event $e1, Event $e2)
163     {
164         list($e1Year, $e1Month, $e1Day) = explode('-', $e1->date);
165         list($e2Year, $e2Month, $e2Day) = explode('-', $e2->date);
166
167         if ($e1Month < 3 && $e2Month > 10) {
168             return 1;
169         } else if ($e1Month > 10 && $e2Month < 3) {
170             return -1;
171         } else if ($e1Month != $e2Month) {
172             return $e1Month - $e2Month;
173         } else if ($e1Day != $e2Day) {
174             return $e1Day - $e2Day;
175         }
176         return strcmp($e1->title, $e2->title);
177     }
178 }
179 ?>