2017-01-24 09:47:14 +03:00
|
|
|
<?php
|
2021-03-04 16:30:21 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-01-24 09:47:14 +03:00
|
|
|
/**
|
|
|
|
* @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-01-24 09:47:14 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-24 09:47:14 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Contacts\ContactsMenu;
|
|
|
|
|
|
|
|
use OCP\Contacts\ContactsMenu\IAction;
|
|
|
|
use OCP\Contacts\ContactsMenu\IEntry;
|
|
|
|
|
|
|
|
class Entry implements IEntry {
|
|
|
|
|
|
|
|
/** @var string|int|null */
|
|
|
|
private $id = null;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $fullName = '';
|
|
|
|
|
|
|
|
/** @var string[] */
|
|
|
|
private $emailAddresses = [];
|
|
|
|
|
|
|
|
/** @var string|null */
|
|
|
|
private $avatar;
|
|
|
|
|
|
|
|
/** @var IAction[] */
|
|
|
|
private $actions = [];
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
private $properties = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $id
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function setId(string $id): void {
|
2017-01-24 09:47:14 +03:00
|
|
|
$this->id = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $displayName
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function setFullName(string $displayName): void {
|
2017-01-24 09:47:14 +03:00
|
|
|
$this->fullName = $displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function getFullName(): string {
|
2017-01-24 09:47:14 +03:00
|
|
|
return $this->fullName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $address
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function addEMailAddress(string $address): void {
|
2017-01-24 09:47:14 +03:00
|
|
|
$this->emailAddresses[] = $address;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-04 16:30:21 +03:00
|
|
|
* @return string[]
|
2017-01-24 09:47:14 +03:00
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function getEMailAddresses(): array {
|
2017-01-24 09:47:14 +03:00
|
|
|
return $this->emailAddresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $avatar
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function setAvatar(string $avatar): void {
|
2017-01-24 09:47:14 +03:00
|
|
|
$this->avatar = $avatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function getAvatar(): ?string {
|
2017-01-24 09:47:14 +03:00
|
|
|
return $this->avatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IAction $action
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function addAction(IAction $action): void {
|
2017-01-24 09:47:14 +03:00
|
|
|
$this->actions[] = $action;
|
|
|
|
$this->sortActions();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return IAction[]
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function getActions(): array {
|
2017-01-24 09:47:14 +03:00
|
|
|
return $this->actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sort the actions by priority and name
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
private function sortActions(): void {
|
2020-04-09 14:53:40 +03:00
|
|
|
usort($this->actions, function (IAction $action1, IAction $action2) {
|
2017-01-24 09:47:14 +03:00
|
|
|
$prio1 = $action1->getPriority();
|
|
|
|
$prio2 = $action2->getPriority();
|
|
|
|
|
|
|
|
if ($prio1 === $prio2) {
|
|
|
|
// Ascending order for same priority
|
|
|
|
return strcasecmp($action1->getName(), $action2->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Descending order when priority differs
|
|
|
|
return $prio2 - $prio1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $contact key-value array containing additional properties
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function setProperties(array $contact): void {
|
2017-01-24 09:47:14 +03:00
|
|
|
$this->properties = $contact;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function getProperty(string $key) {
|
2017-01-24 09:47:14 +03:00
|
|
|
if (!isset($this->properties[$key])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->properties[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-03-04 16:30:21 +03:00
|
|
|
public function jsonSerialize(): array {
|
2017-01-24 09:47:14 +03:00
|
|
|
$topAction = !empty($this->actions) ? $this->actions[0]->jsonSerialize() : null;
|
2020-04-09 14:53:40 +03:00
|
|
|
$otherActions = array_map(function (IAction $action) {
|
2017-01-24 09:47:14 +03:00
|
|
|
return $action->jsonSerialize();
|
|
|
|
}, array_slice($this->actions, 1));
|
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => $this->id,
|
|
|
|
'fullName' => $this->fullName,
|
|
|
|
'avatar' => $this->getAvatar(),
|
|
|
|
'topAction' => $topAction,
|
|
|
|
'actions' => $otherActions,
|
|
|
|
'lastMessage' => '',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|