|
@@ -227,7 +227,7 @@ class HelperController
|
|
$field = $request->get('field');
|
|
$field = $request->get('field');
|
|
$code = $request->get('code');
|
|
$code = $request->get('code');
|
|
$objectId = $request->get('objectId');
|
|
$objectId = $request->get('objectId');
|
|
- $value = $request->get('value');
|
|
|
|
|
|
+ $value = $originalValue = $request->get('value');
|
|
$context = $request->get('context');
|
|
$context = $request->get('context');
|
|
|
|
|
|
$admin = $this->pool->getInstance($code);
|
|
$admin = $this->pool->getInstance($code);
|
|
@@ -235,39 +235,36 @@ class HelperController
|
|
|
|
|
|
// alter should be done by using a post method
|
|
// alter should be done by using a post method
|
|
if (!$request->isXmlHttpRequest()) {
|
|
if (!$request->isXmlHttpRequest()) {
|
|
- return new JsonResponse(array('status' => 'KO', 'message' => 'Expected a XmlHttpRequest request header'));
|
|
|
|
|
|
+ return new JsonResponse('Expected an XmlHttpRequest request header', 405);
|
|
}
|
|
}
|
|
|
|
|
|
if ($request->getMethod() != 'POST') {
|
|
if ($request->getMethod() != 'POST') {
|
|
- return new JsonResponse(array('status' => 'KO', 'message' => 'Expected a POST Request'));
|
|
|
|
|
|
+ return new JsonResponse('Expected a POST Request', 405);
|
|
}
|
|
}
|
|
|
|
|
|
$rootObject = $object = $admin->getObject($objectId);
|
|
$rootObject = $object = $admin->getObject($objectId);
|
|
|
|
|
|
if (!$object) {
|
|
if (!$object) {
|
|
- return new JsonResponse(array('status' => 'KO', 'message' => 'Object does not exist'));
|
|
|
|
|
|
+ return new JsonResponse('Object does not exist', 404);
|
|
}
|
|
}
|
|
|
|
|
|
// check user permission
|
|
// check user permission
|
|
if (false === $admin->hasAccess('edit', $object)) {
|
|
if (false === $admin->hasAccess('edit', $object)) {
|
|
- return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid permissions'));
|
|
|
|
|
|
+ return new JsonResponse('Invalid permissions', 403);
|
|
}
|
|
}
|
|
|
|
|
|
if ($context == 'list') {
|
|
if ($context == 'list') {
|
|
$fieldDescription = $admin->getListFieldDescription($field);
|
|
$fieldDescription = $admin->getListFieldDescription($field);
|
|
} else {
|
|
} else {
|
|
- return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid context'));
|
|
|
|
|
|
+ return new JsonResponse('Invalid context', 400);
|
|
}
|
|
}
|
|
|
|
|
|
if (!$fieldDescription) {
|
|
if (!$fieldDescription) {
|
|
- return new JsonResponse(array('status' => 'KO', 'message' => 'The field does not exist'));
|
|
|
|
|
|
+ return new JsonResponse('The field does not exist', 400);
|
|
}
|
|
}
|
|
|
|
|
|
if (!$fieldDescription->getOption('editable')) {
|
|
if (!$fieldDescription->getOption('editable')) {
|
|
- return new JsonResponse(array(
|
|
|
|
- 'status' => 'KO',
|
|
|
|
- 'message' => 'The field cannot be edit, editable option must be set to true',
|
|
|
|
- ));
|
|
|
|
|
|
+ return new JsonResponse('The field cannot be edited, editable option must be set to true', 400);
|
|
}
|
|
}
|
|
|
|
|
|
$propertyPath = new PropertyPath($field);
|
|
$propertyPath = new PropertyPath($field);
|
|
@@ -291,6 +288,37 @@ class HelperController
|
|
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
|
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // Handle entity choice association type, transforming the value into entity
|
|
|
|
+ if ('' !== $value && $fieldDescription->getType() == 'choice' && $fieldDescription->getOption('class')) {
|
|
|
|
+ // Get existing associations for current object
|
|
|
|
+ $associations = $admin->getModelManager()
|
|
|
|
+ ->getEntityManager($admin->getClass())->getClassMetadata($admin->getClass())
|
|
|
|
+ ->getAssociationNames();
|
|
|
|
+
|
|
|
|
+ if (!in_array($field, $associations)) {
|
|
|
|
+ return new JsonResponse(
|
|
|
|
+ sprintf(
|
|
|
|
+ 'Unknown association "%s", association does not exist in entity "%s", available associations are "%s".',
|
|
|
|
+ $field,
|
|
|
|
+ $this->admin->getClass(),
|
|
|
|
+ implode(', ', $associations)),
|
|
|
|
+ 404);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $value = $admin->getConfigurationPool()->getContainer()->get('doctrine')->getManager()
|
|
|
|
+ ->getRepository($fieldDescription->getOption('class'))
|
|
|
|
+ ->find($value);
|
|
|
|
+
|
|
|
|
+ if (!$value) {
|
|
|
|
+ return new JsonResponse(
|
|
|
|
+ sprintf(
|
|
|
|
+ 'Edit failed, object with id "%s" not found in association "%s".',
|
|
|
|
+ $originalValue,
|
|
|
|
+ $field),
|
|
|
|
+ 404);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
$this->pool->getPropertyAccessor()->setValue($object, $propertyPath, '' !== $value ? $value : null);
|
|
$this->pool->getPropertyAccessor()->setValue($object, $propertyPath, '' !== $value ? $value : null);
|
|
|
|
|
|
$violations = $this->validator->validate($object);
|
|
$violations = $this->validator->validate($object);
|
|
@@ -302,7 +330,7 @@ class HelperController
|
|
$messages[] = $violation->getMessage();
|
|
$messages[] = $violation->getMessage();
|
|
}
|
|
}
|
|
|
|
|
|
- return new JsonResponse(array('status' => 'KO', 'message' => implode("\n", $messages)));
|
|
|
|
|
|
+ return new JsonResponse(implode("\n", $messages), 400);
|
|
}
|
|
}
|
|
|
|
|
|
$admin->update($object);
|
|
$admin->update($object);
|
|
@@ -313,7 +341,7 @@ class HelperController
|
|
|
|
|
|
$content = $extension->renderListElement($this->twig, $rootObject, $fieldDescription);
|
|
$content = $extension->renderListElement($this->twig, $rootObject, $fieldDescription);
|
|
|
|
|
|
- return new JsonResponse(array('status' => 'OK', 'content' => $content));
|
|
|
|
|
|
+ return new JsonResponse($content, 200);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|