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.
210 lines
6.1 KiB
210 lines
6.1 KiB
<?php |
|
|
|
namespace common\modules\forms\controllers\manage; |
|
|
|
use common\modules\forms\services\FormManageService; |
|
use common\modules\forms\forms\FormSearch; |
|
use common\modules\forms\entities\Form; |
|
use common\modules\pages\entities\Page; |
|
use common\modules\forms\forms\FormForm; |
|
use yii\web\NotFoundHttpException; |
|
use yii\filters\AccessControl; |
|
use yii\filters\VerbFilter; |
|
use yii\web\Controller; |
|
use yii\web\Response; |
|
use Yii; |
|
|
|
class FormController extends Controller |
|
{ |
|
private $_service; |
|
|
|
public function __construct($id, $module, FormManageService $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 FormSearch(); |
|
$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) |
|
{ |
|
return $this->render('view', [ |
|
'form' => $this->findModel($id), |
|
]); |
|
} |
|
|
|
/** |
|
* @return mixed |
|
*/ |
|
public function actionCreate() |
|
{ |
|
$form = new FormForm(); |
|
if ($form->load(Yii::$app->request->post()) && $form->validate()) { |
|
try { |
|
$form = $this->_service->create($form); |
|
|
|
return $this->redirect(['view', 'id' => $form->id]); |
|
} catch (\DomainException $e) { |
|
Yii::$app->errorHandler->logException($e); |
|
Yii::$app->session->setFlash('error', $e->getMessage()); |
|
} |
|
} |
|
|
|
return $this->render('create', [ |
|
'model' => $form, |
|
]); |
|
} |
|
|
|
/** |
|
* @param $id |
|
* |
|
* @return string|\yii\web\Response |
|
* @throws NotFoundHttpException |
|
*/ |
|
public function actionUpdate($id) |
|
{ |
|
$form_model = $this->findModel($id); |
|
|
|
$form = new FormForm($form_model); |
|
if ($form->load(Yii::$app->request->post()) && $form->validate()) { |
|
try { |
|
$this->_service->edit($form_model->id, $form); |
|
|
|
return $this->redirect(['view', 'id' => $form_model->id]); |
|
} catch (\DomainException $e) { |
|
Yii::$app->errorHandler->logException($e); |
|
Yii::$app->session->setFlash('error', $e->getMessage()); |
|
} |
|
} |
|
|
|
return $this->render('update', [ |
|
'model' => $form, |
|
'form_model' => $form_model, |
|
]); |
|
} |
|
|
|
/** |
|
* @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']); |
|
} |
|
|
|
|
|
public function actionPageSearch($q = null, $id = null) |
|
{ |
|
\Yii::$app->response->format = Response::FORMAT_JSON; |
|
$out = ['results' => ['id' => '', 'text' => '']]; |
|
if (!is_null($q)) { |
|
$data = []; |
|
$pages = Page::find() |
|
->with('translation') |
|
->leftJoin('{{%pages_lng}}', '`pages_lng`.`page_id` = `pages`.`id`') |
|
->andWhere(['like', 'pages_lng.title', $q]) |
|
->limit(20) |
|
->all(); |
|
foreach ($pages as $page) { |
|
$data[] = [ |
|
'id' => $page->id, |
|
'text' => isset($page->translation) ? $page->translation->title : null, |
|
]; |
|
} |
|
$out['results'] = array_values($data); |
|
} elseif ($id > 0) { |
|
$tag_name = Page::findOne($id)->translation->title; |
|
$out['results'] = ['id' => $tag_name, 'text' => $tag_name]; |
|
} else { |
|
/*$data = []; |
|
$pages = Page::find() |
|
->with('translation') |
|
->leftJoin('{{%pages_lng}}', '`pages_lng`.`page_id` = `pages`.`id`') |
|
->limit(20) |
|
->all(); |
|
foreach ($pages as $page) { |
|
$data[] = [ |
|
'id' => $page->id, |
|
'text' => isset($page->translation) ? $page->translation->title : null, |
|
]; |
|
}*/ |
|
|
|
$data = []; |
|
$pages = Page::find()->typePublic()->orderBy(['id' => SORT_DESC])->limit(20)->all(); |
|
foreach ($pages as $page) { |
|
$data[] = [ |
|
'id' => $page->id, |
|
'text' => isset($page->translation) ? $page->translation->title : null, |
|
]; |
|
} |
|
$out['results'] = array_values($data); |
|
} |
|
|
|
return $out; |
|
} |
|
|
|
/** |
|
* @param integer $id |
|
* |
|
* @return Form the loaded model |
|
* @throws NotFoundHttpException if the model cannot be found |
|
*/ |
|
protected function findModel($id): Form |
|
{ |
|
if (($model = Form::findOne($id)) !== null) { |
|
return $model; |
|
} |
|
throw new NotFoundHttpException('The requested form does not exist.'); |
|
} |
|
}
|
|
|