Completed
Push — master ( a54493...36663b )
by Fabien
05:49
created

FacetController::getFacetSuggestionService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Fab\Vidi\Controller;
3
4
/**
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use Fab\Vidi\Facet\FacetInterface;
18
use Fab\Vidi\Tca\Tca;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
21
22
/**
23
 * Controller which handles actions related to "Facet" in a Vidi module.
24
 */
25
class FacetController extends ActionController
26
{
27
28
    /**
29
     * Suggest values according to a facet.
30
     * Output a json list of key / values.
31
     *
32
     * @param string $facet
33
     * @param string $searchTerm
34
     * @validate $facet Fab\Vidi\Domain\Validator\FacetValidator
35
     * @return string
36
     */
37
    public function autoSuggestAction($facet, $searchTerm)
38
    {
39
40
        $suggestions = $this->getFacetSuggestionService()->getSuggestions($facet);
41
42
        # Json header is not automatically sent in the BE...
43
        $this->response->setHeader('Content-Type', 'application/json');
44
        $this->response->sendHeaders();
45
        return json_encode($suggestions);
46
    }
47
48
    /**
49
     * Suggest values for all configured facets in the Grid.
50
     * Output a json list of key / values.
51
     *
52
     * @return string
53
     */
54
    public function autoSuggestsAction()
55
    {
56
57
        $suggestions = array();
58
        foreach (Tca::grid()->getFacets() as $facet) {
59
            /** @var FacetInterface $facet */
60
            $name = $facet->getName();
61
            $suggestions[$name] = $this->getFacetSuggestionService()->getSuggestions($name);
62
        }
63
64
        # Json header is not automatically sent in the BE...
65
        $this->response->setHeader('Content-Type', 'application/json');
66
        $this->response->sendHeaders();
67
        return json_encode($suggestions);
68
    }
69
70
    /**
71
     * @return \Fab\Vidi\Facet\FacetSuggestionService
72
     */
73
    protected function getFacetSuggestionService()
74
    {
75
        return GeneralUtility::makeInstance('Fab\Vidi\Facet\FacetSuggestionService');
76
    }
77
78
}
79