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\models; |
12
|
|
|
|
13
|
|
|
use hipanel\models\Obj; |
14
|
|
|
use hipanel\models\Ref; |
15
|
|
|
use Yii; |
16
|
|
|
use yii\helpers\ArrayHelper; |
17
|
|
|
|
18
|
|
|
class Backuping extends \hipanel\base\Model |
19
|
|
|
{ |
20
|
|
|
use \hipanel\base\ModelTrait; |
21
|
|
|
|
22
|
|
|
const STATE_DELETED = 'deleted'; |
23
|
|
|
const STATE_DISABLED = 'disabled'; |
24
|
|
|
|
25
|
|
|
/** {@inheritdoc} */ |
26
|
|
|
public function rules() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
[['id', 'service_id', 'server_id', 'account_id', 'client_id', 'seller_id'], 'integer'], |
30
|
|
|
[['client', 'seller'], 'safe'], |
31
|
|
|
[['day', 'hour', 'path', 'include', 'exclude'], 'safe'], |
32
|
|
|
[['method', 'method_label', 'server', 'account', 'name', 'object', 'service'], 'safe'], |
33
|
|
|
[['backup_last'], 'date'], |
34
|
|
|
[['backup_count', 'total_du', 'total_du_gb'], 'integer'], |
35
|
|
|
[['type', 'type_label', 'state', 'state_label'], 'safe'], |
36
|
|
|
|
37
|
|
|
[['id', 'type'], 'safe', 'on' => ['update']], |
38
|
|
|
[['id'], 'integer', 'on' => ['delete', 'disable', 'enable', 'undelete']], |
39
|
|
|
|
40
|
|
|
// Update settings |
41
|
|
|
[['id', 'service_id'], 'integer', 'on' => 'update'], |
42
|
|
|
[['skip_lock'], 'boolean', 'on' => 'update'], |
43
|
|
|
[['day', 'hour'], 'integer', 'on' => 'update'], |
44
|
|
|
[['day', 'hour'], 'default', 'value' => 0, 'on' => 'update'], |
45
|
|
|
[['path', 'include', 'exclude', 'type', 'method'], 'string', 'on' => 'update'], |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** {@inheritdoc} */ |
50
|
|
|
public function attributeLabels() |
51
|
|
|
{ |
52
|
|
|
return $this->mergeAttributeLabels([ |
53
|
|
|
'day' => Yii::t('hipanel', 'Date'), |
54
|
|
|
'hour' => Yii::t('hipanel', 'Time'), |
55
|
|
|
'backup_last' => Yii::t('hipanel:hosting', 'Last backup'), |
56
|
|
|
'backup_count' => Yii::t('hipanel', 'Count'), |
57
|
|
|
'total_du' => Yii::t('hipanel:hosting', 'Disk usage'), |
58
|
|
|
'total_du_gb' => Yii::t('hipanel:hosting', 'Disk'), |
59
|
|
|
'method_label' => Yii::t('hipanel:hosting', 'Method'), |
60
|
|
|
'method_id' => Yii::t('hipanel:hosting', 'Method'), |
61
|
|
|
'account_id' => Yii::t('hipanel', 'Account'), |
62
|
|
|
'server_id' => Yii::t('hipanel', 'Server'), |
63
|
|
|
'state_label' => Yii::t('hipanel', 'State'), |
64
|
|
|
'state' => Yii::t('hipanel', 'State'), |
65
|
|
|
'type' => Yii::t('hipanel:hosting', 'Periodicity'), |
66
|
|
|
'object' => Yii::t('hipanel:hosting', 'Object'), |
67
|
|
|
'skip_lock' => Yii::t('hipanel:hosting', 'Skip Lock'), |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function getObj() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
return Yii::createObject([ |
74
|
|
|
'class' => Obj::class, |
75
|
|
|
'id' => $this->id, |
|
|
|
|
76
|
|
|
'name' => $this->name, |
|
|
|
|
77
|
|
|
'class_name' => $this->object, |
|
|
|
|
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function canBeDeleted() : bool |
82
|
|
|
{ |
83
|
|
|
return $this->can('account.delete') && !$this->isDeleted(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function canBeDisabled() : bool |
87
|
|
|
{ |
88
|
|
|
return $this->can('account.update') && $this->isEnabled(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function canBeRestored() : bool |
92
|
|
|
{ |
93
|
|
|
return $this->can('account.update') && $this->isDeleted(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function canBeEnabled() : bool |
97
|
|
|
{ |
98
|
|
|
return $this->can('account.update') && !$this->isEnabled() && !$this->isDeleted(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function isDeleted() : bool |
102
|
|
|
{ |
103
|
|
|
return $this->state === self::STATE_DELETED; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function isDisabled() : bool |
107
|
|
|
{ |
108
|
|
|
return $this->state === self::STATE_DISABLED; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function isEnabled() : bool |
112
|
|
|
{ |
113
|
|
|
return !$this->isDeleted() && !$this->isDisabled(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function getTypeOptions(): array |
117
|
|
|
{ |
118
|
|
|
return Yii::$app->get('cache')->getOrSet([__METHOD__], function () { |
119
|
|
|
return ArrayHelper::map(Ref::find()->where([ |
120
|
|
|
'gtype' => 'type,backuping', 'select' => 'full', |
121
|
|
|
])->all(), 'name', function ($model) { |
122
|
|
|
return Yii::t('hipanel.hosting.backuping.periodicity', $model->name); |
123
|
|
|
}); |
124
|
|
|
}, 86400 * 24); // 24 days |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function can(string $permission) : bool |
128
|
|
|
{ |
129
|
|
|
return Yii::$app->user->can($permission); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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.