Manager.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Exception;
  28. use OCP\App\IAppManager;
  29. use OCP\Constants;
  30. use OCP\Contacts\ContactsMenu\IEntry;
  31. use OCP\IConfig;
  32. use OCP\IUser;
  33. class Manager {
  34. private ContactsStore $store;
  35. private ActionProviderStore $actionProviderStore;
  36. private IAppManager $appManager;
  37. private IConfig $config;
  38. public function __construct(ContactsStore $store, ActionProviderStore $actionProviderStore, IAppManager $appManager, IConfig $config) {
  39. $this->store = $store;
  40. $this->actionProviderStore = $actionProviderStore;
  41. $this->appManager = $appManager;
  42. $this->config = $config;
  43. }
  44. /**
  45. * @param IUser $user
  46. * @param string|null $filter
  47. * @return array
  48. * @throws Exception
  49. */
  50. public function getEntries(IUser $user, ?string $filter): array {
  51. $maxAutocompleteResults = max(0, $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT));
  52. $minSearchStringLength = $this->config->getSystemValueInt('sharing.minSearchStringLength');
  53. $topEntries = [];
  54. if (strlen($filter ?? '') >= $minSearchStringLength) {
  55. $entries = $this->store->getContacts($user, $filter, $maxAutocompleteResults);
  56. $sortedEntries = $this->sortEntries($entries);
  57. $topEntries = array_slice($sortedEntries, 0, $maxAutocompleteResults);
  58. $this->processEntries($topEntries, $user);
  59. }
  60. $contactsEnabled = $this->appManager->isEnabledForUser('contacts', $user);
  61. return [
  62. 'contacts' => $topEntries,
  63. 'contactsAppEnabled' => $contactsEnabled,
  64. ];
  65. }
  66. /**
  67. * @throws Exception
  68. */
  69. public function findOne(IUser $user, int $shareType, string $shareWith): ?IEntry {
  70. $entry = $this->store->findOne($user, $shareType, $shareWith);
  71. if ($entry) {
  72. $this->processEntries([$entry], $user);
  73. }
  74. return $entry;
  75. }
  76. /**
  77. * @param IEntry[] $entries
  78. * @return IEntry[]
  79. */
  80. private function sortEntries(array $entries): array {
  81. usort($entries, function (IEntry $entryA, IEntry $entryB) {
  82. return strcasecmp($entryA->getFullName(), $entryB->getFullName());
  83. });
  84. return $entries;
  85. }
  86. /**
  87. * @param IEntry[] $entries
  88. * @param IUser $user
  89. * @throws Exception
  90. */
  91. private function processEntries(array $entries, IUser $user) {
  92. $providers = $this->actionProviderStore->getProviders($user);
  93. foreach ($entries as $entry) {
  94. foreach ($providers as $provider) {
  95. $provider->process($entry);
  96. }
  97. }
  98. }
  99. }