1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created for IG Monitoring. |
4
|
|
|
* User: jakim <[email protected]> |
5
|
|
|
* Date: 06.02.2018 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace app\modules\admin\widgets; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use app\components\ArrayHelper; |
12
|
|
|
use app\dictionaries\TrackerType; |
13
|
|
|
use app\models\Tag; |
14
|
|
|
use app\modules\admin\models\MonitoringForm; |
15
|
|
|
use yii\base\Widget; |
16
|
|
|
use yii\helpers\Url; |
17
|
|
|
|
18
|
|
|
class OnOffMonitoringButton extends Widget |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \app\models\Tag|\app\models\Account |
22
|
|
|
*/ |
23
|
|
|
public $model; |
24
|
|
|
public $form; |
25
|
|
|
public $btnCssClass = 'btn btn-block'; |
26
|
|
|
public $stopBtnCssClass = 'btn-danger'; |
27
|
|
|
public $startBtnCssClass = 'btn-success'; |
28
|
|
|
public $stopBtnLabel = '<span class="fa fa-stop"></span> Turn off monitoring'; |
29
|
|
|
public $startBtnLabel = '<span class="fa fa-play"></span> Turn on monitoring'; |
30
|
|
|
|
31
|
|
|
public $offAjaxOptions = []; |
32
|
|
|
|
33
|
|
|
protected $trackerType = TrackerType::ACCOUNT; |
34
|
|
|
|
35
|
|
|
public function init() |
36
|
|
|
{ |
37
|
|
|
parent::init(); |
38
|
|
|
if ($this->model instanceof Tag) { |
39
|
|
|
$this->trackerType = TrackerType::TAG; |
40
|
|
|
} else { |
41
|
|
|
$this->trackerType = TrackerType::ACCOUNT; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function run() |
46
|
|
|
{ |
47
|
|
|
if ($this->model->monitoring) { |
48
|
|
|
return AjaxButton::widget([ |
49
|
|
|
'confirm' => true, |
50
|
|
|
'url' => Url::to(["monitoring/delete-{$this->trackerType}", 'id' => $this->model->id]), |
51
|
|
|
'options' => [ |
52
|
|
|
'class' => "{$this->btnCssClass} {$this->stopBtnCssClass}", |
53
|
|
|
'data' => [ |
54
|
|
|
'style' => 'slide-right', |
55
|
|
|
], |
56
|
|
|
], |
57
|
|
|
'text' => $this->stopBtnLabel, |
58
|
|
|
'ajaxOptions' => $this->offAjaxOptions, |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$form = $this->form ?: new MonitoringForm([ |
63
|
|
|
'scenario' => $this->trackerType, |
64
|
|
|
'names' => $this->getIdent(), |
65
|
|
|
'categories' => $this->trackerType == TrackerType::ACCOUNT ? ArrayHelper::getColumn($this->model->categories, 'name') : null, |
|
|
|
|
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
return CreateMonitoringModal::widget([ |
69
|
|
|
'form' => $form, |
70
|
|
|
'trackerType' => $this->trackerType, |
71
|
|
|
'modalToggleButton' => [ |
72
|
|
|
'class' => "{$this->btnCssClass} {$this->startBtnCssClass}", |
73
|
|
|
'label' => $this->startBtnLabel, |
74
|
|
|
], |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function getIdent(): string |
79
|
|
|
{ |
80
|
|
|
switch ($this->trackerType) { |
81
|
|
|
case TrackerType::ACCOUNT: |
82
|
|
|
$ident = 'username'; |
83
|
|
|
break; |
84
|
|
|
case TrackerType::TAG: |
85
|
|
|
default: |
86
|
|
|
$ident = 'name'; |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return ArrayHelper::getValue($this->model, $ident); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |