1<?php
2
3declare(strict_types=1);
4
5/**
6 * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
7 *
8 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
9 * @author Roeland Jago Douma <roeland@famdouma.nl>
10 *
11 * @license GNU AGPL version 3 or any later version
12 *
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Affero General Public License as
15 * published by the Free Software Foundation, either version 3 of the
16 * License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Affero General Public License for more details.
22 *
23 * You should have received a copy of the GNU Affero General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 */
27
28namespace OC\Contacts\ContactsMenu;
29
30use OCP\Contacts\ContactsMenu\IAction;
31use OCP\Contacts\ContactsMenu\IEntry;
32
33class Entry implements IEntry {
34
35	/** @var string|int|null */
36	private $id = null;
37
38	/** @var string */
39	private $fullName = '';
40
41	/** @var string[] */
42	private $emailAddresses = [];
43
44	/** @var string|null */
45	private $avatar;
46
47	/** @var string|null */
48	private $profileTitle;
49
50	/** @var string|null */
51	private $profileUrl;
52
53	/** @var IAction[] */
54	private $actions = [];
55
56	/** @var array */
57	private $properties = [];
58
59	/**
60	 * @param string $id
61	 */
62	public function setId(string $id): void {
63		$this->id = $id;
64	}
65
66	/**
67	 * @param string $displayName
68	 */
69	public function setFullName(string $displayName): void {
70		$this->fullName = $displayName;
71	}
72
73	/**
74	 * @return string
75	 */
76	public function getFullName(): string {
77		return $this->fullName;
78	}
79
80	/**
81	 * @param string $address
82	 */
83	public function addEMailAddress(string $address): void {
84		$this->emailAddresses[] = $address;
85	}
86
87	/**
88	 * @return string[]
89	 */
90	public function getEMailAddresses(): array {
91		return $this->emailAddresses;
92	}
93
94	/**
95	 * @param string $avatar
96	 */
97	public function setAvatar(string $avatar): void {
98		$this->avatar = $avatar;
99	}
100
101	/**
102	 * @return string
103	 */
104	public function getAvatar(): ?string {
105		return $this->avatar;
106	}
107
108	/**
109	 * @param string $profileTitle
110	 */
111	public function setProfileTitle(string $profileTitle): void {
112		$this->profileTitle = $profileTitle;
113	}
114
115	/**
116	 * @return string
117	 */
118	public function getProfileTitle(): ?string {
119		return $this->profileTitle;
120	}
121
122	/**
123	 * @param string $profileUrl
124	 */
125	public function setProfileUrl(string $profileUrl): void {
126		$this->profileUrl = $profileUrl;
127	}
128
129	/**
130	 * @return string
131	 */
132	public function getProfileUrl(): ?string {
133		return $this->profileUrl;
134	}
135
136	/**
137	 * @param IAction $action
138	 */
139	public function addAction(IAction $action): void {
140		$this->actions[] = $action;
141		$this->sortActions();
142	}
143
144	/**
145	 * @return IAction[]
146	 */
147	public function getActions(): array {
148		return $this->actions;
149	}
150
151	/**
152	 * sort the actions by priority and name
153	 */
154	private function sortActions(): void {
155		usort($this->actions, function (IAction $action1, IAction $action2) {
156			$prio1 = $action1->getPriority();
157			$prio2 = $action2->getPriority();
158
159			if ($prio1 === $prio2) {
160				// Ascending order for same priority
161				return strcasecmp($action1->getName(), $action2->getName());
162			}
163
164			// Descending order when priority differs
165			return $prio2 - $prio1;
166		});
167	}
168
169	/**
170	 * @param array $contact key-value array containing additional properties
171	 */
172	public function setProperties(array $contact): void {
173		$this->properties = $contact;
174	}
175
176	/**
177	 * @param string $key
178	 * @return mixed
179	 */
180	public function getProperty(string $key) {
181		if (!isset($this->properties[$key])) {
182			return null;
183		}
184		return $this->properties[$key];
185	}
186
187	/**
188	 * @return array
189	 */
190	public function jsonSerialize(): array {
191		$topAction = !empty($this->actions) ? $this->actions[0]->jsonSerialize() : null;
192		$otherActions = array_map(function (IAction $action) {
193			return $action->jsonSerialize();
194		}, array_slice($this->actions, 1));
195
196		return [
197			'id' => $this->id,
198			'fullName' => $this->fullName,
199			'avatar' => $this->getAvatar(),
200			'topAction' => $topAction,
201			'actions' => $otherActions,
202			'lastMessage' => '',
203			'emailAddresses' => $this->getEMailAddresses(),
204			'profileTitle' => $this->profileTitle,
205			'profileUrl' => $this->profileUrl,
206		];
207	}
208}
209