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

history rework #1502

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
vendor/
.phpunit.result.cache
.php-cs-fixer.cache
/.idea/*
14 changes: 14 additions & 0 deletions plugins/manager/assets/history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

$(document).on('rex:ready', function (event, container) {
container.find('#rex-yform-history-modal').on('shown.bs.modal', function () {
// init tooltips
container.find('[data-toggle="tooltip"]').tooltip({
html: true
});

// history restore confirm dialog
$("#yform-manager-history-restore").on("click", function() {
return confirm($(this).attr('data-confirm-text'));
});
});
});
121 changes: 121 additions & 0 deletions plugins/manager/assets/manager.css
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,124 @@ body.rex-theme-dark .yform-be-relation-wrapper > div {
background: #1a3332
}
}

#rex-yform-history-modal.in {
display: flex !important;
}

#rex-yform-history-modal header.panel-heading:not(:first-of-type) {
margin-top: 3px;
}

#rex-yform-history-modal header.panel-heading .panel-title i {
display: inline-block;
width: 25px;
}

#rex-yform-history-modal header + .panel-collapse {
padding-top: 5px;
}

.history-list tr.current-dataset-row {
background-color: #a0e4c1;
}

.history-list .current-dataset-cell {
text-align: center;
}

.history-list .current-dataset-hint {
color: #5bb585;
font-style: italic;
}

.history-diff-table tbody td {
line-height: 1.5
}

.history-diff-table ins,
.history-diff-table .diff {
padding: 1px 4px;
border-radius: 3px;
}

.history-diff-table del {
color: #d68787;
}

.history-diff-table ins {
background-color: #a6e1a6;
text-decoration: none;
}

.history-diff-table .diff {
background-color: #c6daff;
}

.history-diff-table thead th:first-child {
padding-left: 10px;
}

.history-diff-table tbody th:first-child {
padding-left: 45px;
position: relative;
}

.history-diff-table tbody th:first-child > i,
.history-diff-table tbody th:first-child > span {
display: flex;
background-color: #F5F5F5;
width: 35px;
text-align: left;
position: absolute;
left: 0;
top: 1px;
bottom: 1px;
justify-content: center;
padding-top: 12px;
color: #000;
}

/* special for numbers */
.history-diff-table tbody th:first-child > i.rex-icon.fa-1:before {
content: '123';
}

.history-diff-table tbody th:first-child > span:before {
content: '';
display: block;
width: 23px;
height: 14px;
border: 1px solid #000;
border-radius: 3px;
}

.history-diff-table tbody th:first-child > span:after {
content: 'abc';
font-size: 8px;
position: absolute;
top: 15px;
left: 9px;
font-weight: bold;
}

#collapse-history-table-added .history-diff-table tbody th:first-child {
color: #088808;
}

#collapse-history-table-removed .history-diff-table tbody th:first-child {
color: #e30d0d;
}

#collapse-history-table-added .history-diff-table tbody td em,
#collapse-history-table-removed .history-diff-table tbody td em {
color: #AAA;
}

@media (min-width: 768px) {
#rex-yform-history-modal.in .modal-dialog {
max-width: calc(100% - 40px);
width: auto;
display: inline-block;
}
}
1 change: 1 addition & 0 deletions plugins/manager/boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
rex_view::addJsFile($this->getAssetsUrl('relations.js'));
rex_view::addCssFile($this->getAssetsUrl('manager.css'));
rex_view::addJsFile($this->getAssetsUrl('widget.js'));
rex_view::addJsFile($this->getAssetsUrl('history.js'));

if (!rex::getUser()->isAdmin()) {
$page = $this->getProperty('page');
Expand Down
199 changes: 199 additions & 0 deletions plugins/manager/fragments/yform/manager/history.diff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php

/**
* @var rex_fragment $this
* @psalm-scope-this rex_fragment
*/

$historyId = $this->getVar('history_id', null);
$datasetId = $this->getVar('dataset_id', null);
$currentDataset = $this->getVar('current_dataset', null);

/** @var rex_yform_manager_table|null $table */
$table = $this->getVar('table', null);

$sql = rex_sql::factory();
$timestamp = $sql->setQuery(sprintf('SELECT `timestamp` FROM %s WHERE id = :id', rex::getTable('yform_history')), [':id' => $historyId])->getValue('timestamp');

$data = $sql->getArray(sprintf('SELECT * FROM %s WHERE history_id = :history_id', rex::getTable('yform_history_field')), [':history_id' => $historyId]);
$data = array_column($data, 'value', 'field');

if(null !== $historyId && null !== $table):

$diffs = [
'changed' => ['icon' => 'fas fa-not-equal', 'count' => 0, 'rows' => ''],
'added' => ['icon' => 'fas fa-layer-plus', 'count' => 0, 'rows' => ''],
'removed' => ['icon' => 'far fa-layer-minus', 'count' => 0, 'rows' => ''],
'unchanged' => ['icon' => 'fas fa-equals', 'count' => 0, 'rows' => ''],
// 'prev_changed' => ['icon' => 'fas fa-not-equal', 'count' => 0, 'rows' => ''],
];

$fieldsInDataset = [];

$tableFields = $table->getFields();

foreach ($table->getValueFields() as $field) {
if (!array_key_exists($field->getName(), $data)) {
if($currentDataset->hasValue($field->getName())) {
++$diffs['added']['count'];
$diffs['added']['rows'] .= '
<tr>
<th>' . rex_yform_history_helper::getFieldTypeIcon($field) . ' ' . $field->getLabel() . '</th>
<td>' . rex_yform_history_helper::getFieldValue($field, $currentDataset, $table) . '</td>
<td><em>' . rex_i18n::msg('yform_history_diff_not_yet_existing') . '</em></td>
</tr>';
}

continue;
}

$change = 'unchanged';

$fieldsInDataset[] = $field->getName();
$historyValue = $data[$field->getName()];
$currentValue = ($currentDataset->hasValue($field->getName()) ? $currentDataset->getValue($field->getName()) : '-');

$class = 'rex_yform_value_' . $field->getTypeName();

// count diffs
if(!$currentDataset->hasValue($field->getName())) {
$change = 'removed';
} elseif('' . $historyValue !== '' . $currentValue) {
$change = 'changed';
}

++$diffs[$change]['count'];

if (is_callable([$class, 'getListValue']) && !in_array($field->getTypeName(), ['text', 'textarea'], true)) {
// to ensure correct replacement with list value, ensure datatype by current dataset
if(gettype($currentValue) !== gettype($historyValue)) {
settype($historyValue, gettype($currentValue));
}

// get (formatted) value for history entry
$historyValue = $class::getListValue([
'value' => $historyValue,
'subject' => $historyValue,
'field' => $field->getName(),
'params' => [
'field' => $field->toArray(),
'fields' => $this->table->getFields(),
],
]);

// get (formatted) value for current entry
if($currentDataset->hasValue($field->getName())) {
$currentValue = $class::getListValue([
'value' => $currentValue,
'subject' => $currentValue,
'field' => $field->getName(),
'params' => [
'field' => $field->toArray(),
'fields' => $tableFields,
],
]);
} else {
$currentValue = '-';
}
} else {
$historyValue = rex_escape($historyValue);
$currentValue = rex_escape($currentValue);
}

// diff values for specific fields
if('changed' === $change) {
switch($field->getTypeName()) {
case 'text':
case 'textarea':
$diff = rex_yform_history_helper::diffStringsToHtml($currentValue, $historyValue);
$historyValue = $diff;
break;

default:
if($historyValue !== $currentValue) {
$historyValue = '<span class="diff">' . $historyValue . '</span>';
}
break;
}
}

$diffs[$change]['rows'] .= '
<tr>
<th>' . rex_yform_history_helper::getFieldTypeIcon($field) . ' ' . $field->getLabel() . '</th>
<td>' . $currentValue . '</td>
<td>' . $historyValue . '</td>
</tr>';

// remove field from dataset to collect meanwhile deleted fields
unset($data[$field->getName()]);
}

foreach ($data as $field => $value) {
++$diffs['removed']['count'];
$diffs['removed']['rows'] .= '
<tr>
<th><i data-toggle="tooltip" data-placement="top" title="" class="rex-icon fas fa-question" data-original-title="' .
rex_i18n::msg('yform_manager_type_name') . ': ' . rex_i18n::msg('yform_manager_type_unknown') . '"></i> ' . $field . '</th>
<td><em>' . rex_i18n::msg('yform_history_diff_no_longer_existing') . '</em></td>
<td>' . $value . '</td>
</tr>';
}

// build restore url
$restoreUrl = http_build_query([
'table_name' => $table->getTableName(),
'func' => 'history',
'subfunc' => 'restore',
'data_id' => $datasetId,
'history_id' => $historyId,
]) . http_build_query(rex_csrf_token::factory($this->getVar('csrf_key', ''))->getUrlParams());

$content = '
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">
' . rex_i18n::msg('yform_history_dataset_id') . ': ' . $datasetId . '
<small>[' . date('d.m.Y H:i:s', strtotime($timestamp)) . ']</small>
</h4>
</div>
<div class="modal-body panel-default">
';

foreach ($diffs as $change => $diff) {
$content .= '
<header class="panel-heading" ' . ('' !== $diff['rows'] ? 'data-toggle="collapse" data-target="#collapse-history-table-' . $change . '"' : '') . '>
<div class="panel-title"><i class="rex-icon ' . $diff['icon'] . '"></i> ' . rex_i18n::msg('yform_history_diff_headline_' . $change) .
' [' . ($diff['count'] > 0 ? '<b>' : '') . $diff['count'] . ($diff['count'] > 0 ? '</b>' : '') . ']' . '
</div>
</header>
<div id="collapse-history-table-' . $change . '" ' . ('' !== $diff['rows'] ? 'class="panel-collapse collapse ' . ('changed' === $change ? 'in' : '') . '"' : '') . '>';

if('' !== $diff['rows']) {
$content .= '
<table class="table history-diff-table" data-change-mode="' . $change . '">
<thead>
<tr>
<th class="rex-table-width-7 field-name"><i class="rex-icon-"></i>' . rex_i18n::msg('yform_tablefield') . '</th>
<th class="rex-table-width-10">' . rex_i18n::msg('yform_history_dataset_current') . '</th>
<th class="rex-table-width-10">' . date('d.m.Y H:i:s', strtotime($timestamp)) . '</th>
</tr>
</thead>
<tbody>' . $diff['rows'] . '</tbody>
</table>';
}

$content .= '</div>';
}

$content .= '
</div>
<div class="modal-footer">
<a href="index.php?page=yform/manager/data_edit?' . $restoreUrl . '" class="btn btn-warning" id="yform-manager-history-restore" data-confirm-text="' . rex_i18n::msg('yform_history_restore_confirm') . '">' .
rex_i18n::msg('yform_history_restore_this') .
'</a>
<button type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true">&times;</button>
</div>
';

echo $content;
endif;
Loading
Loading