|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\core\models; |
|
4
|
|
|
|
|
5
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* This is the model class for table "{{%content_block_group}}". |
|
10
|
|
|
* |
|
11
|
|
|
* @property integer $id |
|
12
|
|
|
* @property integer $parent_id |
|
13
|
|
|
* @property string $name |
|
14
|
|
|
* @property integer $sort_order |
|
15
|
|
|
* @property ContentBlockGroup[] $child |
|
16
|
|
|
* @property ContentBlock[] $contentBlocks |
|
17
|
|
|
*/ |
|
18
|
|
|
class ContentBlockGroup extends \yii\db\ActiveRecord |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
const DEFAULT_PARENT_ID = 1; |
|
22
|
|
|
const DELETE_METHOD_ALL = 1; |
|
23
|
|
|
const DELETE_METHOD_PARENT_ROOT = 2; |
|
24
|
|
|
|
|
25
|
|
|
public $deleteMethod = null; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritdoc |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function tableName() |
|
32
|
|
|
{ |
|
33
|
|
|
return '{{%content_block_group}}'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritdoc |
|
38
|
|
|
*/ |
|
39
|
|
|
public function behaviors() |
|
40
|
|
|
{ |
|
41
|
|
|
return [ |
|
42
|
|
|
[ |
|
43
|
|
|
'class' => ActiveRecordHelper::className(), |
|
44
|
|
|
], |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
*/ |
|
51
|
|
|
public function rules() |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
[['parent_id', 'sort_order', 'deleteMethod'], 'integer'], |
|
55
|
|
|
[['name', 'sort_order', 'parent_id'], 'required'], |
|
56
|
|
|
[['name'], 'string', 'max' => 250], |
|
57
|
|
|
[['sort_order'], 'default', 'value' => 0], |
|
58
|
|
|
[['parent_id'], 'default', 'value' => self::DEFAULT_PARENT_ID], |
|
59
|
|
|
[['deleteMethod'], 'in', 'range' => [self::DELETE_METHOD_ALL, self::DELETE_METHOD_PARENT_ROOT]] |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
|
|
*/ |
|
66
|
|
|
public function attributeLabels() |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
|
|
'id' => Yii::t('app', 'ID'), |
|
70
|
|
|
'parent_id' => Yii::t('app', 'Parent ID'), |
|
71
|
|
|
'name' => Yii::t('app', 'Name'), |
|
72
|
|
|
'sort_order' => Yii::t('app', 'Sort Order'), |
|
73
|
|
|
'deleteMethod' => Yii::t('app', 'Delete Method'), |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return ContentBlockGroup[] |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getChild() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->hasMany(ContentBlockGroup::className(), ['parent_id' => 'id']); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return ContentBlock[] |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getContentBlocks() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->hasMany(ContentBlock::className(), ['group_id' => 'id']); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getNameWithCount() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->name . ' (' . ContentBlockGroup::find()->where(['group_id' => $this->id])->count() . ')'; |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param $id |
|
103
|
|
|
* @return null|static |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function findById($id) |
|
106
|
|
|
{ |
|
107
|
|
|
return self::findOne(['id' => $id]); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
public function beforeDelete() |
|
114
|
|
|
{ |
|
115
|
|
|
if ($this->id == 1) { |
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return parent::beforeDelete(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @throws \Exception |
|
124
|
|
|
*/ |
|
125
|
|
|
public function afterDelete() |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
foreach ($this->child as $model) { |
|
128
|
|
|
$model->delete(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
foreach ($this->contentBlocks as $block) { |
|
132
|
|
|
if ($this->deleteMethod == self::DELETE_METHOD_ALL) { |
|
133
|
|
|
$block->delete(); |
|
134
|
|
|
} else { |
|
135
|
|
|
$block->group_id = self::DEFAULT_PARENT_ID; |
|
136
|
|
|
$block->save(); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
return parent::afterDelete(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.