<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="/bundles/pimcoreadmin/css/object_versions.css"/>
</head>

<body>

{% set fields = object1.getClass().getFieldDefinitions()  %}

<table class="preview" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <th>{{ 'Name'|trans([], 'admin') }}</th>
        <th>{{ 'Key'|trans([], 'admin') }}</th>
        {% if isImportPreview is defined %}
            {% if isNew  is defined %}
                <th>{{ 'New Object or unable to resolve'|trans([], 'admin') }}</th>
            {% else %}
                <th>{{ 'Before'|trans([], 'admin') }}</th>
                <th>{{ 'After'|trans([], 'admin') }}</th>
            {% endif %}
        {% else %}
            <th>{{ 'Version'|trans([], 'admin') }} 1</th>
            <th>{{ 'Version'|trans([], 'admin') }} 2</th>
        {% endif %}
    </tr>
    <tr class="system">
        <td>{{ 'Date'|trans([], 'admin') }}</td>
        <td>modificationDate</td>
        {% if isImportPreview is not defined or isNew is not defined %}
            <td>{{ object1.getModificationDate()|date('Y-m-d H:i:s') }}</td>
        {% endif %}
        <td>{{ object2.getModificationDate()|date('Y-m-d H:i:s') }}</td>
    </tr>
    <tr class="system">
        <td>{{ 'Path'|trans([], 'admin') }}</td>
        <td>path</td>
        {% if isImportPreview is not defined or isNew is not defined %}
            <td> {{ object1.getRealFullPath() }} </td>
        {% endif %}
        {% set modifiedPathClass = object1.getRealFullPath() is not same as(object2.getRealFullPath()) ? 'modified' : ''  %}
        <td class="{{ modifiedPathClass }}">{{ object2.getRealFullPath() }}</td>
    </tr>
    <tr class="system">
        <td>{{ 'Published'|trans([], 'admin') }}</td>
        <td>published</td>
        {% if isImportPreview is not defined or isNew is not defined %}
            <td> {{ object1.getPublished() }} </td>
        {% endif %}
        {% set modifiedPublishedClass = object1.getPublished() is not same as(object2.getPublished()) ? 'modified' : ''  %}
        <td class="{{ modifiedPublishedClass }}">{{ object2.getPublished() ? 'true' : 'false' }}</td>
    </tr>
    {% if versionNote1 or versionNote2 %}
        <tr class="system">
            <td>{{ 'Version Note'|trans([], 'admin') }}</td>
            <td>&nbsp;</td>
            <td>{{ versionNote1|nl2br }}</td>
            <td>{{ versionNote2|nl2br }}</td>
        </tr>
    {% endif %}
    <tr class="system">
        <td>{{ 'Id'|trans([], 'admin') }}</td>
        <td>id</td>
        {% if isImportPreview is not defined or isNew is not defined %}
            <td> {{ object1.getId() }} </td>
        {% endif %}
        <td>{{ object2.getId() }}</td>
    </tr>


    <tr class="">
        <td colspan="3">&nbsp;</td>
    </tr>

    {% set properties1 = object1.getProperties() %}
    {% set properties2 = object2.getProperties() %}
    {% set allProperties = properties1|merge(properties2) %}

    {% for propertyName, property in allProperties %}
        {% set prop1 = properties1[propertyName] ?? null %}
        {% set prop2 = properties2[propertyName] ?? null %}
        {% set propData1 = prop1 ? prop1.getData() : null %}
        {% set propData2 = prop2 ? prop2.getData() : null %}

        <tr {% if loop.index is odd %}class="odd"{% endif %}>
            <td>property.{{ propertyName }}</td>
            <td>{{ propertyName }}</td>
            {% if isImportPreview is not defined or isNew is not defined %}
                <td>{{ propData1 }}</td>
            {% endif %}
            {% set modifiedPropertyClass = propData1 is not same as(propData2) ? 'modified' : '' %}
            <td class="{{ modifiedPropertyClass }}">{{ propData2 }}</td>
        </tr>
    {% endfor %}

    <tr class="">
        <td colspan="3">&nbsp;</td>
    </tr>

    {% for fieldName, definition in fields %}
        {% if definition is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\Localizedfields') %}
            {% for language in validLanguages %}
                {% for lfd in definition.getFieldDefinitions() %}
                    {% set vlContainer = object1.getValueForFieldName(fieldName) %}
                    {% set keyData1 = vlContainer is not empty ? vlContainer.getLocalizedValue(lfd.getName(), language) : null %}
                    {% set v1 = lfd.getVersionPreview(keyData1) %}

                    {% set v2Container = object2.getValueForFieldName(fieldName) %}
                    {% set keyData2 = v2Container is not empty ? v2Container.getLocalizedValue(lfd.getName(), language) : null %}
                    {% set v2 = v2Container ? lfd.getVersionPreview(keyData2) %}

                    <tr {% if loop.index is odd %}class="odd"{% endif %}>
                    <td>{{ lfd.getTitle()|trans([],'admin') }} ({{ language }})</td>
                    <td>{{ lfd.getName() }}</td>
                        {% if isImportPreview is not defined or isNew is not defined %}
                        <td>{{ v1 | raw }}</td>
                    {% endif %}
                        {% set fieldIsEqual = lfd is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\EqualComparisonInterface') and not lfd.isEqual(keyData1, keyData2) ? 'modified' : '' %}
                        <td class="{{ fieldIsEqual }}">{{ v2 | raw }}</td>
                    </tr>
                {% endfor %}
            {% endfor %}
        {% elseif definition is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\Classificationstore') %}
            {% set storedata1 = object1.getValueForFieldName(fieldName) %}
            {% set storedata2 = object2.getValueForFieldName(fieldName) %}

            {% set existingGroups = [] %}

            {% set activeGroups1 = [] %}
            {% if storedata1 %}
                {% set activeGroups1 = storedata1.getActiveGroups() %}
            {% endif %}

            {% set activeGroups2 = [] %}
            {% if storedata2 %}
                {% set activeGroups2 = storedata2.getActiveGroups() %}
            {% endif %}

            {% set activeGroups = activeGroups1 + activeGroups2 %}

            {% for activeGroupId, enabled in activeGroups %}
                {% set existingGroups = existingGroups + {(activeGroupId): enabled} %}
            {% endfor %}

            {% if existingGroups is not empty %}
                {% set languages = ['default'] %}
                {% if definition.isLocalized() %}
                    {% set languages = validLanguages|merge(languages) %}
                {% endif %}

                {% for activeGroupId, enabled in existingGroups %}
                    {% if activeGroups1[activeGroupId] is defined or activeGroups2[activeGroupId] is defined %}
                        {% set groupDefinition = pimcore_object_classificationstore_group(activeGroupId) %}
                        {% if groupDefinition is not empty %}
                            {% set keyGroupRelations = groupDefinition.getRelations() %}

                            {% for keyGroupRelation in keyGroupRelations %}
                                {% set keyDef = pimcore_object_classificationstore_get_field_definition_from_json(keyGroupRelation.getDefinition(), keyGroupRelation.getType())  %}

                                {% if keyDef is not empty %}
                                    {% for language in languages %}
                                        {% set keyData1 = storedata1 ? storedata1.getLocalizedKeyValue(activeGroupId, keyGroupRelation.getKeyId(), language, true, true) : null %}
                                        {% set preview1 = keyDef.getVersionPreview(keyData1) %}
                                        {% set keyData2 = storedata2 ? storedata2.getLocalizedKeyValue(activeGroupId, keyGroupRelation.getKeyId(), language, true, true) : null %}
                                        {% set preview2 = keyDef.getVersionPreview(keyData2) %}

                                        <tr {% if loop.index is odd %}class="odd"{% endif %}>
                                            <td>{{ definition.getTitle()|trans([],'admin') }}</td>
                                            <td>{{ groupDefinition.getName() }} - {{ keyGroupRelation.getName() }} {{ definition.isLocalized() ? "/ " ~ language : "" }}</td>
                                            {% if isImportPreview is not defined or isNew is not defined %}
                                                <td>{{ preview1 | raw }}</td>
                                            {% endif %}
                                            {% set isActiveInOneVersionOnly = activeGroups1[activeGroupId] is not defined or activeGroups2[activeGroupId] is not defined %}
                                            {% set fieldIsEqual = keyDef is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\EqualComparisonInterface') and not keyDef.isEqual(keyData1, keyData2) or isActiveInOneVersionOnly ? 'modified' : '' %}
                                            <td class="{{ fieldIsEqual }}">{{ preview2 }}</td>
                                        </tr>
                                    {% endfor %}
                                {% endif %}
                            {% endfor %}
                        {% endif %}
                    {% endif %}
                {% endfor %}
            {% endif %}
        {% elseif definition is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\Objectbricks') %}
            {% for asAllowedType in definition.getAllowedTypes() %}
                {% set collectionDef = pimcore_object_brick_definition_key(asAllowedType) %}

                {% for lfd in collectionDef.getFieldDefinitions() %}
                    {% set value = null %}

                    {% set bricks1 = object1.get(fieldName) %}
                    {% set bricks2 = object2.get(fieldName) %}

                    {% if bricks1 is not empty and bricks2 is not empty %}
                        {% if lfd is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\Localizedfields') %}
                            {% for language in validLanguages %}
                                {% for localizedFieldDefinition in lfd.getFieldDefinitions() %}
                                    <tr {% if loop.index is odd %}class="odd"{% endif %}>
                                        <td>{{ localizedFieldDefinition.getTitle()|trans([],'admin') }} ({{ language }})</td>
                                        <td>{{ localizedFieldDefinition.getName() }}</td>

                                        {% set v1,localizedBrickValue1 = null,null %}
                                        {% set v2,localizedBrickValue2 = null,null %}

                                        {% if bricks1 %}
                                            {% set brickGetter = "get" ~ asAllowedType %}
                                            {% set brick1Value = attribute(bricks1,brickGetter) %}

                                            {% if brick1Value %}
                                                {% set localizedBrickValues = brick1Value.getLocalizedFields() %}
                                                {% set localizedBrickValue1 = localizedBrickValues.getLocalizedValue(localizedFieldDefinition.getName(), language) %}
                                                {% if localizedBrickValue1 != false %}
                                                    {% set v1 = localizedFieldDefinition.getVersionPreview(localizedBrickValue1) %}
                                                {% else %}
                                                    {% set localizedBrickValue1 = null %}
                                                {% endif %}
                                            {% endif %}
                                        {% endif %}

                                        {% if bricks2 %}
                                            {% set brickGetter = "get" ~ asAllowedType %}
                                            {% set brick2Value = attribute(bricks2,brickGetter) %}

                                            {% if brick2Value %}
                                                {% set localizedBrickValues = brick2Value.getLocalizedFields() %}
                                                {% set localizedBrickValue2 = localizedBrickValues.getLocalizedValue(localizedFieldDefinition.getName(), language) %}
                                                {% if localizedBrickValue2 != false %}
                                                    {% set v2 = localizedFieldDefinition.getVersionPreview(localizedBrickValue2) %}
                                                {% else %}
                                                    {% set localizedBrickValue2 = null %}
                                                {% endif %}
                                            {% endif %}
                                        {% endif %}

                                        {% if isImportPreview is not defined or isNew is not defined %}
                                            <td>{{ v1 | raw }}</td>
                                        {% endif %}
                                        {% set fieldIsEqual = localizedFieldDefinition is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\EqualComparisonInterface') and not localizedFieldDefinition.isEqual(localizedBrickValue1, localizedBrickValue2) ? 'modified' : '' %}
                                        <td class="{{ fieldIsEqual }}">{{ v2 }}</td>
                                    </tr>
                                {% endfor %}
                                {% set break = true %}
                            {% endfor %}
                        {% else %}
                            {% set v1 = null %}

                            {% if bricks1 %}
                                {% set brickGetter = "get" ~ asAllowedType %}
                                {% set brick1Value = attribute(bricks1,brickGetter) %}

                                {% if brick1Value %}
                                    {% set brick1Value = brick1Value.getValueForFieldName(lfd.getName()) %}
                                    {% if brick1Value != false %}
                                        {% set v1 = lfd.getVersionPreview(brick1Value) %}
                                    {% else %}
                                        {% set brick1Value = null %}
                                    {% endif %}

                                {% endif %}
                            {% endif %}

                            {% set v2 = null %}

                            {% if bricks2 %}
                                {% set brickGetter = "get" ~ asAllowedType %}
                                {% set brick2Value = attribute(bricks2,brickGetter) %}

                                {% if brick2Value %}
                                    {% set brick2Value = brick2Value.getValueForFieldName(lfd.getName()) ?? null %}
                                    {% if brick2Value != false %}
                                        {% set v2 = lfd.getVersionPreview(brick2Value) %}
                                    {% else %}
                                        {% set brick2Value = null %}
                                    {% endif %}
                                {% endif %}
                            {% endif %}

                            <tr {% if loop.index is odd %}class="odd"{% endif %}>
                                <td>{{ asAllowedType|capitalize ~ " - " ~ lfd.getTitle() ? lfd.getTitle()|trans([],'admin') }}</td>
                                <td>{{ lfd.getName() }}</td>
                                {% if isImportPreview is not defined or isNew is not defined %}
                                    <td>{{ v1 | raw }}</td>
                                {% endif %}
                                {% set fieldIsEqual = lfd is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\EqualComparisonInterface') and not lfd.isEqual(brick1Value,brick2Value) ? 'modified' : '' %}
                                <td class="{{ fieldIsEqual }}">{{ v2 | raw }}</td>
                            </tr>
                        {% endif %}
                    {% endif %}

                {% endfor %}
            {% endfor %}
        {% elseif definition is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\Fieldcollections') %}
            {% set fields1,fields2 = object1.get(fieldName),object2.get(fieldName) %}
            {% set fieldDefinitions1,fieldItems1 = null,null %}
            {% set fieldDefinitions2,fieldItems2 = null,null %}
            {% set skipFieldItems2 = [] %}

            {% if fields1 %}
                {% set fieldDefinitions1 = fields1.getItemDefinitions() %}
                {% set fieldItems1 = fields1.getItems() %}
            {% endif %}

            {% if fields2 %}
                {% set fieldDefinitions2 = fields2.getItemDefinitions() %}
                {% set fieldItems2 = fields2.getItems() %}
            {% endif %}

            {% if fieldItems1 is iterable and fieldItems1|length > 0 %}
                {% for fkey1,fieldItem1  in fieldItems1 %}
                    {% set fieldKeys1 = fieldDefinitions1[fieldItem1.getType()].getFieldDefinitions() %}

                    {% if fieldItems2[fkey1] is defined and fieldItem1.getType() == fieldItems2[fkey1].getType() %}
                        {% set ffkey2 = fieldItems2[fkey1]  %}
                        {% set fieldKeys2 = fieldDefinitions2[ffkey2.getType()].getFieldDefinitions() %}
                        {% set skipFieldItems2 = skipFieldItems2|merge([fkey1, fkey1]) %}
                    {% endif %}

                    {% for fkey,fieldKey1 in fieldKeys1 %}
                        {% set v2,keyData2 = null,null %}
                        {% set keyData1 = fieldItem1.get(fieldKey1.name) %}
                        {% set v1 = fieldKey1.getVersionPreview(keyData1) %}

                        {% if ffkey2 is defined and fieldKeys2[fkey] is defined %}
                            {% set keyData2 = ffkey2.get(fieldKeys2[fkey].name) %}
                            {% set v2 = fieldKey1.getVersionPreview(keyData2) %}
                        {% endif %}

                        {% if keyData2 is not empty and fieldKey1 is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\Localizedfields') %}
                            {# localized loop #}
                            {% for language in validLanguages %}
                                {% for lfd in fieldKey1.getFieldDefinitions() %}
                                    {% set localizedValue1 = fieldItem1.Localizedfields.getLocalizedValue(lfd.getName(), language) ?? '' %}
                                    {% if keyData2.getLocalizedValue(lfd.getName(), language) is not empty %}
                                        {% set localizedValue2 = keyData2.getLocalizedValue(lfd.getName(), language) %}
                                    {% else %}
                                        {% set localizedValue2 = '' %}
                                    {% endif %}
                                    {% set fieldIsEqual = lfd is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\EqualComparisonInterface') and not lfd.isEqual(localizedValue1, localizedValue2) ? 'modified' : '' %}
                                    <tr {% if loop.index is odd %}class="odd"{% endif %}>
                                        <td>{{ fieldItem1.type }} - {{ lfd.getTitle()|trans([],'admin') }} ({{ language }})</td>
                                        <td>{{ fieldItem1.fieldName }} - {{ lfd.getName() }}/{{ language }}</td>
                                        <td>{{ localizedValue1 }}</td>
                                        <td class="{{ fieldIsEqual }}">{{ localizedValue2 }}</td>
                                    </tr>
                                {% endfor %}
                            {% endfor %}
                        {% else %}
                            <tr {% if loop.index is odd %}class="odd"{% endif %}>
                                <td>{{ fieldItem1.getType() ~ " - " ~ fieldKey1.getTitle()|trans([],'admin') }}</td>
                                <td>{{ fieldKey1.name }}</td>
                                {% if isImportPreview is not defined or isNew is not defined %}
                                    <td>{{ v1 | raw }}</td>
                                {% endif %}
                                {% set fieldIsEqual = fieldKey1 is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\EqualComparisonInterface') and not fieldKey1.isEqual(keyData1, keyData2) ? 'modified' : '' %}
                                <td class="{{ fieldIsEqual }}">{{ v2 | raw }}</td>
                            </tr>
                        {% endif %}
                    {% endfor %}
                {% endfor %}
            {% endif %}

            {% if fieldItems2 is iterable and fieldItems2|length > 0 %}

                {% for fkey2,fieldItem2  in fieldItems2 %}
                    {% if fkey2 not in skipFieldItems2|keys %}
                        {% set fieldKeys2 = fieldDefinitions2[fieldItem2.getType()].getFieldDefinitions() %}

                        {% if fieldItems1[fkey2] is defined and fieldItem2.getType() == fieldItems1[fkey2].getType() %}
                            {% set ffkey1 = fieldItems1[fkey2]  %}
                            {% set fieldKeys1 = fieldDefinitions1[ffkey1.getType()].getFieldDefinitions() %}
                        {% endif %}

                        {% for fkey,fieldKey2 in fieldKeys2 %}
                            {% set v1,keyData1 = null,null %}
                            {% set keyData2 = fieldItem2.get(fieldKey2.name) %}
                            {% set v2 = fieldKey2.getVersionPreview(keyData2) %}

                            {% if ffkey1 is defined and fieldKeys1[fkey] is defined %}
                                {% set keyData1 = ffkey1.get(fieldKeys1[fkey].name) %}
                                {% set v1 = fieldKey2.getVersionPreview(keyData1) %}
                            {% endif %}

                            {% if keyData2 is not empty and fieldKey2 is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\Localizedfields') %}
                                {# localized loop #}
                                {% for language in validLanguages %}
                                    {% for lfd in fieldKey2.getFieldDefinitions() %}
                                        {% set localizedValue1 = fieldItem2.Localizedfields.getLocalizedValue(lfd.getName(), language) ?? '' %}
                                        {% if keyData2.getLocalizedValue(lfd.getName(), language) is not empty %}
                                            {% set localizedValue2 = keyData2.getLocalizedValue(lfd.getName(), language) %}
                                        {% else %}
                                            {% set localizedValue2 = '' %}
                                        {% endif %}
                                        {% set fieldIsEqual = lfd is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\EqualComparisonInterface') and not lfd.isEqual(localizedValue1, localizedValue2) ? 'modified' : '' %}
                                        <tr {% if loop.index is odd %}class="odd"{% endif %}>
                                            <td>{{ fieldItem2.type }} - {{ lfd.getTitle()|trans([],'admin') }} ({{ language }})</td>
                                            <td>{{ fieldItem2.fieldName }} - {{ lfd.getName() }}/{{ language }}</td>
                                            <td>{{ localizedValue1 }}</td>
                                            <td class="{{ fieldIsEqual }}">{{ localizedValue2 }}</td>
                                        </tr>
                                    {% endfor %}
                                {% endfor %}
                            {% else %}
                                <tr {% if loop.index is odd %}class="odd"{% endif %}>
                                    <td>{{ fieldItem2.getType() ~ " - " ~ fieldKey2.getTitle()|trans([],'admin') }}</td>
                                    <td>{{ fieldKey2.name }}</td>
                                    {% if isImportPreview is not defined or isNew is not defined %}
                                        <td>{{ v1 | raw }}</td>
                                    {% endif %}
                                    {% set fieldIsEqual = fieldKey2 is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\EqualComparisonInterface') and not fieldKey2.isEqual(keyData1, keyData2) ? 'modified' : '' %}
                                    <td class="{{ fieldIsEqual }}">{{ v2 | raw }}</td>
                                </tr>
                            {% endif %}
                        {% endfor %}
                    {% endif %}
                {% endfor %}
            {% endif %}
        {% else %}
            {% set keyData1 = object1.getValueForFieldName(fieldName) is not same as(false) ? object1.getValueForFieldName(fieldName) : null %}
            {% set v1 = definition.getVersionPreview(keyData1) %}
            {% set keyData2 = object2.getValueForFieldName(fieldName) is not same as(false) ? object2.getValueForFieldName(fieldName) : null %}
            {% set v2 = definition.getVersionPreview(keyData2) %}

            <tr {% if loop.index is odd %}class="odd"{% endif %}>
                <td>{{ definition.getTitle()|trans([],'admin') }}</td>
                <td>{{ definition.getName() }}</td>
                {% if isImportPreview is not defined or isNew is not defined %}
                    <td>{{ v1 | raw }}</td>
                {% endif %}
                {% set fieldIsEqual = definition is instanceof('\\Pimcore\\Model\\DataObject\\ClassDefinition\\Data\\EqualComparisonInterface') and not definition.isEqual(keyData1, keyData2) ? 'modified' : '' %}
                <td class="{{ fieldIsEqual }}">{{ v2 | raw }}</td>
            </tr>
        {% endif %}
    {% endfor %}
</table>

</body>
</html>
