Timeline   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 19
eloc 51
c 6
b 0
f 1
dl 0
loc 130
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
B renderItems() 0 27 7
A run() 0 4 1
A constructIcon() 0 7 1
A constructTime() 0 6 1
A replaceMacros() 0 3 1
A constructHeader() 0 10 2
A getMacros() 0 3 2
A constructBody() 0 3 1
A constructBodyLayout() 0 3 1
A getBody() 0 3 2
1
<?php
2
3
namespace micetm\timeline;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use yii\base\Widget;
8
use yii\helpers\Html;
9
10
class Timeline extends Widget
11
{
12
    public $items = [];
13
14
    public $eventIcons = [];
15
16
    public $startMacros = '{';
17
18
    public $endMacros = '}';
19
20
    /**
21
     * Path to view for rendering $model->body
22
     *
23
     * The widget includes a 'timeline_body' view that displays an array of the following structure
24
     * [
25
     *     'key' => [
26
     *         'old' => 'oldValue',
27
     *         'new' => 'newValue'
28
     *     ]
29
     * ]
30
     *
31
     * @var string | null
32
     */
33
    public $bodyLayout = null;
34
35
    protected $defaultIcons = [
36
        'update' => 'fa fa-pencil bg-orange',
37
        'create' => 'fa fa-check bg-green',
38
        'delete' => 'fa fa-trash bg-red',
39
        'default' => 'fa fa-check'
40
    ];
41
42
    /**
43
     * Renders the widget.
44
     */
45
    public function run()
46
    {
47
        $this->eventIcons = array_merge($this->defaultIcons, $this->eventIcons);
48
        return $this->renderItems();
49
    }
50
51
    /**
52
     * Renders tab items as specified on [[items]].
53
     * @return string the rendering result.
54
     * @throws InvalidConfigException.
55
     */
56
    protected function renderItems()
57
    {
58
        $lis = [];
59
        foreach ($this->items as $model) {
60
            $nodes = [];
61
            if (!isset($date) || $date != Yii::$app->formatter->asDate($model->log_date)) {
62
                $date = Yii::$app->formatter->asDate($model->log_date);
63
                $lis[] = Html::tag('li', Html::tag('span', $date, ["class" => "bg-blue"]), ["class" => "time-label"]);
64
            }
65
66
            $nodes[] = $this->constructIcon($model->action);
67
            $timelineItem = [];
68
            $timelineItem[] = $this->constructTime($model->log_date);
69
70
            if ($model->title) {
71
                $timelineItem[] = $this->constructHeader($model);
72
            }
73
            if ($model->body) {
74
                $timelineItem[] = $this->bodyLayout
75
                    ? $this->constructBodyLayout($model)
76
                    : $this->constructBody($model);
77
            }
78
            $nodes[] = Html::tag('div', implode("\n", $timelineItem), ['class' => 'timeline-item']);
79
            $lis[] = Html::tag('li', implode("\n", $nodes));
80
        }
81
        $lis[] = Html::tag('li', Html::tag('i', '', ["class" => "fa fa-clock-o"]));
82
        return Html::tag('ul', implode("\n", $lis), ['class' => "timeline"]);
83
    }
84
85
86
    protected function constructIcon($action)
87
    {
88
        return Html::tag(
89
            'i',
90
            '',
91
            [
92
                'class' => $this->eventIcons[$action] ?? $this->eventIcons['default']
93
            ]
94
        );
95
    }
96
97
    protected function constructTime($time)
98
    {
99
        $nodes = [];
100
        $nodes[] = Html::tag('i', '', ['class' => 'fa fa-clock-o']);
101
        $nodes[] = Yii::$app->formatter->asTime($time);
102
        return Html::tag('span', implode("\n", $nodes), ['class' => 'time']);
103
    }
104
105
    protected function constructHeader($model)
106
    {
107
        $macros = $this->getMacros($model);
108
        if (isset($macros['admin_name'])) {
109
            $macros['admin_name'] = Html::a(
110
                $macros['admin_name'],
111
                ['/user/view', 'id' => $macros['admin_id'] ?? '']
112
            );
113
        }
114
        return Html::tag('h3', $this->replaceMacros($model->title, $macros), ['class' => 'timeline-header']);
115
    }
116
117
    protected function constructBody($model)
118
    {
119
        return Html::tag('h3', $this->replaceMacros($model->body, $this->getMacros($model)), ['class' => 'timeline-header']);
120
    }
121
122
    protected function constructBodyLayout($model)
123
    {
124
        return $this->render($this->bodyLayout, ['items' => $this->getBody($model)]);
125
    }
126
127
    protected function replaceMacros($template, $replacement)
128
    {
129
        return \Yii::t('timeline', $template, $replacement);
130
    }
131
132
    protected function getMacros($model)
133
    {
134
        return method_exists($model, 'getMacros') ? $model->getMacros() : $model->macros;
135
    }
136
137
    protected function getBody($model)
138
    {
139
        return method_exists($model, 'getBody') ? $model->getBody() : $model->body;
140
    }
141
}
142