/
Provider.php
83 lines (70 loc) 路 1.99 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace OCA\Cookbook\Search;
use OCA\Cookbook\AppInfo\Application;
use OCA\Cookbook\Service\RecipeService;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
class Provider implements IProvider {
/** @var IL10N */
private $l;
/** @var IURLGenerator */
private $urlGenerator;
/** @var RecipeService */
private $recipeService;
public function __construct(
IL10n $il10n,
IURLGenerator $urlGenerator,
RecipeService $recipeService
) {
$this->l = $il10n;
$this->urlGenerator = $urlGenerator;
$this->recipeService = $recipeService;
}
public function getId(): string {
return Application::APP_ID;
}
public function getName(): string {
return $this->l->t('Recipes');
}
public function getOrder(string $route, array $routeParameters): int {
if (strpos($route, 'files' . '.') === 0) {
return 25;
} elseif (strpos($route, Application::APP_ID . '.') === 0) {
return -1;
}
return 4;
}
public function search(IUser $user, ISearchQuery $query): SearchResult {
$recipes = $this->recipeService->findRecipesInSearchIndex($query->getTerm());
$result = array_map(
function (array $recipe) use ($user): SearchResultEntry {
$id = $recipe['recipe_id'];
$subline = '';
$category = $recipe['category'];
if ($category !== null) {
// TRANSLATORS Will be shown in search results, listing the recipe category, e.g., 'in Salads'
$subline = $this->l->t('in %s', [$category]);
}
return new SearchResultEntry(
// Thumb image
$this->urlGenerator->linkToRoute('cookbook.recipe.image', ['id' => $id, 'size' => 'thumb']),
// Name as title
$recipe['name'],
// Category as subline
$subline,
// Link to Vue route of recipe
$this->urlGenerator->linkToRouteAbsolute('cookbook.main.index') . '#/recipe/' . $id
);
}, $recipes
);
return SearchResult::complete(
$this->getName(),
$result
);
}
}