CreatePricesButton::init()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 34

Duplication

Lines 21
Ratio 61.76 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 21
loc 34
ccs 0
cts 33
cp 0
rs 9.376
c 0
b 0
f 0
cc 3
nc 1
nop 0
crap 12
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\widgets;
12
13
use hipanel\modules\finance\models\Plan;
14
use hipanel\widgets\AjaxModal;
15
use Yii;
16
use yii\base\Widget;
17
use yii\bootstrap\Dropdown;
18
use yii\bootstrap\Html;
19
use yii\bootstrap\Modal;
20
use yii\web\View;
21
22
class CreatePricesButton extends Widget
23
{
24
    /**
25
     * @var Plan
26
     */
27
    public $model;
28
29
    public function init()
30
    {
31
        $this->view->on(View::EVENT_END_BODY, function () {
32
            echo AjaxModal::widget([
33
                'id' => 'create-prices-modal',
34
                'header' => Html::tag('h4', Yii::t('hipanel.finance.price', 'Create prices'), ['class' => 'modal-title']),
35
                'scenario' => 'create-prices',
36
                'actionUrl' => ['@plan/suggest-prices-modal', 'id' => $this->model->id],
37
                'size' => Modal::SIZE_SMALL,
38
                'toggleButton' => false,
39
            ]);
40 View Code Duplication
            if ($this->model->supportsSharedPricesCreation()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
                echo AjaxModal::widget([
42
                    'id' => 'create-shared-prices-modal',
43
                    'header' => Html::tag('h4', Yii::t('hipanel.finance.price', 'Create shared prices'),
44
                        ['class' => 'modal-title']),
45
                    'scenario' => 'create-prices',
46
                    'actionUrl' => ['@plan/suggest-shared-prices-modal', 'id' => $this->model->id],
47
                    'size' => Modal::SIZE_SMALL,
48
                    'toggleButton' => false,
49
                ]);
50
            }
51 View Code Duplication
            if ($this->model->is_grouping) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
                echo AjaxModal::widget([
53
                    'id' => 'create-grouping-prices-modal',
54
                    'header' => Html::tag('h4', Yii::t('hipanel.finance.price', 'Create grouping prices'), ['class' => 'modal-title']),
55
                    'scenario' => 'create-prices',
56
                    'actionUrl' => ['@plan/suggest-grouping-prices-modal', 'id' => $this->model->id],
57
                    'size' => Modal::SIZE_SMALL,
58
                    'toggleButton' => false,
59
                ]);
60
            }
61
        });
62
    }
63
64
    public function run()
65
    {
66
        return Html::tag('div', Html::a(Yii::t('hipanel.finance.price', 'Create prices') .
67
                '&nbsp;&nbsp;' .
68
                Html::tag('span', null, ['class' => 'caret']), '#', [
69
                'data-toggle' => 'dropdown', 'class' => 'dropdown-toggle btn btn-success btn-sm',
70
            ]) . Dropdown::widget([
71
                'options' => ['class' => 'pull-right'],
72
                'items' => array_filter([
73
                    $this->model->supportsIndividualPricesCreation() ? [
74
                        'encode' => false,
75
                        'url' => '#',
76
                        'label' => Yii::t('hipanel.finance.price', 'Create prices'),
77
                        'linkOptions' => [
78
                            'data' => [
79
                                'toggle' => 'modal',
80
                                'target' => '#create-prices-modal',
81
                            ],
82
                        ],
83
                    ] : null,
84
                    $this->model->supportsSharedPricesCreation() ? [
85
                        'encode' => false,
86
                        'url' => '#',
87
                        'label' => Yii::t('hipanel.finance.price', 'Create shared prices'),
88
                        'linkOptions' => [
89
                            'data' => [
90
                                'toggle' => 'modal',
91
                                'target' => '#create-shared-prices-modal',
92
                            ],
93
                        ],
94
                    ] : null,
95
                    $this->model->is_grouping ? [
96
                        'encode' => false,
97
                        'url' => '#',
98
                        'label' => Yii::t('hipanel.finance.price', 'Create grouping prices'),
99
                        'linkOptions' => [
100
                            'data' => [
101
                                'toggle' => 'modal',
102
                                'target' => '#create-grouping-prices-modal',
103
                            ],
104
                        ],
105
                    ] : null,
106
                ]),
107
            ]), ['class' => 'dropdown']);
108
    }
109
}
110