Completed
Push — master ( 669d4a...cba942 )
by Andrii
05:17 queued 03:45
created

BackupingController::getTypeOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
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-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\hosting\controllers;
12
13
use hipanel\actions\IndexAction;
14
use hipanel\actions\SmartDeleteAction;
15
use hipanel\actions\SmartPerformAction;
16
use hipanel\actions\SmartUpdateAction;
17
use hipanel\actions\ValidateFormAction;
18
use hipanel\actions\ViewAction;
19
use hipanel\filters\EasyAccessControl;
20
use hipanel\helpers\ArrayHelper;
21
use hipanel\models\Ref;
22
use hipanel\modules\hosting\models\Backuping;
23
use hipanel\modules\hosting\models\BackupSearch;
24
use Yii;
25
26
class BackupingController extends \hipanel\base\CrudController
27
{
28 View Code Duplication
    public function behaviors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
29
    {
30
        return array_merge(parent::behaviors(), [
31
            [
32
                'class' => EasyAccessControl::class,
33
                'actions' => [
34
                    'update' => 'account.update',
35
                    'delete' => 'account.delete',
36
                    'undelete' => 'account.update',
37
                    '*' => 'account.read',
38
                ],
39
            ],
40
        ]);
41
    }
42
43
    public function actions()
44
    {
45
        return array_merge(parent::actions(), [
46
            'index' => [
47
                'class' => IndexAction::class,
48
                'data' => function ($action) {
49
                    return [
50
                        'stateOptions' => $action->controller->getStateOptions(),
51
                    ];
52
                },
53
                'filterStorageMap' => [
54
                    'state' => 'hosting.backuping.state',
55
                    'server' => 'server.server.name',
56
                    'account' => 'hosting.account.login',
57
                    'client_id' => 'client.client.id',
58
                ],
59
            ],
60
            'update' => [
61
                'class' => SmartUpdateAction::class,
62
                'success' => Yii::t('hipanel:hosting', 'Backup settings have been changed'),
63
                'data' => function ($action) {
64
                    return [
65
                        'methodOptions' => $action->controller->getMethodOptions(),
66
                        'dayOptions' => $action->controller->getDayOptions(),
67
                        'hourOptions' => $action->controller->getHourOptions(),
68
                    ];
69
                },
70
            ],
71
            'disable' => [
72
                'class' => SmartPerformAction::class,
73
            ],
74
            'undelete' => [
75
                'class' => SmartPerformAction::class,
76
            ],
77
            'enable' => [
78
                'class' => SmartPerformAction::class,
79
            ],
80
            'delete' => [
81
                'class' => SmartDeleteAction::class,
82
                'success' => Yii::t('hipanel', 'Deleted'),
83
                'error' => Yii::t('hipanel', 'An error occurred. Try again please.'),
84
            ],
85
            'view' => [
86
                'class' => ViewAction::class,
87
                'findOptions' => ['show_deleted' => true],
88
                'data' => function ($action) {
89
                    $backupSearch = new BackupSearch();
90
                    $backupsDataProvider = $backupSearch->search([$backupSearch->formName() => ['object_id' => $action->getId()]]);
91
                    $backupsDataProvider->setSort(['defaultOrder' => ['time' => SORT_DESC]]);
92
93
                    return [
94
                        'stateOptions' => $action->controller->getStateOptions(),
95
                        'backupsDataProvider' => $backupsDataProvider,
96
                        'hasBackup' => Backuping::find()->where(['id' => $action->getId()])->exists(),
97
                    ];
98
                },
99
            ],
100
            'validate-form' => [
101
                'class' => ValidateFormAction::class,
102
            ],
103
        ]);
104
    }
105
106
    public function getDayOptions()
107
    {
108
        return ArrayHelper::map(Ref::find()->where(['gtype' => 'type,day', 'select' => 'full'])->all(), 'id', function ($model) {
109
            return Yii::t('hipanel:hosting', $model->label);
110
        });
111
    }
112
113
    public function getHourOptions()
114
    {
115
        return ArrayHelper::map(Ref::find()->where(['gtype' => 'type,hour', 'select' => 'full'])->all(), 'id', function ($model) {
116
            return Yii::t('hipanel:hosting', $model->label);
117
        });
118
    }
119
120
    public function getMethodOptions()
121
    {
122
        return $this->getRefs('type,backup_method', 'hipanel:hosting');
123
    }
124
125
    public function getStateOptions()
126
    {
127
        return [
128
            'ok' => Yii::t('hipanel', 'Ok'),
129
            'disabled' => Yii::t('hipanel', 'Disabled'),
130
            'deleted' => Yii::t('hipanel', 'Deleted'),
131
        ];
132
    }
133
134
    public static function getTypeOptions()
135
    {
136
        return Backuping::getTypeOptions();
137
    }
138
}
139