|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.writesdown.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2015 WritesDown |
|
5
|
|
|
* @license http://www.writesdown.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace common\models; |
|
9
|
|
|
|
|
10
|
|
|
use common\components\Json; |
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\db\ActiveRecord; |
|
13
|
|
|
use yii\db\Expression; |
|
14
|
|
|
use yii\helpers\ArrayHelper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* This is the model class for table "{{%module}}". |
|
18
|
|
|
* |
|
19
|
|
|
* @property integer $id |
|
20
|
|
|
* @property string $name |
|
21
|
|
|
* @property string $title |
|
22
|
|
|
* @property string $description |
|
23
|
|
|
* @property string $config |
|
24
|
|
|
* @property integer $status |
|
25
|
|
|
* @property string $directory |
|
26
|
|
|
* @property integer $backend_bootstrap |
|
27
|
|
|
* @property integer $frontend_bootstrap |
|
28
|
|
|
* @property string $date |
|
29
|
|
|
* @property string $modified |
|
30
|
|
|
* |
|
31
|
|
|
* @author Agiel K. Saputra <[email protected]> |
|
32
|
|
|
* @since 0.2.0 |
|
33
|
|
|
*/ |
|
34
|
|
|
class Module extends ActiveRecord |
|
35
|
|
|
{ |
|
36
|
|
|
// Constant for module activation. |
|
37
|
|
|
const STATUS_ACTIVE = 1; |
|
38
|
|
|
const STATUS_NOT_ACTIVE = 0; |
|
39
|
|
|
// Constant for module backend bootstrapping . |
|
40
|
|
|
const BACKEND_BOOTSTRAP = 1; |
|
41
|
|
|
const NOT_BACKEND_BOOTSTRAP = 0; |
|
42
|
|
|
// Constant for module backend bootstrapping . |
|
43
|
|
|
const FRONTEND_BOOTSTRAP = 1; |
|
44
|
|
|
const NOT_FRONTEND_BOOTSTRAP = 0; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var \yii\web\UploadedFile |
|
48
|
|
|
*/ |
|
49
|
|
|
public $file; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function tableName() |
|
55
|
|
|
{ |
|
56
|
|
|
return '{{%module}}'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritdoc |
|
61
|
|
|
*/ |
|
62
|
|
|
public function rules() |
|
63
|
|
|
{ |
|
64
|
|
|
return [ |
|
65
|
|
|
[['name', 'title', 'config', 'directory'], 'required'], |
|
66
|
|
|
[['title', 'description', 'config'], 'string'], |
|
67
|
|
|
['name', 'string', 'max' => 64], |
|
68
|
|
|
['directory', 'string', 'max' => 128], |
|
69
|
|
|
[['name', 'directory'], 'unique'], |
|
70
|
|
|
[['date', 'modified'], 'safe'], |
|
71
|
|
|
[['status', 'backend_bootstrap', 'frontend_bootstrap'], 'integer'], |
|
72
|
|
|
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_NOT_ACTIVE]], |
|
73
|
|
|
['status', 'default', 'value' => self::STATUS_NOT_ACTIVE], |
|
74
|
|
|
['backend_bootstrap', 'in', 'range' => [self::BACKEND_BOOTSTRAP, self::NOT_BACKEND_BOOTSTRAP]], |
|
75
|
|
|
['backend_bootstrap', 'default', 'value' => self::NOT_BACKEND_BOOTSTRAP], |
|
76
|
|
|
['frontend_bootstrap', 'in', 'range' => [self::FRONTEND_BOOTSTRAP, self::NOT_BACKEND_BOOTSTRAP]], |
|
77
|
|
|
['frontend_bootstrap', 'default', 'value' => self::NOT_FRONTEND_BOOTSTRAP], |
|
78
|
|
|
['file', 'required', 'on' => 'create'], |
|
79
|
|
|
['file', 'file', 'extensions' => 'zip'], |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @inheritdoc |
|
85
|
|
|
*/ |
|
86
|
|
|
public function attributeLabels() |
|
87
|
|
|
{ |
|
88
|
|
|
return [ |
|
89
|
|
|
'id' => Yii::t('writesdown', 'ID'), |
|
90
|
|
|
'name' => Yii::t('writesdown', 'Name'), |
|
91
|
|
|
'title' => Yii::t('writesdown', 'Title'), |
|
92
|
|
|
'description' => Yii::t('writesdown', 'Description'), |
|
93
|
|
|
'config' => Yii::t('writesdown', 'Config'), |
|
94
|
|
|
'status' => Yii::t('writesdown', 'Active'), |
|
95
|
|
|
'directory' => Yii::t('writesdown', 'Directory'), |
|
96
|
|
|
'frontend_bootstrap' => Yii::t('writesdown', 'Is Frontend Bootstrap'), |
|
97
|
|
|
'backend_bootstrap' => Yii::t('writesdown', 'Is Backend Bootstrap'), |
|
98
|
|
|
'date' => Yii::t('writesdown', 'Installed'), |
|
99
|
|
|
'modified' => Yii::t('writesdown', 'Updated'), |
|
100
|
|
|
'file' => Yii::t('writesdown', 'Module (ZIP)'), |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get module status as array |
|
106
|
|
|
*/ |
|
107
|
|
View Code Duplication |
public function getStatuses() |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
return [ |
|
110
|
|
|
self::STATUS_ACTIVE => Yii::t('writesdown', 'Yes'), |
|
111
|
|
|
self::STATUS_NOT_ACTIVE => Yii::t('writesdown', 'No'), |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get array |
|
117
|
|
|
*/ |
|
118
|
|
View Code Duplication |
public function getBackendBootstraps() |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
return [ |
|
121
|
|
|
self::BACKEND_BOOTSTRAP => Yii::t('writesdown', 'Yes'), |
|
122
|
|
|
self::NOT_BACKEND_BOOTSTRAP => Yii::t('writesdown', 'No'), |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get array |
|
128
|
|
|
*/ |
|
129
|
|
View Code Duplication |
public function getFrontendBootstraps() |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
return [ |
|
132
|
|
|
self::FRONTEND_BOOTSTRAP => Yii::t('writesdown', 'Yes'), |
|
133
|
|
|
self::NOT_FRONTEND_BOOTSTRAP => Yii::t('writesdown', 'No'), |
|
134
|
|
|
]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get active modules. |
|
140
|
|
|
* |
|
141
|
|
|
* @return array|Module[] |
|
142
|
|
|
*/ |
|
143
|
|
|
public static function getActiveModules() |
|
144
|
|
|
{ |
|
145
|
|
|
return static::find()->where(['status' => self::STATUS_ACTIVE])->all(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get config as array. |
|
150
|
|
|
* |
|
151
|
|
|
* @return mixed |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getConfig() |
|
154
|
|
|
{ |
|
155
|
|
|
return Json::decode($this->config); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get module backend config. |
|
160
|
|
|
* |
|
161
|
|
|
* @return array |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getBackendConfig() |
|
164
|
|
|
{ |
|
165
|
|
|
return ArrayHelper::getValue($this->getConfig(), 'backend', []); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Get module frontend config. |
|
170
|
|
|
* |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getFrontendConfig() |
|
174
|
|
|
{ |
|
175
|
|
|
return ArrayHelper::getValue($this->getConfig(), 'frontend', []); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get module param path. |
|
180
|
|
|
* |
|
181
|
|
|
* @return string. |
|
|
|
|
|
|
182
|
|
|
*/ |
|
183
|
|
|
public function getParamPath() |
|
184
|
|
|
{ |
|
185
|
|
|
return Yii::getAlias('@modules/' . $this->directory . '/config/params.php'); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Get module config path. |
|
190
|
|
|
* |
|
191
|
|
|
* @return string. |
|
|
|
|
|
|
192
|
|
|
*/ |
|
193
|
|
|
public function getConfigPath() |
|
194
|
|
|
{ |
|
195
|
|
|
return Yii::getAlias('@modules/' . $this->directory . '/config/config.php'); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @inheritdoc |
|
200
|
|
|
*/ |
|
201
|
|
View Code Duplication |
public function beforeSave($insert) |
|
|
|
|
|
|
202
|
|
|
{ |
|
203
|
|
|
if (parent::beforeSave($insert)) { |
|
204
|
|
|
if ($this->isNewRecord) { |
|
205
|
|
|
$this->date = new Expression('NOW()'); |
|
206
|
|
|
} |
|
207
|
|
|
$this->modified = new Expression('NOW()'); |
|
208
|
|
|
|
|
209
|
|
|
return true; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return false; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
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.