OptionController.php 3.68 KB
<?php

namespace backend\controllers;

use Yii;
use frontend\models\Option;
use frontend\models\OptionSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use frontend\models\OptionLang;

/**
 * OptionController implements the CRUD actions for Option model.
 */
class OptionController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Option models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new OptionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    /**
     * Displays a single Option model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Option model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $form[0] = Option::create(\Yii::$app->request->post(), 'User', 10, [['name' => 'phone', 'template' => 'text', 'translate' => true], ['name' => 'adres', 'template' => 'text', 'translate' => false]]);
        if($form[0]['success'] == false) {
            return $this->render('create', ['forms' => $form]);
        } else {
            return $this->redirect(['index']);
        }
    }
    
    /**
     * Updates an existing Option model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $form[0] = Option::change($id, \Yii::$app->request->post(), 'User', 10);
        if($form[0]['success'] == false) {
            return $this->render('update', ['forms' => $form]);
        } else {
            return $this->redirect(['view', 'id' => $id]);
        }
    }

    /**
     * Deletes an existing Option model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $children = $model->hasMany(Option::className(), ['parent_id' => 'option_id'])->all();
        $langs = array();
        if(!empty($children)) {
            foreach($children as $child) {
                $langs = OptionLang::findAll(['id' => $child->option_id]);
                foreach($langs as $lang) {
                    $lang->delete();
                }
                $child->delete();
            }
        }
        $langs = OptionLang::findAll(['id' => $id]);
        foreach($langs as $lang) {
            $lang->delete();
        }
        $model->delete();

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

    /**
     * Finds the Option model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Option the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Option::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}