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.

160 lines
4.2 KiB

6 years ago
<?php
namespace common\modules\banners\controllers\manage;
use common\modules\banners\entities\BannerPlace;
use common\modules\banners\forms\BannerPlaceForm;
use common\modules\banners\forms\search\PlaceSearch;
use common\modules\banners\services\BannerPlaceManageService;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use Yii;
class PlaceController extends Controller
{
private $_service;
6 years ago
public function __construct($id, $module, BannerPlaceManageService $service, $config = [])
{
parent::__construct($id, $module, $config);
$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'],
],
],
];
}
/**
* @return mixed
*/
public function actionIndex()
{
$searchModel = new PlaceSearch();
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
*/
6 years ago
public function actionView($id)
{
return $this->render('view', [
'place' => $this->findModel($id),
]);
}
/**
* @return mixed
*/
public function actionCreate()
{
$form = new BannerPlaceForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$form = $this->_service->create($form);
6 years ago
return $this->redirect(['view', 'id' => $form->id]);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
} else {
$form->active = $form->active ?: BannerPlace::STATUS_ACTIVE;
6 years ago
}
6 years ago
return $this->render('create', [
'model' => $form,
]);
}
/**
* @param $id
*
* @return string|\yii\web\Response
* @throws NotFoundHttpException
*/
6 years ago
public function actionUpdate($id)
{
$place = $this->findModel($id);
$form = new BannerPlaceForm($place);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$this->_service->edit($place->id, $form);
6 years ago
return $this->redirect(['view', 'id' => $place->id]);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('update', [
'model' => $form,
'place' => $place,
]);
}
/**
* @param integer $id
*
6 years ago
* @return mixed
*/
public function actionDelete($id)
{
try {
$this->_service->remove($id);
6 years ago
} catch (\DomainException $e) {
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 BannerPlace the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id): BannerPlace
{
if (($model = BannerPlace::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested place does not exist.');
}
}