98b8e0061e77514802ae84de512d4ff5f1a14259
[bdrem.git] / src / bdrem / Source / vCard.php
1 <?php
2 /**
3  * Part of bdrem
4  *
5  * PHP version 7
6  *
7  * @category  Tools
8  * @package   Bdrem
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2023 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 use Sabre\VObject;
17 use Sabre\VObject\Component\VCard;
18
19 /**
20  * Read a folder of vcard files
21  * 2 subfolder levels are supported.
22  *
23  * @category  Tools
24  * @package   Bdrem
25  * @author    Christian Weiske <cweiske@cweiske.de>
26  * @copyright 2023 Christian Weiske
27  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
28  * @link      http://cweiske.de/bdrem.htm
29  */
30 class Source_vCard
31 {
32     /**
33      * Full path to a folder with .vcf files
34      */
35     protected string $folder;
36
37     /**
38      * Set the VCard folder path
39      */
40     public function __construct(array $config)
41     {
42         $this->folder = $config['folder'];
43         if (!is_dir($this->folder)) {
44             throw new \Exception(
45                 'VCard folder does not exist: ' . $this->folder
46             );
47         }
48     }
49
50     /**
51      * Return all events for the given date range
52      *
53      * @param string  $strDate       Date the events shall be found for,
54      *                               YYYY-MM-DD
55      * @param integer $nDaysPrevious Include number of days before $strDate
56      * @param integer $nDaysNext     Include number of days after $strDate
57      *
58      * @return Event[] Array of matching event objects
59      */
60     public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
61     {
62         $vcfFiles = glob($this->folder . '/{*,*/*,*/*/*}.vcf', GLOB_BRACE);
63         if (count($vcfFiles) == 0) {
64             throw new \Exception('No .vcf files found in folder');
65         }
66
67         $arEvents = [];
68         foreach ($vcfFiles as $vcfFile) {
69             $vcard = VObject\Reader::read(file_get_contents($vcfFile));
70
71             if (isset($vcard->BDAY)) {
72                 $event = new Event(
73                     $this->getName($vcard),
74                     'Birthday',
75                     $vcard->BDAY->getDateTime()->format('Y-m-d')
76                 );
77                 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
78                     $arEvents[] = $event;
79                 }
80             }
81
82             if (isset($vcard->{'X-ANNIVERSARY'})) {
83                 $event = new Event(
84                     $this->getName($vcard),
85                     'Anniversary',
86                     $vcard->{'X-ANNIVERSARY'}->getDateTime()->format('Y-m-d')
87                 );
88                 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
89                     $arEvents[] = $event;
90                 }
91             }
92         }
93
94         return $arEvents;
95     }
96
97     protected function getName(VCard $vcard)
98     {
99         return (string) $vcard->FN;
100     }
101 }
102 ?>