]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/TagController.php
1823b0dc8b1e7ee47ddd8188fd0430942231fe5d
[bookstack] / app / Http / Controllers / TagController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Repos\TagRepo;
4 use Illuminate\Http\Request;
5 use BookStack\Http\Requests;
6
7 class TagController extends Controller
8 {
9
10     protected $tagRepo;
11
12     /**
13      * TagController constructor.
14      * @param $tagRepo
15      */
16     public function __construct(TagRepo $tagRepo)
17     {
18         $this->tagRepo = $tagRepo;
19     }
20
21     /**
22      * Get all the Tags for a particular entity
23      * @param $entityType
24      * @param $entityId
25      */
26     public function getForEntity($entityType, $entityId)
27     {
28         $tags = $this->tagRepo->getForEntity($entityType, $entityId);
29         return response()->json($tags);
30     }
31
32     /**
33      * Update the tags for a particular entity.
34      * @param $entityType
35      * @param $entityId
36      * @param Request $request
37      * @return mixed
38      */
39     public function updateForEntity($entityType, $entityId, Request $request)
40     {
41         $entity = $this->tagRepo->getEntity($entityType, $entityId, 'update');
42         if ($entity === null) return $this->jsonError("Entity not found", 404);
43
44         $inputTags = $request->input('tags');
45         $tags = $this->tagRepo->saveTagsToEntity($entity, $inputTags);
46         return response()->json([
47             'tags' => $tags,
48             'message' => 'Tags successfully updated'
49         ]);
50     }
51
52     /**
53      * Get tag name suggestions from a given search term.
54      * @param Request $request
55      */
56     public function getNameSuggestions(Request $request)
57     {
58         $searchTerm = $request->get('search');
59         $suggestions = $this->tagRepo->getNameSuggestions($searchTerm);
60         return response()->json($suggestions);
61     }
62
63     /**
64      * Get tag value suggestions from a given search term.
65      * @param Request $request
66      */
67     public function getValueSuggestions(Request $request)
68     {
69         $searchTerm = $request->get('search');
70         $suggestions = $this->tagRepo->getValueSuggestions($searchTerm);
71         return response()->json($suggestions);
72     }
73
74 }