1
0

Manager.php 2.9 KB

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