first sql source implementation
[bdrem.git] / src / bdrem / Event.php
1 <?php
2 namespace bdrem;
3
4 class Event
5 {
6     /**
7      * Title of the event or name of the person that has the event
8      */
9     public $title;
10
11     /**
12      * Type of the event, e.g. "birthday"
13      */
14     public $type;
15
16     /**
17      * Date of the event. 
18      * ???? as year is allowed
19      *
20      * @var string YYYY-MM-DD
21      */
22     public $date;
23
24     /**
25      * Reference date against which $age and $days are calculated
26      * (often today)
27      *
28      * @var string YYYY-MM-DD
29      */
30     public $refDate;
31
32     /**
33      * "Localized" $date used for calculations against $refDate.
34      * Month and day are the same as in $date, year is near $refDate's year.
35      *
36      * @var string YYYY-MM-DD
37      */
38     public $localDate;
39
40     /**
41      * Which repetition this is
42      *
43      * @var integer
44      */
45     public $age;
46
47     /**
48      * Number of days until the event (positive) or since the event (negative)
49      *
50      * @var integer
51      */
52     public $days;
53
54
55
56     public function __construct($title = null, $type = null, $date = null)
57     {
58         $this->title = $title;
59         $this->type  = $type;
60         $this->date  = $date;
61     }
62
63     /**
64      * Checks if the event's date is within the given date.
65      * Also calculates the age and days since the event.
66      *
67      * @return boolean True if the event's date is within the given range
68      */
69     public function isWithin($strDate, $nDaysBefore, $nDaysAfter)
70     {
71         $this->refDate = $strDate;
72         list($rYear, $rMonth, $rDay) = explode('-', $strDate);
73         list($eYear, $eMonth, $eDay) = explode('-', $this->date);
74
75         if ($rMonth == $eMonth && $rDay == $eDay) {
76             $this->localDate = $strDate;
77             $this->days = 0;
78             if ($eYear == '????') {
79                 $this->age = null;
80             } else {
81                 $this->age = $rYear - $eYear;
82             }
83             return true;
84         }
85
86         $yearOffset = 0;
87         if ($eMonth < 3 && $rMonth > 10) {
88             $yearOffset = 1;
89         } else if ($eMonth > 10 && $rMonth < 3) {
90             $yearOffset = -1;
91         }
92
93         $this->localDate = ($rYear + $yearOffset) . '-' . $eMonth . '-' . $eDay;
94         $rD = new \DateTime($strDate);
95         $eD = new \DateTime($this->localDate);
96
97         $nDiff = (int) $rD->diff($eD)->format('%r%a');
98
99         $this->days = $nDiff;
100         if ($eYear == '????') {
101             $this->age = null;
102         } else {
103             $this->age = $rYear - $eYear + $yearOffset;
104         }
105
106         if ($nDiff > 0) {
107             return $nDiff <= $nDaysAfter;
108         } else {
109             return -$nDiff <= $nDaysBefore;
110         }
111
112         return false;
113     }
114
115     /**
116      * @return integer x < 0: e1 is less than e2
117      *                 x > 0: e1 is larger than e2
118      */
119     public static function compare(Event $e1, Event $e2)
120     {
121         list($e1Year, $e1Month, $e1Day) = explode('-', $e1->date);
122         list($e2Year, $e2Month, $e2Day) = explode('-', $e2->date);
123         
124         if ($e1Month < 3 && $e2Month > 10) {
125             return 1;
126         } else if ($e1Month > 10 && $e2Month < 3) {
127             return -1;
128         } else if ($e1Month != $e2Month) {
129             return $e1Month - $e2Month;
130         } else if ($e1Day != $e2Day) {
131             return $e1Day - $e2Day;
132         }
133         return strcmp($e1->title, $e2->title);
134     }
135 }
136 ?>