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
|
<?php
/**
* Part of bdrem
*
* PHP version 7
*
* @category Tools
* @package Bdrem
* @author Christian Weiske <cweiske@cweiske.de>
* @copyright 2023 Christian Weiske
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
* @link http://cweiske.de/bdrem.htm
*/
namespace bdrem;
use Sabre\VObject;
use Sabre\VObject\Component\VCard;
/**
* Read a folder of vcard files
* 2 subfolder levels are supported.
*
* @category Tools
* @package Bdrem
* @author Christian Weiske <cweiske@cweiske.de>
* @copyright 2023 Christian Weiske
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
* @link http://cweiske.de/bdrem.htm
*/
class Source_vCard
{
/**
* Full path to a folder with .vcf files
*/
protected string $folder;
/**
* Set the VCard folder path
*/
public function __construct($config)
{
if (is_string($config)) {
$config = array(
'folder' => $config
);
}
$this->folder = $config['folder'];
if (!is_dir($this->folder)) {
throw new \Exception(
'VCard folder does not exist: ' . $this->folder
);
}
}
/**
* Return all events for the given date range
*
* @param string $strDate Date the events shall be found for,
* YYYY-MM-DD
* @param integer $nDaysPrevious Include number of days before $strDate
* @param integer $nDaysNext Include number of days after $strDate
*
* @return Event[] Array of matching event objects
*/
public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
{
$vcfFiles = glob($this->folder . '/{*,*/*,*/*/*}.vcf', GLOB_BRACE);
if (count($vcfFiles) == 0) {
throw new \Exception('No .vcf files found in folder');
}
$arEvents = [];
foreach ($vcfFiles as $vcfFile) {
$vcard = VObject\Reader::read(file_get_contents($vcfFile));
if (isset($vcard->BDAY)) {
$event = new Event(
$this->getName($vcard),
'Birthday',
$vcard->BDAY->getDateTime()->format('Y-m-d')
);
if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
$arEvents[] = $event;
}
}
if (isset($vcard->{'X-ANNIVERSARY'})) {
$event = new Event(
$this->getName($vcard),
'Anniversary',
$vcard->{'X-ANNIVERSARY'}->getDateTime()->format('Y-m-d')
);
if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
$arEvents[] = $event;
}
}
}
return $arEvents;
}
protected function getName(VCard $vcard)
{
return (string) $vcard->FN;
}
}
?>
|