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.

169 lines
4.3 KiB

7 years ago
<?php
namespace backend\controllers;
use core\entities\Slider;
use core\forms\SliderForm;
use core\services\SliderService;
3 years ago
use DomainException;
7 years ago
use Yii;
use yii\data\ActiveDataProvider;
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 SliderController extends Controller
{
3 years ago
private SliderService $service;
7 years ago
public function __construct($id, $module, SliderService $service, $config = [])
{
parent::__construct($id, $module, $config);
3 years ago
$this->service = $service;
7 years ago
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'actions' => ['create', 'view', 'index', 'update', 'delete'],
'allow' => true,
'roles' => ['SliderManagement'],
],
[ // all the action are accessible to admin
'allow' => true,
'roles' => ['admin'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
7 years ago
'actions' => [
'delete' => ['POST'],
],
],
];
}
3 years ago
public function actionIndex(): string
7 years ago
{
$query = Slider::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
],
'sort' => [
'defaultOrder' => [
'id' => SORT_ASC,
]
],
]);
7 years ago
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
/**
* @param $id
*
* @return string
* @throws NotFoundHttpException
*/
3 years ago
public function actionView($id): string
7 years ago
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new User model.
* If creation is successful, the browser will be redirected to the 'view' page.
3 years ago
* @return string|Response
7 years ago
*/
3 years ago
public function actionCreate(): Response|string
7 years ago
{
$form = new SliderForm();
7 years ago
$form->scenario = Slider::SCENARIO_CREATE;
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$slider = $this->service->create($form);
7 years ago
return $this->redirect(['view', 'id' => $slider->id]);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
7 years ago
return $this->render('create', [
'model' => $form,
]);
}
/**
* @param $id
*
3 years ago
* @return string|Response
* @throws NotFoundHttpException
*/
3 years ago
public function actionUpdate($id): Response|string
7 years ago
{
$slider = $this->findModel($id);
$form = new SliderForm($slider);
$form->scenario = Slider::SCENARIO_UPDATE;
7 years ago
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$this->service->edit($slider->id, $form);
7 years ago
return $this->redirect(['view', 'id' => $slider->id]);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
7 years ago
return $this->render('update', [
'model' => $form,
7 years ago
'slider' => $slider,
]);
}
/**
* @param $id
*
3 years ago
* @return Response
*/
3 years ago
public function actionDelete($id): Response
7 years ago
{
3 years ago
$this->service->remove($id);
7 years ago
return $this->redirect(['index']);
}
3 years ago
/**
* @param $id
* @return Slider|null
* @throws NotFoundHttpException
*/
7 years ago
protected function findModel($id): ?Slider
{
if (($model = Slider::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}