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.

189 lines
5.1 KiB

7 years ago
<?php
namespace backend\controllers\post;
use core\entities\post\PostType;
use core\forms\post\PostCategoryForm;
use core\services\post\PostCategoryManageService;
3 years ago
use DomainException;
7 years ago
use Yii;
use core\entities\post\PostCategory;
use core\forms\post\search\PostCategorySearch;
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 CategoryController extends Controller
{
3 years ago
private PostCategoryManageService $service;
7 years ago
public function __construct($id, $module, PostCategoryManageService $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'],
],
],
];
}
/**
* @param $tid
*
* @return string
* @throws NotFoundHttpException
*/
3 years ago
public function actionIndex($tid): string
7 years ago
{
$type = $this->findType($tid);
$searchModel = new PostCategorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'type' => $type,
]);
}
/**
* @param $id
*
* @return string
* @throws NotFoundHttpException
*/
3 years ago
public function actionView($id): string
7 years ago
{
$category = $this->findModel($id);
$type = $this->findType($category->type_id);
return $this->render('view', [
'category' => $category,
'type' => $type,
]);
}
/**
* @param $tid
*
3 years ago
* @return string|Response
7 years ago
* @throws NotFoundHttpException
*/
3 years ago
public function actionCreate($tid): Response|string
7 years ago
{
$type = $this->findType($tid);
$form = new PostCategoryForm();
$form->type_id = $type->id;
$form->updateSort();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$category = $this->service->create($form);
return $this->redirect(['view', 'id' => $category->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('create', [
'model' => $form,
'type' => $type,
]);
}
/**
* @param $id
*
3 years ago
* @return string|Response
7 years ago
* @throws NotFoundHttpException
*/
3 years ago
public function actionUpdate($id): Response|string
7 years ago
{
$category = $this->findModel($id);
$type = $this->findType($category->type_id);
$form = new PostCategoryForm($category);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$this->service->edit($category->id, $form);
return $this->redirect(['view', 'id' => $category->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', [
'model' => $form,
'category' => $category,
'type' => $type,
]);
}
/**
* @param $id
*
3 years ago
* @return Response
7 years ago
* @throws NotFoundHttpException
*/
3 years ago
public function actionDelete($id): Response
7 years ago
{
$category = $this->findModel($id);
$type = $this->findType($category->type_id);
try {
$this->service->remove($id);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['index', 'tid' => $type->id]);
}
/**
* @param integer $id
* @return PostCategory the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
3 years ago
protected function findModel(int $id): PostCategory
7 years ago
{
if (($model = PostCategory::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
3 years ago
/**
* @param integer $id
* @return PostType
* @throws NotFoundHttpException
*/
protected function findType(int $id): PostType
7 years ago
{
if (($type = PostType::findOne($id)) !== null) {
return $type;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}