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.

Manager.php 3.5KB

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