diff --git a/frontend/controllers/ScribbleController.php b/frontend/controllers/ScribbleController.php new file mode 100644 index 0000000..4334940 --- /dev/null +++ b/frontend/controllers/ScribbleController.php @@ -0,0 +1,114 @@ + [ + 'class' => AccessControl::class, + 'rules' => [ + [ + 'actions' => [ + 'reverse-favorite' + ], + 'allow' => true, + 'roles' => ['@'], + ], + ], + ], + 'verbs' => [ + 'class' => VerbFilter::class, + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ] + ); + } + + public function actionReverseFavorite(int $id) + { + $scribble = $this->getModelWithValidation($id); + $scribble->favorite = !$scribble->favorite; + if (!$scribble->save()) { + throw new ServerErrorHttpException(); + } + } + + public function actionSetAsFavorite(int $id) + { + $scribble = $this->getModelWithValidation($id); + $scribble->favorite = true; + if (!$scribble->save()) { + throw new ServerErrorHttpException(); + } + } + + public function actionUnsetAsFavorite(int $id) + { + $scribble = $this->getModelWithValidation($id); + $scribble->favorite = false; + if (!$scribble->save()) { + throw new ServerErrorHttpException(); + } + } + + /** + * @param int $scribbleId + * + * @return Scribble + * + * @throws ForbiddenHttpException + * @throws MethodNotAllowedHttpException + * @throws NotFoundHttpException + */ + private function getModelWithValidation(int $scribbleId): Scribble + { + if (!Yii::$app->request->isAjax) { + throw new MethodNotAllowedHttpException(Yii::t('app', 'ERROR_AJAX_REQUESTS_ONLY')); + } + + $scribble = $this->findModel($scribbleId); + + if (!$scribble->scribblePack->canUserControlYou()) { + throw new ForbiddenHttpException(Yii::t('app', 'SCRIBBLE_DENIED_ACCESS')); + } + + return $scribble; + } + + /** + * Finds the Scribble model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param int $scribble_id Scribble ID + * @return Scribble the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($scribble_id) + { + if (($model = Scribble::findOne(['scribble_id' => $scribble_id])) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } +} diff --git a/frontend/views/scribble/_modal_box.php b/frontend/views/scribble/_modal_box.php index 4809216..58bec3a 100644 --- a/frontend/views/scribble/_modal_box.php +++ b/frontend/views/scribble/_modal_box.php @@ -1,21 +1,43 @@ Yii::t('app', 'SCRIBBLES_TITLE_NO'), + true => Yii::t('app', 'SCRIBBLES_TITLE_YES'), +]; + ?>