Blame view

common/behaviors/GalleryBehavior.php 1.76 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
  <?php
  namespace common\behaviors;
  
  use Exception;
  use yii\base\Behavior;
  use yii\db\ActiveRecord;
  
  class GalleryBehavior extends Behavior
  {
      /**
       * @var array Массив аттрибутов которые должны быть обработаны с HtmlPurifier
       */
      public $attributes = [];
  
  
      /** @var string название переменной в которой храниться галлерея */
      public $galleryRow = '';
  
      /** @var string сценарий */
      public $scenario = 'backend';
  
      /** @var string filePath */
      public $path = '';
  
      /** @var   */
      private $_galleryManagerNamespace = \common\components\GalleryManager::class;
  
      /** @var null gallery manager */
      private $_galleryManagerModel = null;
  
  
      public function events()
      {
          return [
              ActiveRecord::EVENT_BEFORE_INSERT => 'saveGallery',
              ActiveRecord::EVENT_BEFORE_UPDATE => 'saveGallery',
          ];
      }
  
      /**
       * Save seo model
       */
      public function saveGallery()
      {
          $model = $this->owner;
          if (isset($model->{$this->galleryRow})) {
              $galleryManager = $this->getGalleryManager();
              $model->{$this->galleryRow} = $galleryManager->getOptionsForSaving($model->{$this->galleryRow}, $this->path);
              if (! $model->isScenario($this->scenario)) {
                  throw new Exception(get_class($model) . '::' . $this->scenario . " scenario doesn't exist");
              }
          }
      }
  
      /**
       * Gallery manager
       * @return null
       */
      private function getGalleryManager()
      {
          if ($this->_galleryManagerModel === null) {
              $this->_galleryManagerModel = new $this->_galleryManagerNamespace();
          }
  
          return $this->_galleryManagerModel;
      }
  }