File: /home/safarimaris/home/safarimaris/backend/controllers/ReviewController.php
<?php
namespace backend\controllers;
use common\components\ReviewStatus;
use common\models\Entity;
use Yii;
use common\models\Review;
use app\models\ReviewSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\helpers\Url;
/**
* ReviewController implements the CRUD actions for Review model.
*/
class ReviewController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['index', 'view'/*, 'create'*/, 'update', 'delete', 'approve', 'reject'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Review models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new ReviewSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->pagination->pageSize=50;
Url::remember();
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Review model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Review model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Review();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(Url::previous());
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Review model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) {
$model->setRating();
if ($model->save()) {
return $this->redirect(Url::previous());
} else {
return $this->render('update', [
'model' => $model,
]);
}
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Review model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$review = $this->findModel($id);
$boatId = $review->entityId;
if($review->delete()){
$boat = Entity::findOne($boatId);
$reviews = Review::findAll(['entityId' => $boatId, 'status' => ReviewStatus::STATUS_APPROVED]);
if(!$reviews){
$boat->reviewCount = null;
$boat->ratingValue = null;
}else{
$sum = array_reduce($reviews, function($i, $obj)
{
return $i += $obj->rating;
});
$boat->reviewCount = count($reviews);
$boat->ratingValue = round($sum/$boat->reviewCount, 1);
}
$boat->save(false);
}
return $this->redirect(Url::previous());
}
/**
* Change status of review to approved.
* @param integer $id
* @return mixed
*/
public function actionApprove($id)
{
$model = $this->findModel($id);
$model->status = ReviewStatus::STATUS_APPROVED;
$model->save(false);
return $this->redirect(Url::previous());
}
/**
* Change status of review to rejected
* @param integer $id
* @return mixed
*/
public function actionReject($id)
{
$model = $this->findModel($id);
$model->status = ReviewStatus::STATUS_REJECTED;
$model->save(false);
return $this->redirect(Url::previous());
}
/**
* Finds the Review model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Review the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Review::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}