Completed
Push — master ( 91cf72...94b139 )
by Andrii
15:21
created

BillController::behaviors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
ccs 8
cts 8
cp 1
rs 8.8571
cc 1
eloc 18
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * Finance module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-finance
7
 * @package   hipanel-module-finance
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\finance\controllers;
13
14
use hipanel\actions\IndexAction;
15
use hipanel\actions\OrientationAction;
16
use hipanel\actions\SmartCreateAction;
17
use hipanel\actions\SmartPerformAction;
18
use hipanel\actions\SmartUpdateAction;
19
use hipanel\actions\ValidateFormAction;
20
use hipanel\actions\ViewAction;
21
use Yii;
22
use yii\filters\AccessControl;
23
24
class BillController extends \hipanel\base\CrudController
25 1
{
26
    public function behaviors()
27
    {
28
        return array_merge(parent::behaviors(), [
29
            'access-bill' => [
30
                'class' => AccessControl::class,
31
                'only' => ['index', 'view', 'create', 'update', 'delete'],
32
                'rules' => [
33 1
                    [
34
                        'allow' => true,
35 1
                        'roles' => ['manage', 'deposit'],
36
                        'actions' => ['index', 'view'],
37
                    ],
38
                    [
39
                        'allow' => true,
40 1
                        'roles' => ['create-bills'],
41 1
                        'actions' => ['create'],
42
                    ],
43
                    [
44 1
                        'allow' => true,
45
                        'roles' => ['update-bills'],
46
                        'actions' => ['update'],
47 1
                    ],
48
                    [
49 1
                        'allow' => true,
50 1
                        'roles' => ['delete-bills'],
51
                        'actions' => ['delete'],
52
                    ],
53
                ],
54
            ],
55
        ]);
56
    }
57
58
    public function actions()
59
    {
60
        return [
61
            'set-orientation' => [
62
                'class' => OrientationAction::class,
63
                'allowedRoutes' => [
64
                    '@bill/index'
65
                ],
66
            ],
67
            'index' => [
68
                'class'     => IndexAction::class,
69
                'data'      => function ($action) {
70
                    return [
71
                        'type' => $action->controller->getPaymentType(),
72
                    ];
73 1
                },
74 1
            ],
75 1
            'view' => [
76
                'class'     => ViewAction::class,
77 1
            ],
78 1
            'validate-form' => [
79 1
                'class'     => ValidateFormAction::class,
80
            ],
81 1
            'create' => [
82 1
                'class'     => SmartCreateAction::class,
83 1
                'data' => function ($action) {
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84 1
                    $types = $this->getRefs('type,bill', 'hipanel/finance', ['with_hierarchy' => 1, 'orderby' => 'name_asc']);
85
                    $billTypes = [];
86
                    $billGroupLabels = [];
87
88
                    foreach ($types as $key => $title) {
89
                        list($type, $name) = explode(',', $key);
90
91
                        if (!isset($billTypes[$type])) {
92
                            $billTypes[$type] = [];
93
                            $billGroupLabels[$type] = ['label' => $title];
94
                        }
95
96
                        if (isset($name)) {
97
                            foreach ($types as $k => $t) {
98
                                if (strpos($k, $type . ',') === 0) {
99
                                    $billTypes[$type][$k] = $t;
100
                                }
101
                            }
102
                        }
103
                    }
104
105
                    return ['billTypes' => $billTypes, 'billGroupLabels' => $billGroupLabels];
106
                },
107
                'success'   => Yii::t('hipanel/finance', 'Bill was created successfully'),
108
            ],
109
            'update' => [
110
                'class'     => SmartUpdateAction::class,
111
                'success'   => Yii::t('hipanel/finance', 'Bill was updated successfully'),
112
            ],
113
            'delete' => [
114
                'class'     => SmartPerformAction::class,
115
                'success'   => Yii::t('hipanel/finance', 'Bill was deleted successfully'),
116
            ],
117
        ];
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function getPaymentType()
124
    {
125
        return $this->getRefs('type,bill', 'hipanel/finance', Yii::$app->user->can('support') ? ['with_hierarchy' => true] : []);
126
    }
127
}
128