Create.php 3.02 KB
<?php
    
    namespace backend\actions;
    
    use artbox\core\admin\actions\Save;
    use artbox\core\models\Language;
    use function call_user_func;
    use yii\base\Model;
    
    /**
     * Create action for Artbox CRUD
     *
     * @package artbox\core\admin\actions
     */
    class Create extends Save
    {
        /**
         * @var string
         */
        public $viewPath = '@backend/actions/views/create';
        
        /**
         * If this parameter is setted - when creating aliases for model, write this parameter to aliase's entity
         * Created to keep back reference with FilterHelper
         *
         * @var null
         */
        public $overwriteEntity = null;
        
        /**
         * Creates new model, loading aliases, languages and gallery
         *
         * @return string|\yii\web\Response
         */
        public function run()
        {
            $model = call_user_func(
                [
                    $this->controller,
                    'newModel',
                ]
            );
            $languages = Language::getActive();
            if ($this->hasAlias) {
                /**
                 * @var \artbox\core\models\Alias[] $aliases
                 */
                $aliases = $model->loadAliases();
            }
            
            $post = \Yii::$app->request->post();
            
            if (!empty($this->languageFields)) {
                Model::loadMultiple($model->getVariationModels(), $post);
            }
            
            if ($model->load($post) && $model->save()) {
                if ($this->hasAlias) {
                    /**
                     * @var \artbox\core\models\Alias[] $aliases
                     */
                    Model::loadMultiple($aliases, $post);
                    foreach ($aliases as $alias) {
                        $alias->route = $model->getRoute();
                        if ($this->overwriteEntity === null) {
                            $alias->entity = $model::className();
                        } else {
                            $alias->entity = $this->overwriteEntity;
                        }
                        $alias->save();
                        
                        /**
                         * @var \yii\db\ActiveRecord $modelLang
                         */
                        $modelLang = $model->getVariationModel($alias->language_id);
                        $modelLang->alias_id = $alias->id;
                        $modelLang->save();
                    }
                }
                
                if ($this->hasGallery) {
                    $model->saveImages($post);
                }
                
                return $this->controller->redirect([ 'index' ]);
            }
            
            return $this->controller->render(
                $this->viewPath,
                [
                    'action'    => $this,
                    'model'     => $model,
                    'languages' => $languages,
                ]
            );
        }
    }