Blame view

thread/actions/ManyToManyCreate.php 2.68 KB
d1f8bd40   Alexey Boroda   first commit
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
98
99
100
101
102
103
104
105
106
107
108
  <?php
  namespace thread\actions;
  
  use Yii;
  use yii\base\Exception;
  use yii\log\Logger;
  //
  use thread\app\base\models\ActiveRecord;
  
  /**
   * Class Create
   *
   * @package thread\actions
   * @author FilamentV <vortex.filament@gmail.com>
   * @copyright (c) 2016, VipDesign
   * @usage
   * public function actions()
   * {
   *      return [
   *          ...
   *          'create' => [
   *              'class' => Create::class,
   *              'modelClass' => Model::class,
   *          ],
   *          ...
   *      ];
   * }
   *
   */
  class ManyToManyCreate extends ActionCRUD
  {
      /** @var null nameSpace  */
      private $namespane = null;
  
      private $className = null;
  
      /**
       * Init action
       *
       * @inheritdoc
       * @throws Exception
       */
      public function init()
      {
          if ($this->modelClass === null) {
              throw new Exception(__CLASS__ . '::$modelClass must be set.');
          }
          /** @var ActiveRecord $this ->model */
          $this->model = new $this->modelClass;
          $this->model->loadDefaultValues();
          if ($this->model === null) {
              throw new Exception($this->modelClass . 'must be exists.');
          }
          if (!$this->model->isScenario($this->scenario)) {
              throw new Exception($this->modelClass . '::' . $this->scenario . " scenario doesn't exist");
          }
      }
  
      /**
       * Run action
       *
       * @inheritdoc
       * @inheritdoc
       * @return mixed
       */
      public function run()
      {
          if (Yii::$app->getRequest()->isAjax) {
              return $this->controller->renderPartial($this->view, [
                  'model' => $this->model,
              ]);
          } else {
              if ($this->saveModel()) {
                  return $this->controller->redirect($this->getRedirect());
              } else {
                  $this->controller->layout = $this->layout;
                  return $this->controller->render($this->view, [
                      'model' => $this->model,
                  ]);
              }
          }
      }
  
      /**
       * Save data to model
       * Model Scenario 'backend' should be set
       *
       * @return bool
       */
      public function saveModel()
      {
          $save = false;
          $this->model->setScenario($this->scenario);
          if ($this->model->load(Yii::$app->getRequest()->post())) {
              $model = $this->model;
              $transaction = $model::getDb()->beginTransaction();
              try {
                  $save = $this->model->save();
  
                  ($save) ? $transaction->commit() : $transaction->rollBack();
              } catch (Exception $e) {
                  Yii::getLogger()->log($e->getMessage(), Logger::LEVEL_ERROR);
                  $transaction->rollBack();
              }
          }
          return $save;
      }
  }