Option.php 8.83 KB
<?php

namespace frontend\models;

use Yii;

/**
 * This is the model class for table "option".
 *
 * @property integer $option_id
 * @property string $model
 * @property integer $model_id
 * @property string $name
 * @property string $template
 * @property integer $parent_id
 */
class Option extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'option';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['model', 'model_id', 'name', 'template'], 'required'],
            [['model_id', 'parent_id'], 'integer'],
            [['model', 'name', 'template'], 'string', 'max' => 200]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'option_id' => Yii::t('app', 'Option ID'),
            'model' => Yii::t('app', 'Model'),
            'model_id' => Yii::t('app', 'Model ID'),
            'name' => Yii::t('app', 'Name'),
            'template' => Yii::t('app', 'Template'),
            'parent_id' => Yii::t('app', 'Parent ID'),
        ];
    }
    public function getLangs() {
        return (new Language())->find()->where(['>', 'language_id', 0])->asArray()->all();
    }
    public static function change($id, $post, $modeldb, $model_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;
            }
        }
        $ok = 1;
        if(!empty($post)) {
            foreach($post['Option'] as $key => $option) {
                if(in_array($key, array('model', 'model_id'))) { continue; }
                if(empty($option['value'][$models[$key]->name]) && !empty($option['lang'])) {
                    foreach($option['lang'] as $lang_id => $lang) {
                        if(!empty($lang)) {
                            $option['value'][$models[$key]->name] = $lang;
                            break;
                        }
                    }
                }
                $modellang[$key][0]->value = $option['value'][$models[$key]->name];
                if(empty($modellang[$key][0]->value) || !$modellang[$key][0]->save()) {
                    $ok = 0;
                    $models[$key]->addError('value', 'Value must be set');
                    $modellang[$key][0]->addError('value', 'Value must be set');
                }
                if(!empty($option['lang'])) {
                    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 array('id' => $id, 'models' => $models, 'modelslang' => $modelslang, 'success' => true);
            } else {
                return array(
                    'models' => $models,
                    'modellang' => $modellang,
                    'modeldb' => $modeldb,
                    'model_id' => $model_id,
                    'success' => false
                );
            }
        }
        return array(
            'models' => $models,
            'modellang' => $modellang,
            'modeldb' => $modeldb,
            'model_id' => $model_id,
            'success' => false
        );
    }
    public static function create($post, $modeldb, $model_id, $fields) {
        $model = new Option();
        $modellang = new OptionLang();
        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[$key]['template'];
                    $models[$index][$key]->translate = $option[$key]['translate']?1:0;
                    $models[$index][$key]->name = $key;
                    if(!$first) {
                        $models[$index][$key]->parent_id = $parentid;
                    }
                    $modelslang[$index][$key][0] = new OptionLang();
                    if(!empty($option['lang'][$key])) {
                        foreach($option['lang'][$key] as $code => $lang) {
                            if(!empty($lang)) {
                                $value = $lang;
                                break;
                            }
                        }
                    }
                    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]->addError('value', 'Value must be set');
                        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 array('models' => $models, 'modelslang' => $modelslang, 'success' => true);
            } else {
                return array(
                    'models' => $models,
                    'modellang' => $modelslang,
                    'modeldb' => $modeldb,
                    'model_id' => $model_id,
                    'fields' => $fields,
                    'success' => false
                );
            }
        } else {
            return array(
                'model' => $model,
                'modeldb' => $modeldb,
                'model_id' => $model_id,
                'fields' => $fields,
                'success' => false
            );
        }
    }
}