@@ -36,117 +36,117 @@ |
||
36 | 36 | |
37 | 37 | class SectionSearch implements IProvider { |
38 | 38 | |
39 | - /** @var IManager */ |
|
40 | - protected $settingsManager; |
|
41 | - |
|
42 | - /** @var IGroupManager */ |
|
43 | - protected $groupManager; |
|
44 | - |
|
45 | - /** @var IURLGenerator */ |
|
46 | - protected $urlGenerator; |
|
47 | - |
|
48 | - /** @var IL10N */ |
|
49 | - protected $l; |
|
50 | - |
|
51 | - public function __construct(IManager $settingsManager, |
|
52 | - IGroupManager $groupManager, |
|
53 | - IURLGenerator $urlGenerator, |
|
54 | - IL10N $l) { |
|
55 | - $this->settingsManager = $settingsManager; |
|
56 | - $this->groupManager = $groupManager; |
|
57 | - $this->urlGenerator = $urlGenerator; |
|
58 | - $this->l = $l; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @inheritDoc |
|
63 | - */ |
|
64 | - public function getId(): string { |
|
65 | - return 'settings'; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @inheritDoc |
|
70 | - */ |
|
71 | - public function getName(): string { |
|
72 | - return $this->l->t('Settings'); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @inheritDoc |
|
77 | - */ |
|
78 | - public function getOrder(string $route, array $routeParameters): int { |
|
79 | - if ($route === 'settings.PersonalSettings.index' || $route === 'settings.AdminSettings.index') { |
|
80 | - return -1; |
|
81 | - } |
|
82 | - // At the very bottom |
|
83 | - return 500; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @inheritDoc |
|
88 | - */ |
|
89 | - public function search(IUser $user, ISearchQuery $query): SearchResult { |
|
90 | - $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
|
91 | - |
|
92 | - $result = $this->searchSections( |
|
93 | - $query, |
|
94 | - $this->settingsManager->getPersonalSections(), |
|
95 | - $isAdmin ? $this->l->t('Personal') : '', |
|
96 | - 'settings.PersonalSettings.index' |
|
97 | - ); |
|
98 | - |
|
99 | - if ($this->groupManager->isAdmin($user->getUID())) { |
|
100 | - $result = array_merge($result, $this->searchSections( |
|
101 | - $query, |
|
102 | - $this->settingsManager->getAdminSections(), |
|
103 | - $this->l->t('Administration'), |
|
104 | - 'settings.AdminSettings.index' |
|
105 | - )); |
|
106 | - } |
|
107 | - |
|
108 | - return SearchResult::complete( |
|
109 | - $this->l->t('Settings'), |
|
110 | - $result |
|
111 | - ); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param ISearchQuery $query |
|
116 | - * @param ISection[][] $sections |
|
117 | - * @param string $subline |
|
118 | - * @param string $routeName |
|
119 | - * @return array |
|
120 | - */ |
|
121 | - public function searchSections(ISearchQuery $query, array $sections, string $subline, string $routeName): array { |
|
122 | - $result = []; |
|
123 | - foreach ($sections as $priority => $sectionsByPriority) { |
|
124 | - foreach ($sectionsByPriority as $section) { |
|
125 | - if ( |
|
126 | - stripos($section->getName(), $query->getTerm()) === false && |
|
127 | - stripos($section->getID(), $query->getTerm()) === false |
|
128 | - ) { |
|
129 | - continue; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * We can't use the icon URL at the moment as they don't invert correctly for dark theme |
|
134 | - * $iconUrl = ''; |
|
135 | - * if ($section instanceof IIconSection) { |
|
136 | - * $iconUrl = $section->getIcon(); |
|
137 | - * } |
|
138 | - */ |
|
139 | - |
|
140 | - $result[] = new SearchResultEntry( |
|
141 | - '', |
|
142 | - $section->getName(), |
|
143 | - $subline, |
|
144 | - $this->urlGenerator->linkToRouteAbsolute($routeName, ['section' => $section->getID()]), |
|
145 | - 'icon-settings' |
|
146 | - ); |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - return $result; |
|
151 | - } |
|
39 | + /** @var IManager */ |
|
40 | + protected $settingsManager; |
|
41 | + |
|
42 | + /** @var IGroupManager */ |
|
43 | + protected $groupManager; |
|
44 | + |
|
45 | + /** @var IURLGenerator */ |
|
46 | + protected $urlGenerator; |
|
47 | + |
|
48 | + /** @var IL10N */ |
|
49 | + protected $l; |
|
50 | + |
|
51 | + public function __construct(IManager $settingsManager, |
|
52 | + IGroupManager $groupManager, |
|
53 | + IURLGenerator $urlGenerator, |
|
54 | + IL10N $l) { |
|
55 | + $this->settingsManager = $settingsManager; |
|
56 | + $this->groupManager = $groupManager; |
|
57 | + $this->urlGenerator = $urlGenerator; |
|
58 | + $this->l = $l; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @inheritDoc |
|
63 | + */ |
|
64 | + public function getId(): string { |
|
65 | + return 'settings'; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @inheritDoc |
|
70 | + */ |
|
71 | + public function getName(): string { |
|
72 | + return $this->l->t('Settings'); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @inheritDoc |
|
77 | + */ |
|
78 | + public function getOrder(string $route, array $routeParameters): int { |
|
79 | + if ($route === 'settings.PersonalSettings.index' || $route === 'settings.AdminSettings.index') { |
|
80 | + return -1; |
|
81 | + } |
|
82 | + // At the very bottom |
|
83 | + return 500; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @inheritDoc |
|
88 | + */ |
|
89 | + public function search(IUser $user, ISearchQuery $query): SearchResult { |
|
90 | + $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
|
91 | + |
|
92 | + $result = $this->searchSections( |
|
93 | + $query, |
|
94 | + $this->settingsManager->getPersonalSections(), |
|
95 | + $isAdmin ? $this->l->t('Personal') : '', |
|
96 | + 'settings.PersonalSettings.index' |
|
97 | + ); |
|
98 | + |
|
99 | + if ($this->groupManager->isAdmin($user->getUID())) { |
|
100 | + $result = array_merge($result, $this->searchSections( |
|
101 | + $query, |
|
102 | + $this->settingsManager->getAdminSections(), |
|
103 | + $this->l->t('Administration'), |
|
104 | + 'settings.AdminSettings.index' |
|
105 | + )); |
|
106 | + } |
|
107 | + |
|
108 | + return SearchResult::complete( |
|
109 | + $this->l->t('Settings'), |
|
110 | + $result |
|
111 | + ); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param ISearchQuery $query |
|
116 | + * @param ISection[][] $sections |
|
117 | + * @param string $subline |
|
118 | + * @param string $routeName |
|
119 | + * @return array |
|
120 | + */ |
|
121 | + public function searchSections(ISearchQuery $query, array $sections, string $subline, string $routeName): array { |
|
122 | + $result = []; |
|
123 | + foreach ($sections as $priority => $sectionsByPriority) { |
|
124 | + foreach ($sectionsByPriority as $section) { |
|
125 | + if ( |
|
126 | + stripos($section->getName(), $query->getTerm()) === false && |
|
127 | + stripos($section->getID(), $query->getTerm()) === false |
|
128 | + ) { |
|
129 | + continue; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * We can't use the icon URL at the moment as they don't invert correctly for dark theme |
|
134 | + * $iconUrl = ''; |
|
135 | + * if ($section instanceof IIconSection) { |
|
136 | + * $iconUrl = $section->getIcon(); |
|
137 | + * } |
|
138 | + */ |
|
139 | + |
|
140 | + $result[] = new SearchResultEntry( |
|
141 | + '', |
|
142 | + $section->getName(), |
|
143 | + $subline, |
|
144 | + $this->urlGenerator->linkToRouteAbsolute($routeName, ['section' => $section->getID()]), |
|
145 | + 'icon-settings' |
|
146 | + ); |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + return $result; |
|
151 | + } |
|
152 | 152 | } |
@@ -38,134 +38,134 @@ |
||
38 | 38 | */ |
39 | 39 | class TasksSearchProvider extends ACalendarSearchProvider { |
40 | 40 | |
41 | - /** |
|
42 | - * @var string[] |
|
43 | - */ |
|
44 | - private static $searchProperties = [ |
|
45 | - 'SUMMARY', |
|
46 | - 'DESCRIPTION', |
|
47 | - 'CATEGORIES', |
|
48 | - ]; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var string[] |
|
52 | - */ |
|
53 | - private static $searchParameters = []; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var string |
|
57 | - */ |
|
58 | - private static $componentType = 'VTODO'; |
|
59 | - |
|
60 | - /** |
|
61 | - * @inheritDoc |
|
62 | - */ |
|
63 | - public function getId(): string { |
|
64 | - return 'tasks'; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @inheritDoc |
|
69 | - */ |
|
70 | - public function getName(): string { |
|
71 | - return $this->l10n->t('Tasks'); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @inheritDoc |
|
76 | - */ |
|
77 | - public function getOrder(string $route, array $routeParameters): int { |
|
78 | - if ($route === 'tasks.Page.index') { |
|
79 | - return -1; |
|
80 | - } |
|
81 | - return 35; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @inheritDoc |
|
86 | - */ |
|
87 | - public function search(IUser $user, |
|
88 | - ISearchQuery $query): SearchResult { |
|
89 | - if (!$this->appManager->isEnabledForUser('tasks', $user)) { |
|
90 | - return SearchResult::complete($this->getName(), []); |
|
91 | - } |
|
92 | - |
|
93 | - $principalUri = 'principals/users/' . $user->getUID(); |
|
94 | - $calendarsById = $this->getSortedCalendars($principalUri); |
|
95 | - $subscriptionsById = $this->getSortedSubscriptions($principalUri); |
|
96 | - |
|
97 | - $searchResults = $this->backend->searchPrincipalUri( |
|
98 | - $principalUri, |
|
99 | - $query->getTerm(), |
|
100 | - [self::$componentType], |
|
101 | - self::$searchProperties, |
|
102 | - self::$searchParameters, |
|
103 | - [ |
|
104 | - 'limit' => $query->getLimit(), |
|
105 | - 'offset' => $query->getCursor(), |
|
106 | - ] |
|
107 | - ); |
|
108 | - $formattedResults = \array_map(function (array $taskRow) use ($calendarsById, $subscriptionsById):SearchResultEntry { |
|
109 | - $component = $this->getPrimaryComponent($taskRow['calendardata'], self::$componentType); |
|
110 | - $title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled task')); |
|
111 | - $subline = $this->generateSubline($component); |
|
112 | - |
|
113 | - if ($taskRow['calendartype'] === CalDavBackend::CALENDAR_TYPE_CALENDAR) { |
|
114 | - $calendar = $calendarsById[$taskRow['calendarid']]; |
|
115 | - } else { |
|
116 | - $calendar = $subscriptionsById[$taskRow['calendarid']]; |
|
117 | - } |
|
118 | - $resourceUrl = $this->getDeepLinkToTasksApp($calendar['uri'], $taskRow['uri']); |
|
119 | - |
|
120 | - return new SearchResultEntry('', $title, $subline, $resourceUrl, 'icon-checkmark', false); |
|
121 | - }, $searchResults); |
|
122 | - |
|
123 | - return SearchResult::paginated( |
|
124 | - $this->getName(), |
|
125 | - $formattedResults, |
|
126 | - $query->getCursor() + count($formattedResults) |
|
127 | - ); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * @param string $calendarUri |
|
132 | - * @param string $taskUri |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - protected function getDeepLinkToTasksApp(string $calendarUri, |
|
136 | - string $taskUri): string { |
|
137 | - return $this->urlGenerator->getAbsoluteURL( |
|
138 | - $this->urlGenerator->linkToRoute('tasks.page.index') |
|
139 | - . '#/calendars/' |
|
140 | - . $calendarUri |
|
141 | - . '/tasks/' |
|
142 | - . $taskUri |
|
143 | - ); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @param Component $taskComponent |
|
148 | - * @return string |
|
149 | - */ |
|
150 | - protected function generateSubline(Component $taskComponent): string { |
|
151 | - if ($taskComponent->COMPLETED) { |
|
152 | - $completedDateTime = new \DateTime($taskComponent->COMPLETED->getDateTime()->format(\DateTime::ATOM)); |
|
153 | - $formattedDate = $this->l10n->l('date', $completedDateTime, ['width' => 'medium']); |
|
154 | - return $this->l10n->t('Completed on %s', [$formattedDate]); |
|
155 | - } |
|
156 | - |
|
157 | - if ($taskComponent->DUE) { |
|
158 | - $dueDateTime = new \DateTime($taskComponent->DUE->getDateTime()->format(\DateTime::ATOM)); |
|
159 | - $formattedDate = $this->l10n->l('date', $dueDateTime, ['width' => 'medium']); |
|
160 | - |
|
161 | - if ($taskComponent->DUE->hasTime()) { |
|
162 | - $formattedTime = $this->l10n->l('time', $dueDateTime, ['width' => 'short']); |
|
163 | - return $this->l10n->t('Due on %s by %s', [$formattedDate, $formattedTime]); |
|
164 | - } |
|
165 | - |
|
166 | - return $this->l10n->t('Due on %s', [$formattedDate]); |
|
167 | - } |
|
168 | - |
|
169 | - return ''; |
|
170 | - } |
|
41 | + /** |
|
42 | + * @var string[] |
|
43 | + */ |
|
44 | + private static $searchProperties = [ |
|
45 | + 'SUMMARY', |
|
46 | + 'DESCRIPTION', |
|
47 | + 'CATEGORIES', |
|
48 | + ]; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var string[] |
|
52 | + */ |
|
53 | + private static $searchParameters = []; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var string |
|
57 | + */ |
|
58 | + private static $componentType = 'VTODO'; |
|
59 | + |
|
60 | + /** |
|
61 | + * @inheritDoc |
|
62 | + */ |
|
63 | + public function getId(): string { |
|
64 | + return 'tasks'; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @inheritDoc |
|
69 | + */ |
|
70 | + public function getName(): string { |
|
71 | + return $this->l10n->t('Tasks'); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @inheritDoc |
|
76 | + */ |
|
77 | + public function getOrder(string $route, array $routeParameters): int { |
|
78 | + if ($route === 'tasks.Page.index') { |
|
79 | + return -1; |
|
80 | + } |
|
81 | + return 35; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @inheritDoc |
|
86 | + */ |
|
87 | + public function search(IUser $user, |
|
88 | + ISearchQuery $query): SearchResult { |
|
89 | + if (!$this->appManager->isEnabledForUser('tasks', $user)) { |
|
90 | + return SearchResult::complete($this->getName(), []); |
|
91 | + } |
|
92 | + |
|
93 | + $principalUri = 'principals/users/' . $user->getUID(); |
|
94 | + $calendarsById = $this->getSortedCalendars($principalUri); |
|
95 | + $subscriptionsById = $this->getSortedSubscriptions($principalUri); |
|
96 | + |
|
97 | + $searchResults = $this->backend->searchPrincipalUri( |
|
98 | + $principalUri, |
|
99 | + $query->getTerm(), |
|
100 | + [self::$componentType], |
|
101 | + self::$searchProperties, |
|
102 | + self::$searchParameters, |
|
103 | + [ |
|
104 | + 'limit' => $query->getLimit(), |
|
105 | + 'offset' => $query->getCursor(), |
|
106 | + ] |
|
107 | + ); |
|
108 | + $formattedResults = \array_map(function (array $taskRow) use ($calendarsById, $subscriptionsById):SearchResultEntry { |
|
109 | + $component = $this->getPrimaryComponent($taskRow['calendardata'], self::$componentType); |
|
110 | + $title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled task')); |
|
111 | + $subline = $this->generateSubline($component); |
|
112 | + |
|
113 | + if ($taskRow['calendartype'] === CalDavBackend::CALENDAR_TYPE_CALENDAR) { |
|
114 | + $calendar = $calendarsById[$taskRow['calendarid']]; |
|
115 | + } else { |
|
116 | + $calendar = $subscriptionsById[$taskRow['calendarid']]; |
|
117 | + } |
|
118 | + $resourceUrl = $this->getDeepLinkToTasksApp($calendar['uri'], $taskRow['uri']); |
|
119 | + |
|
120 | + return new SearchResultEntry('', $title, $subline, $resourceUrl, 'icon-checkmark', false); |
|
121 | + }, $searchResults); |
|
122 | + |
|
123 | + return SearchResult::paginated( |
|
124 | + $this->getName(), |
|
125 | + $formattedResults, |
|
126 | + $query->getCursor() + count($formattedResults) |
|
127 | + ); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * @param string $calendarUri |
|
132 | + * @param string $taskUri |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + protected function getDeepLinkToTasksApp(string $calendarUri, |
|
136 | + string $taskUri): string { |
|
137 | + return $this->urlGenerator->getAbsoluteURL( |
|
138 | + $this->urlGenerator->linkToRoute('tasks.page.index') |
|
139 | + . '#/calendars/' |
|
140 | + . $calendarUri |
|
141 | + . '/tasks/' |
|
142 | + . $taskUri |
|
143 | + ); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @param Component $taskComponent |
|
148 | + * @return string |
|
149 | + */ |
|
150 | + protected function generateSubline(Component $taskComponent): string { |
|
151 | + if ($taskComponent->COMPLETED) { |
|
152 | + $completedDateTime = new \DateTime($taskComponent->COMPLETED->getDateTime()->format(\DateTime::ATOM)); |
|
153 | + $formattedDate = $this->l10n->l('date', $completedDateTime, ['width' => 'medium']); |
|
154 | + return $this->l10n->t('Completed on %s', [$formattedDate]); |
|
155 | + } |
|
156 | + |
|
157 | + if ($taskComponent->DUE) { |
|
158 | + $dueDateTime = new \DateTime($taskComponent->DUE->getDateTime()->format(\DateTime::ATOM)); |
|
159 | + $formattedDate = $this->l10n->l('date', $dueDateTime, ['width' => 'medium']); |
|
160 | + |
|
161 | + if ($taskComponent->DUE->hasTime()) { |
|
162 | + $formattedTime = $this->l10n->l('time', $dueDateTime, ['width' => 'short']); |
|
163 | + return $this->l10n->t('Due on %s by %s', [$formattedDate, $formattedTime]); |
|
164 | + } |
|
165 | + |
|
166 | + return $this->l10n->t('Due on %s', [$formattedDate]); |
|
167 | + } |
|
168 | + |
|
169 | + return ''; |
|
170 | + } |
|
171 | 171 | } |
@@ -40,203 +40,203 @@ |
||
40 | 40 | */ |
41 | 41 | class EventsSearchProvider extends ACalendarSearchProvider { |
42 | 42 | |
43 | - /** |
|
44 | - * @var string[] |
|
45 | - */ |
|
46 | - private static $searchProperties = [ |
|
47 | - 'SUMMARY', |
|
48 | - 'LOCATION', |
|
49 | - 'DESCRIPTION', |
|
50 | - 'ATTENDEE', |
|
51 | - 'ORGANIZER', |
|
52 | - 'CATEGORIES', |
|
53 | - ]; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var string[] |
|
57 | - */ |
|
58 | - private static $searchParameters = [ |
|
59 | - 'ATTENDEE' => ['CN'], |
|
60 | - 'ORGANIZER' => ['CN'], |
|
61 | - ]; |
|
62 | - |
|
63 | - /** |
|
64 | - * @var string |
|
65 | - */ |
|
66 | - private static $componentType = 'VEVENT'; |
|
67 | - |
|
68 | - /** |
|
69 | - * @inheritDoc |
|
70 | - */ |
|
71 | - public function getId(): string { |
|
72 | - return 'calendar'; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @inheritDoc |
|
77 | - */ |
|
78 | - public function getName(): string { |
|
79 | - return $this->l10n->t('Events'); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @inheritDoc |
|
84 | - */ |
|
85 | - public function getOrder(string $route, array $routeParameters): int { |
|
86 | - if ($route === 'calendar.View.index') { |
|
87 | - return -1; |
|
88 | - } |
|
89 | - return 30; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @inheritDoc |
|
94 | - */ |
|
95 | - public function search(IUser $user, |
|
96 | - ISearchQuery $query): SearchResult { |
|
97 | - if (!$this->appManager->isEnabledForUser('calendar', $user)) { |
|
98 | - return SearchResult::complete($this->getName(), []); |
|
99 | - } |
|
100 | - |
|
101 | - $principalUri = 'principals/users/' . $user->getUID(); |
|
102 | - $calendarsById = $this->getSortedCalendars($principalUri); |
|
103 | - $subscriptionsById = $this->getSortedSubscriptions($principalUri); |
|
104 | - |
|
105 | - $searchResults = $this->backend->searchPrincipalUri( |
|
106 | - $principalUri, |
|
107 | - $query->getTerm(), |
|
108 | - [self::$componentType], |
|
109 | - self::$searchProperties, |
|
110 | - self::$searchParameters, |
|
111 | - [ |
|
112 | - 'limit' => $query->getLimit(), |
|
113 | - 'offset' => $query->getCursor(), |
|
114 | - ] |
|
115 | - ); |
|
116 | - $formattedResults = \array_map(function (array $eventRow) use ($calendarsById, $subscriptionsById):SearchResultEntry { |
|
117 | - $component = $this->getPrimaryComponent($eventRow['calendardata'], self::$componentType); |
|
118 | - $title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled event')); |
|
119 | - $subline = $this->generateSubline($component); |
|
120 | - |
|
121 | - if ($eventRow['calendartype'] === CalDavBackend::CALENDAR_TYPE_CALENDAR) { |
|
122 | - $calendar = $calendarsById[$eventRow['calendarid']]; |
|
123 | - } else { |
|
124 | - $calendar = $subscriptionsById[$eventRow['calendarid']]; |
|
125 | - } |
|
126 | - $resourceUrl = $this->getDeepLinkToCalendarApp($calendar['principaluri'], $calendar['uri'], $eventRow['uri']); |
|
127 | - |
|
128 | - return new SearchResultEntry('', $title, $subline, $resourceUrl, 'icon-calendar-dark', false); |
|
129 | - }, $searchResults); |
|
130 | - |
|
131 | - return SearchResult::paginated( |
|
132 | - $this->getName(), |
|
133 | - $formattedResults, |
|
134 | - $query->getCursor() + count($formattedResults) |
|
135 | - ); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @param string $principalUri |
|
140 | - * @param string $calendarUri |
|
141 | - * @param string $calendarObjectUri |
|
142 | - * @return string |
|
143 | - */ |
|
144 | - protected function getDeepLinkToCalendarApp(string $principalUri, |
|
145 | - string $calendarUri, |
|
146 | - string $calendarObjectUri): string { |
|
147 | - $davUrl = $this->getDavUrlForCalendarObject($principalUri, $calendarUri, $calendarObjectUri); |
|
148 | - // This route will automatically figure out what recurrence-id to open |
|
149 | - return $this->urlGenerator->getAbsoluteURL( |
|
150 | - $this->urlGenerator->linkToRoute('calendar.view.index') |
|
151 | - . 'edit/' |
|
152 | - . base64_encode($davUrl) |
|
153 | - ); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @param string $principalUri |
|
158 | - * @param string $calendarUri |
|
159 | - * @param string $calendarObjectUri |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - protected function getDavUrlForCalendarObject(string $principalUri, |
|
163 | - string $calendarUri, |
|
164 | - string $calendarObjectUri): string { |
|
165 | - [,, $principalId] = explode('/', $principalUri, 3); |
|
166 | - |
|
167 | - return $this->urlGenerator->linkTo('', 'remote.php') . '/dav/calendars/' |
|
168 | - . $principalId . '/' |
|
169 | - . $calendarUri . '/' |
|
170 | - . $calendarObjectUri; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @param Component $eventComponent |
|
175 | - * @return string |
|
176 | - */ |
|
177 | - protected function generateSubline(Component $eventComponent): string { |
|
178 | - $dtStart = $eventComponent->DTSTART; |
|
179 | - $dtEnd = $this->getDTEndForEvent($eventComponent); |
|
180 | - $isAllDayEvent = $dtStart instanceof Property\ICalendar\Date; |
|
181 | - $startDateTime = new \DateTime($dtStart->getDateTime()->format(\DateTime::ATOM)); |
|
182 | - $endDateTime = new \DateTime($dtEnd->getDateTime()->format(\DateTime::ATOM)); |
|
183 | - |
|
184 | - if ($isAllDayEvent) { |
|
185 | - $endDateTime->modify('-1 day'); |
|
186 | - if ($this->isDayEqual($startDateTime, $endDateTime)) { |
|
187 | - return $this->l10n->l('date', $startDateTime, ['width' => 'medium']); |
|
188 | - } |
|
189 | - |
|
190 | - $formattedStart = $this->l10n->l('date', $startDateTime, ['width' => 'medium']); |
|
191 | - $formattedEnd = $this->l10n->l('date', $endDateTime, ['width' => 'medium']); |
|
192 | - return "$formattedStart - $formattedEnd"; |
|
193 | - } |
|
194 | - |
|
195 | - $formattedStartDate = $this->l10n->l('date', $startDateTime, ['width' => 'medium']); |
|
196 | - $formattedEndDate = $this->l10n->l('date', $endDateTime, ['width' => 'medium']); |
|
197 | - $formattedStartTime = $this->l10n->l('time', $startDateTime, ['width' => 'short']); |
|
198 | - $formattedEndTime = $this->l10n->l('time', $endDateTime, ['width' => 'short']); |
|
199 | - |
|
200 | - if ($this->isDayEqual($startDateTime, $endDateTime)) { |
|
201 | - return "$formattedStartDate $formattedStartTime - $formattedEndTime"; |
|
202 | - } |
|
203 | - |
|
204 | - return "$formattedStartDate $formattedStartTime - $formattedEndDate $formattedEndTime"; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * @param Component $eventComponent |
|
209 | - * @return Property |
|
210 | - */ |
|
211 | - protected function getDTEndForEvent(Component $eventComponent):Property { |
|
212 | - if (isset($eventComponent->DTEND)) { |
|
213 | - $end = $eventComponent->DTEND; |
|
214 | - } elseif (isset($eventComponent->DURATION)) { |
|
215 | - $isFloating = $eventComponent->DTSTART->isFloating(); |
|
216 | - $end = clone $eventComponent->DTSTART; |
|
217 | - $endDateTime = $end->getDateTime(); |
|
218 | - $endDateTime = $endDateTime->add(DateTimeParser::parse($eventComponent->DURATION->getValue())); |
|
219 | - $end->setDateTime($endDateTime, $isFloating); |
|
220 | - } elseif (!$eventComponent->DTSTART->hasTime()) { |
|
221 | - $isFloating = $eventComponent->DTSTART->isFloating(); |
|
222 | - $end = clone $eventComponent->DTSTART; |
|
223 | - $endDateTime = $end->getDateTime(); |
|
224 | - $endDateTime = $endDateTime->modify('+1 day'); |
|
225 | - $end->setDateTime($endDateTime, $isFloating); |
|
226 | - } else { |
|
227 | - $end = clone $eventComponent->DTSTART; |
|
228 | - } |
|
229 | - |
|
230 | - return $end; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * @param \DateTime $dtStart |
|
235 | - * @param \DateTime $dtEnd |
|
236 | - * @return bool |
|
237 | - */ |
|
238 | - protected function isDayEqual(\DateTime $dtStart, |
|
239 | - \DateTime $dtEnd) { |
|
240 | - return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d'); |
|
241 | - } |
|
43 | + /** |
|
44 | + * @var string[] |
|
45 | + */ |
|
46 | + private static $searchProperties = [ |
|
47 | + 'SUMMARY', |
|
48 | + 'LOCATION', |
|
49 | + 'DESCRIPTION', |
|
50 | + 'ATTENDEE', |
|
51 | + 'ORGANIZER', |
|
52 | + 'CATEGORIES', |
|
53 | + ]; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var string[] |
|
57 | + */ |
|
58 | + private static $searchParameters = [ |
|
59 | + 'ATTENDEE' => ['CN'], |
|
60 | + 'ORGANIZER' => ['CN'], |
|
61 | + ]; |
|
62 | + |
|
63 | + /** |
|
64 | + * @var string |
|
65 | + */ |
|
66 | + private static $componentType = 'VEVENT'; |
|
67 | + |
|
68 | + /** |
|
69 | + * @inheritDoc |
|
70 | + */ |
|
71 | + public function getId(): string { |
|
72 | + return 'calendar'; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @inheritDoc |
|
77 | + */ |
|
78 | + public function getName(): string { |
|
79 | + return $this->l10n->t('Events'); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @inheritDoc |
|
84 | + */ |
|
85 | + public function getOrder(string $route, array $routeParameters): int { |
|
86 | + if ($route === 'calendar.View.index') { |
|
87 | + return -1; |
|
88 | + } |
|
89 | + return 30; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @inheritDoc |
|
94 | + */ |
|
95 | + public function search(IUser $user, |
|
96 | + ISearchQuery $query): SearchResult { |
|
97 | + if (!$this->appManager->isEnabledForUser('calendar', $user)) { |
|
98 | + return SearchResult::complete($this->getName(), []); |
|
99 | + } |
|
100 | + |
|
101 | + $principalUri = 'principals/users/' . $user->getUID(); |
|
102 | + $calendarsById = $this->getSortedCalendars($principalUri); |
|
103 | + $subscriptionsById = $this->getSortedSubscriptions($principalUri); |
|
104 | + |
|
105 | + $searchResults = $this->backend->searchPrincipalUri( |
|
106 | + $principalUri, |
|
107 | + $query->getTerm(), |
|
108 | + [self::$componentType], |
|
109 | + self::$searchProperties, |
|
110 | + self::$searchParameters, |
|
111 | + [ |
|
112 | + 'limit' => $query->getLimit(), |
|
113 | + 'offset' => $query->getCursor(), |
|
114 | + ] |
|
115 | + ); |
|
116 | + $formattedResults = \array_map(function (array $eventRow) use ($calendarsById, $subscriptionsById):SearchResultEntry { |
|
117 | + $component = $this->getPrimaryComponent($eventRow['calendardata'], self::$componentType); |
|
118 | + $title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled event')); |
|
119 | + $subline = $this->generateSubline($component); |
|
120 | + |
|
121 | + if ($eventRow['calendartype'] === CalDavBackend::CALENDAR_TYPE_CALENDAR) { |
|
122 | + $calendar = $calendarsById[$eventRow['calendarid']]; |
|
123 | + } else { |
|
124 | + $calendar = $subscriptionsById[$eventRow['calendarid']]; |
|
125 | + } |
|
126 | + $resourceUrl = $this->getDeepLinkToCalendarApp($calendar['principaluri'], $calendar['uri'], $eventRow['uri']); |
|
127 | + |
|
128 | + return new SearchResultEntry('', $title, $subline, $resourceUrl, 'icon-calendar-dark', false); |
|
129 | + }, $searchResults); |
|
130 | + |
|
131 | + return SearchResult::paginated( |
|
132 | + $this->getName(), |
|
133 | + $formattedResults, |
|
134 | + $query->getCursor() + count($formattedResults) |
|
135 | + ); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @param string $principalUri |
|
140 | + * @param string $calendarUri |
|
141 | + * @param string $calendarObjectUri |
|
142 | + * @return string |
|
143 | + */ |
|
144 | + protected function getDeepLinkToCalendarApp(string $principalUri, |
|
145 | + string $calendarUri, |
|
146 | + string $calendarObjectUri): string { |
|
147 | + $davUrl = $this->getDavUrlForCalendarObject($principalUri, $calendarUri, $calendarObjectUri); |
|
148 | + // This route will automatically figure out what recurrence-id to open |
|
149 | + return $this->urlGenerator->getAbsoluteURL( |
|
150 | + $this->urlGenerator->linkToRoute('calendar.view.index') |
|
151 | + . 'edit/' |
|
152 | + . base64_encode($davUrl) |
|
153 | + ); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @param string $principalUri |
|
158 | + * @param string $calendarUri |
|
159 | + * @param string $calendarObjectUri |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + protected function getDavUrlForCalendarObject(string $principalUri, |
|
163 | + string $calendarUri, |
|
164 | + string $calendarObjectUri): string { |
|
165 | + [,, $principalId] = explode('/', $principalUri, 3); |
|
166 | + |
|
167 | + return $this->urlGenerator->linkTo('', 'remote.php') . '/dav/calendars/' |
|
168 | + . $principalId . '/' |
|
169 | + . $calendarUri . '/' |
|
170 | + . $calendarObjectUri; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @param Component $eventComponent |
|
175 | + * @return string |
|
176 | + */ |
|
177 | + protected function generateSubline(Component $eventComponent): string { |
|
178 | + $dtStart = $eventComponent->DTSTART; |
|
179 | + $dtEnd = $this->getDTEndForEvent($eventComponent); |
|
180 | + $isAllDayEvent = $dtStart instanceof Property\ICalendar\Date; |
|
181 | + $startDateTime = new \DateTime($dtStart->getDateTime()->format(\DateTime::ATOM)); |
|
182 | + $endDateTime = new \DateTime($dtEnd->getDateTime()->format(\DateTime::ATOM)); |
|
183 | + |
|
184 | + if ($isAllDayEvent) { |
|
185 | + $endDateTime->modify('-1 day'); |
|
186 | + if ($this->isDayEqual($startDateTime, $endDateTime)) { |
|
187 | + return $this->l10n->l('date', $startDateTime, ['width' => 'medium']); |
|
188 | + } |
|
189 | + |
|
190 | + $formattedStart = $this->l10n->l('date', $startDateTime, ['width' => 'medium']); |
|
191 | + $formattedEnd = $this->l10n->l('date', $endDateTime, ['width' => 'medium']); |
|
192 | + return "$formattedStart - $formattedEnd"; |
|
193 | + } |
|
194 | + |
|
195 | + $formattedStartDate = $this->l10n->l('date', $startDateTime, ['width' => 'medium']); |
|
196 | + $formattedEndDate = $this->l10n->l('date', $endDateTime, ['width' => 'medium']); |
|
197 | + $formattedStartTime = $this->l10n->l('time', $startDateTime, ['width' => 'short']); |
|
198 | + $formattedEndTime = $this->l10n->l('time', $endDateTime, ['width' => 'short']); |
|
199 | + |
|
200 | + if ($this->isDayEqual($startDateTime, $endDateTime)) { |
|
201 | + return "$formattedStartDate $formattedStartTime - $formattedEndTime"; |
|
202 | + } |
|
203 | + |
|
204 | + return "$formattedStartDate $formattedStartTime - $formattedEndDate $formattedEndTime"; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * @param Component $eventComponent |
|
209 | + * @return Property |
|
210 | + */ |
|
211 | + protected function getDTEndForEvent(Component $eventComponent):Property { |
|
212 | + if (isset($eventComponent->DTEND)) { |
|
213 | + $end = $eventComponent->DTEND; |
|
214 | + } elseif (isset($eventComponent->DURATION)) { |
|
215 | + $isFloating = $eventComponent->DTSTART->isFloating(); |
|
216 | + $end = clone $eventComponent->DTSTART; |
|
217 | + $endDateTime = $end->getDateTime(); |
|
218 | + $endDateTime = $endDateTime->add(DateTimeParser::parse($eventComponent->DURATION->getValue())); |
|
219 | + $end->setDateTime($endDateTime, $isFloating); |
|
220 | + } elseif (!$eventComponent->DTSTART->hasTime()) { |
|
221 | + $isFloating = $eventComponent->DTSTART->isFloating(); |
|
222 | + $end = clone $eventComponent->DTSTART; |
|
223 | + $endDateTime = $end->getDateTime(); |
|
224 | + $endDateTime = $endDateTime->modify('+1 day'); |
|
225 | + $end->setDateTime($endDateTime, $isFloating); |
|
226 | + } else { |
|
227 | + $end = clone $eventComponent->DTSTART; |
|
228 | + } |
|
229 | + |
|
230 | + return $end; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * @param \DateTime $dtStart |
|
235 | + * @param \DateTime $dtEnd |
|
236 | + * @return bool |
|
237 | + */ |
|
238 | + protected function isDayEqual(\DateTime $dtStart, |
|
239 | + \DateTime $dtEnd) { |
|
240 | + return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d'); |
|
241 | + } |
|
242 | 242 | } |
@@ -38,163 +38,163 @@ |
||
38 | 38 | |
39 | 39 | class ContactsSearchProvider implements IProvider { |
40 | 40 | |
41 | - /** @var IAppManager */ |
|
42 | - private $appManager; |
|
43 | - |
|
44 | - /** @var IL10N */ |
|
45 | - private $l10n; |
|
46 | - |
|
47 | - /** @var IURLGenerator */ |
|
48 | - private $urlGenerator; |
|
49 | - |
|
50 | - /** @var CardDavBackend */ |
|
51 | - private $backend; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var string[] |
|
55 | - */ |
|
56 | - private static $searchProperties = [ |
|
57 | - 'N', |
|
58 | - 'FN', |
|
59 | - 'NICKNAME', |
|
60 | - 'EMAIL', |
|
61 | - 'ADR', |
|
62 | - ]; |
|
63 | - |
|
64 | - /** |
|
65 | - * ContactsSearchProvider constructor. |
|
66 | - * |
|
67 | - * @param IAppManager $appManager |
|
68 | - * @param IL10N $l10n |
|
69 | - * @param IURLGenerator $urlGenerator |
|
70 | - * @param CardDavBackend $backend |
|
71 | - */ |
|
72 | - public function __construct(IAppManager $appManager, |
|
73 | - IL10N $l10n, |
|
74 | - IURLGenerator $urlGenerator, |
|
75 | - CardDavBackend $backend) { |
|
76 | - $this->appManager = $appManager; |
|
77 | - $this->l10n = $l10n; |
|
78 | - $this->urlGenerator = $urlGenerator; |
|
79 | - $this->backend = $backend; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @inheritDoc |
|
84 | - */ |
|
85 | - public function getId(): string { |
|
86 | - return 'contacts'; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @inheritDoc |
|
91 | - */ |
|
92 | - public function getName(): string { |
|
93 | - return $this->l10n->t('Contacts'); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @inheritDoc |
|
98 | - */ |
|
99 | - public function getOrder(string $route, array $routeParameters): int { |
|
100 | - if ($route === 'contacts.Page.index') { |
|
101 | - return -1; |
|
102 | - } |
|
103 | - return 25; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @inheritDoc |
|
108 | - */ |
|
109 | - public function search(IUser $user, ISearchQuery $query): SearchResult { |
|
110 | - if (!$this->appManager->isEnabledForUser('contacts', $user)) { |
|
111 | - return SearchResult::complete($this->getName(), []); |
|
112 | - } |
|
113 | - |
|
114 | - $principalUri = 'principals/users/' . $user->getUID(); |
|
115 | - $addressBooks = $this->backend->getAddressBooksForUser($principalUri); |
|
116 | - $addressBooksById = []; |
|
117 | - foreach ($addressBooks as $addressBook) { |
|
118 | - $addressBooksById[(int) $addressBook['id']] = $addressBook; |
|
119 | - } |
|
120 | - |
|
121 | - $searchResults = $this->backend->searchPrincipalUri( |
|
122 | - $principalUri, |
|
123 | - $query->getTerm(), |
|
124 | - self::$searchProperties, |
|
125 | - [ |
|
126 | - 'limit' => $query->getLimit(), |
|
127 | - 'offset' => $query->getCursor(), |
|
128 | - ] |
|
129 | - ); |
|
130 | - $formattedResults = \array_map(function (array $contactRow) use ($addressBooksById):SearchResultEntry { |
|
131 | - $addressBook = $addressBooksById[$contactRow['addressbookid']]; |
|
132 | - |
|
133 | - /** @var VCard $vCard */ |
|
134 | - $vCard = Reader::read($contactRow['carddata']); |
|
135 | - $thumbnailUrl = ''; |
|
136 | - if ($vCard->PHOTO) { |
|
137 | - $thumbnailUrl = $this->getDavUrlForContact($addressBook['principaluri'], $addressBook['uri'], $contactRow['uri']) . '?photo'; |
|
138 | - } |
|
139 | - |
|
140 | - $title = (string)$vCard->FN; |
|
141 | - $subline = $this->generateSubline($vCard); |
|
142 | - $resourceUrl = $this->getDeepLinkToContactsApp($addressBook['uri'], (string) $vCard->UID); |
|
143 | - |
|
144 | - return new SearchResultEntry($thumbnailUrl, $title, $subline, $resourceUrl, 'icon-contacts-dark', true); |
|
145 | - }, $searchResults); |
|
146 | - |
|
147 | - return SearchResult::paginated( |
|
148 | - $this->getName(), |
|
149 | - $formattedResults, |
|
150 | - $query->getCursor() + count($formattedResults) |
|
151 | - ); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * @param string $principalUri |
|
156 | - * @param string $addressBookUri |
|
157 | - * @param string $contactsUri |
|
158 | - * @return string |
|
159 | - */ |
|
160 | - protected function getDavUrlForContact(string $principalUri, |
|
161 | - string $addressBookUri, |
|
162 | - string $contactsUri): string { |
|
163 | - [, $principalType, $principalId] = explode('/', $principalUri, 3); |
|
164 | - |
|
165 | - return $this->urlGenerator->getAbsoluteURL( |
|
166 | - $this->urlGenerator->linkTo('', 'remote.php') . '/dav/addressbooks/' |
|
167 | - . $principalType . '/' |
|
168 | - . $principalId . '/' |
|
169 | - . $addressBookUri . '/' |
|
170 | - . $contactsUri |
|
171 | - ); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param string $addressBookUri |
|
176 | - * @param string $contactUid |
|
177 | - * @return string |
|
178 | - */ |
|
179 | - protected function getDeepLinkToContactsApp(string $addressBookUri, |
|
180 | - string $contactUid): string { |
|
181 | - return $this->urlGenerator->getAbsoluteURL( |
|
182 | - $this->urlGenerator->linkToRoute('contacts.contacts.direct', [ |
|
183 | - 'contact' => $contactUid . '~' . $addressBookUri |
|
184 | - ]) |
|
185 | - ); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @param VCard $vCard |
|
190 | - * @return string |
|
191 | - */ |
|
192 | - protected function generateSubline(VCard $vCard): string { |
|
193 | - $emailAddresses = $vCard->select('EMAIL'); |
|
194 | - if (!is_array($emailAddresses) || empty($emailAddresses)) { |
|
195 | - return ''; |
|
196 | - } |
|
197 | - |
|
198 | - return (string)$emailAddresses[0]; |
|
199 | - } |
|
41 | + /** @var IAppManager */ |
|
42 | + private $appManager; |
|
43 | + |
|
44 | + /** @var IL10N */ |
|
45 | + private $l10n; |
|
46 | + |
|
47 | + /** @var IURLGenerator */ |
|
48 | + private $urlGenerator; |
|
49 | + |
|
50 | + /** @var CardDavBackend */ |
|
51 | + private $backend; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var string[] |
|
55 | + */ |
|
56 | + private static $searchProperties = [ |
|
57 | + 'N', |
|
58 | + 'FN', |
|
59 | + 'NICKNAME', |
|
60 | + 'EMAIL', |
|
61 | + 'ADR', |
|
62 | + ]; |
|
63 | + |
|
64 | + /** |
|
65 | + * ContactsSearchProvider constructor. |
|
66 | + * |
|
67 | + * @param IAppManager $appManager |
|
68 | + * @param IL10N $l10n |
|
69 | + * @param IURLGenerator $urlGenerator |
|
70 | + * @param CardDavBackend $backend |
|
71 | + */ |
|
72 | + public function __construct(IAppManager $appManager, |
|
73 | + IL10N $l10n, |
|
74 | + IURLGenerator $urlGenerator, |
|
75 | + CardDavBackend $backend) { |
|
76 | + $this->appManager = $appManager; |
|
77 | + $this->l10n = $l10n; |
|
78 | + $this->urlGenerator = $urlGenerator; |
|
79 | + $this->backend = $backend; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @inheritDoc |
|
84 | + */ |
|
85 | + public function getId(): string { |
|
86 | + return 'contacts'; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @inheritDoc |
|
91 | + */ |
|
92 | + public function getName(): string { |
|
93 | + return $this->l10n->t('Contacts'); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @inheritDoc |
|
98 | + */ |
|
99 | + public function getOrder(string $route, array $routeParameters): int { |
|
100 | + if ($route === 'contacts.Page.index') { |
|
101 | + return -1; |
|
102 | + } |
|
103 | + return 25; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @inheritDoc |
|
108 | + */ |
|
109 | + public function search(IUser $user, ISearchQuery $query): SearchResult { |
|
110 | + if (!$this->appManager->isEnabledForUser('contacts', $user)) { |
|
111 | + return SearchResult::complete($this->getName(), []); |
|
112 | + } |
|
113 | + |
|
114 | + $principalUri = 'principals/users/' . $user->getUID(); |
|
115 | + $addressBooks = $this->backend->getAddressBooksForUser($principalUri); |
|
116 | + $addressBooksById = []; |
|
117 | + foreach ($addressBooks as $addressBook) { |
|
118 | + $addressBooksById[(int) $addressBook['id']] = $addressBook; |
|
119 | + } |
|
120 | + |
|
121 | + $searchResults = $this->backend->searchPrincipalUri( |
|
122 | + $principalUri, |
|
123 | + $query->getTerm(), |
|
124 | + self::$searchProperties, |
|
125 | + [ |
|
126 | + 'limit' => $query->getLimit(), |
|
127 | + 'offset' => $query->getCursor(), |
|
128 | + ] |
|
129 | + ); |
|
130 | + $formattedResults = \array_map(function (array $contactRow) use ($addressBooksById):SearchResultEntry { |
|
131 | + $addressBook = $addressBooksById[$contactRow['addressbookid']]; |
|
132 | + |
|
133 | + /** @var VCard $vCard */ |
|
134 | + $vCard = Reader::read($contactRow['carddata']); |
|
135 | + $thumbnailUrl = ''; |
|
136 | + if ($vCard->PHOTO) { |
|
137 | + $thumbnailUrl = $this->getDavUrlForContact($addressBook['principaluri'], $addressBook['uri'], $contactRow['uri']) . '?photo'; |
|
138 | + } |
|
139 | + |
|
140 | + $title = (string)$vCard->FN; |
|
141 | + $subline = $this->generateSubline($vCard); |
|
142 | + $resourceUrl = $this->getDeepLinkToContactsApp($addressBook['uri'], (string) $vCard->UID); |
|
143 | + |
|
144 | + return new SearchResultEntry($thumbnailUrl, $title, $subline, $resourceUrl, 'icon-contacts-dark', true); |
|
145 | + }, $searchResults); |
|
146 | + |
|
147 | + return SearchResult::paginated( |
|
148 | + $this->getName(), |
|
149 | + $formattedResults, |
|
150 | + $query->getCursor() + count($formattedResults) |
|
151 | + ); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * @param string $principalUri |
|
156 | + * @param string $addressBookUri |
|
157 | + * @param string $contactsUri |
|
158 | + * @return string |
|
159 | + */ |
|
160 | + protected function getDavUrlForContact(string $principalUri, |
|
161 | + string $addressBookUri, |
|
162 | + string $contactsUri): string { |
|
163 | + [, $principalType, $principalId] = explode('/', $principalUri, 3); |
|
164 | + |
|
165 | + return $this->urlGenerator->getAbsoluteURL( |
|
166 | + $this->urlGenerator->linkTo('', 'remote.php') . '/dav/addressbooks/' |
|
167 | + . $principalType . '/' |
|
168 | + . $principalId . '/' |
|
169 | + . $addressBookUri . '/' |
|
170 | + . $contactsUri |
|
171 | + ); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param string $addressBookUri |
|
176 | + * @param string $contactUid |
|
177 | + * @return string |
|
178 | + */ |
|
179 | + protected function getDeepLinkToContactsApp(string $addressBookUri, |
|
180 | + string $contactUid): string { |
|
181 | + return $this->urlGenerator->getAbsoluteURL( |
|
182 | + $this->urlGenerator->linkToRoute('contacts.contacts.direct', [ |
|
183 | + 'contact' => $contactUid . '~' . $addressBookUri |
|
184 | + ]) |
|
185 | + ); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @param VCard $vCard |
|
190 | + * @return string |
|
191 | + */ |
|
192 | + protected function generateSubline(VCard $vCard): string { |
|
193 | + $emailAddresses = $vCard->select('EMAIL'); |
|
194 | + if (!is_array($emailAddresses) || empty($emailAddresses)) { |
|
195 | + return ''; |
|
196 | + } |
|
197 | + |
|
198 | + return (string)$emailAddresses[0]; |
|
199 | + } |
|
200 | 200 | } |