Completed
Push — master ( ce2343...7c214b )
by Andrii
28:48 queued 13:58
created

AccountController::getTypeData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Hosting Plugin for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-hosting
6
 * @package   hipanel-module-hosting
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
9
 */
10
11
/**
12
 * @see    http://hiqdev.com/hipanel-module-hosting
13
 * @license http://hiqdev.com/hipanel-module-hosting/license
14
 * @copyright Copyright (c) 2015 HiQDev
15
 */
16
17
namespace hipanel\modules\hosting\controllers;
18
19
use hipanel\actions\IndexAction;
20
use hipanel\actions\OrientationAction;
21
use hipanel\actions\RenderJsonAction;
22
use hipanel\actions\PrepareBulkAction;
23
use hipanel\actions\RedirectAction;
24
use hipanel\actions\SearchAction;
25
use hipanel\actions\SmartCreateAction;
26
use hipanel\actions\SmartDeleteAction;
27
use hipanel\actions\SmartUpdateAction;
28
use hipanel\actions\ValidateFormAction;
29
use hipanel\actions\ViewAction;
30
use Yii;
31
use yii\base\Event;
32
33
class AccountController extends \hipanel\base\CrudController
34
{
35
    public function actions()
36
    {
37
        return [
38
            'index' => [
39
                'class' => IndexAction::class,
40
                'data' => function ($action) {
41
                    return [
42
                        'stateData' => $action->controller->getStateData(),
43
                        'typeData' => $action->controller->getTypeData(),
44
                    ];
45
                },
46
                'filterStorageMap' => [
47
                    'login_like' => 'hosting.account.login',
48
                    'server' => 'server.server.name',
49
                    'state' => 'hosting.account.state',
50
                    'type' => 'hosting.account.type',
51
                    'client_id' => 'client.client.id',
52
                    'seller_id' => 'client.client.seller_id',
53
                ],
54
            ],
55
            'view' => [
56
                'class' => ViewAction::class,
57
                'findOptions' => [
58
                    'with_mail_settings' => true,
59
                ],
60
                'data' => function ($action) {
61
                    return [
62
                        'blockReasons' => $action->controller->getBlockReasons(),
63
                    ];
64
                },
65
            ],
66
            'create' => [
67
                'class' => SmartCreateAction::class,
68
                'success' => Yii::t('hipanel:hosting', 'Account creating task has been added to queue'),
69
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to create account'),
70
            ],
71
            'create-ftponly' => [
72
                'class' => SmartCreateAction::class,
73
                'success' => Yii::t('hipanel:hosting', 'Account creating task has been added to queue'),
74
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to create account'),
75
            ],
76
            'change-password' => [
77
                'class' => SmartUpdateAction::class,
78
                'view' => '_changePasswordModal',
79
                'POST' => [
80
                    'save' => true,
81
                    'success' => [
82
                        'class' => RenderJsonAction::class,
83
                        'return' => function ($action) {
84
                            return ['success' => !$action->collection->hasErrors()];
85
                        },
86
                    ],
87
                ],
88
            ],
89
            'set-allowed-ips' => [
90
                'class' => SmartUpdateAction::class,
91
                'view' => '_ipRestrictionsModal',
92
                'success' => Yii::t('hipanel:hosting', 'Allowed IPs changing task has been successfully added to queue'),
93
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to change allowed IPs'),
94
            ],
95
            'set-mail-settings' => [
96
                'class' => SmartUpdateAction::class,
97
                'view' => '_setMailSettings',
98
                'success' => Yii::t('hipanel:hosting', 'Mail settings where changed'),
99
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to change mail settings'),
100
            ],
101
            'enable-block' => [
102
                'class' => SmartUpdateAction::class,
103
                'success' => Yii::t('hipanel:hosting', 'Account was blocked successfully'),
104
                'error' => Yii::t('hipanel:hosting', 'Error during the account blocking'),
105
            ],
106
            'disable-block' => [
107
                'class' => SmartUpdateAction::class,
108
                'success' => Yii::t('hipanel:hosting', 'Account was unblocked successfully'),
109
                'error' => Yii::t('hipanel:hosting', 'Error during the account unblocking'),
110
            ],
111
            'validate-form' => [
112
                'class' => ValidateFormAction::class,
113
            ],
114
            'single-validate-form' => [
115
                'class' => ValidateFormAction::class,
116
                'validatedInputId' => false,
117
            ],
118
            'delete' => [
119
                'class' => SmartDeleteAction::class,
120
                'success' => Yii::t('hipanel:hosting', 'Account deleting task has been added to queue'),
121
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to delete account'),
122
            ],
123
            'bulk-delete' => [
124
                'class' => SmartDeleteAction::class,
125
                'success' => Yii::t('hipanel:hosting', 'Account deleting task has been added to queue'),
126
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to delete account'),
127
            ],
128
            'bulk-delete-modal' => [
129
                'class' => PrepareBulkAction::class,
130
                'view' => '_bulkDelete',
131
            ],
132
            'get-directories-list' => [
133
                'class' => SearchAction::class,
134
                'findOptions' => ['with_directories' => true],
135
                'ajaxResponseFormatter' => function ($action) {
136
                    $results = [];
137
138
                    $model = $action->collection->first;
139
                    $pathLike = Yii::$app->request->post('path_like');
140
141
                    foreach ($model['path'] as $path) {
142
                        if ($pathLike) {
143
                            if (preg_match('|' . $pathLike . '|', $path)) {
144
                                array_unshift($results, ['id' => $path, 'text' => $path]);
145
                                continue;
146
                            }
147
                        }
148
149
                        $results[] = ['id' => $path, 'text' => $path];
150
                    }
151
152
                    return $results;
153
                },
154
            ],
155
            'bulk-enable-block' => [
156
                'class' => SmartUpdateAction::class,
157
                'scenario' => 'enable-block',
158
                'success' => Yii::t('hipanel:hosting', 'Hosting accounts were blocked successfully'),
159
                'error' => Yii::t('hipanel:hosting', 'Error during the hosting accounts blocking'),
160
                'POST html' => [
161
                    'save'    => true,
162
                    'success' => [
163
                        'class' => RedirectAction::class,
164
                    ],
165
                ],
166
                'on beforeSave' => function (Event $event) {
167
                    /** @var \hipanel\actions\Action $action */
168
                    $action = $event->sender;
169
                    $type = Yii::$app->request->post('type');
170
                    $comment = Yii::$app->request->post('comment');
171
                    if (!empty($type)) {
172
                        foreach ($action->collection->models as $model) {
173
                            $model->setAttributes([
174
                                'type' => $type,
175
                                'comment' => $comment,
176
                            ]);
177
                        }
178
                    }
179
                },
180
            ],
181
            'bulk-enable-block-modal' => [
182
                'class' => PrepareBulkAction::class,
183
                'view' => '_bulkEnableBlock',
184
                'data' => function ($action, $data) {
185
                    return array_merge($data, [
186
                        'blockReasons' => $this->getBlockReasons(),
187
                    ]);
188
                },
189
            ],
190
            'bulk-disable-block' => [
191
                'class' => SmartUpdateAction::class,
192
                'scenario' => 'disable-block',
193
                'success' => Yii::t('hipanel:hosting', 'Hosting accounts were unblocked successfully'),
194
                'error' => Yii::t('hipanel:hosting', 'Error during the hosting accounts unblocking'),
195
                'POST html' => [
196
                    'save'    => true,
197
                    'success' => [
198
                        'class' => RedirectAction::class,
199
                    ],
200
                ],
201 View Code Duplication
                'on beforeSave' => 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...
202
                    /** @var \hipanel\actions\Action $action */
203
                    $action = $event->sender;
204
                    $type = Yii::$app->request->post('type');
205
                    $comment = Yii::$app->request->post('comment');
206
                    if (!empty($type)) {
207
                        foreach ($action->collection->models as $model) {
208
                            $model->setAttribute('comment', $comment);
209
                        }
210
                    }
211
                },
212
            ],
213
            'bulk-disable-block-modal' => [
214
                'class' => PrepareBulkAction::class,
215
                'scenario' => 'disable-block',
216
                'view' => '_bulkDisableBlock',
217
                'data' => function ($action, $data) {
218
                    return array_merge($data, [
219
                        'blockReasons' => $this->getBlockReasons(),
220
                    ]);
221
                },
222
            ],
223
        ];
224
    }
225
226
    public function getStateData()
227
    {
228
        return $this->getRefs('state,account', 'hipanel:hosting');
229
    }
230
231
    public function getTypeData()
232
    {
233
        return $this->getRefs('type,account', 'hipanel:hosting');
234
    }
235
}
236