You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.3 KiB

7 years ago
<?php
namespace backend\controllers\post;
use core\forms\post\search\PostCommentSearch;
use core\forms\post\PostCommentEditForm;
use core\services\post\PostCommentManageService;
3 years ago
use DomainException;
7 years ago
use Yii;
use core\entities\post\Post;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
3 years ago
use yii\web\Response;
7 years ago
class CommentController extends Controller
{
3 years ago
private PostCommentManageService $service;
7 years ago
public function __construct($id, $module, PostCommentManageService $service, $config = [])
{
parent::__construct($id, $module, $config);
$this->service = $service;
}
public function behaviors(): array
{
return [
'access' => [
3 years ago
'class' => AccessControl::class,
7 years ago
'rules' => [
[
'allow' => true,
'roles' => ['PostManagement'],
],
[ // all the action are accessible to admin
'allow' => true,
'roles' => ['admin'],
],
],
],
'verbs' => [
3 years ago
'class' => VerbFilter::class,
7 years ago
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
3 years ago
* @return string
7 years ago
*/
3 years ago
public function actionIndex(): string
7 years ago
{
$searchModel = new PostCommentSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* @param $post_id
* @param $id
*
3 years ago
* @return string|Response
7 years ago
* @throws NotFoundHttpException
*/
3 years ago
public function actionUpdate($post_id, $id): Response|string
7 years ago
{
$post = $this->findModel($post_id);
$comment = $post->getComment($id);
$form = new PostCommentEditForm($comment);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$this->service->edit($post->id, $comment->id, $form);
return $this->redirect(['view', 'post_id' => $post->id, 'id' => $comment->id]);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('update', [
'post' => $post,
'model' => $form,
3 years ago
//'comment' => $comment,
7 years ago
]);
}
/**
* @param $post_id
* @param $id
*
* @return string
* @throws NotFoundHttpException
*/
3 years ago
public function actionView($post_id, $id): string
7 years ago
{
$post = $this->findModel($post_id);
$comment = $post->getComment($id);
return $this->render('view', [
'post' => $post,
'comment' => $comment,
]);
}
/**
* @param $post_id
* @param $id
*
3 years ago
* @return Response
7 years ago
* @throws NotFoundHttpException
*/
3 years ago
public function actionActivate($post_id, $id): Response
7 years ago
{
$post = $this->findModel($post_id);
try {
$this->service->activate($post->id, $id);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['view', 'post_id' => $post_id, 'id' => $id]);
}
/**
3 years ago
* @param integer $post_id
* @param integer $id
7 years ago
*
3 years ago
* @return Response
7 years ago
* @throws NotFoundHttpException
*/
3 years ago
public function actionDelete(int $post_id, int $id): Response
7 years ago
{
$post = $this->findModel($post_id);
try {
$this->service->remove($post->id, $id);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['index']);
}
/**
* @param integer $id
* @return Post the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
3 years ago
protected function findModel(int $id): Post
7 years ago
{
if (($model = Post::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}