Skip to content

Commit

Permalink
cleanup (after rexstan use)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Schulze committed Apr 24, 2024
1 parent 437a842 commit 2cb0637
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
22 changes: 10 additions & 12 deletions plugins/manager/fragments/yform/manager/history.diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
$table = $this->getVar('table', null);

$sql = rex_sql::factory();
$timestamp = (string) $sql->setQuery(sprintf('SELECT `timestamp` FROM %s WHERE id = %d', rex::getTable('yform_history'), $historyId))->getValue('timestamp');
$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 = %d', rex::getTable('yform_history_field'), $historyId));
$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');


Expand Down Expand Up @@ -60,17 +60,15 @@
// count diffs
if(!$currentDataset->hasValue($field->getName())) {
$change = 'deleted';
} elseif("" . $historyValue != "" . $currentValue) {
} elseif("" . $historyValue !== "" . $currentValue) {
$change = 'changed';
}

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

if (is_callable($class, 'getListValue') && !in_array($field->getTypeName(), ['text', 'textarea'])) {
/** @var $class rex_yform_value_abstract */

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)) {
if(gettype($currentValue) !== gettype($historyValue)) {
settype($historyValue, gettype($currentValue));
}

Expand Down Expand Up @@ -105,7 +103,7 @@
}

// diff values for specific fields
if($change == 'changed') {
if($change === 'changed') {
switch($field->getTypeName()) {
case 'text':
case 'textarea':
Expand All @@ -114,7 +112,7 @@
break;

default:
if($historyValue != $currentValue) {
if($historyValue !== $currentValue) {
$historyValue = '<span class="diff">' . $historyValue . '</span>';
}
break;
Expand Down Expand Up @@ -165,14 +163,14 @@

foreach ($diffs as $change => $diff) {
$content .= '
<header class="panel-heading" ' . ($diff['rows'] != '' ? 'data-toggle="collapse" data-target="#collapse-history-table-' . $change . '"' : '') . '>
<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 ' . ($change == 'changed' ? 'in' : '') . '"' : '') . '>';
<div id="collapse-history-table-' . $change . '" ' . ($diff['rows'] !== '' ? 'class="panel-collapse collapse ' . ($change === 'changed' ? 'in' : '') . '"' : '') . '>';

if($diff['rows'] != '') {
if($diff['rows'] !== '') {
$content .= '
<table class="table history-diff-table" data-change-mode="' . $change . '">
<thead>
Expand Down
46 changes: 21 additions & 25 deletions plugins/manager/lib/yform_history_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class rex_yform_history_helper
{
const FIELD_TYPE_ICONS = [
const array FIELD_TYPE_ICONS = [
'question' => 'question',

'checkbox' => 'square-check',
Expand Down Expand Up @@ -40,7 +40,7 @@ class rex_yform_history_helper
'email' => 'at'
];

const FIELD_TYPE_ICON_WEIGHT_CLASS = 'far';
const string FIELD_TYPE_ICON_WEIGHT_CLASS = 'far';

/**
* detect diffs in 2 strings
Expand All @@ -51,13 +51,13 @@ class rex_yform_history_helper
* @created 17.04.2024
* @copyright https://github.com/paulgb/simplediff | Paul Butler (paulgb)
*/
public static function diffStrings($old, $new):array
public static function diffStrings($old, $new): array
{
$matrix = array();
$maxlen = 0;
$maxlen = $omax = $nmax = 0;

foreach ($old as $oindex => $ovalue) {
$nkeys = array_keys($new, $ovalue);
$nkeys = array_keys($new, $ovalue, true);

foreach ($nkeys as $nindex) {
$matrix[$oindex][$nindex] =
Expand All @@ -74,14 +74,14 @@ public static function diffStrings($old, $new):array
}
}

if ($maxlen == 0) {
if ($maxlen === 0) {
return array(array('d' => $old, 'i' => $new));
}

return array_merge(
self::diffStrings(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
rex_yform_history_helper::diffStrings(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
array_slice($new, $nmax, $maxlen),
self::diffStrings(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen))
rex_yform_history_helper::diffStrings(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen))
);
}

Expand All @@ -94,20 +94,16 @@ public static function diffStrings($old, $new):array
* @created 17.04.2024
* @copyright https://github.com/paulgb/simplediff | Paul Butler (paulgb)
*/
public static function diffStringsToHtml($old, $new)
public static function diffStringsToHtml($old, $new): string
{
$ret = '';
$diff = self::diffStrings(preg_split("/[\s]+/", $old), preg_split("/[\s]+/", $new));
$diff = rex_yform_history_helper::diffStrings(preg_split("/[\s]+/", $old), preg_split("/[\s]+/", $new));

foreach ($diff as $k) {
if (is_array($k)) {
$ret .=
(!empty($k['d']) ? "<del>" . implode(' ', $k['d']) . "</del> " : '').
(!empty($k['i']) ? "<ins>" . implode(' ', $k['i']) . "</ins> " : '')
;
} else {
$ret .= $k . ' ';
}
$ret .=
(isset($k['d']) ? "<del>" . implode(' ', $k['d']) . "</del> " : '').
(isset($k['i']) ? "<ins>" . implode(' ', $k['i']) . "</ins> " : '')
;
}

return $ret;
Expand All @@ -126,18 +122,18 @@ public static function diffStringsToHtml($old, $new)
*/
public static function getFieldTypeIcon(rex_yform_manager_field $field, bool $addPrefix = true, bool $outputHtml = true, bool $addTooltip = true, string $tooltipPlacement = 'top'):string
{
$icon = self::FIELD_TYPE_ICONS[$field->getTypeName()] ?? 'default';
$tag = isset(self::FIELD_TYPE_ICONS[$field->getTypeName()]) ? 'i' : 'span';
$icon = rex_yform_history_helper::FIELD_TYPE_ICONS[$field->getTypeName()] ?? 'default';
$tag = isset(rex_yform_history_helper::FIELD_TYPE_ICONS[$field->getTypeName()]) ? 'i' : 'span';

switch($field->getTypeName()) {
case 'choice':
$expanded = (bool)(int)$field->getElement('expanded');
$multiple = (bool)(int)$field->getElement('multiple');

if($expanded && $multiple) {
$icon = self::FIELD_TYPE_ICONS['choice_checkbox'];
$icon = rex_yform_history_helper::FIELD_TYPE_ICONS['choice_checkbox'];
} elseif($expanded) {
$icon = self::FIELD_TYPE_ICONS['choice_radio'];
$icon = rex_yform_history_helper::FIELD_TYPE_ICONS['choice_radio'];
}
break;
}
Expand All @@ -147,7 +143,7 @@ public static function getFieldTypeIcon(rex_yform_manager_field $field, bool $ad
($addTooltip ? ' data-toggle="tooltip" data-placement="' . $tooltipPlacement . '" title="' . rex_i18n::msg('yform_manager_type_name') . ': ' . $field->getTypeName() . '"' : '') .
' class="' : ''
).
($icon !== 'default' ? self::FIELD_TYPE_ICON_WEIGHT_CLASS . ' ' : '').($addPrefix ? 'rex-icon ' : '') . 'fa-' . $icon .
($icon !== 'default' ? rex_yform_history_helper::FIELD_TYPE_ICON_WEIGHT_CLASS . ' ' : '').($addPrefix ? 'rex-icon ' : '') . 'fa-' . $icon .
($outputHtml ? '"></' . $tag . '>' : '');
}

Expand All @@ -164,8 +160,8 @@ public static function getFieldValue(rex_yform_manager_field $field, rex_yform_m
$class = 'rex_yform_value_' . $field->getTypeName();
$currentValue = ($dataset->hasValue($field->getName()) ? $dataset->getValue($field->getName()) : '-');

if (is_callable($class, 'getListValue') && !in_array($field->getTypeName(), ['text','textarea'])) {
/** @var $class rex_yform_value_abstract */
if (is_callable([$class, 'getListValue']) && !in_array($field->getTypeName(), ['text','textarea'], true)) {
/** @var rex_yform_value_abstract $class */

// get (formatted) value for current entry
if($dataset->hasValue($field->getName())) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/manager/pages/data_history.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
$list->setColumnFormat(
$revision,
'custom',
static function($a) use (&$rev, &$historyDatasets, &$table, &$sql, &$dataset) {
static function($a) use (&$rev, &$historyDatasets, &$table, &$sql) {
// early column ... store all values for current revision
$rev = rex::getProperty('YFORM_HISTORY_REVISION', 0);

Expand Down Expand Up @@ -287,7 +287,7 @@ static function($a) use (&$rev, &$historyDatasets, &$table, &$sql, &$dataset) {
$list->setColumnFormat(
$changesCurrent,
'custom',
static function($a) use (&$dataset, $table, $sql, &$historyDatasets, $actionsCell, $normalCell, $changesCurrent) {
static function($a) use (&$dataset, $table, &$historyDatasets, $actionsCell, $normalCell, $changesCurrent) {
$rev = rex::getProperty('YFORM_HISTORY_REVISION', 0) - 1;

$changes = 0;
Expand Down

0 comments on commit 2cb0637

Please sign in to comment.