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