Blame view

backend/actions/Update.php 1.93 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
  <?php
      
      namespace backend\actions;
      
      use artbox\core\admin\actions\Save;
      use artbox\core\models\Language;
      use function call_user_func_array;
      use yii\base\Model;
      
      class Update extends Save
      {
          /**
           * @var string
           */
          public $viewPath = '@abackend/actions/views/update';
          
          /**
           * Updates existing model with langs, aliases and gallery
           *
           * @param $id
           *
           * @return string|\yii\web\Response
           */
          public function run($id)
          {
              $model = call_user_func_array(
                  [
                      $this->controller,
                      'findModel',
                  ],
                  [ $id ]
              );
              $languages = Language::getActive();
              $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
                       */
                      $aliases = $model->loadAliases();
                      Model::loadMultiple($aliases, $post);
                      foreach ($aliases as $alias) {
                          $alias->save();
                      }
                  }
                  
                  if ($this->hasGallery) {
                      $model->saveImages($post);
                  }
                  
                  return $this->controller->redirect([ 'index' ]);
              }
              
              return $this->controller->render(
                  $this->viewPath,
                  [
                      'action'    => $this,
                      'model'     => $model,
                      'languages' => $languages,
                  ]
              );
          }
      }