This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathDatatableController.php
179 lines (156 loc) · 5.42 KB
/
DatatableController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* This file is part of the SgDatatablesBundle package.
*
* (c) stwe <https://github.com/stwe/DatatablesBundle>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sg\DatatablesBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\DBAL\Types\Type;
use Exception;
use DateTime;
/**
* Class DatatableController
*
* @package Sg\DatatablesBundle\Controller
*/
class DatatableController extends Controller
{
//-------------------------------------------------
// Actions
//-------------------------------------------------
/**
* Edit field.
*
* @param Request $request
*
* @Route("/datatables/edit/field", methods={"POST"}, name="sg_datatables_edit")
*
* @return Response
* @throws Exception
*/
public function editAction(Request $request)
{
if ($request->isXmlHttpRequest()) {
// x-editable sends some default parameters
$pk = $request->request->get('pk'); // entity primary key
$field = $request->request->get('name'); // e.g. comments.createdBy.username
$value = $request->request->get('value'); // the new value
// additional params
$entityClassName = $request->request->get('entityClassName'); // e.g. AppBundle\Entity\Post
$token = $request->request->get('token');
$originalTypeOfField = $request->request->get('originalTypeOfField');
$path = $request->request->get('path'); // for toMany - the current element
// check token
if (!$this->isCsrfTokenValid('sg-datatables-editable', $token)) {
throw new AccessDeniedException('DatatableController::editAction(): The CSRF token is invalid.');
}
// get an object by its primary key
$entity = $this->getEntityByPk($entityClassName, $pk);
/** @var PropertyAccessor $accessor */
/** @noinspection PhpUndefinedMethodInspection */
$accessor = PropertyAccess::createPropertyAccessorBuilder()
->enableMagicCall()
->getPropertyAccessor();
// normalize the new value
$value = $this->normalizeValue($originalTypeOfField, $value);
// set new value
null !== $path ? $accessor->setValue($entity, $path, $value) : $accessor->setValue($entity, $field, $value);
// save all
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return new Response('Success', 200);
}
return new Response('Bad request', 400);
}
//-------------------------------------------------
// Helper
//-------------------------------------------------
/**
* Finds an object by its primary key / identifier.
*
* @param string $entityClassName
* @param mixed $pk
*
* @return object
*/
private function getEntityByPk($entityClassName, $pk)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository($entityClassName)->find($pk);
if (!$entity) {
throw $this->createNotFoundException('DatatableController::getEntityByPk(): The entity does not exist.');
}
return $entity;
}
/**
* Normalize value.
*
* @param string $originalTypeOfField
* @param mixed $value
*
* @return bool|DateTime|float|int|null|string
* @throws Exception
*/
private function normalizeValue($originalTypeOfField, $value)
{
switch ($originalTypeOfField) {
case Type::DATETIME:
case Type::TIME:
$value = new DateTime($value);
break;
case Type::BOOLEAN:
$value = $this->strToBool($value);
break;
case Type::TEXT:
case Type::STRING:
break;
case Type::SMALLINT:
case Type::INTEGER:
$value = (int) $value;
break;
case Type::BIGINT:
$value = (string) $value;
break;
case Type::FLOAT:
case Type::DECIMAL:
$value = (float) $value;
break;
default:
throw new Exception("DatatableController::prepareValue(): The field type {$originalTypeOfField} is not editable.");
}
return $value;
}
/**
* String to boolean.
*
* @param string $str
*
* @return null|bool
* @throws Exception
*/
private function strToBool($str)
{
$str = strtolower($str);
if ('null' === $str) {
return null;
}
if ($str === 'true' || $str === '1') {
return true;
} elseif ($str === 'false' || $str === '0') {
return false;
} else {
throw new Exception('DatatableController::strToBool(): Cannot convert string to boolean, expected string "true" or "false".');
}
}
}