Skip to content

Commit

Permalink
Merge pull request #291 from bedezign/feature/improve-differences-views
Browse files Browse the repository at this point in the history
Feature/improve differences views
  • Loading branch information
eluhr authored Apr 8, 2024
2 parents 64db4b9 + 5fc404f commit 6f97ac9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.2.7
- Move differences in audit trail view in DetailWidget
- New module option `$enablePrettyDiffForTrails` to prettyfied json output

## 1.2.6
- Show diff in trails
- Simplify structure for attributes in audit entry view
Expand Down
5 changes: 5 additions & 0 deletions src/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ class Audit extends Module
/* @see \yii\debug\Module::$tracePathMappings (since 2.1.6) */
public $tracePathMappings = [];

/**
* Controls whether the diff output for trails will be pretty printed if the value is json or not
*/
public $enablePrettyDiffForTrails = false;

/**
* @var array
*/
Expand Down
60 changes: 42 additions & 18 deletions src/models/AuditTrail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace bedezign\yii2\audit\models;

use bedezign\yii2\audit\Audit;
use bedezign\yii2\audit\components\db\ActiveRecord;
use Yii;

Expand All @@ -11,15 +12,15 @@
* @property integer $id
* @property integer $entry_id
* @property integer $user_id
* @property string $action
* @property string $model
* @property string $model_id
* @property string $field
* @property string $new_value
* @property string $old_value
* @property string $created
* @property string $action
* @property string $model
* @property string $model_id
* @property string $field
* @property string $new_value
* @property string $old_value
* @property string $created
*
* @property AuditEntry $entry
* @property AuditEntry $entry
*/
class AuditTrail extends ActiveRecord
{
Expand All @@ -38,16 +39,16 @@ public static function tableName()
public function attributeLabels()
{
return [
'id' => Yii::t('audit', 'ID'),
'entry_id' => Yii::t('audit', 'Entry ID'),
'user_id' => Yii::t('audit', 'User ID'),
'action' => Yii::t('audit', 'Action'),
'model' => Yii::t('audit', 'Type'),
'model_id' => Yii::t('audit', 'Model ID'),
'field' => Yii::t('audit', 'Field'),
'id' => Yii::t('audit', 'ID'),
'entry_id' => Yii::t('audit', 'Entry ID'),
'user_id' => Yii::t('audit', 'User ID'),
'action' => Yii::t('audit', 'Action'),
'model' => Yii::t('audit', 'Type'),
'model_id' => Yii::t('audit', 'Model ID'),
'field' => Yii::t('audit', 'Field'),
'old_value' => Yii::t('audit', 'Old Value'),
'new_value' => Yii::t('audit', 'New Value'),
'created' => Yii::t('audit', 'Created'),
'created' => Yii::t('audit', 'Created'),
];
}

Expand All @@ -59,13 +60,36 @@ public function getEntry()
return $this->hasOne(AuditEntry::className(), ['id' => 'entry_id']);
}

public static function prettyPrintIfValueIsJson(mixed $value): string
{
// Check if data a string. Only if it is string, json decode will work correctly
if (is_string($value)) {
// Decode the value and ensure output is an array so we can check its type consistently
$data = json_decode($value, JSON_OBJECT_AS_ARRAY);
if (is_array($data)) {
// Pretty print json by flag for json_encode and ensure line breaks by using print_r
return print_r(json_encode($data, JSON_PRETTY_PRINT), true);
}
}

return (string)$value;
}

/**
* @return mixed
*/
public function getDiffHtml()
{
$old = explode("\n", (string)$this->old_value);
$new = explode("\n", (string)$this->new_value);
if (Audit::getInstance()->enablePrettyDiffForTrails) {
$oldValue = self::prettyPrintIfValueIsJson((string)$this->old_value);
$newValue = self::prettyPrintIfValueIsJson((string)$this->new_value);
} else {
$oldValue = (string)$this->old_value;
$newValue = (string)$this->new_value;
}

$old = explode("\n", $oldValue);
$new = explode("\n", $newValue);

foreach ($old as $i => $line) {
$old[$i] = rtrim((string)$line, "\r\n");
Expand Down
10 changes: 7 additions & 3 deletions src/views/trail/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
'model_id',
'field',
'created',
[
'label' => Yii::t('audit', 'Difference'),
'value' => function ($model) {
return $model->getDiffHtml();
},
'format' => 'raw',
]
],
]);

echo Html::tag('h2', Yii::t('audit', 'Difference'));
echo $model->getDiffHtml();

0 comments on commit 6f97ac9

Please sign in to comment.