Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for editable association fields from type choice in Lis… #4449

Merged
merged 8 commits into from Apr 24, 2017
45 changes: 32 additions & 13 deletions Controller/HelperController.php
Expand Up @@ -227,47 +227,44 @@ public function setObjectFieldValueAction(Request $request)
$field = $request->get('field');
$code = $request->get('code');
$objectId = $request->get('objectId');
$value = $request->get('value');
$value = $originalValue = $request->get('value');
$context = $request->get('context');

$admin = $this->pool->getInstance($code);
$admin->setRequest($request);

// alter should be done by using a post method
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') {
return new JsonResponse(array('status' => 'KO', 'message' => 'Expected a POST Request'));
return new JsonResponse('Expected an POST Request', 405);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post starts with a consonant sound, so "a"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops!

}

$rootObject = $object = $admin->getObject($objectId);

if (!$object) {
return new JsonResponse(array('status' => 'KO', 'message' => 'Object does not exist'));
return new JsonResponse('Object does not exist', 404);
}

// check user permission
if (false === $admin->hasAccess('edit', $object)) {
return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid permissions'));
return new JsonResponse('Invalid permissions', 403);
}

if ($context == 'list') {
$fieldDescription = $admin->getListFieldDescription($field);
} else {
return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid context'));
return new JsonResponse('Invalid context', 400);
}

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')) {
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);
Expand All @@ -291,6 +288,28 @@ public function setObjectFieldValueAction(Request $request)
$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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not PSR-compliant, although not detected yet. Please put each sprintf argument on its own line.

}
}

$this->pool->getPropertyAccessor()->setValue($object, $propertyPath, '' !== $value ? $value : null);

$violations = $this->validator->validate($object);
Expand All @@ -302,7 +321,7 @@ public function setObjectFieldValueAction(Request $request)
$messages[] = $violation->getMessage();
}

return new JsonResponse(array('status' => 'KO', 'message' => implode("\n", $messages)));
return new JsonResponse(implode("\n", $messages), 400);
}

$admin->update($object);
Expand All @@ -313,7 +332,7 @@ public function setObjectFieldValueAction(Request $request)

$content = $extension->renderListElement($this->twig, $rootObject, $fieldDescription);

return new JsonResponse(array('status' => 'OK', 'content' => $content));
return new JsonResponse($content, 200);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions Resources/doc/reference/action_list.rst
Expand Up @@ -67,6 +67,17 @@ Here is an example:
'editable' => true
))

// editable association field
->add('status', 'choice', array(
'editable' => true,
'class' => 'Vendor\ExampleBundle\Entity\ExampleStatus',
'choices' => array(
1 => 'Active',
2 => 'Inactive',
3 => 'Draft',
),
))

// we can add options to the field depending on the type
->add('price', 'currency', array(
'currency' => $this->currencyDetector->getCurrency()->getLabel()
Expand Down Expand Up @@ -141,6 +152,8 @@ Available types and associated options
| | delimiter | Separator of values if multiple. |
+ +----------------+-----------------------------------------------------------------------+
| | catalogue | Translation catalogue. |
+ +----------------+-----------------------------------------------------------------------+
| | class | Class path for editable association field. |
+-----------+----------------+-----------------------------------------------------------------------+
| currency | currency (m) | A currency string (EUR or USD for instance). |
+-----------+----------------+-----------------------------------------------------------------------+
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/reference/field_types.rst
Expand Up @@ -26,7 +26,7 @@ html display (and optionally truncate or strip tags from) raw html
============ =============================================

Theses types accept an ``editable`` parameter to edit the value from within the list action.
This is currently limited to scalar types (text, integer, url...).
This is currently limited to scalar types (text, integer, url...) and choice types with association field.

.. note::

Expand Down
37 changes: 16 additions & 21 deletions Resources/public/Admin.js
Expand Up @@ -55,7 +55,7 @@ var Admin = {
padding: 15,
overflow: 'auto'
});

jQuery(modal).trigger('sonata-admin-setup-list-modal');
},
setup_select2: function(subject) {
Expand Down Expand Up @@ -114,17 +114,14 @@ var Admin = {
container: 'body',
placement: 'auto',
success: function(response) {
if('KO' === response.status) {
return response.message;
}

var html = jQuery(response.content);
var html = jQuery(response);
Admin.setup_xeditable(html);

jQuery(this)
.closest('td')
.replaceWith(html)
;
.replaceWith(html);
},
error: function(xhr, statusText, errorThrown) {
return xhr.responseText;
}
});
},
Expand Down Expand Up @@ -241,22 +238,20 @@ var Admin = {
this.log(jQuery('a.sonata-ba-edit-inline', subject));
jQuery('a.sonata-ba-edit-inline', subject).click(function(event) {
Admin.stopEvent(event);

var subject = jQuery(this);
jQuery.ajax({
url: subject.attr('href'),
type: 'POST',
success: function(json) {
if(json.status === "OK") {
var elm = jQuery(subject).parent();
elm.children().remove();
// fix issue with html comment ...
elm.html(jQuery(json.content.replace(/<!--[\s\S]*?-->/g, "")).html());
elm.effect("highlight", {'color' : '#57A957'}, 2000);
Admin.set_object_field_value(elm);
} else {
jQuery(subject).parent().effect("highlight", {'color' : '#C43C35'}, 2000);
}
success: function(response) {
var elm = jQuery(subject).parent();
elm.children().remove();
// fix issue with html comment ...
elm.html(jQuery(response.replace(/<!--[\s\S]*?-->/g, "")).html());
elm.effect("highlight", {'color' : '#57A957'}, 2000);
Admin.set_object_field_value(elm);
},
error: function(xhr, statusText, errorThrown) {
jQuery(subject).parent().effect("highlight", {'color' : '#C43C35'}, 2000);
}
});
});
Expand Down