Completed
Push — master ( 3c8880...e2c218 )
by Dmitry
06:42 queued 02:46
created

RequestState::run()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 2
nop 0
dl 0
loc 28
ccs 0
cts 24
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\widgets;
13
14
use Yii;
15
use yii\base\Widget;
16
use yii\helpers\Html;
17
use yii\helpers\Json;
18
19
class RequestState extends Widget
20
{
21
    public $module;
22
23
    /**
24
     * @var \hiqdev\hiart\ActiveRecord
25
     */
26
    public $model;
27
28
    /**
29
     * @var array additional options to be passed to the JS plugin call.
30
     */
31
    public $clientOptions = [];
32
33
    /**
34
     * @var string default selector of wrapper with state labels. Will be passed to JS plugin call.
35
     */
36
    public $elementSelector = '#content-pjax';
37
38
    public function init()
39
    {
40
        parent::init();
41
        if ($this->model->request_state_label) {
42
            $this->model->request_state_label = Yii::t('app', $this->model->request_state_label);
43
        }
44
45
        if ($this->model->state_label) {
46
            $this->model->state_label = Yii::t('app', $this->model->state_label);
47
        }
48
49
        if (empty($this->module)) {
50
            $this->module = $this->model->formName();
51
        }
52
    }
53
54
    public function run()
55
    {
56
        if ($this->model->request_state) {
57
            $icon = Html::tag('i', '', [
58
                'class' => ($this->model->request_state !== 'error') ? 'fa fa-circle-o-notch fa-spin' : 'fa fa-exclamation-triangle text-danger',
59
            ]);
60
61
            $res = Html::tag('span', $icon . ' ' . $this->model->request_state_label, [
62
                'class' => 'objectState',
63
                'data'  => [
64
                    'id'         => $this->model->id,
65
                    'module'     => $this->module,
66
                    'norm_state' => $this->model->state_label,
67
                    'with_href'  => 0,
68
                ],
69
            ]);
70
        } else {
71
            $res = Html::tag('span', $this->model->state_label);
72
        }
73
74
        RequestStateAsset::register(Yii::$app->getView());
75
76
        $options   = Json::encode(array_merge(['module' => $this->module], $this->clientOptions));
77
        $plugin_id = 'objectStateWatcher-' . $this->module;
78
        Yii::$app->getView()->registerJs("$('{$this->elementSelector}').objectsStateWatcher($options);", \yii\web\View::POS_READY, $plugin_id);
79
80
        return $res;
81
    }
82
}
83