1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace common\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\behaviors\TimestampBehavior; |
7
|
|
|
use common\components\ActiveRecord; |
8
|
|
|
use kartik\grid\GridView; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This is the model class for table "custom_behavior". |
12
|
|
|
* |
13
|
|
|
* @property int $id |
14
|
|
|
* @property int $user_id |
15
|
|
|
* @property int $category_id |
16
|
|
|
* @property string $name |
17
|
|
|
* @property string $created_at |
18
|
|
|
* @property string $updated_at |
19
|
|
|
* |
20
|
|
|
* @property User $user |
21
|
|
|
*/ |
22
|
|
|
class CustomBehavior extends ActiveRecord { |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
* @codeCoverageIgnore |
26
|
|
|
*/ |
27
|
|
|
public static function tableName() { |
28
|
|
|
return 'custom_behavior'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
* @codeCoverageIgnore |
34
|
|
|
*/ |
35
|
|
|
public function rules() { |
36
|
|
|
return [ |
37
|
|
|
[['user_id', 'category_id', 'name'], 'required'], |
38
|
|
|
[['user_id', 'category_id'], 'integer'], |
39
|
|
|
[['created_at', 'updated_at'], 'safe'], |
40
|
|
|
['category_id', 'in', 'range' => array_keys(\common\models\Category::getCategories())], |
41
|
|
|
[['name'], 'string', 'max' => 255], |
42
|
|
|
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => \common\models\User::class, 'targetAttribute' => ['user_id' => 'id']], |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @codeCoverageIgnore |
48
|
|
|
*/ |
49
|
|
|
public function behaviors() { |
50
|
|
|
return [[ |
51
|
|
|
'class' => TimestampBehavior::className(), |
|
|
|
|
52
|
|
|
'attributes' => [ |
53
|
|
|
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'], |
54
|
|
|
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'], |
55
|
|
|
], |
56
|
|
|
// if you're using datetime instead of UNIX timestamp: |
57
|
|
|
// 'value' => new Expression('NOW()'), |
58
|
|
|
]]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
* @codeCoverageIgnore |
64
|
|
|
*/ |
65
|
|
|
public function attributeLabels() { |
66
|
|
|
return [ |
67
|
|
|
'id' => 'ID', |
68
|
|
|
'user_id' => 'User ID', |
69
|
|
|
'category_id' => 'Category', |
70
|
|
|
'name' => 'Name', |
71
|
|
|
'created_at' => 'Created At', |
72
|
|
|
'updated_at' => 'Updated At', |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @codeCoverageIgnore |
78
|
|
|
* @return \yii\db\ActiveQuery |
79
|
|
|
*/ |
80
|
|
|
public function getUser() { |
81
|
|
|
return $this->hasOne(\common\models\User::class, ['id' => 'user_id']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getGridView() { |
85
|
|
|
$dataProvider = new \yii\data\ActiveDataProvider([ |
86
|
|
|
'query' => \common\models\CustomBehavior::find()->where(['user_id' => Yii::$app->user->id])->indexBy('id'), |
87
|
|
|
'pagination' => [ |
88
|
|
|
'pageSize' => 20, |
89
|
|
|
], |
90
|
|
|
]); |
91
|
|
|
$gridColumns = [[ |
92
|
|
|
'class'=>'kartik\grid\EditableColumn', |
93
|
|
|
'attribute'=>'name', |
94
|
|
|
'editableOptions' => ['formOptions' => ['action' => ['/custom-behavior/update']]], |
95
|
|
|
], [ |
96
|
|
|
'class'=>'kartik\grid\EditableColumn', |
97
|
|
|
'attribute'=>'category_id', |
98
|
|
|
'editableOptions' => [ |
99
|
|
|
'formOptions' => ['action' => ['/custom-behavior/update']], |
100
|
|
|
'inputType' => 'dropDownList', |
101
|
|
|
'data' => \common\models\Category::getCategories(), |
102
|
|
|
'displayValueConfig' => \common\models\Category::getCategories(), |
103
|
|
|
], |
104
|
|
|
], [ |
105
|
|
|
'class' => 'kartik\grid\ActionColumn', |
106
|
|
|
'buttons' => [ |
107
|
|
|
'view' => function ($url, $model) { return ''; }, |
|
|
|
|
108
|
|
|
'update' => function ($url, $model) { return ''; }, |
|
|
|
|
109
|
|
|
'delete' => function ($url, $model) { |
110
|
|
|
return yii\helpers\Html::a('<span class="glyphicon glyphicon-remove"></span>', "/custom-behavior/delete?id={$model->id}", [ |
111
|
|
|
'title' => 'Delete', |
112
|
|
|
'data-pjax' => true, |
113
|
|
|
'data-method' => 'post', |
114
|
|
|
'data-confirm' => 'Are you sure to delete this item?' |
115
|
|
|
]); |
116
|
|
|
} |
117
|
|
|
], |
118
|
|
|
]]; |
119
|
|
|
$gridView = \kartik\grid\GridView::widget([ |
120
|
|
|
'dataProvider'=>$dataProvider, |
121
|
|
|
'columns'=>$gridColumns, |
122
|
|
|
'panel' => false, |
123
|
|
|
'pjax' => true, |
124
|
|
|
'layout' => "\n{toolbar}\n{items}\n{summary}\n{pager}", |
125
|
|
|
'toolbar' => [[ |
126
|
|
|
'content' => '<button type="button" class="btn btn-success btn-sm pull-right add-custom-behavior-btn" aria-label="Add Behavior" title="Add Behavior"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> </button>', |
127
|
|
|
'options' => ['class' => 'btn-group clearfix', 'style' => 'width: 100%;'], |
128
|
|
|
]] |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
return $gridView; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.