Completed
Push — master ( 52d78f...6f3437 )
by Andrii
04:28
created

RequisiteController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 16
cp 0
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\controllers;
12
13
use hipanel\actions\IndexAction;
14
use hipanel\actions\ViewAction;
15
use hipanel\actions\ComboSearchAction;
16
use hipanel\actions\SmartUpdateAction;
17
use hipanel\actions\PrepareBulkAction;
18
use hipanel\actions\RedirectAction;
19
use hipanel\actions\ProxyAction;
20
use hipanel\filters\EasyAccessControl;
21
use hipanel\actions\ValidateFormAction;
22
use hipanel\base\CrudController;
23
use hipanel\helpers\ArrayHelper;
24
use hipanel\modules\finance\models\Requisite;
25
use yii\base\Event;
26
use yii\filters\VerbFilter;
27
use Yii;
28
29
class RequisiteController extends CrudController
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function behaviors()
35
    {
36
        return ArrayHelper::merge(parent::behaviors(), [
37
            [
38
                'class' => EasyAccessControl::class,
39
                'actions' => [
40
                    'reserve-number' => 'requisites.update',
41
                    'create' => 'requisites.create',
42
                    'copy' => 'requisites.create',
43
                    'update' => 'requisites.update',
44
                    'set-templates' => 'requisites.update',
45
                    'set-serie' => 'requisites.update',
46
                    '*' => 'requisites.read',
47
                ],
48
            ],
49
        ]);
50
    }
51
52
    public function actions()
53
    {
54
        return array_merge(parent::actions(), [
55
            'index' => [
56
                'class' => IndexAction::class,
57
            ],
58
            'search' => [
59
                'class' => ComboSearchAction::class,
60
            ],
61
            'view' => [
62
                'class' => ViewAction::class,
63
                'findOptions' => ['with_counters' => 1],
64
                'on beforePerform' => function ($event) {
65
                    /** @var ViewAction $action */
66
                    $action = $event->sender;
67
68
                    /** @var ContactQuery $query */
69
                    $query = $action->getDataProvider()->query;
70
71
                    if (Yii::getAlias('@document', false)) {
72
                        $query->withDocuments();
73
                    }
74
                    $query->withLocalizations();
75
                },
76
            ],
77
            'reserve-number' => [
78
                'class' => SmartUpdateAction::class,
79
                'success' => Yii::t('hipanel:finance', 'Document number was reserved'),
80
                'view' => 'modal/reserveNumber',
81
                'POST html' => [
82
                    'save' => true,
83
                    'success' => [
84
                        'class' => RedirectAction::class,
85
                        'url' => function () {
86
                            $requisite = Yii::$app->request->post('Requisite');
87
88
                            return ['@requisite/view', 'id' => $requisite['id']];
89
                        },
90
                    ],
91
                ],
92
            ],
93
            'set-templates' => [
94
                'class' => SmartUpdateAction::class,
95
                'success' => Yii::t('hipanel:finance', 'Templates changed'),
96
                'POST html' => [
97
                    'save' => true,
98
                    'success' => [
99
                        'class' => RedirectAction::class,
100
                        'url' => 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...
101
                            return Yii::$app->request->referrer;
102
                        },
103
                    ],
104
                ],
105
            ],
106
            'bulk-set-templates' => [
107
                'class' => SmartUpdateAction::class,
108
                'scenario' => 'set-templates',
109
                'view' => 'modal/_bulkSetTemplates',
110
                'success' => Yii::t('hipanel:finance', 'Templates changed'),
111
                'POST pjax' => [
112
                    'save' => true,
113
                    'success' => [
114
                        'class' => ProxyAction::class,
115
                        'action' => 'index',
116
                    ],
117
                ],
118
                'collectionLoader' => function ($action) {
119
                    /** @var SmartPerformAction $action */
120
                    $data = Yii::$app->request->post($action->collection->getModel()->formName());
121
                    $attributes = [];
122
                    foreach (['invoice_id', 'acceptance_id', 'contract_id', 'probation_id', 'nda_id'] as $attribute) {
123
                        $attributes[$attribute] = $data[$attribute];
124
                        unset($data[$attribute]);
125
                    }
126
127
                    foreach ($data as &$item) {
128
                        $item = array_merge($item, $attributes);
129
                    }
130
131
                    $action->collection->load($data);
132
                },
133
                'on beforeFetch' => function (Event $event) {
134
                    /** @var \hipanel\actions\SearchAction $action */
135
                    $action = $event->sender;
136
                    $dataProvider = $action->getDataProvider();
137
                    $dataProvider->query
138
                        ->select(['*'])
139
                        ->addSelect(['templates'])
140
                        ->andWhere(['show_nonrequisite' => 1]);
141
                },
142
            ],
143
            'set-templates-modal' => [
144
                'class' => PrepareBulkAction::class,
145
                'view' => 'modal/_bulkSetTemplates',
146 View Code Duplication
                'on beforePerform' => function (Event $event) {
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...
147
                    /** @var \hipanel\actions\SearchAction $action */
148
                    $action = $event->sender;
149
                    $dataProvider = $action->getDataProvider();
150
                    $dataProvider->query
151
                        ->select(['*'])
152
                        ->addSelect(['templates']);
153
                },
154
            ],
155
            'set-serie' => [
156
                'class' => SmartUpdateAction::class,
157
                'success' => Yii::t('hipanel:finance', 'Serie changed'),
158
                'error' => Yii::t('hipanel:finance', 'Failed to change requisite serie'),
159
            ],
160
            'bulk-set-serie' => [
161
                'class' => SmartUpdateAction::class,
162
                'scenario' => 'set-serie',
163
                'view' => 'modal/_bulkSetSerie',
164
                'success' => Yii::t('hipanel:finance', 'Series changed'),
165
                'collectionLoader' => function ($action) {
166
                    /** @var SmartPerformAction $action */
167
                    $data = Yii::$app->request->post($action->collection->getModel()->formName());
168
                    $serie = $data['serie'];
169
                    unset($data['serie']);
170
                    foreach ($data as &$item) {
171
                        $item['serie'] = $serie;
172
                    }
173
174
                    $action->collection->load($data);
175
                },
176
                'POST pjax' => [
177
                    'save' => true,
178
                    'success' => [
179
                        'class' => ProxyAction::class,
180
                        'action' => 'index',
181
                    ],
182
                ],
183 View Code Duplication
                'on beforeFetch' => function (Event $event) {
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...
184
                    /** @var \hipanel\actions\SearchAction $action */
185
                    $action = $event->sender;
186
                    $dataProvider = $action->getDataProvider();
187
                    $dataProvider->query
188
                        ->select(['*']);
189
                },
190
            ],
191
            'validate-form' => [
192
                'class' => ValidateFormAction::class,
193
            ],
194
        ]);
195
    }
196
}
197