Kaynağa Gözat

Change the compare action to re-use the existing show action for comparison

NLZ13 10 yıl önce
ebeveyn
işleme
91fe1166fa

+ 1 - 3
Controller/CRUDController.php

@@ -794,14 +794,12 @@ class CRUDController extends Controller
         }
         }
 
 
         $this->admin->setSubject($base_object);
         $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(
         return $this->render($this->admin->getTemplate('show_compare'), array(
             'action'            => 'show',
             'action'            => 'show',
             'object'            => $base_object,
             'object'            => $base_object,
             'object_compare'    => $compare_object,
             'object_compare'    => $compare_object,
-            'elements'          => $this->admin->getShow(),
-            'comparison'        => $comparison
+            'elements'          => $this->admin->getShow()
         ));
         ));
     }
     }
 
 

+ 14 - 10
Resources/views/CRUD/base_show.html.twig

@@ -29,21 +29,25 @@ file that was distributed with this source code.
             <table class="table table-bordered">
             <table class="table table-bordered">
                 {% if name %}
                 {% if name %}
                     <thead>
                     <thead>
-                        <tr class="sonata-ba-view-title">
-                            <th colspan="2">
-                                {{ admin.trans(name) }}
-                            </th>
-                        </tr>
+                        {% block show_title %}
+                            <tr class="sonata-ba-view-title">
+                                <th colspan="2">
+                                    {{ admin.trans(name) }}
+                                </th>
+                            </tr>
+                        {% endblock %}
                     </thead>
                     </thead>
                 {% endif %}
                 {% endif %}
 
 
                 <tbody>
                 <tbody>
                     {% for field_name in view_group.fields %}
                     {% for field_name in view_group.fields %}
-                        <tr class="sonata-ba-view-container">
-                            {% if elements[field_name] is defined %}
-                                {{ elements[field_name]|render_view_element(object) }}
-                            {% endif %}
-                        </tr>
+                        {% block show_field %}
+                            <tr class="sonata-ba-view-container">
+                                {% if elements[field_name] is defined %}
+                                    {{ elements[field_name]|render_view_element(object) }}
+                                {% endif %}
+                            </tr>
+                        {% endblock %}
                     {% endfor %}
                     {% endfor %}
                 </tbody>
                 </tbody>
             </table>
             </table>

+ 13 - 34
Resources/views/CRUD/base_show_compare.html.twig

@@ -11,39 +11,18 @@ file that was distributed with this source code.
 
 
 {% extends 'SonataAdminBundle:CRUD:base_show.html.twig' %}
 {% extends 'SonataAdminBundle:CRUD:base_show.html.twig' %}
 
 
-{% 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 }) }}
+{% block show_title %}
+    <tr class="sonata-ba-view-title">
+        <th colspan="3">
+            {{ admin.trans(name) }}
+        </th>
+    </tr>
+{% endblock %}
 
 
-    </div>
+{% block show_field %}
+    <tr class="sonata-ba-view-container history-audit-compare">
+        {% if elements[field_name] is defined %}
+            {{ elements[field_name]|render_view_element_compare(object, object_compare) }}
+        {% endif %}
+    </tr>
 {% endblock %}
 {% endblock %}

+ 10 - 1
Resources/views/CRUD/base_show_field.html.twig

@@ -9,5 +9,14 @@ file that was distributed with this source code.
 
 
 #}
 #}
 
 
-<th>{% block name %}{{ admin.trans(field_description.label, {}, field_description.translationDomain) }}{% endblock %}</th>
+<th{% if(is_diff|default(false)) %} class="diff"{% endif %}>{% 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 %}{% if field_description.options.safe %}{{ value|raw }}{% else %}{{ value|nl2br }}{% endif %}{% endblock %}</td>
+
+{% block field_compare %}
+    {% if(value_compare is defined) %}
+        <td>
+            {% set value = value_compare %}
+            {{ block('field') }}
+        </td>
+    {% endif %}
+{% endblock %}

+ 0 - 14
Resources/views/CRUD/base_show_field_compare.html.twig

@@ -1,14 +0,0 @@
-{#
-
-This file is part of the Sonata package.
-
-(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
-
-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>

+ 34 - 7
Twig/Extension/SonataAdminExtension.php

@@ -200,20 +200,47 @@ class SonataAdminExtension extends \Twig_Extension
      * render a compared view element
      * render a compared view element
      *
      *
      * @param FieldDescriptionInterface $fieldDescription
      * @param FieldDescriptionInterface $fieldDescription
-     * @param mixed                     $old_value
-     * @param mixed                     $new_value
+     * @param mixed                     $baseObject
+     * @param mixed                     $compareObject
      *
      *
      * @return string
      * @return string
      */
      */
-    public function renderViewElementCompare(FieldDescriptionInterface $fieldDescription, $old_value, $new_value)
+    public function renderViewElementCompare(FieldDescriptionInterface $fieldDescription, $baseObject, $compareObject)
     {
     {
-        // The selected template for the comparison should not be defined by the $fieldDescription->getTemplate() for comparing text values
-        $template = $this->environment->loadTemplate('SonataAdminBundle:CRUD:base_show_field_compare.html.twig');
+        $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig');
+
+        try {
+            $baseValue = $fieldDescription->getValue($baseObject);
+        } catch (NoValueException $e) {
+            $baseValue = null;
+        }
+
+        try {
+            $compareValue = $fieldDescription->getValue($compareObject);
+        } catch (NoValueException $e) {
+            $compareValue = null;
+        }
+
+        $baseValueOutput = $template->render(array(
+            'admin'             => $fieldDescription->getAdmin(),
+            'field_description' => $fieldDescription,
+            'value'             => $baseValue
+        ));
+
+        $compareValueOutput = $template->render(array(
+            'field_description' => $fieldDescription,
+            'admin'             => $fieldDescription->getAdmin(),
+            'value'             => $compareValue
+        ));
+
+        // Compare the rendered output of both objects by using the (possibly) overridden field block
+        $isDiff = $baseValueOutput !== $compareValueOutput;
 
 
         return $this->output($fieldDescription, $template, array(
         return $this->output($fieldDescription, $template, array(
             'field_description' => $fieldDescription,
             'field_description' => $fieldDescription,
-            'value'             => $old_value,
-            'value_compare'     => $new_value,
+            'value'             => $baseValue,
+            'value_compare'     => $compareValue,
+            'is_diff'           => $isDiff,
             'admin'             => $fieldDescription->getAdmin()
             'admin'             => $fieldDescription->getAdmin()
         ));
         ));
     }
     }