5
5
use Yii ;
6
6
use yii \filters \VerbFilter ;
7
7
use yii \helpers \Json ;
8
+ use yii \web \BadRequestHttpException ;
8
9
use yii \web \Controller ;
9
10
use yii \web \NotFoundHttpException ;
10
11
use yii \web \Response ;
@@ -54,36 +55,23 @@ public function behaviors()
54
55
* Create comment.
55
56
*
56
57
* @param $entity string encrypt entity
57
- * @return array|null|Response
58
+ * @return array
58
59
*/
59
60
public function actionCreate ($ entity )
60
61
{
61
- /* @var $module Module */
62
- $ module = Yii::$ app ->getModule (Module::$ name );
63
- $ event = Yii::createObject (['class ' => CommentEvent::className (), 'user ' => Yii::$ app ->user ]);
64
- $ commentModelClass = $ module ->commentModelClass ;
65
- $ decryptEntity = Yii::$ app ->getSecurity ()->decryptByKey (utf8_decode ($ entity ), $ module ::$ name );
66
- if ($ decryptEntity !== false ) {
67
- $ entityData = Json::decode ($ decryptEntity );
68
- /* @var $model CommentModel */
69
- $ model = new $ commentModelClass ;
70
- $ model ->setAttributes ($ entityData );
71
- if ($ model ->load (Yii::$ app ->request ->post ()) && $ model ->save ()) {
72
- $ event ->setCommentModel ($ model );
73
- $ this ->trigger (self ::EVENT_AFTER_CREATE , $ event );
74
- return ['status ' => 'success ' ];
75
- } else {
76
- return [
77
- 'status ' => 'error ' ,
78
- 'errors ' => ActiveForm::validate ($ model )
79
- ];
80
- }
81
- } else {
82
- return [
83
- 'status ' => 'error ' ,
84
- 'message ' => Yii::t ('yii2mod.comments ' , 'Oops, something went wrong. Please try again later. ' )
85
- ];
62
+ $ commentModel = Yii::createObject (Yii::$ app ->getModule (Module::$ name )->commentModelClass );
63
+ $ commentModel ->setAttributes ($ this ->getCommentAttributesFromEntity ($ entity ));
64
+ if ($ commentModel ->load (Yii::$ app ->request ->post ()) && $ commentModel ->save ()) {
65
+ $ event = Yii::createObject (['class ' => CommentEvent::className (), 'commentModel ' => $ commentModel ]);
66
+ $ this ->trigger (self ::EVENT_AFTER_CREATE , $ event );
67
+
68
+ return ['status ' => 'success ' ];
86
69
}
70
+
71
+ return [
72
+ 'status ' => 'error ' ,
73
+ 'errors ' => ActiveForm::validate ($ commentModel )
74
+ ];
87
75
}
88
76
89
77
/**
@@ -119,4 +107,22 @@ protected function findModel($id)
119
107
throw new NotFoundHttpException (Yii::t ('yii2mod.comments ' , 'The requested page does not exist. ' ));
120
108
}
121
109
}
110
+
111
+ /**
112
+ * Get list of attributes from encrypted entity
113
+ *
114
+ * @param $entity string encrypted entity
115
+ * @return array|mixed
116
+ *
117
+ * @throws BadRequestHttpException
118
+ */
119
+ protected function getCommentAttributesFromEntity ($ entity )
120
+ {
121
+ $ decryptEntity = Yii::$ app ->getSecurity ()->decryptByKey (utf8_decode ($ entity ), Module::$ name );
122
+ if ($ decryptEntity !== false ) {
123
+ return Json::decode ($ decryptEntity );
124
+ }
125
+
126
+ throw new BadRequestHttpException (Yii::t ('yii2mod.comments ' , 'Oops, something went wrong. Please try again later. ' ));
127
+ }
122
128
}
0 commit comments