Entry.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. class Entry implements IEntry {
  29. /** @var string|int|null */
  30. private $id = null;
  31. private string $fullName = '';
  32. /** @var string[] */
  33. private array $emailAddresses = [];
  34. private ?string $avatar = null;
  35. private ?string $profileTitle = null;
  36. private ?string $profileUrl = null;
  37. /** @var IAction[] */
  38. private array $actions = [];
  39. private array $properties = [];
  40. public function setId(string $id): void {
  41. $this->id = $id;
  42. }
  43. public function setFullName(string $displayName): void {
  44. $this->fullName = $displayName;
  45. }
  46. public function getFullName(): string {
  47. return $this->fullName;
  48. }
  49. public function addEMailAddress(string $address): void {
  50. $this->emailAddresses[] = $address;
  51. }
  52. /**
  53. * @return string[]
  54. */
  55. public function getEMailAddresses(): array {
  56. return $this->emailAddresses;
  57. }
  58. public function setAvatar(string $avatar): void {
  59. $this->avatar = $avatar;
  60. }
  61. public function getAvatar(): ?string {
  62. return $this->avatar;
  63. }
  64. public function setProfileTitle(string $profileTitle): void {
  65. $this->profileTitle = $profileTitle;
  66. }
  67. public function getProfileTitle(): ?string {
  68. return $this->profileTitle;
  69. }
  70. public function setProfileUrl(string $profileUrl): void {
  71. $this->profileUrl = $profileUrl;
  72. }
  73. public function getProfileUrl(): ?string {
  74. return $this->profileUrl;
  75. }
  76. public function addAction(IAction $action): void {
  77. $this->actions[] = $action;
  78. $this->sortActions();
  79. }
  80. /**
  81. * @return IAction[]
  82. */
  83. public function getActions(): array {
  84. return $this->actions;
  85. }
  86. /**
  87. * sort the actions by priority and name
  88. */
  89. private function sortActions(): void {
  90. usort($this->actions, function (IAction $action1, IAction $action2) {
  91. $prio1 = $action1->getPriority();
  92. $prio2 = $action2->getPriority();
  93. if ($prio1 === $prio2) {
  94. // Ascending order for same priority
  95. return strcasecmp($action1->getName(), $action2->getName());
  96. }
  97. // Descending order when priority differs
  98. return $prio2 - $prio1;
  99. });
  100. }
  101. /**
  102. * @param array $contact key-value array containing additional properties
  103. */
  104. public function setProperties(array $contact): void {
  105. $this->properties = $contact;
  106. }
  107. /**
  108. * @param string $key
  109. * @return mixed
  110. */
  111. public function getProperty(string $key) {
  112. if (!isset($this->properties[$key])) {
  113. return null;
  114. }
  115. return $this->properties[$key];
  116. }
  117. /**
  118. * @return array
  119. */
  120. public function jsonSerialize(): array {
  121. $topAction = !empty($this->actions) ? $this->actions[0]->jsonSerialize() : null;
  122. $otherActions = array_map(function (IAction $action) {
  123. return $action->jsonSerialize();
  124. }, array_slice($this->actions, 1));
  125. return [
  126. 'id' => $this->id,
  127. 'fullName' => $this->fullName,
  128. 'avatar' => $this->getAvatar(),
  129. 'topAction' => $topAction,
  130. 'actions' => $otherActions,
  131. 'lastMessage' => '',
  132. 'emailAddresses' => $this->getEMailAddresses(),
  133. 'profileTitle' => $this->profileTitle,
  134. 'profileUrl' => $this->profileUrl,
  135. ];
  136. }
  137. }