Entry.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Contacts\ContactsMenu;
  8. use OCP\Contacts\ContactsMenu\IAction;
  9. use OCP\Contacts\ContactsMenu\IEntry;
  10. use function array_merge;
  11. class Entry implements IEntry {
  12. public const PROPERTY_STATUS_MESSAGE_TIMESTAMP = 'statusMessageTimestamp';
  13. /** @var string|int|null */
  14. private $id = null;
  15. private string $fullName = '';
  16. /** @var string[] */
  17. private array $emailAddresses = [];
  18. private ?string $avatar = null;
  19. private ?string $profileTitle = null;
  20. private ?string $profileUrl = null;
  21. /** @var IAction[] */
  22. private array $actions = [];
  23. private array $properties = [];
  24. private ?string $status = null;
  25. private ?string $statusMessage = null;
  26. private ?int $statusMessageTimestamp = null;
  27. private ?string $statusIcon = null;
  28. public function setId(string $id): void {
  29. $this->id = $id;
  30. }
  31. public function setFullName(string $displayName): void {
  32. $this->fullName = $displayName;
  33. }
  34. public function getFullName(): string {
  35. return $this->fullName;
  36. }
  37. public function addEMailAddress(string $address): void {
  38. $this->emailAddresses[] = $address;
  39. }
  40. /**
  41. * @return string[]
  42. */
  43. public function getEMailAddresses(): array {
  44. return $this->emailAddresses;
  45. }
  46. public function setAvatar(string $avatar): void {
  47. $this->avatar = $avatar;
  48. }
  49. public function getAvatar(): ?string {
  50. return $this->avatar;
  51. }
  52. public function setProfileTitle(string $profileTitle): void {
  53. $this->profileTitle = $profileTitle;
  54. }
  55. public function getProfileTitle(): ?string {
  56. return $this->profileTitle;
  57. }
  58. public function setProfileUrl(string $profileUrl): void {
  59. $this->profileUrl = $profileUrl;
  60. }
  61. public function getProfileUrl(): ?string {
  62. return $this->profileUrl;
  63. }
  64. public function addAction(IAction $action): void {
  65. $this->actions[] = $action;
  66. $this->sortActions();
  67. }
  68. public function setStatus(string $status,
  69. ?string $statusMessage = null,
  70. ?int $statusMessageTimestamp = null,
  71. ?string $icon = null): void {
  72. $this->status = $status;
  73. $this->statusMessage = $statusMessage;
  74. $this->statusMessageTimestamp = $statusMessageTimestamp;
  75. $this->statusIcon = $icon;
  76. }
  77. /**
  78. * @return IAction[]
  79. */
  80. public function getActions(): array {
  81. return $this->actions;
  82. }
  83. /**
  84. * sort the actions by priority and name
  85. */
  86. private function sortActions(): void {
  87. usort($this->actions, function (IAction $action1, IAction $action2) {
  88. $prio1 = $action1->getPriority();
  89. $prio2 = $action2->getPriority();
  90. if ($prio1 === $prio2) {
  91. // Ascending order for same priority
  92. return strcasecmp($action1->getName(), $action2->getName());
  93. }
  94. // Descending order when priority differs
  95. return $prio2 - $prio1;
  96. });
  97. }
  98. public function setProperty(string $propertyName, mixed $value) {
  99. $this->properties[$propertyName] = $value;
  100. }
  101. /**
  102. * @param array $properties key-value array containing additional properties
  103. */
  104. public function setProperties(array $properties): void {
  105. $this->properties = array_merge($this->properties, $properties);
  106. }
  107. public function getProperty(string $key): mixed {
  108. if (!isset($this->properties[$key])) {
  109. return null;
  110. }
  111. return $this->properties[$key];
  112. }
  113. /**
  114. * @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}
  115. */
  116. public function jsonSerialize(): array {
  117. $topAction = !empty($this->actions) ? $this->actions[0]->jsonSerialize() : null;
  118. $otherActions = array_map(function (IAction $action) {
  119. return $action->jsonSerialize();
  120. }, array_slice($this->actions, 1));
  121. return [
  122. 'id' => $this->id,
  123. 'fullName' => $this->fullName,
  124. 'avatar' => $this->getAvatar(),
  125. 'topAction' => $topAction,
  126. 'actions' => $otherActions,
  127. 'lastMessage' => '',
  128. 'emailAddresses' => $this->getEMailAddresses(),
  129. 'profileTitle' => $this->profileTitle,
  130. 'profileUrl' => $this->profileUrl,
  131. 'status' => $this->status,
  132. 'statusMessage' => $this->statusMessage,
  133. 'statusMessageTimestamp' => $this->statusMessageTimestamp,
  134. 'statusIcon' => $this->statusIcon,
  135. 'isUser' => $this->getProperty('isUser') === true,
  136. 'uid' => $this->getProperty('UID'),
  137. ];
  138. }
  139. public function getStatusMessage(): ?string {
  140. return $this->statusMessage;
  141. }
  142. public function getStatusMessageTimestamp(): ?int {
  143. return $this->statusMessageTimestamp;
  144. }
  145. }