aboutsummaryrefslogtreecommitdiff
path: root/src/bdrem/Event.php
blob: 29585e7c30c57b49471edd3bc167c6ae614c99e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace bdrem;

class Event
{
    /**
     * Title of the event or name of the person that has the event
     */
    public $title;

    /**
     * Type of the event, e.g. "birthday"
     */
    public $type;

    /**
     * Date of the event. 
     * ???? as year is allowed
     *
     * @var string YYYY-MM-DD
     */
    public $date;

    /**
     * Reference date against which $age and $days are calculated
     * (often today)
     *
     * @var string YYYY-MM-DD
     */
    public $refDate;

    /**
     * "Localized" $date used for calculations against $refDate.
     * Month and day are the same as in $date, year is near $refDate's year.
     *
     * @var string YYYY-MM-DD
     */
    public $localDate;

    /**
     * Which repetition this is
     *
     * @var integer
     */
    public $age;

    /**
     * Number of days until the event (positive) or since the event (negative)
     *
     * @var integer
     */
    public $days;



    public function __construct($title = null, $type = null, $date = null)
    {
        $this->title = $title;
        $this->type  = $type;
        $this->date  = $date;
    }

    /**
     * Checks if the event's date is within the given date.
     * Also calculates the age and days since the event.
     *
     * @return boolean True if the event's date is within the given range
     */
    public function isWithin($strDate, $nDaysPrev, $nDaysNext)
    {
        $this->refDate = $strDate;
        list($rYear, $rMonth, $rDay) = explode('-', $strDate);
        list($eYear, $eMonth, $eDay) = explode('-', $this->date);

        if ($rMonth == $eMonth && $rDay == $eDay) {
            $this->localDate = $strDate;
            $this->days = 0;
            if ($eYear == '????') {
                $this->age = null;
            } else {
                $this->age = $rYear - $eYear;
            }
            return true;
        }

        $yearOffset = 0;
        if ($eMonth < 3 && $rMonth > 10) {
            $yearOffset = 1;
        } else if ($eMonth > 10 && $rMonth < 3) {
            $yearOffset = -1;
        }

        $this->localDate = ($rYear + $yearOffset) . '-' . $eMonth . '-' . $eDay;
        $rD = new \DateTime($strDate);
        $eD = new \DateTime($this->localDate);

        $nDiff = (int) $rD->diff($eD)->format('%r%a');

        $this->days = $nDiff;
        if ($eYear == '????') {
            $this->age = null;
        } else {
            $this->age = $rYear - $eYear + $yearOffset;
        }

        if ($nDiff > 0) {
            return $nDiff <= $nDaysNext;
        } else {
            return -$nDiff <= $nDaysPrev;
        }

        return false;
    }

    /**
     * @return integer x < 0: e1 is less than e2
     *                 x > 0: e1 is larger than e2
     */
    public static function compare(Event $e1, Event $e2)
    {
        list($e1Year, $e1Month, $e1Day) = explode('-', $e1->date);
        list($e2Year, $e2Month, $e2Day) = explode('-', $e2->date);
        
        if ($e1Month < 3 && $e2Month > 10) {
            return 1;
        } else if ($e1Month > 10 && $e2Month < 3) {
            return -1;
        } else if ($e1Month != $e2Month) {
            return $e1Month - $e2Month;
        } else if ($e1Day != $e2Day) {
            return $e1Day - $e2Day;
        }
        return strcmp($e1->title, $e2->title);
    }
}
?>