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.

163 lines
4.4 KiB

6 years ago
<?php
namespace common\modules\banners\controllers\manage;
use common\modules\banners\entities\Banner;
use common\modules\banners\forms\BannerForm;
use common\modules\banners\forms\search\BannerSearch;
use common\modules\banners\services\BannerManageService;
3 years ago
use DomainException;
6 years ago
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use Yii;
3 years ago
use yii\web\Response;
6 years ago
class BannerController extends Controller
{
3 years ago
private BannerManageService $service;
6 years ago
public function __construct($id, $module, BannerManageService $service, $config = [])
{
parent::__construct($id, $module, $config);
3 years ago
$this->service = $service;
6 years ago
}
public function behaviors(): array
{
return [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'roles' => ['BannersManagement'],
],
[ // all the action are accessible to admin
'allow' => true,
'roles' => ['admin'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
6 years ago
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
3 years ago
* @return string
6 years ago
*/
3 years ago
public function actionIndex(): string
6 years ago
{
$searchModel = new BannerSearch();
6 years ago
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
6 years ago
'dataProvider' => $dataProvider,
]);
}
/**
* @param $id
*
* @return string
* @throws NotFoundHttpException
*/
3 years ago
public function actionView($id): string
6 years ago
{
return $this->render('view', [
'banner' => $this->findModel($id),
]);
}
/**
3 years ago
* @return string|Response
6 years ago
*/
3 years ago
public function actionCreate(): string|Response
6 years ago
{
$form = new BannerForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$form = $this->service->create($form);
6 years ago
return $this->redirect(['view', 'id' => $form->id]);
3 years ago
} catch (DomainException $e) {
6 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
} else {
$form->active = $form->active ?: Banner::STATUS_ACTIVE;
6 years ago
}
6 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
6 years ago
{
$banner = $this->findModel($id);
$form = new BannerForm($banner);
$form->start_at = date('d.m.Y H:i:s', $form->start_at);
$form->end_at = date('d.m.Y H:i:s', $form->end_at);
6 years ago
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$this->service->edit($banner->id, $form);
6 years ago
return $this->redirect(['view', 'id' => $banner->id]);
3 years ago
} catch (DomainException $e) {
6 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('update', [
'model' => $form,
6 years ago
'banner' => $banner,
]);
}
/**
3 years ago
* @param int $id
* @return Response
6 years ago
*/
3 years ago
public function actionDelete(int $id): Response
6 years ago
{
try {
3 years ago
$this->service->remove($id);
} catch (DomainException $e) {
6 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
6 years ago
return $this->redirect(['index']);
}
/**
* @param integer $id
*
6 years ago
* @return Banner the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
3 years ago
protected function findModel(int $id): Banner
6 years ago
{
if (($model = Banner::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested banner does not exist.');
}
}