1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the jquery-datatables-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\JQuery\DatatablesBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use WBW\Bundle\JQuery\DatatablesBundle\API\DataTablesResponse; |
18
|
|
|
use WBW\Bundle\JQuery\DatatablesBundle\API\DataTablesWrapper; |
19
|
|
|
use WBW\Bundle\JQuery\DatatablesBundle\Exception\BadDataTablesRepositoryException; |
20
|
|
|
use WBW\Bundle\JQuery\DatatablesBundle\Exception\UnregisteredDataTablesProviderException; |
21
|
|
|
use WBW\Bundle\JQuery\DatatablesBundle\Manager\DataTablesManager; |
22
|
|
|
use WBW\Bundle\JQuery\DatatablesBundle\Repository\DataTablesRepositoryInterface; |
23
|
|
|
use WBW\Library\Core\IO\HTTPInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* DataTables controller. |
27
|
|
|
* |
28
|
|
|
* @author webeweb <https://github.com/webeweb/> |
29
|
|
|
* @package WBW\Bundle\JQuery\DatatablesBundle\Controller |
30
|
|
|
* @final |
31
|
|
|
*/ |
32
|
|
|
final class DataTablesController extends Controller { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Lists all entities. |
36
|
|
|
* |
37
|
|
|
* @param Request $request The request. |
38
|
|
|
* @param string $name The provider name. |
39
|
|
|
* @return Response Returns the response. |
40
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
41
|
|
|
*/ |
42
|
|
|
public function indexAction(Request $request, $name) { |
43
|
|
|
|
44
|
|
|
// Get the provider. |
45
|
|
|
$dtProvider = $this->get(DataTablesManager::SERVICE_NAME)->getProvider($name); |
46
|
|
|
|
47
|
|
|
// Initialize the DataTables wrapper. |
48
|
|
|
$dtWrapper = new DataTablesWrapper($dtProvider->getPrefix(), HTTPInterface::HTTP_METHOD_POST, "jquery_datatables_index", ["name" => $name]); |
49
|
|
|
foreach ($dtProvider->getColumns() as $dtColumn) { |
50
|
|
|
$dtWrapper->addColumn($dtColumn); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Check if the request is an XML HTTP request. |
54
|
|
|
if (true === $request->isXmlHttpRequest()) { |
55
|
|
|
|
56
|
|
|
// Get the entities manager. |
57
|
|
|
$em = $this->getDoctrine()->getManager(); |
58
|
|
|
|
59
|
|
|
// Get and check the entities repository. |
60
|
|
|
$repository = $em->getRepository($dtProvider->getEntity()); |
61
|
|
|
if (false === ($repository instanceOf DataTablesRepositoryInterface)) { |
62
|
|
|
throw new BadDataTablesRepositoryException($repository); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Parse the request. |
66
|
|
|
$dtWrapper->parse($request); |
67
|
|
|
|
68
|
|
|
// |
69
|
|
|
$filtered = $repository->dataTablesCountFiltered($dtWrapper); |
70
|
|
|
$total = $repository->dataTablesCountTotal($dtWrapper); |
71
|
|
|
$entities = $repository->dataTablesFindAll($dtWrapper); |
72
|
|
|
|
73
|
|
|
// Set the response. |
74
|
|
|
$dtWrapper->getResponse()->setRecordsFiltered($filtered); |
75
|
|
|
$dtWrapper->getResponse()->setRecordsTotal($total); |
76
|
|
|
|
77
|
|
|
// Handle each entity. |
78
|
|
|
foreach ($entities as $entity) { |
79
|
|
|
|
80
|
|
|
// Count the rows. |
81
|
|
|
$rows = count($dtWrapper->getResponse()->getData()); |
82
|
|
|
|
83
|
|
|
// Create a row. |
84
|
|
|
$dtWrapper->getResponse()->addRow(); |
85
|
|
|
|
86
|
|
|
// Render each optional parameters. |
87
|
|
|
foreach (DataTablesResponse::dtRow() as $dtRow) { |
88
|
|
|
$dtWrapper->getResponse()->setRow($dtRow, $dtProvider->renderRow($dtRow, $entity, $rows)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Render each column. |
92
|
|
|
foreach ($dtWrapper->getColumns() as $dtColumn) { |
93
|
|
|
$dtWrapper->getResponse()->setRow($dtColumn->getData(), $dtProvider->renderColumn($dtColumn, $entity)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Return the response. |
98
|
|
|
return new Response(json_encode($dtWrapper->getResponse())); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Initialize the default view. |
102
|
|
|
$dtView = "@JQueryDataTables/DataTables/index.html.twig"; |
103
|
|
|
|
104
|
|
|
// Check the provider view. |
105
|
|
|
if (null !== $dtProvider->getView()) { |
106
|
|
|
$dtView = $dtProvider->getView(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Return the response. |
110
|
|
|
return $this->render($dtView, [ |
111
|
|
|
"dtWrapper" => $dtWrapper, |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|