-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathBirthdayCalendarPlugin.php
More file actions
100 lines (78 loc) 路 2.7 KB
/
Copy pathBirthdayCalendarPlugin.php
File metadata and controls
100 lines (78 loc) 路 2.7 KB
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
<?php
namespace App\Plugins;
use App\Services\BirthdayService;
use Sabre\CalDAV\Backend\PDO as CalendarBackend;
use Sabre\CardDAV;
use Sabre\DAV;
class BirthdayCalendarPlugin extends DAV\ServerPlugin
{
/**
* @var BirthdayService
*/
protected $birthdayService;
/**
* @var DAV\Server
*/
protected $server;
public function __construct(BirthdayService $birthdayService, CalendarBackend $calendarBackend)
{
$this->birthdayService = $birthdayService;
$this->birthdayService->setBackend($calendarBackend);
}
public function initialize(DAV\Server $server)
{
$this->server = $server;
// Hook into card creation
$server->on('afterCreateFile', [$this, 'afterCardCreate']);
// Hook into card updates
$server->on('afterWriteContent', [$this, 'afterCardUpdate']);
// Hook into card deletion
// Note: The node no longer exists at afterCardDelete so we
// use beforeCardDelete for simplicity
$server->on('beforeUnbind', [$this, 'beforeCardDelete']);
}
public function afterCardCreate(string $path, DAV\ICollection $parentNode): void
{
if (!$parentNode instanceof CardDAV\AddressBook) {
return;
}
$this->handleCardChange($path, $parentNode);
}
public function afterCardUpdate(string $path, DAV\IFile $node): void
{
if (!$node instanceof CardDAV\ICard) {
return;
}
$parentPath = dirname($path);
$parentNode = $this->server->tree->getNodeForPath($parentPath);
if (!$parentNode instanceof CardDAV\AddressBook) {
return;
}
$this->handleCardChange($path, $parentNode);
}
public function beforeCardDelete(string $path): void
{
$node = $this->server->tree->getNodeForPath($path);
if (!$node instanceof CardDAV\ICard) {
return;
}
$parentPath = dirname($path);
$parentNode = $this->server->tree->getNodeForPath($parentPath);
if (!$parentNode instanceof CardDAV\AddressBook) {
return;
}
$addressBookId = $parentNode->getProperties(['id'])['id'];
$this->birthdayService->onCardDeleted($addressBookId, basename($path));
}
private function handleCardChange(string $path, CardDAV\AddressBook $parentNode): void
{
$cardUri = basename($path);
$addressBookId = $parentNode->getProperties(['id'])['id'];
$cardNode = $this->server->tree->getNodeForPath($path);
$this->birthdayService->onCardChanged($addressBookId, $cardUri, $cardNode->get());
}
public function getPluginName(): string
{
return 'birthday-calendar';
}
}