Completed
Push5b023f...f64ce5
passed — Build
created

AuditTrail::getDiffHtml()   A

↳ Parent: AuditTrail

Complexity

Conditions 3
Paths 4

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 15
Code Lines 9

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace bedezign\yii2\audit\models;
4
5
use bedezign\yii2\audit\components\db\ActiveRecord;
6
use Yii;
7
8
/**
9
 * AuditTrail
10
 *
11
 * @property integer $id
12
 * @property integer $entry_id
13
 * @property integer $user_id
14
 * @property string  $action
15
 * @property string  $model
16
 * @property string  $model_id
17
 * @property string  $field
18
 * @property string  $new_value
19
 * @property string  $old_value
20
 * @property string  $created
21
 *
22
 * @property AuditEntry    $entry
23
 */
24
class AuditTrail extends ActiveRecord
25
{
26
27
    /**
28
     * @inheritdoc
29
     */
30 29
    public static function tableName()
31
    {
32 29
        return '{{%audit_trail}}';
33
    }
34
35
    /**
36
     * @return array customized attribute labels (name=>label)
37
     */
38 4
    public function attributeLabels()
39
    {
40
        return [
41 4
            'id'        => Yii::t('audit', 'ID'),
42 4
            'entry_id'  => Yii::t('audit', 'Entry ID'),
43 4
            'user_id'   => Yii::t('audit', 'User ID'),
44 4
            'action'    => Yii::t('audit', 'Action'),
45 4
            'model'     => Yii::t('audit', 'Type'),
46 4
            'model_id'  => Yii::t('audit', 'ID'),
47 4
            'field'     => Yii::t('audit', 'Field'),
48 4
            'old_value' => Yii::t('audit', 'Old Value'),
49 4
            'new_value' => Yii::t('audit', 'New Value'),
50 4
            'created'   => Yii::t('audit', 'Created'),
51 4
        ];
52
    }
53
54
    /**
55
     * @return \yii\db\ActiveQuery
56
     */
57 1
    public function getEntry()
58
    {
59 1
        return $this->hasOne(AuditEntry::className(), ['id' => 'entry_id']);
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65 4
    public function getDiffHtml()
66
    {
67 4
        $old = explode("\n", $this->old_value);
68 4
        $new = explode("\n", $this->new_value);
69
70 4
        foreach ($old as $i => $line) {
71 4
            $old[$i] = rtrim($line, "\r\n");
72 4
        }
73 4
        foreach ($new as $i => $line) {
74 4
            $new[$i] = rtrim($line, "\r\n");
75 4
        }
76
77 4
        $diff = new \Diff($old, $new);
78 4
        return $diff->render(new \Diff_Renderer_Html_Inline);
79
    }
80
81
    /**
82
     * @return ActiveRecord|bool
83
     */
84
    public function getParent()
85
    {
86
        $parentModel = new $this->model;
87
        $parent = $parentModel::findOne($this->model_id);
88
        return $parent ? $parent : $parentModel;
89
    }
90
    
91
}
92