Skip to content

Commit

Permalink
Finished the first version of the action override mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed May 7, 2015
1 parent d73c1f4 commit 49458ea
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 90 deletions.
97 changes: 66 additions & 31 deletions Controller/AdminController.php
Expand Up @@ -74,10 +74,10 @@ public function indexAction(Request $request)
)));
}

$customActionName = $action.$this->entity['name'].'Action';
$defaultActionName = $action.'Action';
$customMethodName = $action.$this->entity['name'].'Action';
$defaultMethodName = $action.'Action';

return method_exists($this, $customActionName) ? $this->{$customActionName}() : $this->{$defaultActionName}();
return method_exists($this, $customMethodName) ? $this->{$customMethodName}() : $this->{$defaultMethodName}();
}

/**
Expand Down Expand Up @@ -139,9 +139,10 @@ private function dispatch($eventName, array $arguments = array())
'view' => $this->view,
), $arguments);

$event = new GenericEvent(null, $arguments);
$subject = isset($arguments['paginator']) ? $arguments['paginator'] : $arguments['entity'];
$event = new GenericEvent($subject, $arguments);

$this->get('event_dispatcher')->dispatch($eventName, $arguments);
$this->get('event_dispatcher')->dispatch($eventName, $event);
}

/**
Expand Down Expand Up @@ -187,20 +188,27 @@ protected function editAction()
}

$id = $this->request->query->get('id');
if (!$item = $this->em->getRepository($this->entity['class'])->find($id)) {
if (!$entity = $this->em->getRepository($this->entity['class'])->find($id)) {
throw $this->createNotFoundException(sprintf('Unable to find entity (%s #%d).', $this->entity['name'], $id));
}

$fields = $this->entity['edit']['fields'];
$editForm = $this->createEditForm($item, $fields);
$editForm = $this->createEditForm($entity, $fields);
$deleteForm = $this->createDeleteForm($this->entity['name'], $id);

$editForm->handleRequest($this->request);
if ($editForm->isValid()) {
$this->prepareEditEntityForPersist($item);
$this->dispatch(EasyAdminEvents::PRE_UPDATE, array('entity' => $item));
$this->dispatch(EasyAdminEvents::PRE_UPDATE, array('entity' => $entity));

if (method_exists($this, $customMethodName = 'preUpdate'.$this->entity['name'].'Entity')) {
$this->{$customMethodName}($entity);
} else {
$this->preUpdateEntity($entity);
}

$this->em->flush();
$this->dispatch(EasyAdminEvents::POST_UPDATE, array('entity' => $item));

$this->dispatch(EasyAdminEvents::POST_UPDATE, array('entity' => $entity));

$refererUrl = $this->request->query->get('referer', '');

Expand All @@ -214,7 +222,7 @@ protected function editAction()
return $this->render($this->entity['templates']['edit'], array(
'form' => $editForm->createView(),
'entity_fields' => $fields,
'item' => $item,
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
'view' => 'edit',
));
Expand All @@ -234,7 +242,7 @@ protected function showAction()
}

$id = $this->request->query->get('id');
if (!$item = $this->em->getRepository($this->entity['class'])->find($id)) {
if (!$entity = $this->em->getRepository($this->entity['class'])->find($id)) {
throw $this->createNotFoundException(sprintf('Unable to find entity (%s #%d).', $this->entity['name'], $id));
}

Expand All @@ -244,11 +252,11 @@ protected function showAction()
$this->dispatch(EasyAdminEvents::POST_SHOW, array(
'deleteForm' => $deleteForm,
'fields' => $fields,
'item' => $item,
'entity' => $entity,
));

return $this->render($this->entity['templates']['show'], array(
'item' => $item,
'entity' => $entity,
'fields' => $fields,
'view' => 'show',
'delete_form' => $deleteForm->createView(),
Expand All @@ -268,18 +276,29 @@ protected function newAction()
return $this->renderForbiddenActionError('new');
}

$item = $this->instantiateNewEntity();
if (method_exists($this, $customMethodName = 'createNew'.$this->entity['name'].'Entity')) {
$entity = $this->{$customMethodName}();
} else {
$entity = $this->createNewEntity();
}

$fields = $fields = $this->entity['new']['fields'];
$newForm = $this->createNewForm($item, $fields);
$fields = $this->entity['new']['fields'];
$newForm = $this->createNewForm($entity, $fields);

$newForm->handleRequest($this->request);
if ($newForm->isValid()) {
$this->dispatch(EasyAdminEvents::PRE_PERSIST, array('entity' => $item));
$this->prepareNewEntityForPersist($item);
$this->em->persist($item);
$this->dispatch(EasyAdminEvents::PRE_PERSIST, array('entity' => $entity));

if (method_exists($this, $customMethodName = 'prePersist'.$this->entity['name'].'Entity')) {
$this->{$customMethodName}($entity);
} else {
$this->prePersistEntity($entity);
}

$this->em->persist($entity);
$this->em->flush();
$this->dispatch(EasyAdminEvents::POST_PERSIST, array('entity' => $item));

$this->dispatch(EasyAdminEvents::POST_PERSIST, array('entity' => $entity));

$refererUrl = $this->request->query->get('referer', '');

Expand All @@ -291,13 +310,13 @@ protected function newAction()
$this->dispatch(EasyAdminEvents::POST_NEW, array(
'entity_fields' => $fields,
'form' => $newForm,
'item' => $item,
'entity' => $entity,
));

return $this->render($this->entity['templates']['new'], array(
'form' => $newForm->createView(),
'entity_fields' => $fields,
'item' => $item,
'entity' => $entity,
'view' => 'new',
));
}
Expand Down Expand Up @@ -327,6 +346,12 @@ protected function deleteAction()

$this->dispatch(EasyAdminEvents::PRE_REMOVE, array('entity' => $entity));

if (method_exists($this, $customMethodName = 'preRemove'.$this->entity['name'].'Entity')) {
$this->{$customMethodName}($entity);
} else {
$this->preRemoveEntity($entity);
}

$this->em->remove($entity);
$this->em->flush();

Expand Down Expand Up @@ -408,37 +433,47 @@ protected function ajaxEdit()
*
* @return object
*/
protected function instantiateNewEntity()
protected function createNewEntity()
{
$entityFullyQualifiedClassName = $this->entity['class'];

return new $entityFullyQualifiedClassName();
}

/**
* Allows applications to modify the entity associated with the item being
* created before persisting it.
*
* @param object $entity
*
* @return null
*/
protected function prePersistEntity($entity)
{
}

/**
* Allows applications to modify the entity associated with the item being
* edited before persisting it.
*
* @param object $entity
*
* @return object
* @return null
*/
protected function prepareEditEntityForPersist($entity)
protected function preUpdateEntity($entity)
{
return $entity;
}

/**
* Allows applications to modify the entity associated with the item being
* created before persisting it.
* deleted before removing it.
*
* @param object $entity
*
* @return object
* @return null
*/
protected function prepareNewEntityForPersist($entity)
protected function preRemoveEntity($entity)
{
return $entity;
}

/**
Expand Down
16 changes: 8 additions & 8 deletions Resources/views/default/edit.html.twig
@@ -1,20 +1,20 @@
{% trans_default_domain "EasyAdminBundle" %}
{% form_theme form with easyadmin_config('design.form_theme') %}

{% set _entity = easyadmin_entity(app.request.query.get('entity')) %}
{% set _trans_parameters = { '%entity_name%': _entity.label|trans, '%entity_id%': attribute(item, _entity.primary_key_field_name) } %}
{% set _entity_config = easyadmin_entity(app.request.query.get('entity')) %}
{% set _trans_parameters = { '%entity_name%': _entity_config.label|trans, '%entity_id%': attribute(entity, _entity_config.primary_key_field_name) } %}

{% extends _entity.templates.layout %}
{% extends _entity_config.templates.layout %}

{% block body_class 'admin edit ' ~ _entity.name|lower %}
{% block body_class 'admin edit ' ~ _entity_config.name|lower %}

{% block content_title %}
{{ _entity.edit.title|default('edit.page_title')|trans(_trans_parameters) }}
{{ _entity_config.edit.title|default('edit.page_title')|trans(_trans_parameters) }}
{% endblock %}

{% block main %}
{% block entity_form %}
{{ include(_entity.templates.form, { view: 'edit' }) }}
{{ include(_entity_config.templates.form, { view: 'edit' }) }}
{% endblock entity_form %}

{% block delete_form %}
Expand All @@ -38,8 +38,8 @@
{{ 'action.cancel'|trans(_trans_parameters) }}
</button>

{% if easyadmin_action_is_enabled_for_edit_view('delete', _entity.name) %}
{% set _delete_action = easyadmin_get_action_for_edit_view('delete', _entity.name) %}
{% if easyadmin_action_is_enabled_for_edit_view('delete', _entity_config.name) %}
{% set _delete_action = easyadmin_get_action_for_edit_view('delete', _entity_config.name) %}
<button type="button" data-dismiss="modal" class="btn btn-danger" id="modal-delete-button">
{% if _delete_action.icon %}<i class="fa fa-{{ _delete_action.icon }}"></i>{% endif %}
{{ _delete_action.label|default('action.delete')|trans(_trans_parameters) }}
Expand Down
24 changes: 12 additions & 12 deletions Resources/views/default/form.html.twig
Expand Up @@ -8,7 +8,7 @@
{% set _field_metadata = entity_fields[field.vars.name] %}

{% if _field_metadata['fieldType'] == 'association' %}
{{ easyadmin_render_field_for_edit_view(item, _field_metadata) }}
{{ easyadmin_render_field_for_edit_view(entity, _field_metadata) }}
{% else %}
{% set _field_label = _field_metadata['label']|default(null) %}
{{ form_row(field, { label: _field_label|trans(_trans_parameters) }) }}
Expand Down Expand Up @@ -49,14 +49,14 @@
</button>

{% set _entity_actions = (view == 'new')
? easyadmin_get_actions_for_new_item(_entity.name)
: easyadmin_get_actions_for_edit_item(_entity.name) %}
? easyadmin_get_actions_for_new_item(_entity_config.name)
: easyadmin_get_actions_for_edit_item(_entity_config.name) %}

{% for _action in _entity_actions %}
{% if 'method' == _action.type %}
{% set _action_href = path('admin', { action: _action.name, view: view, entity: _entity.name, id: attribute(item, _entity.primary_key_field_name) }) %}
{% set _action_href = path('admin', { action: _action.name, view: view, entity: _entity_config.name, id: attribute(entity, _entity_config.primary_key_field_name) }) %}
{% elseif 'route' == _action.type %}
{% set _action_href = path(_action.name, { entity: _entity.name, id: attribute(item, _entity.primary_key_field_name) }) %}
{% set _action_href = path(_action.name, { entity: _entity_config.name, id: attribute(entity, _entity_config.primary_key_field_name) }) %}
{% endif %}

<a class="btn {{ _action.class|default('') }}" href="{{ _action_href }}">
Expand All @@ -66,8 +66,8 @@
{% endfor %}

{% if view == 'edit' %}
{% if easyadmin_action_is_enabled_for_edit_view('delete', _entity.name) %}
{% set _action = easyadmin_get_action_for_edit_view('delete', _entity.name) %}
{% if easyadmin_action_is_enabled_for_edit_view('delete', _entity_config.name) %}
{% set _action = easyadmin_get_action_for_edit_view('delete', _entity_config.name) %}
<button type="button" id="button-delete" class="btn {{ _action.class|default('btn-danger') }}">
{% if _action.icon %}<i class="fa fa-{{ _action.icon }}"></i>{% endif %}
{{ _action.label|default('action.delete')|trans(_trans_parameters) }}
Expand All @@ -76,14 +76,14 @@
{% endif %}

{# for aesthetic reasons, the 'list' action is always displayed as a link instead of a button #}
{% if view == 'new' and easyadmin_action_is_enabled_for_new_view('list', _entity.name) %}
{% set _list_action = easyadmin_get_action_for_new_view('list', _entity.name) %}
{% elseif view == 'edit' and easyadmin_action_is_enabled_for_edit_view('list', _entity.name) %}
{% set _list_action = easyadmin_get_action_for_edit_view('list', _entity.name) %}
{% if view == 'new' and easyadmin_action_is_enabled_for_new_view('list', _entity_config.name) %}
{% set _list_action = easyadmin_get_action_for_new_view('list', _entity_config.name) %}
{% elseif view == 'edit' and easyadmin_action_is_enabled_for_edit_view('list', _entity_config.name) %}
{% set _list_action = easyadmin_get_action_for_edit_view('list', _entity_config.name) %}
{% endif %}

{% if _list_action is defined %}
<a class="btn btn-secondary" href="{{ app.request.query.has('referer') ? app.request.query.get('referer') : path('admin', ({ entity: _entity.name, action: _list_action.name, view: view }) ) }}">{% spaceless %}
<a class="btn btn-secondary" href="{{ app.request.query.has('referer') ? app.request.query.get('referer') : path('admin', ({ entity: _entity_config.name, action: _list_action.name, view: view }) ) }}">{% spaceless %}
{% if _list_action.icon %}<i class="fa fa-{{ _list_action.icon }}"></i>{% endif %}
{{ _list_action.label|default('action.list')|trans(_trans_parameters) }}
{% endspaceless %}</a>
Expand Down

0 comments on commit 49458ea

Please sign in to comment.