Blame view

common/modules/blog/models/ArticleCategoryMedia.php 5.59 KB
a8370482   Alexander Karnovsky   init project
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  <?php
  
  namespace common\modules\blog\models;
  
  use common\models\Media;
  use Yii;
  
  /**
   * This is the model class for table "article_category_media".
   *
   * @property integer $article_category_media_id
   * @property integer $article_category_id
   * @property integer $media_id
   * @property string $media_alt
   * @property string $media_title
   * @property string $media_caption
   * @property string $type
   * @property string $language_id
   *
   * @property ArticleCategory $category
   * @property Media $media
   * @property Language $lang
   */
  class ArticleCategoryMedia extends \yii\db\ActiveRecord
  {
      const SCENARIO_FULL = 'full';
      const SCENARIO_PREVIEW = 'preview';
      const SCENARIO_ADDITIONAL = 'additional';
      public $imageFile;
      /**
       * @inheritdoc
       */
      public static function tableName()
      {
          return 'article_category_media';
      }
  
      public function scenarios()
      {
          $scenarios = parent::scenarios();
          $scenarios[self::SCENARIO_FULL] = ['article_category_media_id', 'article_category_id', 'media_id', 'type', 'media_alt', 'media_title', 'media_caption', 'imageFile'];
          $scenarios[self::SCENARIO_PREVIEW] = ['article_category_media_id', 'article_category_id', 'media_id', 'type', 'media_alt', 'media_title', 'media_caption', 'imageFile'];
          $scenarios[self::SCENARIO_ADDITIONAL] = ['article_category_media_id', 'article_category_id', 'media_id', 'type', 'imageFile'];
          return $scenarios;
      }
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [['article_category_id', 'media_id'], 'required'],
              [['article_category_id', 'media_id'], 'integer'],
              [['media_alt', 'media_title', 'media_caption'], 'string'],
              [['type'], 'string', 'max' => 10],
              [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'on' => self::SCENARIO_FULL],
              [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'on' => self::SCENARIO_PREVIEW],
              [['imageFile'], 'file', 'extensions' => 'png, gif, jpg, jpeg', 'skipOnEmpty' => true, 'maxFiles' => 10, 'on' => self::SCENARIO_ADDITIONAL]
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
              'article_category_media_id' => Yii::t('app', 'ID'),
              'article_category_id' => Yii::t('app', 'Category ID'),
              'media_id' => Yii::t('app', 'Media ID'),
              'media_alt' => Yii::t('app', 'Media Alt'),
              'media_title' => Yii::t('app', 'Media Title'),
              'media_caption' => Yii::t('app', 'Media Caption'),
              'type' => Yii::t('app', 'Type'),
              'imageFile' => Yii::t('app', 'Image File'),
              'language_id' => Yii::t('app', 'Language ID'),
          ];
      }
  
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getCategory()
      {
          return $this->hasOne(ArticleCategory::className(), ['article_category_id' => 'article_category_id']);
      }
  
      /**
       * @return \yii\db\ActiveQuery
       */
      public function getMedia()
      {
          return $this->hasOne(Media::className(), ['media_id' => 'media_id']);
      }
  
      public function upload($category_id)
      {
          $this->article_category_id = $category_id;
          if(is_array($this->imageFile)) {
              $ok = true;
              foreach($this->imageFile as $image) {
                  $media_category = clone $this;
                  $media = new Media();
                  $media->imageFile = $image;
                  $media->upload();
                  $media_category->media_id = $media->media_id;
                  $ok = $media_category->save() && $ok;
                  unset($media_category);
              }
              return $ok;
          } elseif(!empty($this->imageFile)) {
              $media = new Media();
              $media->imageFile = $this->imageFile;
              $media->upload();
              $this->media_id = $media->media_id;
              return $this->save();
          }
      }
  
      public function replace($category_id, $removeMedia = false)
      {
          $this->article_category_id = $category_id;
          if($removeMedia) {
              $category_media = ArticleCategoryMedia::find()->select('media_id')->where(['article_category_id' => $this->article_category_id, 'type' => $this->type])->column();
              $media = array();
              foreach($category_media as $media_id) {
                  $media[] = Media::findOne(['media_id' => $media_id]);
              }
              $media = array_unique($media);
              foreach($media as $one_media) {
                  if($one_media instanceof Media) {
                      $one_media->delete();
                  }
              }
              unset($media);
              unset($category_media);
          }
          if(is_array($this->imageFile)) {
              $ok = true;
              foreach($this->imageFile as $image) {
                  $media_category = clone $this;
                  $media = new Media();
                  $media->imageFile = $image;
                  $media->upload();
                  $media_category->media_id = $media->media_id;
                  $ok = $media_category->save() && $ok;
                  unset($media_category);
              }
              return $ok;
          } elseif(!empty($this->imageFile)) {
              ArticleCategoryMedia::deleteAll(['category_id' => $this->article_category_id, 'type' => $this->type]);
              $media = new Media();
              $media->imageFile = $this->imageFile;
              $media->upload();
              $this->media_id = $media->media_id;
              $this->setIsNewRecord(true);
              return $this->save();
          }
      }
  
  }