Completed
Push — master ( 7f2d14...7fc8f2 )
by Fabien
02:11
created

ToJsonViewHelper::getRowsViewHelper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Fab\Vidi\ViewHelpers\Result;
3
4
/*
5
 * This file is part of the Fab/Vidi project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10
11
use Fab\Vidi\View\Grid\Rows;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
14
15
/**
16
 * View helper for rendering a JSON response.
17
 */
18
class ToJsonViewHelper extends AbstractViewHelper
19
{
20
21
    /**
22
     * Render a Json response
23
     *
24
     * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException
25
     */
26
    public function render()
27
    {
28
29
        $objects = $this->templateVariableContainer->get('objects');
30
        $columns = $this->templateVariableContainer->get('columns');
31
        $output = array(
32
            'sEcho' => $this->getNextTransactionId(),
33
            'iTotalRecords' => $this->templateVariableContainer->get('numberOfObjects'),
34
            'iTotalDisplayRecords' => $this->templateVariableContainer->get('numberOfObjects'),
35
            'iNumberOfRecords' => count($objects),
36
            'aaData' => $this->getRowsView()->render($objects, $columns),
37
        );
38
39
        $this->setHttpHeaders();
40
        print json_encode($output);
41
    }
42
43
    /**
44
     * @return int
45
     */
46
    protected function getNextTransactionId()
47
    {
48
        $transaction = 0;
49
        if (GeneralUtility::_GET('sEcho')) {
50
            $transaction = (int)GeneralUtility::_GET('sEcho') + 1;
51
        }
52
        return $transaction;
53
    }
54
55
    /**
56
     * @return void
57
     * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException
58
     * @throws \InvalidArgumentException
59
     */
60
    protected function setHttpHeaders()
61
    {
62
        /** @var \TYPO3\CMS\Extbase\Mvc\Web\Response $response */
63
        $response = $this->templateVariableContainer->get('response');
64
        $response->setHeader('Content-Type', 'application/json');
65
        $response->sendHeaders();
66
    }
67
68
    /**
69
     * @return Rows|object
70
     */
71
    protected function getRowsView()
72
    {
73
        return GeneralUtility::makeInstance(Rows::class);
74
    }
75
}
76