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.
 
 
 
 
 

105 lines
2.5 KiB

<?php
namespace common\modules\forms\controllers\manage;
use common\modules\forms\entities\FormMessage;
use common\modules\forms\forms\FormMessageSearch;
use common\modules\forms\services\FormMessageManageService;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use Yii;
class FormMessageController extends Controller
{
private $service;
public function __construct($id, $module, FormMessageManageService $service, $config = [])
{
parent::__construct($id, $module, $config);
$this->service = $service;
}
public function behaviors(): array
{
return [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'roles' => ['FormsManagement'],
],
[ // all the action are accessible to admin
'allow' => true,
'roles' => ['admin'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* @return mixed
*/
public function actionIndex()
{
$searchModel = new FormMessageSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* @param $id
*
* @return string
* @throws NotFoundHttpException
*/
public function actionView($id)
{
$this->service->setRead($id);
return $this->render('view', [
'message' => $this->findModel($id),
]);
}
/**
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
try {
$this->service->remove($id);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['index']);
}
/**
* @param $id
*
* @return FormMessage
* @throws NotFoundHttpException
*/
protected function findModel($id): FormMessage
{
if (($model = FormMessage::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The message does not exist.');
}
}