OptionController.php 9.2 KB
<?php

namespace frontend\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()
    {
        $model = new Option();
        $modellang = new OptionLang();
        $modeldb = 'user';
        $model_id = '10';
        $fields = [['name' => 'phone', 'template' => 'input'], ['name' => 'adres', 'template' => 'input']];
        $post = \Yii::$app->request->post();
        if(!empty($post['Option'])) {
            $ok = 1;
            $parentid = null;
            $models = array();
            foreach($post['Option'] as $index => $option) {
                if(in_array($index, array('model', 'model_id')) && $index !== 0) { continue; }
                $first = 1;
                foreach($option['value'] as $key => $value) {
                    $models[$index][$key] = new Option();
                    $models[$index][$key]->model = $post['Option']['model'];
                    $models[$index][$key]->model_id = $post['Option']['model_id'];
                    $models[$index][$key]->template = $option['template'];
                    $models[$index][$key]->name = $key;
                    if(!$first) {
                        $models[$index][$key]->parent_id = $parentid;
                    }
                    $modelslang[$index][$key][0] = new OptionLang();
                    if(!empty($value) && $models[$index][$key]->save()) {
                        if($first) {
                            $parentid = $models[$index][$key]->option_id;
                        }
                        $modelslang[$index][$key][0]->id = $models[$index][$key]->option_id;
                        $modelslang[$index][$key][0]->lang_id = 0;
                        $modelslang[$index][$key][0]->value = $value;
                        if($modelslang[$index][$key][0]->save()) {
                            if(!empty($option['lang'][$key])) {
                                foreach($option['lang'][$key] as $code => $lang) {
                                    if(!empty($lang)) {
                                        $modelslang[$index][$key][$code] = new OptionLang();
                                        $modelslang[$index][$key][$code]->id = $models[$index][$key]->option_id;
                                        $modelslang[$index][$key][$code]->lang_id = $code;
                                        $modelslang[$index][$key][$code]->value = $lang;
                                        if(!$modelslang[$index][$key][$code]->save()) {
                                            $ok = 0;
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        $models[$index][$key]->validate();
                        $modelslang[$index][$key][0]->validate();
                        $modelslang[$index][$key][0];
                        if(!empty($option['lang'][$key])) {
                            foreach($option['lang'][$key] as $code => $lang) {
                                if(!empty($lang)) {
                                    $modelslang[$index][$key][$code] = new OptionLang();
                                    $modelslang[$index][$key][$code]->id = $models[$index][$key]->option_id;
                                    $modelslang[$index][$key][$code]->lang_id = $code;
                                    $modelslang[$index][$key][$code]->value = $lang;
                                }
                            }
                        }
                        $ok = 0;
                    }
                    $first = 0;
                }
            }
            if($ok) {
                return $this->redirect(['view', 'id' => $parentid]);
            } else {
                return $this->render('create', [
                    'model' => $model,
                    'models' => $models,
                    'modellang' => $modelslang,
                    'modeldb' => $modeldb,
                    'model_id' => $model_id,
                    'fields' => $fields
                ]);
            }
        }
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->option_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'modeldb' => $modeldb,
                'model_id' => $model_id,
                'fields' => $fields
            ]);
        }
    }

    /**
     * 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)
    {
        $models[$id] = Option::findOne($id);
        $modellang[$id] = array();
        $langs = OptionLang::findAll(['id' => $id]);
        foreach($langs as $lang) {
            $modellang[$id][$lang->lang_id] = $lang;
        }
        $children = (new Option())->find()->where(['parent_id' => $id])->all();
        foreach($children as $child) {
            $models[$child->option_id] = $child;
            $modellang[$child->option_id] = array();
            $langs = OptionLang::findAll(['id' =>$child->option_id]);
            foreach($langs as $lang) {
                $modellang[$child->option_id][$lang->lang_id] = $lang;
            }
        }
        $modeldb = 'user';
        $model_id = '10';
        $fields = [['name' => 'phone', 'template' => 'input'], ['name' => 'adres', 'template' => 'input']];
        $post = \Yii::$app->request->post();
        $ok = 1;
        if(!empty($post)) {
            foreach($post['Option'] as $key => $option) {
                if(in_array($key, array('model', 'model_id'))) { continue; }
                $modellang[$key][0]->value = $option['value'][$models[$key]->name];
                if(!$modellang[$key][0]->save()) {
                    $ok = 0;
                }
                foreach($option['lang'] as $lang_id => $lang) {
                    if(empty($modellang[$key][$lang_id])) {
                        $modellang[$key][$lang_id] = new OptionLang();
                        $modellang[$key][$lang_id]->id = $models[$key]->option_id;
                        $modellang[$key][$lang_id]->lang_id = $lang_id;
                        $modellang[$key][$lang_id]->value = $lang;
                    } else {
                        $modellang[$key][$lang_id]->value = $lang;
                    }
                    if(!$modellang[$key][$lang_id]->save()) {
                        $ok = 0;
                    }
                }
            }
            if($ok) {
                return $this->redirect(['view', 'id' => $id]);
            } else {
                return $this->render('update', [
                    'models' => $models,
                    'modellang' => $modellang,
                    'modeldb' => $modeldb,
                    'model_id' => $model_id
                ]);
            }
        }
        return $this->render('update', [
            'models' => $models,
            'modellang' => $modellang,
            'modeldb' => $modeldb,
            'model_id' => $model_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)
    {
        $this->findModel($id)->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.');
        }
    }
}