SimpleOperation::getIsOperable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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