CountDownWidget   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 32
c 0
b 0
f 0
dl 0
loc 103
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 6 1
A init() 0 13 4
A registerResource() 0 10 1
A run() 0 6 2
1
<?php
2
3
namespace dominus77\maintenance\widgets\timer;
4
5
use Yii;
6
use yii\base\Widget;
7
use yii\helpers\Html;
8
use yii\helpers\Json;
9
use yii\helpers\ArrayHelper;
10
use dominus77\maintenance\widgets\timer\assets\CountDownAsset;
11
12
/**
13
 * Class CountDownWidget
14
 * @package dominus77\maintenance\widgets\timer
15
 *
16
 * @property array $options
17
 */
18
class CountDownWidget extends Widget
19
{
20
    /**
21
     * Widget on/off
22
     * @var bool
23
     */
24
    public $status = true;
25
26
    /**
27
     * Finish timestamp
28
     * @var integer
29
     */
30
    public $timestamp;
31
32
    /**
33
     * Message complete note
34
     * @var string
35
     */
36
    public $message = '';
37
38
    /**
39
     * Tag options count container
40
     * @var array
41
     */
42
    public $countContainerOptions = [];
43
44
    /**
45
     * Tag options note container
46
     * @var array
47
     */
48
    public $noteContainerOptions = [];
49
50
    /**
51
     * Plugin options
52
     * @see https://tutorialzine.com/2011/12/countdown-jquery
53
     * @var array
54
     */
55
    public $clientOptions = [];
56
57
    /**
58
     * en/ru
59
     * @var string
60
     */
61
    public $locale;
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function init()
67
    {
68
        parent::init();
69
        if (empty($this->timestamp)) {
70
            $this->status = false;
71
        }
72
        $this->timestamp *= 1000;
73
        $this->locale = $this->locale ?: Yii::$app->language;
74
        $this->countContainerOptions = ArrayHelper::merge($this->countContainerOptions, ['id' => 'countdown_' . $this->id]);
75
76
        $cssClass = ArrayHelper::remove($this->noteContainerOptions, 'class');
77
        $cssClass = $cssClass ? 'note ' . $cssClass : 'note';
78
        $this->noteContainerOptions = ArrayHelper::merge($this->noteContainerOptions, ['id' => 'note_' . $this->id, 'class' => $cssClass]);
79
    }
80
81
    /**
82
     * @inheritDoc
83
     * @return string|void
84
     */
85
    public function run()
86
    {
87
        if ($this->status === true) {
88
            $this->registerResource();
89
            echo Html::tag('div', '', $this->countContainerOptions) . PHP_EOL;
90
            echo Html::tag('div', '', $this->noteContainerOptions) . PHP_EOL;
91
        }
92
    }
93
94
    /**
95
     * Plugin options
96
     * @see https://tutorialzine.com/2011/12/countdown-jquery
97
     * @return array
98
     */
99
    protected function getOptions()
100
    {
101
        return ArrayHelper::merge($this->clientOptions, [
102
            'id' => $this->id,
103
            'timestamp' => $this->timestamp,
104
            'msg' => $this->message
105
        ]);
106
    }
107
108
    /**
109
     * Register resource
110
     */
111
    protected function registerResource()
112
    {
113
        $view = $this->getView();
114
        CountDownAsset::register($view);
115
        $options = Json::encode($this->getOptions());
116
        $translate = Json::encode(Translate::messages($this->locale));
117
        $script = "            
118
            initCountDownTimer({$options}, {$translate});
119
        ";
120
        $view->registerJs($script);
121
    }
122
}
123