]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/TagController.php
00aaf2b7899f775b9db43915b2b50659f7f3ba93
[bookstack] / app / Http / Controllers / TagController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Actions\TagRepo;
6 use BookStack\Util\SimpleListOptions;
7 use Illuminate\Http\Request;
8
9 class TagController extends Controller
10 {
11     public function __construct(
12         protected TagRepo $tagRepo
13     ) {
14     }
15
16     /**
17      * Show a listing of existing tags in the system.
18      */
19     public function index(Request $request)
20     {
21         $listOptions = SimpleListOptions::fromRequest($request, 'tags')->withSortOptions([
22             'name' => trans('common.sort_name'),
23             'usages' => trans('entities.tags_usages'),
24         ]);
25
26         $nameFilter = $request->get('name', '');
27         $tags = $this->tagRepo
28             ->queryWithTotals($listOptions, $nameFilter)
29             ->paginate(50)
30             ->appends(array_filter(array_merge($listOptions->getPaginationAppends(), [
31                 'name'   => $nameFilter,
32             ])));
33
34         $this->setPageTitle(trans('entities.tags'));
35
36         return view('tags.index', [
37             'tags'        => $tags,
38             'nameFilter'  => $nameFilter,
39             'listOptions' => $listOptions,
40         ]);
41     }
42
43     /**
44      * Get tag name suggestions from a given search term.
45      */
46     public function getNameSuggestions(Request $request)
47     {
48         $searchTerm = $request->get('search', '');
49         $suggestions = $this->tagRepo->getNameSuggestions($searchTerm);
50
51         return response()->json($suggestions);
52     }
53
54     /**
55      * Get tag value suggestions from a given search term.
56      */
57     public function getValueSuggestions(Request $request)
58     {
59         $searchTerm = $request->get('search', '');
60         $tagName = $request->get('name', '');
61         $suggestions = $this->tagRepo->getValueSuggestions($searchTerm, $tagName);
62
63         return response()->json($suggestions);
64     }
65 }