1<?php
2
3declare(strict_types=1);
4
5/**
6 * @copyright 2021 Anna Larch <anna.larch@gmx.net>
7 *
8 * @author Anna Larch <anna.larch@gmx.net>
9 *
10 * @license GNU AGPL version 3 or any later version
11 *
12 * This code is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Affero General Public License, version 3,
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License, version 3,
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23 *
24 */
25
26namespace OCA\Calendar\Db;
27
28use JsonSerializable;
29use OCP\AppFramework\Db\Entity;
30use function json_decode;
31use function json_encode;
32
33/**
34 * @method int getId()
35 * @method void setId(int $id)
36 * @method string getToken()
37 * @method void setToken(string $token)
38 * @method string getName()
39 * @method void setName(string $name)
40 * @method string|null getDescription()
41 * @method void setDescription(?string $description)
42 * @method string getLocation()
43 * @method void setLocation(?string $location)
44 * @method string getVisibility()
45 * @method void setVisibility(string $visibility)
46 * @method string getUserId()
47 * @method void setUserId(string $userId)
48 * @method string getTargetCalendarUri()
49 * @method void setTargetCalendarUri(string $targetCalendarUri)
50 * @method string|null getCalendarFreebusyUris()
51 * @method void setCalendarFreebusyUris(?string $freebusyUris)
52 * @method string getAvailability()
53 * @method void setAvailability(?string $availability)
54 * @method int|null getStart()
55 * @method void setStart(?int $start)
56 * @method int|null getEnd()
57 * @method void setEnd(?int $end)
58 * @method int getLength()
59 * @method void setLength(int $length)
60 * @method int getIncrement()
61 * @method void setIncrement(int $increment)
62 * @method int getPreparationDuration()
63 * @method void setPreparationDuration(int $prepDuration)
64 * @method int getFollowupDuration()
65 * @method void setFollowupDuration(int $followup)
66 * @method int getTimeBeforeNextSlot()
67 * @method void setTimeBeforeNextSlot(int $buffer)
68 * @method int|null getDailyMax()
69 * @method void setDailyMax(?int $max)
70 */
71class AppointmentConfig extends Entity implements JsonSerializable {
72
73	/** @var string */
74	protected $token;
75
76	/** @var string */
77	protected $name = '';
78
79	/** @var string|null */
80	protected $description;
81
82	/** @var string|null */
83	protected $location;
84
85	/** @var string */
86	protected $visibility;
87
88	/** @var string */
89	protected $userId;
90
91	/** @var string */
92	protected $targetCalendarUri;
93
94	/** @var string|null */
95	protected $calendarFreebusyUris;
96
97	/** @var string|null */
98	protected $availability;
99
100	/** @var int|null */
101	protected $start;
102
103	/** @var int|null */
104	protected $end;
105
106	/** @var int */
107	protected $length;
108
109	/** @var int */
110	protected $increment;
111
112	/** @var int */
113	protected $preparationDuration;
114
115	/** @var int */
116	protected $followupDuration;
117
118	/** @var int */
119	protected $timeBeforeNextSlot;
120
121	/** @var int|null */
122	protected $dailyMax;
123
124	/** @var string */
125	public const VISIBILITY_PUBLIC = 'PUBLIC';
126
127	/** @var string */
128	public const VISIBILITY_PRIVATE = 'PRIVATE';
129
130	public function __construct() {
131		$this->addType('start', 'int');
132		$this->addType('end', 'int');
133		$this->addType('length', 'int');
134		$this->addType('increment', 'int');
135		$this->addType('preparationDuration', 'int');
136		$this->addType('followupDuration', 'int');
137		$this->addType('timeBeforeNextSlot', 'int');
138		$this->addType('dailyMax', 'int');
139	}
140
141	/**
142	 * Total length of one slot of the appointment config
143	 * in minutes
144	 *
145	 * @return int  Minutes of Appointment slot length
146	 */
147	public function getTotalLength(): int {
148		return $this->getLength() + $this->getPreparationDuration() + $this->getFollowupDuration();
149	}
150
151	/**
152	 * Principals always have the same format
153	 *
154	 * @return string
155	 */
156	public function getPrincipalUri(): string {
157		return 'principals/users/' . $this->userId;
158	}
159
160	public function getCalendarFreebusyUrisAsArray(): array {
161		return json_decode($this->getCalendarFreebusyUris(), true);
162	}
163
164	/**
165	 * @param string[] $uris
166	 */
167	public function setCalendarFreeBusyUrisAsArray(array $uris): self {
168		$this->setCalendarFreebusyUris(json_encode($uris));
169
170		return $this;
171	}
172
173	public function setAvailabilityAsArray(array $availability): self {
174		$this->setAvailability(json_encode($availability));
175
176		return $this;
177	}
178
179	public function jsonSerialize() {
180		return [
181			'id' => $this->id,
182			'token' => $this->getToken(),
183			'name' => $this->getName(),
184			'description' => $this->getDescription(),
185			'location' => $this->getLocation(),
186			'visibility' => $this->getVisibility(),
187			'userId' => $this->getUserId(),
188			'targetCalendarUri' => $this->getTargetCalendarUri(),
189			'calendarFreeBusyUris' => $this->getCalendarFreebusyUrisAsArray(),
190			'availability' => $this->getAvailability() === null ? null : json_decode($this->getAvailability(), true),
191			'start' => $this->getStart(),
192			'end' => $this->getEnd(),
193			'length' => $this->getLength(),
194			'increment' => $this->getIncrement(),
195			'preparationDuration' => $this->getPreparationDuration(),
196			'followUpDuration' => $this->getFollowupDuration(),
197			'totalLength' => $this->getTotalLength(),
198			'timeBeforeNextSlot' => $this->getTimeBeforeNextSlot(),
199			'dailyMax' => $this->getDailyMax()
200		];
201	}
202}
203