Completed
Push — master ( 243704...61a537 )
by
unknown
42:04
created
apps/systemtags/lib/Search/TagSearchProvider.php 2 patches
Indentation   +143 added lines, -143 removed lines patch added patch discarded remove patch
@@ -32,147 +32,147 @@
 block discarded – undo
32 32
 
33 33
 class TagSearchProvider implements IProvider {
34 34
 
35
-	public function __construct(
36
-		private IL10N $l10n,
37
-		private IURLGenerator $urlGenerator,
38
-		private IMimeTypeDetector $mimeTypeDetector,
39
-		private IRootFolder $rootFolder,
40
-		private ISystemTagObjectMapper $objectMapper,
41
-		private ISystemTagManager $tagManager,
42
-	) {
43
-	}
44
-
45
-	/**
46
-	 * @inheritDoc
47
-	 */
48
-	public function getId(): string {
49
-		return 'systemtags';
50
-	}
51
-
52
-	/**
53
-	 * @inheritDoc
54
-	 */
55
-	public function getName(): string {
56
-		return $this->l10n->t('Tags');
57
-	}
58
-
59
-	/**
60
-	 * @inheritDoc
61
-	 */
62
-	public function getOrder(string $route, array $routeParameters): int {
63
-		if ($route === 'files.View.index') {
64
-			return -4;
65
-		}
66
-		return 6;
67
-	}
68
-
69
-	/**
70
-	 * @inheritDoc
71
-	 */
72
-	public function search(IUser $user, ISearchQuery $query): SearchResult {
73
-		$matchingTags = $this->tagManager->getAllTags(true, $query->getTerm());
74
-		if (count($matchingTags) === 0) {
75
-			return SearchResult::complete($this->l10n->t('Tags'), []);
76
-		}
77
-
78
-		$userFolder = $this->rootFolder->getUserFolder($user->getUID());
79
-		$fileQuery = new SearchQuery(
80
-			new SearchComparison(ISearchComparison::COMPARE_LIKE, 'systemtag', '%' . $query->getTerm() . '%'),
81
-			$query->getLimit(),
82
-			(int)$query->getCursor(),
83
-			$query->getSortOrder() === ISearchQuery::SORT_DATE_DESC ? [
84
-				new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
85
-			] : [],
86
-			$user
87
-		);
88
-
89
-		// do search
90
-		$searchResults = $userFolder->search($fileQuery);
91
-		$resultIds = array_map(function (Node $node) {
92
-			return $node->getId();
93
-		}, $searchResults);
94
-		$matchedTags = $this->objectMapper->getTagIdsForObjects($resultIds, 'files');
95
-
96
-		// prepare direct tag results
97
-		$tagResults = array_map(function (ISystemTag $tag) {
98
-			$thumbnailUrl = '';
99
-			$link = $this->urlGenerator->linkToRoute('files.view.indexView', [
100
-				'view' => 'tags',
101
-			]) . '?dir=' . $tag->getId();
102
-			$searchResultEntry = new SearchResultEntry(
103
-				$thumbnailUrl,
104
-				$this->l10n->t('All tagged %s …', [$tag->getName()]),
105
-				'',
106
-				$this->urlGenerator->getAbsoluteURL($link),
107
-				'icon-tag'
108
-			);
109
-			return $searchResultEntry;
110
-		}, $matchingTags);
111
-
112
-		// prepare files results
113
-		return SearchResult::paginated(
114
-			$this->l10n->t('Tags'),
115
-			[
116
-				...$tagResults,
117
-				...array_map(function (Node $result) use ($userFolder, $matchedTags, $query) {
118
-					$nodeId = $result->getId();
119
-					// Generate thumbnail url
120
-					$thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $nodeId]);
121
-					$path = $userFolder->getRelativePath($result->getPath());
122
-
123
-					// Use shortened link to centralize the various
124
-					// files/folder url redirection in files.View.showFile
125
-					$link = $this->urlGenerator->linkToRoute(
126
-						'files.View.showFile',
127
-						['fileid' => $nodeId]
128
-					);
129
-
130
-					$searchResultEntry = new SearchResultEntry(
131
-						$thumbnailUrl,
132
-						$result->getName(),
133
-						$this->formatSubline($query, $matchedTags[$nodeId]),
134
-						$this->urlGenerator->getAbsoluteURL($link),
135
-						$result->getMimetype() === FileInfo::MIMETYPE_FOLDER
136
-							? 'icon-folder'
137
-							: $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype())
138
-					);
139
-					$searchResultEntry->addAttribute('fileId', (string)$nodeId);
140
-					$searchResultEntry->addAttribute('path', $path);
141
-					return $searchResultEntry;
142
-				}, $searchResults)
143
-			],
144
-			$query->getCursor() + $query->getLimit()
145
-		);
146
-	}
147
-
148
-	/**
149
-	 * Format subline for tagged files: Show the first 3 tags
150
-	 *
151
-	 * @param $query
152
-	 * @param array $tagInfo
153
-	 * @return string
154
-	 */
155
-	private function formatSubline(ISearchQuery $query, array $tagInfo): string {
156
-		/**
157
-		 * @var ISystemTag[]
158
-		 */
159
-		$tags = $this->tagManager->getTagsByIds($tagInfo);
160
-		$tagNames = array_map(function ($tag) {
161
-			return $tag->getName();
162
-		}, array_filter($tags, function ($tag) {
163
-			return $tag->isUserVisible();
164
-		}));
165
-
166
-		// show the tag that you have searched for first
167
-		usort($tagNames, function ($tagName) use ($query) {
168
-			return strpos($tagName, $query->getTerm()) !== false? -1 :  1;
169
-		});
170
-
171
-		return $this->l10n->t('tagged %s', [implode(', ', array_slice($tagNames, 0, 3))]);
172
-	}
173
-
174
-	private function flattenArray($array) {
175
-		$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
176
-		return iterator_to_array($it, true);
177
-	}
35
+    public function __construct(
36
+        private IL10N $l10n,
37
+        private IURLGenerator $urlGenerator,
38
+        private IMimeTypeDetector $mimeTypeDetector,
39
+        private IRootFolder $rootFolder,
40
+        private ISystemTagObjectMapper $objectMapper,
41
+        private ISystemTagManager $tagManager,
42
+    ) {
43
+    }
44
+
45
+    /**
46
+     * @inheritDoc
47
+     */
48
+    public function getId(): string {
49
+        return 'systemtags';
50
+    }
51
+
52
+    /**
53
+     * @inheritDoc
54
+     */
55
+    public function getName(): string {
56
+        return $this->l10n->t('Tags');
57
+    }
58
+
59
+    /**
60
+     * @inheritDoc
61
+     */
62
+    public function getOrder(string $route, array $routeParameters): int {
63
+        if ($route === 'files.View.index') {
64
+            return -4;
65
+        }
66
+        return 6;
67
+    }
68
+
69
+    /**
70
+     * @inheritDoc
71
+     */
72
+    public function search(IUser $user, ISearchQuery $query): SearchResult {
73
+        $matchingTags = $this->tagManager->getAllTags(true, $query->getTerm());
74
+        if (count($matchingTags) === 0) {
75
+            return SearchResult::complete($this->l10n->t('Tags'), []);
76
+        }
77
+
78
+        $userFolder = $this->rootFolder->getUserFolder($user->getUID());
79
+        $fileQuery = new SearchQuery(
80
+            new SearchComparison(ISearchComparison::COMPARE_LIKE, 'systemtag', '%' . $query->getTerm() . '%'),
81
+            $query->getLimit(),
82
+            (int)$query->getCursor(),
83
+            $query->getSortOrder() === ISearchQuery::SORT_DATE_DESC ? [
84
+                new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
85
+            ] : [],
86
+            $user
87
+        );
88
+
89
+        // do search
90
+        $searchResults = $userFolder->search($fileQuery);
91
+        $resultIds = array_map(function (Node $node) {
92
+            return $node->getId();
93
+        }, $searchResults);
94
+        $matchedTags = $this->objectMapper->getTagIdsForObjects($resultIds, 'files');
95
+
96
+        // prepare direct tag results
97
+        $tagResults = array_map(function (ISystemTag $tag) {
98
+            $thumbnailUrl = '';
99
+            $link = $this->urlGenerator->linkToRoute('files.view.indexView', [
100
+                'view' => 'tags',
101
+            ]) . '?dir=' . $tag->getId();
102
+            $searchResultEntry = new SearchResultEntry(
103
+                $thumbnailUrl,
104
+                $this->l10n->t('All tagged %s …', [$tag->getName()]),
105
+                '',
106
+                $this->urlGenerator->getAbsoluteURL($link),
107
+                'icon-tag'
108
+            );
109
+            return $searchResultEntry;
110
+        }, $matchingTags);
111
+
112
+        // prepare files results
113
+        return SearchResult::paginated(
114
+            $this->l10n->t('Tags'),
115
+            [
116
+                ...$tagResults,
117
+                ...array_map(function (Node $result) use ($userFolder, $matchedTags, $query) {
118
+                    $nodeId = $result->getId();
119
+                    // Generate thumbnail url
120
+                    $thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $nodeId]);
121
+                    $path = $userFolder->getRelativePath($result->getPath());
122
+
123
+                    // Use shortened link to centralize the various
124
+                    // files/folder url redirection in files.View.showFile
125
+                    $link = $this->urlGenerator->linkToRoute(
126
+                        'files.View.showFile',
127
+                        ['fileid' => $nodeId]
128
+                    );
129
+
130
+                    $searchResultEntry = new SearchResultEntry(
131
+                        $thumbnailUrl,
132
+                        $result->getName(),
133
+                        $this->formatSubline($query, $matchedTags[$nodeId]),
134
+                        $this->urlGenerator->getAbsoluteURL($link),
135
+                        $result->getMimetype() === FileInfo::MIMETYPE_FOLDER
136
+                            ? 'icon-folder'
137
+                            : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype())
138
+                    );
139
+                    $searchResultEntry->addAttribute('fileId', (string)$nodeId);
140
+                    $searchResultEntry->addAttribute('path', $path);
141
+                    return $searchResultEntry;
142
+                }, $searchResults)
143
+            ],
144
+            $query->getCursor() + $query->getLimit()
145
+        );
146
+    }
147
+
148
+    /**
149
+     * Format subline for tagged files: Show the first 3 tags
150
+     *
151
+     * @param $query
152
+     * @param array $tagInfo
153
+     * @return string
154
+     */
155
+    private function formatSubline(ISearchQuery $query, array $tagInfo): string {
156
+        /**
157
+         * @var ISystemTag[]
158
+         */
159
+        $tags = $this->tagManager->getTagsByIds($tagInfo);
160
+        $tagNames = array_map(function ($tag) {
161
+            return $tag->getName();
162
+        }, array_filter($tags, function ($tag) {
163
+            return $tag->isUserVisible();
164
+        }));
165
+
166
+        // show the tag that you have searched for first
167
+        usort($tagNames, function ($tagName) use ($query) {
168
+            return strpos($tagName, $query->getTerm()) !== false? -1 :  1;
169
+        });
170
+
171
+        return $this->l10n->t('tagged %s', [implode(', ', array_slice($tagNames, 0, 3))]);
172
+    }
173
+
174
+    private function flattenArray($array) {
175
+        $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
176
+        return iterator_to_array($it, true);
177
+    }
178 178
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -77,9 +77,9 @@  discard block
 block discarded – undo
77 77
 
78 78
 		$userFolder = $this->rootFolder->getUserFolder($user->getUID());
79 79
 		$fileQuery = new SearchQuery(
80
-			new SearchComparison(ISearchComparison::COMPARE_LIKE, 'systemtag', '%' . $query->getTerm() . '%'),
80
+			new SearchComparison(ISearchComparison::COMPARE_LIKE, 'systemtag', '%'.$query->getTerm().'%'),
81 81
 			$query->getLimit(),
82
-			(int)$query->getCursor(),
82
+			(int) $query->getCursor(),
83 83
 			$query->getSortOrder() === ISearchQuery::SORT_DATE_DESC ? [
84 84
 				new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
85 85
 			] : [],
@@ -88,17 +88,17 @@  discard block
 block discarded – undo
88 88
 
89 89
 		// do search
90 90
 		$searchResults = $userFolder->search($fileQuery);
91
-		$resultIds = array_map(function (Node $node) {
91
+		$resultIds = array_map(function(Node $node) {
92 92
 			return $node->getId();
93 93
 		}, $searchResults);
94 94
 		$matchedTags = $this->objectMapper->getTagIdsForObjects($resultIds, 'files');
95 95
 
96 96
 		// prepare direct tag results
97
-		$tagResults = array_map(function (ISystemTag $tag) {
97
+		$tagResults = array_map(function(ISystemTag $tag) {
98 98
 			$thumbnailUrl = '';
99 99
 			$link = $this->urlGenerator->linkToRoute('files.view.indexView', [
100 100
 				'view' => 'tags',
101
-			]) . '?dir=' . $tag->getId();
101
+			]).'?dir='.$tag->getId();
102 102
 			$searchResultEntry = new SearchResultEntry(
103 103
 				$thumbnailUrl,
104 104
 				$this->l10n->t('All tagged %s …', [$tag->getName()]),
@@ -114,7 +114,7 @@  discard block
 block discarded – undo
114 114
 			$this->l10n->t('Tags'),
115 115
 			[
116 116
 				...$tagResults,
117
-				...array_map(function (Node $result) use ($userFolder, $matchedTags, $query) {
117
+				...array_map(function(Node $result) use ($userFolder, $matchedTags, $query) {
118 118
 					$nodeId = $result->getId();
119 119
 					// Generate thumbnail url
120 120
 					$thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $nodeId]);
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
 							? 'icon-folder'
137 137
 							: $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype())
138 138
 					);
139
-					$searchResultEntry->addAttribute('fileId', (string)$nodeId);
139
+					$searchResultEntry->addAttribute('fileId', (string) $nodeId);
140 140
 					$searchResultEntry->addAttribute('path', $path);
141 141
 					return $searchResultEntry;
142 142
 				}, $searchResults)
@@ -157,15 +157,15 @@  discard block
 block discarded – undo
157 157
 		 * @var ISystemTag[]
158 158
 		 */
159 159
 		$tags = $this->tagManager->getTagsByIds($tagInfo);
160
-		$tagNames = array_map(function ($tag) {
160
+		$tagNames = array_map(function($tag) {
161 161
 			return $tag->getName();
162
-		}, array_filter($tags, function ($tag) {
162
+		}, array_filter($tags, function($tag) {
163 163
 			return $tag->isUserVisible();
164 164
 		}));
165 165
 
166 166
 		// show the tag that you have searched for first
167
-		usort($tagNames, function ($tagName) use ($query) {
168
-			return strpos($tagName, $query->getTerm()) !== false? -1 :  1;
167
+		usort($tagNames, function($tagName) use ($query) {
168
+			return strpos($tagName, $query->getTerm()) !== false ? -1 : 1;
169 169
 		});
170 170
 
171 171
 		return $this->l10n->t('tagged %s', [implode(', ', array_slice($tagNames, 0, 3))]);
Please login to merge, or discard this patch.