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.

122 lines
3.4 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Contacts\ContactsMenu;
  27. use OCP\App\IAppManager;
  28. use OCP\Contacts\ContactsMenu\IEntry;
  29. use OCP\IConfig;
  30. use OCP\IUser;
  31. class Manager {
  32. /** @var ContactsStore */
  33. private $store;
  34. /** @var ActionProviderStore */
  35. private $actionProviderStore;
  36. /** @var IAppManager */
  37. private $appManager;
  38. /** @var IConfig */
  39. private $config;
  40. /**
  41. * @param ContactsStore $store
  42. * @param ActionProviderStore $actionProviderStore
  43. * @param IAppManager $appManager
  44. */
  45. public function __construct(ContactsStore $store, ActionProviderStore $actionProviderStore, IAppManager $appManager, IConfig $config) {
  46. $this->store = $store;
  47. $this->actionProviderStore = $actionProviderStore;
  48. $this->appManager = $appManager;
  49. $this->config = $config;
  50. }
  51. /**
  52. * @param IUser $user
  53. * @param string $filter
  54. * @return array
  55. */
  56. public function getEntries(IUser $user, $filter) {
  57. $maxAutocompleteResults = $this->config->getSystemValueInt('sharing.maxAutocompleteResults', 25);
  58. $minSearchStringLength = $this->config->getSystemValueInt('sharing.minSearchStringLength', 0);
  59. $topEntries = [];
  60. if (strlen($filter) >= $minSearchStringLength) {
  61. $entries = $this->store->getContacts($user, $filter, $maxAutocompleteResults);
  62. $sortedEntries = $this->sortEntries($entries);
  63. $topEntries = array_slice($sortedEntries, 0, $maxAutocompleteResults);
  64. $this->processEntries($topEntries, $user);
  65. }
  66. $contactsEnabled = $this->appManager->isEnabledForUser('contacts', $user);
  67. return [
  68. 'contacts' => $topEntries,
  69. 'contactsAppEnabled' => $contactsEnabled,
  70. ];
  71. }
  72. /**
  73. * @param IUser $user
  74. * @param integer $shareType
  75. * @param string $shareWith
  76. * @return IEntry
  77. */
  78. public function findOne(IUser $user, $shareType, $shareWith) {
  79. $entry = $this->store->findOne($user, $shareType, $shareWith);
  80. if ($entry) {
  81. $this->processEntries([$entry], $user);
  82. }
  83. return $entry;
  84. }
  85. /**
  86. * @param IEntry[] $entries
  87. * @return IEntry[]
  88. */
  89. private function sortEntries(array $entries) {
  90. usort($entries, function (IEntry $entryA, IEntry $entryB) {
  91. return strcasecmp($entryA->getFullName(), $entryB->getFullName());
  92. });
  93. return $entries;
  94. }
  95. /**
  96. * @param IEntry[] $entries
  97. * @param IUser $user
  98. */
  99. private function processEntries(array $entries, IUser $user) {
  100. $providers = $this->actionProviderStore->getProviders($user);
  101. foreach ($entries as $entry) {
  102. foreach ($providers as $provider) {
  103. $provider->process($entry);
  104. }
  105. }
  106. }
  107. }