1
|
|
|
<?php |
2
|
|
|
namespace site\controllers; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use common\components\Controller; |
7
|
|
|
use common\models\CustomBehavior; |
8
|
|
|
use yii\filters\VerbFilter; |
9
|
|
|
use common\components\AccessControl; |
10
|
|
|
use kartik\grid\GridView; |
11
|
|
|
use kartik\grid\EditableColumnAction; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Custom Behavior controller |
17
|
|
|
*/ |
18
|
|
|
class CustomBehaviorController extends Controller { |
19
|
|
|
|
20
|
|
|
public function behaviors() { |
21
|
|
|
return [ |
22
|
|
|
'access' => [ |
23
|
|
|
'class' => AccessControl::class, |
24
|
|
|
'rules' => [ |
25
|
|
|
[ |
26
|
|
|
'actions' => ['create', 'update', 'delete'], |
27
|
|
|
'allow' => true, |
28
|
|
|
'roles' => ['@'], |
29
|
|
|
], |
30
|
|
|
], |
31
|
|
|
], |
32
|
|
|
'verbs' => [ |
33
|
|
|
'class' => VerbFilter::class, |
34
|
|
|
'actions' => [ |
35
|
|
|
'create' => ['post'], |
36
|
|
|
'update' => ['post'], |
37
|
|
|
'delete' => ['post'], |
38
|
|
|
], |
39
|
|
|
], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function actions() { |
44
|
|
|
return array_replace_recursive(parent::actions(), [ |
45
|
|
|
'update' => [ // identifier for your editable column action |
46
|
|
|
'class' => EditableColumnAction::className(), // action class name |
|
|
|
|
47
|
|
|
'modelClass' => CustoMBehavior::className(), // the model for the record being edited |
|
|
|
|
48
|
|
|
'scenario' => CustomBehavior::SCENARIO_DEFAULT, // model scenario assigned before validation & update |
49
|
|
|
'showModelErrors' => true, // show model validation errors after save |
50
|
|
|
'errorOptions' => ['header' => ''], // error summary HTML options |
51
|
|
|
'postOnly' => true, |
52
|
|
|
'ajaxOnly' => true, |
53
|
|
|
'findModel' => function($id, $action) { |
|
|
|
|
54
|
|
|
if(($model = CustomBehavior::findOne(['id' => $id, 'user_id' => Yii::$app->user->identity->id])) !== null) { |
|
|
|
|
55
|
|
|
return $model; |
56
|
|
|
} |
57
|
|
|
throw new NotFoundHttpException('The specified behavior does not exist.'); |
|
|
|
|
58
|
|
|
}, |
59
|
|
|
// 'checkAccess' => function($action, $model) {} |
60
|
|
|
] |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function actionCreate() { |
65
|
|
|
$form = Yii::$container->get(\site\models\CustomBehaviorForm::class, [Yii::$app->user->identity]); |
66
|
|
|
|
67
|
|
|
if($form->load(Yii::$app->request->post()) && $form->validate()) { |
68
|
|
|
$form->save(); |
69
|
|
|
return $this->redirect(['profile/index']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; |
73
|
|
|
if($errors = $form->getErrorSummary(true)) { |
74
|
|
|
return $errors; |
75
|
|
|
} else { |
76
|
|
|
return ["success" => false, "message" => "Unknown Error"]; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function actionDelete(int $id) { |
81
|
|
|
$model = $this->findModel($id); |
82
|
|
|
$model->delete(); |
83
|
|
|
return $this->renderContent($model->getGridView()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function findModel($id) { |
87
|
|
|
if(($model = CustomBehavior::findOne(['id' => $id, 'user_id' => Yii::$app->user->identity->id])) !== null) { |
|
|
|
|
88
|
|
|
return $model; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.