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.

174 lines
4.6 KiB

<?php
namespace common\modules\languages\controllers\manage;
use common\modules\languages\entities\Language;
use common\modules\languages\forms\LanguageForm;
use common\modules\languages\forms\search\LanguageSearch;
use common\modules\languages\services\LanguageManageService;
3 years ago
use DomainException;
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;
class LanguageController extends Controller
{
3 years ago
private LanguageManageService $service;
public function __construct($id, $module, LanguageManageService $service, $config = [])
{
parent::__construct($id, $module, $config);
$this->service = $service;
}
public function behaviors(): array
{
return [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'roles' => ['LanguagesManagement'],
],
[ // all the action are accessible to admin
'allow' => true,
'roles' => ['admin'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'delete' => ['POST'],
'set-default' => ['POST'],
],
],
];
}
/**
3 years ago
* @return string
*/
3 years ago
public function actionIndex(): string
{
$searchModel = new LanguageSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* @param $id
*
* @return string
* @throws NotFoundHttpException
*/
3 years ago
public function actionView($id): string
{
return $this->render('view', [
'language' => $this->findModel($id),
]);
}
/**
3 years ago
* @return string|Response
*/
3 years ago
public function actionCreate(): string|Response
{
$form = new LanguageForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$form = $this->service->create($form);
return $this->redirect(['view', 'id' => $form->id]);
3 years ago
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
else {
$form->status = $form->status ?: Language::STATUS_ACTIVE;
}
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
{
$language = $this->findModel($id);
$form = new LanguageForm($language);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$this->service->edit($language->id, $form);
return $this->redirect(['view', 'id' => $language->id]);
3 years ago
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('update', [
'model' => $form,
'language' => $language,
]);
}
/**
* @param integer $id
3 years ago
* @return Response
*/
3 years ago
public function actionDelete(int $id): Response
{
try {
$this->service->remove($id);
3 years ago
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['index']);
}
3 years ago
/**
* @param $id
* @return Response
* @throws NotFoundHttpException
*/
public function actionSetDefault($id): Response
{
$language = $this->findModel($id);
if ($language) {
$this->service->setDefault($language);
return $this->redirect(['index']);
}
throw new NotFoundHttpException('Language is not exists');
}
/**
* @param integer $id
* @return Language the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
3 years ago
protected function findModel(int $id): Language
{
if (($model = Language::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested language does not exist.');
}
}