Completed
Push — master ( c1d777...82fec5 )
by Dmitry
14:05 queued 10s
created

src/controllers/BackupingController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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-2017, 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\helpers\ArrayHelper;
20
use hipanel\models\Ref;
21
use hipanel\modules\hosting\models\Backuping;
22
use hipanel\modules\hosting\models\BackupSearch;
23
use hipanel\filters\EasyAccessControl;
24
use Yii;
25
use yii\filters\AccessControl;
26
27
class BackupingController extends \hipanel\base\CrudController
28
{
29 View Code Duplication
    public function behaviors()
0 ignored issues
show
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...
30
    {
31
        return array_merge(parent::behaviors(), [
32
            [
33
                'class' => EasyAccessControl::class,
34
                'actions' => [
35
                    'update' => 'account.update',
36
                    'delete' => 'account.delete',
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
                        'typeOptions' => $action->controller->getTypeOptions(),
52
                    ];
53
                },
54
                'filterStorageMap' => [
55
                    'state' => 'hosting.backuping.state',
56
                    'server' => 'server.server.name',
57
                    'account' => 'hosting.account.login',
58
                    'client_id' => 'client.client.id',
59
                ],
60
            ],
61
            'update' => [
62
                'class' => SmartUpdateAction::class,
63
                'success' => Yii::t('hipanel:hosting', 'Backup settings have been changed'),
64
                'data' => function ($action) {
65
                    return [
66
                        'typeOptions' => $action->controller->getTypeOptions(),
67
                        'methodOptions' => $action->controller->getMethodOptions(),
68
                        'dayOptions' => $action->controller->getDayOptions(),
69
                        'hourOptions' => $action->controller->getHourOptions(),
70
                    ];
71
                },
72
            ],
73
            'disable' => [
74
                'class' => SmartPerformAction::class,
75
            ],
76
            'enable' => [
77
                'class' => SmartPerformAction::class,
78
            ],
79
            'delete' => [
80
                'class' => SmartDeleteAction::class,
81
                'success' => Yii::t('hipanel', 'Deleted'),
82
                'error' => Yii::t('hipanel', 'An error occurred. Try again please.'),
83
            ],
84
            'view' => [
85
                'class' => ViewAction::class,
86
                'findOptions' => ['show_deleted' => true],
87
                'data' => function ($action) {
88
                    $backupSearch = new BackupSearch();
89
                    $backupsDataProvider = $backupSearch->search([$backupSearch->formName() => ['object_id' => $action->getId()]]);
90
                    $backupsDataProvider->setSort(['defaultOrder' => ['time' => SORT_DESC]]);
91
92
                    return [
93
                        'stateOptions' => $action->controller->getStateOptions(),
94
                        'typeOptions' => $action->controller->getTypeOptions(),
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 getTypeOptions()
121
    {
122
        return $this->getRefs('type,backuping', 'hipanel:hosting:backuping:periodicity');
123
    }
124
125
    public function getMethodOptions()
126
    {
127
        return $this->getRefs('type,backup_method', 'hipanel:hosting');
128
    }
129
130
    public function getStateOptions()
131
    {
132
        return [
133
            'ok' => Yii::t('hipanel', 'Ok'),
134
            'disabled' => Yii::t('hipanel', 'Disabled'),
135
            'deleted' => Yii::t('hipanel', 'Deleted'),
136
        ];
137
    }
138
}
139