Completed
Push — master ( 34a4d8...837209 )
by Andrii
19s
created

SimpleOperation::buildModalOptions()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
rs 6.7272
cc 7
eloc 25
nc 2
nop 0
1
<?php
2
/**
3
 * Server module for HiPanel.
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-core
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\widgets;
12
13
use hipanel\widgets\ModalButton;
14
use Yii;
15
use yii\base\Widget;
16
use yii\helpers\Html;
17
use yii\helpers\ArrayHelper;
18
19
20
class SimpleOperation extends Widget
21
{
22
    /**
23
     * @var ActiveRecord
24
     */
25
    public $model;
26
27
    /**
28
     * @var string
29
     */
30
    public $scenario;
31
32
    /**
33
     * @var array. Store default options for [[ModalButton]]
34
     */
35
    public $modalOptions = [
36
        'button' => [
37
            'class' => 'btn btn-default btn-block',
38
        ],
39
    ];
40
41
    /**
42
     * @var array|Modal stores options for [[ModalButton]]
43
     * After Modal creating, stores the object.
44
     */
45
    protected $modal = [];
46
47
    /**
48
     * @var string
49
     */
50
    public $buttonLabel;
51
    public $buttonClass;
52
    public $buttonPosition;
53
    public $buttonVisible;
54
    public $body;
55
    public $modalHeaderLabel;
56
    public $modalFooter;
57
    public $modalFooterLabel;
58
    public $modalFooterLoading;
59
    public $modalFooterClass;
60
61
    /**
62
     * @var array
63
     */
64
    public $form;
65
    public $modalHeaderOptions;
66
67
    /**
68
     * @var boolean for ignoring device states
69
     */
70
    public $skipCheckOperable = false;
71
72
    public function init()
73
    {
74
        parent::init();
75
76
        if ($this->model === null) {
77
            throw new InvalidConfigException('Please specify the "model" property.');
78
        }
79
80
        if ($this->scenario === null) {
81
            throw new InvalidConfigException('Please specify the "scenario" property.');
82
        }
83
    }
84
85
    protected function buildModalOptions()
86
    {
87
         $config = ArrayHelper::merge([
88
            'class' => ModalButton::class,
89
            'model' => $this->model,
90
            'scenario' => $this->scenario,
91
            'button' => [
92
                'label' => $this->buttonLabel,
93
                'class' => $this->buttonClass,
94
                'disabled' => (method_exists($this->model, 'isOperable') ? !$this->model->isOperable() : false) && !$this->skipCheckOperable,
95
                'position' => $this->buttonPosition,
96
                'visible' => $this->buttonVisible === null ? true : $this->buttonVisible,
97
            ],
98
            'body' => $this->body,
99
            'form' => $this->form ? : [],
100
            'modal' => [
101
                'header' => Html::tag('h4', $this->modalHeaderLabel),
102
                'headerOptions' =>  $this->modalHeaderOptions,
103
                'footer' => $this->modalFooter === null ? [
104
                    'label' => $this->modalFooterLabel,
105
                    'data-loading-text' => $this->modalFooterLoading,
106
                    'class' => $this->modalFooterClass,
107
                ] : $this->modalFooter,
108
            ],
109
        ], $this->modalOptions);
110
111
        if ($this->buttonClass !== null) {
112
            $config['button']['class'] = $this->buttonClass;
113
        }
114
115
        return $config;
116
    }
117
118
    public function run()
119
    {
120
        echo Yii::createObject($this->buildModalOptions())->run();
121
    }
122
}
123