Blame view

backend/actions/Create.php 3.02 KB
adc7e08f   Anastasia   add slug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  <?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,
                  ]
              );
          }
      }