Completed
Push — master ( 8e4a64...521cc3 )
by Paweł
04:47
created

OnOffMonitoringButton::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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' => ArrayHelper::getValue($this->model, $this->trackerType == TrackerType::ACCOUNT ? 'username' : 'name'),
65
            'proxy_id' => $this->model->proxy_id,
66
            'proxy_tag_id' => $this->model->proxy_tag_id,
67
68
            'tags' => $this->trackerType == TrackerType::ACCOUNT ? ArrayHelper::getColumn($this->model->tags, 'name') : null,
0 ignored issues
show
Bug Best Practice introduced by
The property tags does not exist on app\models\Tag. Since you implemented __get, consider adding a @property annotation.
Loading history...
69
        ]);
70
71
        return CreateMonitoringModal::widget([
72
            'form' => $form,
73
            'trackerType' => $this->trackerType,
74
            'modalToggleButton' => [
75
                'class' => "{$this->btnCssClass} {$this->startBtnCssClass}",
76
                'label' => $this->startBtnLabel,
77
            ],
78
        ]);
79
    }
80
}