VNCOperation   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 41
c 2
b 0
f 0
dl 0
loc 78
ccs 0
cts 65
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderToggleButton() 0 32 2
A renderEnableButton() 0 14 1
A run() 0 7 3
A init() 0 11 5
1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\widgets;
12
13
use Yii;
14
use yii\base\InvalidConfigException;
15
use yii\base\Widget;
16
use yii\helpers\Html;
17
use yii\web\JsExpression;
18
19
class VNCOperation extends Widget
20
{
21
    protected $vncEnd;
22
    protected $vncEnabled;
23
    public $model;
24
25
    public function init()
26
    {
27
        parent::init();
28
        if ($this->model === null) {
29
            throw new InvalidConfigException('Please specify the "model" property.');
30
        }
31
32
        $this->vncEnabled = $this->model->vnc['enabled'];
33
34
        $this->vncEnd = $this->model->statuses['serverEnableVNC'] === null ? 0 : strtotime('+8 hours', strtotime($this->model->statuses['serverEnableVNC']));
35
        $this->vncEnd = $this->vncEnd < time() ? ($this->model->vnc['endTime'] > time() ? $this->model->vnc['endTime'] : $this->vncEnd) : $this->vncEnd;
36
    }
37
38
    public function run()
39
    {
40
        if ($this->model->isVNCSupported()) {
41
            if ($this->vncEnabled === false) {
42
                echo $this->renderEnableButton();
43
            } else {
44
                echo $this->renderToggleButton();
45
            }
46
        }
47
    }
48
49
    protected function renderEnableButton()
50
    {
51
        $form = Html::beginForm(['enable-vnc', 'id' => $this->model->id], 'POST', ['data' => ['pjax' => 1], 'class' => 'inline']);
52
        $form .= Html::submitButton(
53
            Yii::t('hipanel:server', 'Enable'),
54
            [
55
                'class' => 'btn btn-success btn-block',
56
                'data-loading-text' => Yii::t('hipanel:server', 'Enabling...'),
57
                'onClick' => new JsExpression("$(this).closest('form').submit(); $(this).button('loading')"),
58
                'disabled' => !$this->model->canEnableVnc(),
59
            ]
60
        );
61
62
        return $form . Html::endForm();
63
    }
64
65
    protected function renderToggleButton()
66
    {
67
        $data = Html::button(
68
            Yii::t('hipanel', 'Show'),
69
            [
70
                'class' => 'btn btn-success btn-block',
71
                'onClick' => new JsExpression("
72
                    if ($('#vnc-access-data').hasClass('hidden')) {
73
                        $(this).text('" . Yii::t('hipanel', 'Hide') . "');
74
                        $('#vnc-access-data').removeClass('hidden');
75
                    } else {
76
                        $(this).text('" . Yii::t('hipanel', 'Show') . "');
77
                        $('#vnc-access-data').addClass('hidden');
78
                    }
79
                "),
80
            ]
81
        );
82
        $fields = [
83
            Yii::t('hipanel:server', 'IP') => $this->model->vnc['vnc_ip'],
84
            Yii::t('hipanel:server', 'Port') => $this->model->vnc['vnc_port'],
85
            Yii::t('hipanel:server', 'Password') => $this->model->vnc['vnc_password'],
86
        ];
87
        $data .= Html::beginTag('dl', ['class' => 'dl-horizontal hidden', 'id' => 'vnc-access-data']);
88
        foreach ($fields as $name => $value) {
89
            $data .= Html::tag('dt', $name);
90
            $data .= Html::tag('dd', $value);
91
        }
92
        $data .= Html::endTag('dl');
93
        $data .= Yii::t('hipanel:server', 'VNC will be disabled {time}',
94
                ['time' => Yii::$app->formatter->asRelativeTime($this->model->vnc['endTime'])]);
95
96
        return $data;
97
    }
98
}
99