Skip to content

Commit

Permalink
Add Entity Audit diff to support revision comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels V committed Jul 31, 2014
1 parent ce64615 commit 5633138
Show file tree
Hide file tree
Showing 19 changed files with 469 additions and 10 deletions.
64 changes: 64 additions & 0 deletions Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,11 +685,16 @@ public function historyAction($id = null)
$reader = $manager->getReader($this->admin->getClass());

$revisions = $reader->findRevisions($this->admin->getClass(), $id);
$currentRevision = false;
if(count($revisions) > 0){
$currentRevision = $revisions[0];
}

return $this->render($this->admin->getTemplate('history'), array(
'action' => 'history',
'object' => $object,
'revisions' => $revisions,
'currentRevision' => $currentRevision,
));
}

Expand Down Expand Up @@ -742,6 +747,65 @@ public function historyViewRevisionAction($id = null, $revision = null)
));
}

/**
* Compare history revisions of object
*
* @param int|string|null $id
* @param int|string|null $base_revision
* @param int|string|null $compare_revision
*
* @return Response
*
* @throws AccessDeniedException If access is not granted
* @throws NotFoundHttpException If the object or revision does not exist or the audit reader is not available
*/
public function historyCompareRevisionsAction($id = null, $base_revision = null, $compare_revision = null)
{
if (false === $this->admin->isGranted('EDIT')) {
throw new AccessDeniedException();
}

$id = $this->get('request')->get($this->admin->getIdParameter());

$object = $this->admin->getObject($id);

if (!$object) {
throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
}

$manager = $this->get('sonata.admin.audit.manager');

if (!$manager->hasReader($this->admin->getClass())) {
throw new NotFoundHttpException(sprintf('unable to find the audit reader for class : %s', $this->admin->getClass()));
}

$reader = $manager->getReader($this->admin->getClass());

// retrieve the base revision
$base_object = $reader->find($this->admin->getClass(), $id, $base_revision);
if (!$base_object) {
throw new NotFoundHttpException(sprintf('unable to find the targeted object `%s` from the revision `%s` with classname : `%s`', $id, $base_revision, $this->admin->getClass()));
}

// retrieve the compare revision
$compare_object = $reader->find($this->admin->getClass(), $id, $compare_revision);
if (!$compare_object) {
throw new NotFoundHttpException(sprintf('unable to find the targeted object `%s` from the revision `%s` with classname : `%s`', $id, $compare_revision, $this->admin->getClass()));
}

$this->admin->setSubject($base_object);
$comparison = $reader->diff(get_class($base_object), $id, $base_revision, $compare_revision);

return $this->render($this->admin->getTemplate('show_compare'), array(
'action' => 'show',
'object' => $base_object,
'object_compare' => $compare_object,
'elements' => $this->admin->getShow(),
'comparison' => $comparison
));

}

/**
* Export data to specified format
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ public function fixTemplates(ContainerBuilder $container, Definition $definition
'list' => 'SonataAdminBundle:CRUD:list.html.twig',
'filter' => 'SonataAdminBundle:Form:filter_admin_fields.html.twig',
'show' => 'SonataAdminBundle:CRUD:show.html.twig',
'show_compare' => 'SonataAdminBundle:CRUD:show_compare.html.twig',
'edit' => 'SonataAdminBundle:CRUD:edit.html.twig',
'history' => 'SonataAdminBundle:CRUD:history.html.twig',
'history_revision_timestamp' => 'SonataAdminBundle:CRUD:history_revision_timestamp.html.twig',
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public function getConfigTreeBuilder()
->scalarNode('list')->defaultValue('SonataAdminBundle:CRUD:list.html.twig')->cannotBeEmpty()->end()
->scalarNode('filter')->defaultValue('SonataAdminBundle:Form:filter_admin_fields.html.twig')->cannotBeEmpty()->end()
->scalarNode('show')->defaultValue('SonataAdminBundle:CRUD:show.html.twig')->cannotBeEmpty()->end()
->scalarNode('show_compare')->defaultValue('SonataAdminBundle:CRUD:show_compare.html.twig')->cannotBeEmpty()->end()
->scalarNode('edit')->defaultValue('SonataAdminBundle:CRUD:edit.html.twig')->cannotBeEmpty()->end()
->scalarNode('preview')->defaultValue('SonataAdminBundle:CRUD:preview.html.twig')->cannotBeEmpty()->end()
->scalarNode('history')->defaultValue('SonataAdminBundle:CRUD:history.html.twig')->cannotBeEmpty()->end()
Expand Down
8 changes: 8 additions & 0 deletions Model/AuditReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ public function findRevision($classname, $revision);
* @param string $id
*/
public function findRevisions($className, $id);

/**
* @param string $className
* @param int $id
* @param int $oldRevision
* @param int $newRevision
*/
public function diff($className, $id, $oldRevision, $newRevision);
}
1 change: 1 addition & 0 deletions Resources/doc/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Full Configuration Options
list: SonataAdminBundle:CRUD:list.html.twig
filter: SonataAdminBundle:Form:filter_admin_fields.html.twig
show: SonataAdminBundle:CRUD:show.html.twig
show_compare: SonataAdminBundle:CRUD:show_compare.html.twig
edit: SonataAdminBundle:CRUD:edit.html.twig
preview: SonataAdminBundle:CRUD:preview.html.twig
history: SonataAdminBundle:CRUD:history.html.twig
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/reference/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ You can specify your templates in the config.yml file, like so:
ajax: SonataAdminBundle::ajax_layout.html.twig
list: SonataAdminBundle:CRUD:list.html.twig
show: SonataAdminBundle:CRUD:show.html.twig
show_compare: SonataAdminBundle:CRUD:show_compare.html.twig
edit: SonataAdminBundle:CRUD:edit.html.twig
history: SonataAdminBundle:CRUD:history.html.twig
preview: SonataAdminBundle:CRUD:preview.html.twig
Expand Down
11 changes: 11 additions & 0 deletions Resources/public/css/layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,17 @@ div.sonata-medium-date div {
background-color: #f5f5f5;
}

.sonata-ba-view-container.history-audit-compare th {
width: 10%;
}

.sonata-ba-view-container.history-audit-compare td {
width: 40%;
}
.sonata-ba-view-container.history-audit-compare.diff th {
background: pink;
}

.container-fluid > .sidebar {
top: auto;
}
Expand Down
4 changes: 4 additions & 0 deletions Resources/translations/SonataAdminBundle.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@
<source>label_view_revision</source>
<target>View Revision</target>
</trans-unit>
<trans-unit id="label_compare_revision">
<source>label_compare_revision</source>
<target>Compare revision</target>
</trans-unit>
<trans-unit id="list_results_count">
<source>list_results_count</source>
<target>1 result|%count% results</target>
Expand Down
4 changes: 4 additions & 0 deletions Resources/translations/SonataAdminBundle.nl.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@
<source>label_view_revision</source>
<target>Toon revisie</target>
</trans-unit>
<trans-unit id="label_compare_revision">
<source>label_compare_revision</source>
<target>Vergelijk revisie</target>
</trans-unit>
<trans-unit id="list_results_count">
<source>list_results_count</source>
<target>1 resultaat|%count% resultaten</target>
Expand Down
26 changes: 21 additions & 5 deletions Resources/views/CRUD/base_history.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,23 @@ file that was distributed with this source code.
<th>{{ "td_timestamp"|trans({}, 'SonataAdminBundle') }}</th>
<th>{{ "td_username"|trans({}, 'SonataAdminBundle') }}</th>
<th>{{ "td_action"|trans({}, 'SonataAdminBundle') }}</th>
<th>{{ "td_action"|trans({}, 'SonataAdminBundle') }}</th>
</tr>
</thead>
<tbody>
{% for revision in revisions %}
<tr>
<tr class="{% if (revision.rev == currentRevision.rev) %}current-revision{% endif %}">
<td>{{ revision.rev}}</td>
<td>{% include admin.getTemplate('history_revision_timestamp') %}</td>
<td>{{ revision.username}}</td>
<td><a href="{{ admin.generateObjectUrl('history_view_revision', object, {'revision': revision.rev }) }}" class="revision-link" rel="{{ revision.rev }}">{{ "label_view_revision"|trans({}, 'SonataAdminBundle') }}</a></td>
<td>
{% if (revision.rev == currentRevision.rev) %}
/
{% else %}
<a href="{{ admin.generateObjectUrl('history_compare_revisions', object, {'base_revision': currentRevision.rev, 'compare_revision': revision.rev }) }}" class="revision-compare-link" rel="{{ revision.rev }}">{{ "label_compare_revision"|trans({}, 'SonataAdminBundle') }}</a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
Expand All @@ -49,13 +57,20 @@ file that was distributed with this source code.
<script>
jQuery(document).ready(function() {
jQuery('a.revision-link').bind('click', function(event) {
jQuery('a.revision-link, a.revision-compare-link').bind('click', function(event) {
event.stopPropagation();
event.preventDefault();
action = jQuery(this).hasClass('revision-link')
? 'show'
: 'compare';
jQuery('#revision-detail').html('');
jQuery('table#revisions tbody tr').removeClass('current');
jQuery(this).parent('').removeClass('current');
if(action == 'show'){
jQuery('table#revisions tbody tr').removeClass('current');
jQuery(this).parent('').removeClass('current');
}
jQuery.ajax({
url: jQuery(this).attr('href'),
Expand All @@ -66,7 +81,8 @@ file that was distributed with this source code.
});
return false;
})
});
});
</script>
{% endblock %}
59 changes: 59 additions & 0 deletions Resources/views/CRUD/base_show_compare.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}
{% extends 'SonataAdminBundle:CRUD:base_show.html.twig' %}

{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}

{% block show %}
<div class="sonata-ba-view">

{{ sonata_block_render_event('sonata.admin.show.top', { 'admin': admin, 'object': object }) }}

{% for name, view_group in admin.showgroups %}
<table class="table table-bordered">
{% if name %}
<thead>
<tr class="sonata-ba-view-title">
<th colspan="3">
{{ admin.trans(name) }}
</th>
</tr>
</thead>
{% endif %}

<tbody>
{% for field_name, compare in comparison %}
{% set old_value = (compare.same == '' ? compare.old : compare.same) %}
{% set new_value = (compare.same == '' ? compare.new : compare.same) %}

<tr class="sonata-ba-view-container history-audit-compare{% if(old_value|raw != new_value|raw) %} diff{% endif %}">
{% if elements[field_name] is defined %}
{{ elements[field_name]|render_view_element_compare(old_value, new_value) }}
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}

{{ sonata_block_render_event('sonata.admin.show.bottom', { 'admin': admin, 'object': object }) }}

</div>
{% endblock %}
14 changes: 14 additions & 0 deletions Resources/views/CRUD/base_show_field_compare.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}

<th>{% block name %}{{ admin.trans(field_description.label, {}, field_description.translationDomain) }}{% endblock %}</th>
<td>{% block field %}{% if field_description.options.safe %}{{ value|raw }}{% else %}{{ value|nl2br }}{% endif %}{% endblock %}</td>
<td>{% block field_compare %}{% if field_description.options.safe %}{{ value_compare|raw }}{% else %}{{ value_compare|nl2br }}{% endif %}{% endblock %}</td>
12 changes: 12 additions & 0 deletions Resources/views/CRUD/show_compare.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}

{% extends 'SonataAdminBundle:CRUD:base_show_compare.html.twig' %}
1 change: 1 addition & 0 deletions Route/PathInfoBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function build(AdminInterface $admin, RouteCollection $collection)
if ($this->manager->hasReader($admin->getClass())) {
$collection->add('history', $admin->getRouterIdParameter().'/history');
$collection->add('history_view_revision', $admin->getRouterIdParameter().'/history/{revision}/view');
$collection->add('history_compare_revisions', $admin->getRouterIdParameter().'/history/{base_revision}/{compare_revision}/compare');
}

if ($admin->isAclEnabled()) {
Expand Down
1 change: 1 addition & 0 deletions Route/QueryStringBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function build(AdminInterface $admin, RouteCollection $collection)
if ($this->manager->hasReader($admin->getClass())) {
$collection->add('history', '/audit-history');
$collection->add('history_view_revision', '/audit-history-view');
$collection->add('history_compare_revisions', '/audit-history-compare');
}

if ($admin->isAclEnabled()) {
Expand Down
Loading

0 comments on commit 5633138

Please sign in to comment.