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.

194 lines
4.9 KiB

7 years ago
<?php
namespace frontend\controllers;
use core\entities\post\PostType;
use core\forms\post\PostCommentForm;
use core\repositories\post\read\PostCategoryReadRepository;
use core\repositories\post\read\PostReadRepository;
use core\repositories\post\read\PostTagReadRepository;
use core\services\post\PostCommentService;
3 years ago
use DomainException;
use frontend\components\FrontendController;
7 years ago
use Yii;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
3 years ago
use yii\web\Response;
7 years ago
class PostController extends FrontendController
7 years ago
{
public $layout = 'post';
3 years ago
private PostCommentService $service;
private PostReadRepository $posts;
private PostCategoryReadRepository $categories;
private PostTagReadRepository $tags;
7 years ago
public function __construct(
$id,
$module,
PostCommentService $service,
PostReadRepository $posts,
PostCategoryReadRepository $categories,
PostTagReadRepository $tags,
$config = []
)
{
parent::__construct($id, $module, $config);
$this->service = $service;
$this->posts = $posts;
$this->categories = $categories;
$this->tags = $tags;
}
public function behaviors(): array
{
return [
'access' => [
3 years ago
'class' => AccessControl::class,
7 years ago
'rules' => [
[
'actions' => ['index', 'category', 'tag', 'post'],
'allow' => true,
'roles' => ['Posts'],
],
[
'actions' => ['comment'],
'allow' => true,
'roles' => ['Comments'],
],
[ // all the action are accessible to admin
'allow' => true,
'roles' => ['admin'],
],
],
],
];
}
/**
* @param $tid
*
* @return string
* @throws NotFoundHttpException
*/
3 years ago
public function actionIndex($tid): string
7 years ago
{
$type = $this->findType($tid);
/* @var $dataProvider ActiveDataProvider */
$dataProvider = $this->posts->getAll($type->id);
$dataProvider->pagination->pageSize = 10;
$this->layout = $type->name;
return $this->render($type->name . '/index', [
'dataProvider' => $dataProvider,
'type' => $type,
]);
}
/**
* @param $id
* @param $tid
*
* @return string
* @throws NotFoundHttpException
*/
3 years ago
public function actionCategory($id, $tid): string
7 years ago
{
if (!$category = $this->categories->find($id)) {
throw new NotFoundHttpException('The requested page does not exist.');
}
$type = $this->findType($tid);
$dataProvider = $this->posts->getAllByCategory($category);
return $this->render($type->name . '/category', [
'category' => $category,
'dataProvider' => $dataProvider,
'type' => $type,
]);
}
/**
* @param $id
3 years ago
* @return string
7 years ago
* @throws NotFoundHttpException
*/
3 years ago
public function actionTag($id): string
7 years ago
{
if (!$tag = $this->tags->find($id)) {
throw new NotFoundHttpException('The requested page does not exist.');
}
$dataProvider = $this->posts->getAllByTag($tag);
return $this->render('tag', [
'tag' => $tag,
'dataProvider' => $dataProvider,
]);
}
/**
* @param $id
3 years ago
* @return string
7 years ago
* @throws NotFoundHttpException
*/
3 years ago
public function actionPost($id): string
7 years ago
{
if (!$post = $this->posts->find($id)) {
throw new NotFoundHttpException('The requested page does not exist.');
}
$type = $this->findType($post->type_id);
$this->layout = $type->name;
return $this->render($type->name . '/post', [
'post' => $post,
'type' => $type,
]);
}
/**
* @param $id
3 years ago
* @return string|Response
7 years ago
* @throws NotFoundHttpException
*/
3 years ago
public function actionComment($id): string|Response
7 years ago
{
if (!$post = $this->posts->find($id)) {
throw new NotFoundHttpException('The requested page does not exist.');
}
$form = new PostCommentForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$comment = $this->service->create($post->id, Yii::$app->user->id, $form);
return $this->redirect(['post', 'id' => $post->id, '#' => 'comment_' . $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('comment', [
'post' => $post,
'model' => $form,
]);
}
3 years ago
/**
* @param $id
* @return PostType
* @throws NotFoundHttpException
*/
7 years ago
protected function findType($id): PostType
{
if (($type = PostType::findOne($id)) !== null) {
return $type;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}