You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
3.5 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Contacts\ContactsMenu;
  24. use OCP\Contacts\ContactsMenu\IAction;
  25. use OCP\Contacts\ContactsMenu\IEntry;
  26. class Entry implements IEntry {
  27. /** @var string|int|null */
  28. private $id = null;
  29. /** @var string */
  30. private $fullName = '';
  31. /** @var string[] */
  32. private $emailAddresses = [];
  33. /** @var string|null */
  34. private $avatar;
  35. /** @var IAction[] */
  36. private $actions = [];
  37. /** @var array */
  38. private $properties = [];
  39. /**
  40. * @param string $id
  41. */
  42. public function setId($id) {
  43. $this->id = $id;
  44. }
  45. /**
  46. * @param string $displayName
  47. */
  48. public function setFullName($displayName) {
  49. $this->fullName = $displayName;
  50. }
  51. /**
  52. * @return string
  53. */
  54. public function getFullName() {
  55. return $this->fullName;
  56. }
  57. /**
  58. * @param string $address
  59. */
  60. public function addEMailAddress($address) {
  61. $this->emailAddresses[] = $address;
  62. }
  63. /**
  64. * @return string
  65. */
  66. public function getEMailAddresses() {
  67. return $this->emailAddresses;
  68. }
  69. /**
  70. * @param string $avatar
  71. */
  72. public function setAvatar($avatar) {
  73. $this->avatar = $avatar;
  74. }
  75. /**
  76. * @return string
  77. */
  78. public function getAvatar() {
  79. return $this->avatar;
  80. }
  81. /**
  82. * @param IAction $action
  83. */
  84. public function addAction(IAction $action) {
  85. $this->actions[] = $action;
  86. $this->sortActions();
  87. }
  88. /**
  89. * @return IAction[]
  90. */
  91. public function getActions() {
  92. return $this->actions;
  93. }
  94. /**
  95. * sort the actions by priority and name
  96. */
  97. private function sortActions() {
  98. usort($this->actions, function (IAction $action1, IAction $action2) {
  99. $prio1 = $action1->getPriority();
  100. $prio2 = $action2->getPriority();
  101. if ($prio1 === $prio2) {
  102. // Ascending order for same priority
  103. return strcasecmp($action1->getName(), $action2->getName());
  104. }
  105. // Descending order when priority differs
  106. return $prio2 - $prio1;
  107. });
  108. }
  109. /**
  110. * @param array $contact key-value array containing additional properties
  111. */
  112. public function setProperties(array $contact) {
  113. $this->properties = $contact;
  114. }
  115. /**
  116. * @param string $key
  117. * @return mixed
  118. */
  119. public function getProperty($key) {
  120. if (!isset($this->properties[$key])) {
  121. return null;
  122. }
  123. return $this->properties[$key];
  124. }
  125. /**
  126. * @return array
  127. */
  128. public function jsonSerialize() {
  129. $topAction = !empty($this->actions) ? $this->actions[0]->jsonSerialize() : null;
  130. $otherActions = array_map(function (IAction $action) {
  131. return $action->jsonSerialize();
  132. }, array_slice($this->actions, 1));
  133. return [
  134. 'id' => $this->id,
  135. 'fullName' => $this->fullName,
  136. 'avatar' => $this->getAvatar(),
  137. 'topAction' => $topAction,
  138. 'actions' => $otherActions,
  139. 'lastMessage' => '',
  140. ];
  141. }
  142. }