1
0

Entry.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Contacts\ContactsMenu;
  26. use OCP\Contacts\ContactsMenu\IAction;
  27. use OCP\Contacts\ContactsMenu\IEntry;
  28. use function array_merge;
  29. class Entry implements IEntry {
  30. public const PROPERTY_STATUS_MESSAGE_TIMESTAMP = 'statusMessageTimestamp';
  31. /** @var string|int|null */
  32. private $id = null;
  33. private string $fullName = '';
  34. /** @var string[] */
  35. private array $emailAddresses = [];
  36. private ?string $avatar = null;
  37. private ?string $profileTitle = null;
  38. private ?string $profileUrl = null;
  39. /** @var IAction[] */
  40. private array $actions = [];
  41. private array $properties = [];
  42. private ?string $status = null;
  43. private ?string $statusMessage = null;
  44. private ?int $statusMessageTimestamp = null;
  45. private ?string $statusIcon = null;
  46. public function setId(string $id): void {
  47. $this->id = $id;
  48. }
  49. public function setFullName(string $displayName): void {
  50. $this->fullName = $displayName;
  51. }
  52. public function getFullName(): string {
  53. return $this->fullName;
  54. }
  55. public function addEMailAddress(string $address): void {
  56. $this->emailAddresses[] = $address;
  57. }
  58. /**
  59. * @return string[]
  60. */
  61. public function getEMailAddresses(): array {
  62. return $this->emailAddresses;
  63. }
  64. public function setAvatar(string $avatar): void {
  65. $this->avatar = $avatar;
  66. }
  67. public function getAvatar(): ?string {
  68. return $this->avatar;
  69. }
  70. public function setProfileTitle(string $profileTitle): void {
  71. $this->profileTitle = $profileTitle;
  72. }
  73. public function getProfileTitle(): ?string {
  74. return $this->profileTitle;
  75. }
  76. public function setProfileUrl(string $profileUrl): void {
  77. $this->profileUrl = $profileUrl;
  78. }
  79. public function getProfileUrl(): ?string {
  80. return $this->profileUrl;
  81. }
  82. public function addAction(IAction $action): void {
  83. $this->actions[] = $action;
  84. $this->sortActions();
  85. }
  86. public function setStatus(string $status,
  87. string $statusMessage = null,
  88. int $statusMessageTimestamp = null,
  89. string $icon = null): void {
  90. $this->status = $status;
  91. $this->statusMessage = $statusMessage;
  92. $this->statusMessageTimestamp = $statusMessageTimestamp;
  93. $this->statusIcon = $icon;
  94. }
  95. /**
  96. * @return IAction[]
  97. */
  98. public function getActions(): array {
  99. return $this->actions;
  100. }
  101. /**
  102. * sort the actions by priority and name
  103. */
  104. private function sortActions(): void {
  105. usort($this->actions, function (IAction $action1, IAction $action2) {
  106. $prio1 = $action1->getPriority();
  107. $prio2 = $action2->getPriority();
  108. if ($prio1 === $prio2) {
  109. // Ascending order for same priority
  110. return strcasecmp($action1->getName(), $action2->getName());
  111. }
  112. // Descending order when priority differs
  113. return $prio2 - $prio1;
  114. });
  115. }
  116. public function setProperty(string $propertyName, mixed $value) {
  117. $this->properties[$propertyName] = $value;
  118. }
  119. /**
  120. * @param array $properties key-value array containing additional properties
  121. */
  122. public function setProperties(array $properties): void {
  123. $this->properties = array_merge($this->properties, $properties);
  124. }
  125. public function getProperty(string $key): mixed {
  126. if (!isset($this->properties[$key])) {
  127. return null;
  128. }
  129. return $this->properties[$key];
  130. }
  131. /**
  132. * @return array{id: int|string|null, fullName: string, avatar: string|null, topAction: mixed, actions: array, lastMessage: '', emailAddresses: string[], profileTitle: string|null, profileUrl: string|null, status: string|null, statusMessage: null|string, statusMessageTimestamp: null|int, statusIcon: null|string, isUser: bool, uid: mixed}
  133. */
  134. public function jsonSerialize(): array {
  135. $topAction = !empty($this->actions) ? $this->actions[0]->jsonSerialize() : null;
  136. $otherActions = array_map(function (IAction $action) {
  137. return $action->jsonSerialize();
  138. }, array_slice($this->actions, 1));
  139. return [
  140. 'id' => $this->id,
  141. 'fullName' => $this->fullName,
  142. 'avatar' => $this->getAvatar(),
  143. 'topAction' => $topAction,
  144. 'actions' => $otherActions,
  145. 'lastMessage' => '',
  146. 'emailAddresses' => $this->getEMailAddresses(),
  147. 'profileTitle' => $this->profileTitle,
  148. 'profileUrl' => $this->profileUrl,
  149. 'status' => $this->status,
  150. 'statusMessage' => $this->statusMessage,
  151. 'statusMessageTimestamp' => $this->statusMessageTimestamp,
  152. 'statusIcon' => $this->statusIcon,
  153. 'isUser' => $this->getProperty('isUser') === true,
  154. 'uid' => $this->getProperty('UID'),
  155. ];
  156. }
  157. public function getStatusMessage(): ?string {
  158. return $this->statusMessage;
  159. }
  160. public function getStatusMessageTimestamp(): ?int {
  161. return $this->statusMessageTimestamp;
  162. }
  163. }