|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\core\models; |
|
4
|
|
|
|
|
5
|
|
|
use app\modules\core\helpers\ContentBlockHelper; |
|
6
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
|
7
|
|
|
use yii\caching\TagDependency; |
|
8
|
|
|
use yii\data\ActiveDataProvider; |
|
9
|
|
|
use Yii; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This is the model class for table "content_block". |
|
13
|
|
|
* @property integer $id |
|
14
|
|
|
* @property string $name |
|
15
|
|
|
* @property string $key |
|
16
|
|
|
* @property string $value |
|
17
|
|
|
* @property integer $preload |
|
18
|
|
|
* @property integer $group_id |
|
19
|
|
|
*/ |
|
20
|
|
|
class ContentBlock extends \yii\db\ActiveRecord |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var null|string |
|
25
|
|
|
*/ |
|
26
|
|
|
public $newGroup = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritdoc |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function tableName() |
|
32
|
|
|
{ |
|
33
|
|
|
return 'content_block'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritdoc |
|
38
|
|
|
*/ |
|
39
|
|
|
public function rules() |
|
40
|
|
|
{ |
|
41
|
|
|
return [ |
|
42
|
|
|
[['value'], 'string'], |
|
43
|
|
|
[['preload', 'group_id'], 'integer'], |
|
44
|
|
|
[['key'], 'unique'], |
|
45
|
|
|
[['group_id'], 'default', 'value' => 1], |
|
46
|
|
|
[['name', 'key'], 'string', 'max' => 255], |
|
47
|
|
|
[['newGroup'], 'safe'] |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
View Code Duplication |
public function attributeLabels() |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
return [ |
|
57
|
|
|
'id' => 'ID', |
|
58
|
|
|
'name' => Yii::t('app', 'Name'), |
|
59
|
|
|
'key' => Yii::t('app', 'Key'), |
|
60
|
|
|
'value' => Yii::t('app', 'Value'), |
|
61
|
|
|
'preload' => Yii::t('app', 'Preload'), |
|
62
|
|
|
'group_id' => Yii::t('app', 'Group Id'), |
|
63
|
|
|
'newGroup' => Yii::t('app', 'New Group'), |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritdoc |
|
69
|
|
|
*/ |
|
70
|
|
|
public function behaviors() |
|
71
|
|
|
{ |
|
72
|
|
|
return [ |
|
73
|
|
|
[ |
|
74
|
|
|
'class' => ActiveRecordHelper::className(), |
|
75
|
|
|
], |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param $params |
|
81
|
|
|
* @return ActiveDataProvider |
|
82
|
|
|
*/ |
|
83
|
|
|
public function search($params) |
|
84
|
|
|
{ |
|
85
|
|
|
/* @var $query \yii\db\ActiveQuery */ |
|
86
|
|
|
$query = self::find(); |
|
87
|
|
|
|
|
88
|
|
|
if ($this->group_id) { |
|
89
|
|
|
$query->andWhere(['group_id' => $this->group_id]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$dataProvider = new ActiveDataProvider( |
|
93
|
|
|
[ |
|
94
|
|
|
'query' => $query, |
|
95
|
|
|
'pagination' => [ |
|
96
|
|
|
'pageSize' => 10, |
|
97
|
|
|
], |
|
98
|
|
|
] |
|
99
|
|
|
); |
|
100
|
|
|
if (!($this->load($params))) { |
|
101
|
|
|
return $dataProvider; |
|
102
|
|
|
} |
|
103
|
|
|
$query->andFilterWhere(['id' => $this->id]); |
|
104
|
|
|
$query->andFilterWhere(['like', 'name', $this->name]); |
|
105
|
|
|
$query->andFilterWhere(['like', 'key', $this->key]); |
|
106
|
|
|
$query->andFilterWhere(['preload' => $this->preload]); |
|
107
|
|
|
return $dataProvider; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @deprecated use ContentBlockHelper::getChunk($key, $params, $model) instead |
|
112
|
|
|
* Get chunk model or value |
|
113
|
|
|
* @param string $key |
|
114
|
|
|
* @param bool $valueOnly |
|
115
|
|
|
* @param mixed $defaultValue |
|
116
|
|
|
* @return ContentBlock|string |
|
|
|
|
|
|
117
|
|
|
*/ |
|
118
|
|
|
public static function getChunk($key, $valueOnly = true, $defaultValue = null) |
|
119
|
|
|
{ |
|
120
|
|
|
return ContentBlockHelper::getChunk($key); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return ContentBlockGroup[] |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getGroup() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->hasOne(ContentBlockGroup::className(), ['id' => 'group_id']); |
|
130
|
|
|
} |
|
131
|
|
|
} |
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.