|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fab\Vidi\ViewHelpers\Result; |
|
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\ViewHelpers\Grid\RowsViewHelper; |
|
18
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
19
|
|
|
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* View helper for rendering a JSON response. |
|
23
|
|
|
*/ |
|
24
|
|
|
class ToJsonViewHelper extends AbstractViewHelper |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Render a Json response |
|
29
|
|
|
* |
|
30
|
|
|
* @return boolean |
|
31
|
|
|
* @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function render() |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
$objects = $this->templateVariableContainer->get('objects'); |
|
37
|
|
|
$columns = $this->templateVariableContainer->get('columns'); |
|
38
|
|
|
$output = array( |
|
39
|
|
|
'sEcho' => $this->getNextTransactionId(), |
|
40
|
|
|
'iTotalRecords' => $this->templateVariableContainer->get('numberOfObjects'), |
|
41
|
|
|
'iTotalDisplayRecords' => $this->templateVariableContainer->get('numberOfObjects'), |
|
42
|
|
|
'iNumberOfRecords' => count($objects), |
|
43
|
|
|
'aaData' => $this->getRowsViewHelper()->render($objects, $columns), |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
$output = $this->encodeItems($output); |
|
47
|
|
|
$this->setHttpHeaders(); |
|
48
|
|
|
return json_encode($output); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return int |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function getNextTransactionId() |
|
55
|
|
|
{ |
|
56
|
|
|
$transaction = 0; |
|
57
|
|
|
if (GeneralUtility::_GET('sEcho')) { |
|
58
|
|
|
$transaction = (int)GeneralUtility::_GET('sEcho') + 1; |
|
59
|
|
|
} |
|
60
|
|
|
return $transaction; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $values |
|
65
|
|
|
* @return mixed |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function encodeItems(array $values) { |
|
68
|
|
|
foreach($values as $key => $value) { |
|
69
|
|
|
if(is_array($value)) { |
|
70
|
|
|
$values[$key] = $this->encodeItems($value); |
|
71
|
|
|
} |
|
72
|
|
|
else { |
|
73
|
|
|
$values[$key] = utf8_encode($value); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
return $values; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function setHttpHeaders() |
|
83
|
|
|
{ |
|
84
|
|
|
/** @var \TYPO3\CMS\Extbase\Mvc\Web\Response $response */ |
|
85
|
|
|
$response = $this->templateVariableContainer->get('response'); |
|
86
|
|
|
$response->setHeader('Content-Type', 'application/json'); |
|
87
|
|
|
$response->sendHeaders(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return RowsViewHelper |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function getRowsViewHelper() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->objectManager->get(RowsViewHelper::class); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|