<?php
namespace common\modules\users\controllers;


use common\components\AdminController;
use common\modules\users\models\UserStopWords;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;

class StopwordsAdminController extends AdminController {
    /**
     * @return array|void
     */
    public static function actionsTitles(){
        return [
            'Manage' => 'Управление стоп словами',
            'Create' => 'Добавление нового стоп-слова',
            'View'   => 'Просмотр стоп слова',
            'Update' => 'Редактирование стоп-слова',
            'Delete' => 'Удаление стоп слова',
        ];
    }

    /**
     * @return string
     */
    public function actionManage(){

        $dataProvider = new ActiveDataProvider([
            'query' => UserStopWords::find(),
        ]);

        return $this->render(
            'manage',
            [
                'dataProvider' => $dataProvider,
            ]
        );
    }

    /**
     * @return string|\yii\web\Response
     */
    public function actionCreate(){
        $model = new UserStopWords();
        if ($model->load(\Yii::$app->request->post())) {
            if ($model->validate() && $model->save())
                return $this->redirect([
                    'manage'
                ]);
        }
        $form = new \common\components\BaseForm('/common/modules/users/forms/StopWordsForm', $model);
        return $this->render(
            'create',
            [
                'form' => $form
            ]
        );
    }

    /**
     * @param $id
     * @return string
     */
    public function actionView($id){
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * @param $id
     * @return string|\yii\web\Response
     */
    public function actionUpdate($id){
        /** @var UserStopWords $model */
        $model = $this->findModel($id);
        if ($model->load(\Yii::$app->request->post())) {
            if ($model->validate() && $model->save())
                return $this->redirect([
                    'manage'
                ]);
        }
        $form = new \common\components\BaseForm('/common/modules/users/forms/StopWordsForm', $model);
        return $this->render(
            'update',
            [
                'form' => $form
            ]
        );
    }

    /**
     * @param $id
     * @return UserStopWords|null
     * @throws NotFoundHttpException
     */
    public function findModel($id)
    {
        if (($model = UserStopWords::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    /**
     * @param $id
     * @return \yii\web\Response
     */
    public function actionDelete($id){
        $this->findModel($id)->delete();

        return $this->redirect(['manage']);
    }
} 