|
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->title, $model->macros); |
|
72
|
|
|
} |
|
73
|
|
|
if ($model->body) { |
|
74
|
|
|
$timelineItem[] = $this->bodyLayout |
|
75
|
|
|
? $this->constructBodyLayout($model->body) |
|
76
|
|
|
: $this->constructBody($model->body, $model->macros); |
|
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($title, $macros) |
|
106
|
|
|
{ |
|
107
|
|
|
if (isset($macros['admin_name'])) { |
|
108
|
|
|
$macros['admin_name'] = Html::a( |
|
109
|
|
|
$macros['admin_name'], |
|
110
|
|
|
['/user/view', 'id' => $macros['admin_id']] |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
return Html::tag('h3', $this->replaceMacros($title, $macros), ['class' => 'timeline-header']); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
protected function constructBody($body, $macros) |
|
117
|
|
|
{ |
|
118
|
|
|
return Html::tag('h3', $this->replaceMacros($body, $macros), ['class' => 'timeline-header']); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
protected function constructBodyLayout($body) |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->render($this->bodyLayout, ['items' => (array)$body]); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
protected function replaceMacros($template, $replacement) |
|
127
|
|
|
{ |
|
128
|
|
|
return \Yii::t('timeline', $template, $replacement); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|