Passed
Pull Request — master (#1545)
by Julius
03:01
created

Calendar::createFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * @copyright 2020, Georg Ehrke <[email protected]>
4
 *
5
 * @author Georg Ehrke <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\Deck\DAV;
24
25
use OCA\DAV\CalDAV\Integration\ExternalCalendar;
26
use OCA\DAV\CalDAV\Plugin;
27
use OCA\DAV\DAV\Sharing\IShareable;
28
use OCA\Deck\Db\Board;
29
use OCA\Deck\Db\Card;
30
use OCA\Deck\Service\CardService;
31
use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet;
32
use Sabre\DAV\PropPatch;
33
34
class Calendar extends ExternalCalendar implements IShareable {
35
36
	/** @var string */
37
	private $principalUri;
38
39
	/** @var string */
40
	private $calendarUri;
41
42
	/** @var string[] */
43
	private $children;
44
	/**
45
	 * @var \stdClass
46
	 */
47
	private $cardService;
48
49
	/**
50
	 * Calendar constructor.
51
	 *
52
	 * @param string $principalUri
53
	 * @param string $calendarUri
54
	 */
55
	public function __construct(string $principalUri, string $calendarUri, Board $board = null) {
56
		parent::__construct('deck', $calendarUri);
57
58
		$this->board = $board;
59
60
		$this->principalUri = $principalUri;
61
		$this->calendarUri = $calendarUri;
62
63
64
		if ($board) {
65
			/** @var CardService cardService */
66
			$cardService = \OC::$server->query(CardService::class);
67
			$this->children = $cardService->findCalendarEntries($board->getId());
68
		} else {
69
			$this->children = [];
70
		}
71
	}
72
73
74
	/**
75
	 * @inheritDoc
76
	 */
77
	function getOwner() {
78
		return $this->principalUri;
79
	}
80
81
	/**
82
	 * @inheritDoc
83
	 */
84
	function getACL() {
85
		return [
86
			[
87
				'privilege' => '{DAV:}read',
88
				'principal' => $this->getOwner(),
89
				'protected' => true,
90
			],
91
			[
92
				'privilege' => '{DAV:}read',
93
				'principal' => $this->getOwner() . '/calendar-proxy-write',
94
				'protected' => true,
95
			],
96
			[
97
				'privilege' => '{DAV:}read',
98
				'principal' => $this->getOwner() . '/calendar-proxy-read',
99
				'protected' => true,
100
			],
101
		];
102
	}
103
104
	/**
105
	 * @inheritDoc
106
	 */
107
	function setACL(array $acl) {
108
		throw new \Sabre\DAV\Exception\Forbidden('Setting ACL is not supported on this node');
109
	}
110
111
	/**
112
	 * @inheritDoc
113
	 */
114
	function getSupportedPrivilegeSet() {
115
		return null;
116
	}
117
118
	/**
119
	 * @inheritDoc
120
	 */
121
	function calendarQuery(array $filters) {
122
		// In a real implementation this should actually filter
123
		return array_map(function (Card $card) {
124
			return $card->getId() . '.ics';
125
		}, $this->children);
126
	}
127
128
	/**
129
	 * @inheritDoc
130
	 */
131
	function createFile($name, $data = null) {
132
		return null;
133
	}
134
135
	/**
136
	 * @inheritDoc
137
	 */
138
	function getChild($name) {
139
		if ($this->childExists($name)) {
140
			$card = array_values(array_filter(
141
				$this->children,
142
				function ($card) use (&$name) {
143
					return $card->getId() . '.ics' === $name;
144
				}
145
			));
146
			if (count($card) > 0) {
147
				return new CalendarObject($this, $name, $card[0]);
148
			}
149
		}
150
	}
151
152
	/**
153
	 * @inheritDoc
154
	 */
155
	function getChildren() {
156
		$childNames = array_map(function (Card $card) {
157
			return $card->getId() . '.ics';
158
		}, $this->children);
159
160
		$children = [];
161
162
		foreach ($childNames as $name) {
163
			$children[] = $this->getChild($name);
164
		}
165
166
		return $children;
167
	}
168
169
	/**
170
	 * @inheritDoc
171
	 */
172
	function childExists($name) {
173
		return count(array_filter(
174
			$this->children,
175
			function ($card) use (&$name) {
176
				return $card->getId() . '.ics' === $name;
177
			}
178
		)) > 0;
179
	}
180
181
	/**
182
	 * @inheritDoc
183
	 */
184
	function delete() {
185
		return null;
186
	}
187
188
	/**
189
	 * @inheritDoc
190
	 */
191
	function getLastModified() {
192
		return $this->board->getLastModified();
193
	}
194
195
	/**
196
	 * @inheritDoc
197
	 */
198
	function getGroup() {
199
		return [];
200
	}
201
202
	/**
203
	 * @inheritDoc
204
	 */
205
	function propPatch(PropPatch $propPatch) {
206
		// We can just return here and let oc_properties handle everything
207
	}
208
209
	/**
210
	 * @inheritDoc
211
	 */
212
	function getProperties($properties) {
213
		// A backend should provide at least minimum properties
214
		return [
215
			'{DAV:}displayname' => 'Deck: ' . ($this->board ? $this->board->getTitle() : 'no board object provided'),
216
			'{http://apple.com/ns/ical/}calendar-color'  => '#' . $this->board->getColor(),
217
			'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']),
218
		];
219
	}
220
221
	/**
222
	 * @inheritDoc
223
	 */
224
	function updateShares(array $add, array $remove) {
225
		// TODO: Implement updateShares() method.
226
	}
227
228
	/**
229
	 * @inheritDoc
230
	 */
231
	function getShares() {
232
		return [];
233
	}
234
235
	/**
236
	 * @inheritDoc
237
	 */
238
	public function getResourceId() {
239
		// TODO: Implement getResourceId() method.
240
	}
241
}
242