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