summaryrefslogtreecommitdiff
path: root/includes/composer/ical/ical.php
blob: 84d637c834ecab717b0ed31a15bbc291a6b04799 (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
<?php declare(strict_types=1);

require_once('icalEvent.php');

class iCal
{
    public $Events = array();

    public function __construct(string $content)
    {
        $isUrl = strpos($content, 'http') === 0 && filter_var($content, FILTER_VALIDATE_URL);
        $isFile = strpos($content, "\n") === false && file_exists($content);

        if ($isUrl || $isFile)
        {
            $this->parse(file_get_contents($content));
        }
    }

    protected function parse(string $content) : iCal
    {
        $content = str_replace("\r\n ", '', $content);

        preg_match_all('`BEGIN:VEVENT(.+)END:VEVENT`Us', $content, $matches);
        foreach($matches[0] as $eventContent)
        {
            $this->Events[] = new iCalEvent($eventContent);
        }

        return $this;
    }

    public function getEventsAfterDate(string $date) : array
    {
        $output = array();

        $date = strtotime($date);
        foreach ($this->Events as $event)
        {
            $eventTimestamp = strtotime($event->startDateTime);
            if ($eventTimestamp >= $date)
            {
                $output[] = $event;
            }
        }

        asort($output);
        return $output;
    }

    public function getActiveEvents() : array
    {
        $output = array();

        $currentDate = strtotime(date('Y-m-d'));
        foreach ($this->Events as $event)
        {
            $eventStartTimestamp = strtotime($event->startDateTime);
            $eventEndTimestamp = strtotime($event->endDateTime);
            if ($currentDate >= $eventStartTimestamp && $currentDate <= $eventEndTimestamp)
            {
                $output[] = $event;
            }
        }

        asort($output);
        return $output;
    }
}