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.3KB

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