Blame view

common/models/Project.php 5.9 KB
36d1807a   Yarik   Big commit.
1
  <?php
36d1807a   Yarik   Big commit.
2
      
cc658b4c   Yarik   Big commit
3
      namespace common\models;
36d1807a   Yarik   Big commit.
4
      
d55d2fe0   Yarik   Multilanguage
5
      use common\modules\language\behaviors\LanguageBehavior;
cc658b4c   Yarik   Big commit
6
      use common\modules\product\models\ProductVariant;
d55d2fe0   Yarik   Multilanguage
7
8
9
      use yii\db\ActiveQuery;
      use yii\db\ActiveRecord;
      use yii\web\Request;
36d1807a   Yarik   Big commit.
10
11
      
      /**
cc658b4c   Yarik   Big commit
12
13
       * This is the model class for table "project".
       * @property integer            $project_id
cc658b4c   Yarik   Big commit
14
15
16
17
18
19
       * @property integer            $date_add
       * @property ProjectImage[]     $images
       * @property ProjectImage       $image
       * @property array              $imagesConfig
       * @property ProductVariant[]   $variants
       * @property ProductToProject[] $productToProject
d55d2fe0   Yarik   Multilanguage
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
       * * From language behavior *
       * @property ProjectLang        $lang
       * @property ProjectLang[]      $langs
       * @property ProjectLang        $object_lang
       * @property string             $ownerKey
       * @property string             $langKey
       * @method string           getOwnerKey()
       * @method void             setOwnerKey( string $value )
       * @method string           getLangKey()
       * @method void             setLangKey( string $value )
       * @method ActiveQuery      getLangs()
       * @method ActiveQuery      getLang( integer $language_id )
       * @method ProjectLang[]    generateLangs()
       * @method void             loadLangs( Request $request, ActiveRecord[] $model_langs )
       * @method bool             linkLangs( ActiveRecord[] $model_langs )
       * @method bool             saveLangs( ActiveRecord[] $model_langs )
       * * End language behavior *
36d1807a   Yarik   Big commit.
37
       */
cc658b4c   Yarik   Big commit
38
      class Project extends \yii\db\ActiveRecord
36d1807a   Yarik   Big commit.
39
      {
cc658b4c   Yarik   Big commit
40
41
42
43
44
45
46
47
48
          
          public $imagesUpload = [];
          
          /**
           * @inheritdoc
           */
          public static function tableName()
          {
              return 'project';
36d1807a   Yarik   Big commit.
49
          }
cc658b4c   Yarik   Big commit
50
51
52
53
54
55
56
          
          /**
           * @inheritdoc
           */
          public function behaviors()
          {
              return [
d55d2fe0   Yarik   Multilanguage
57
58
59
                  'language' => [
                      'class' => LanguageBehavior::className(),
                  ],
cc658b4c   Yarik   Big commit
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
              ];
          }
          
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [ 'date_add' ],
                      'default',
                      'value' => function() {
                          return time();
                      },
                  ],
                  [
cc658b4c   Yarik   Big commit
77
78
79
                      [
                          'date_add',
                          'imagesUpload',
36d1807a   Yarik   Big commit.
80
                      ],
cc658b4c   Yarik   Big commit
81
82
83
                      'safe',
                  ],
                  [
cc658b4c   Yarik   Big commit
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
                      [ 'date_add' ],
                      'filter',
                      'filter' => function($value) {
                          return strtotime($value) ? : time();
                      },
                  ],
              ];
          }
          
          /**
           * @inheritdoc
           */
          public function attributeLabels()
          {
              return [
                  'project_id'  => 'Project ID',
cc658b4c   Yarik   Big commit
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
                  'description' => 'Description',
                  'date_add'    => 'Date Add',
                  'images'      => 'Images',
              ];
          }
          
          public function getImages()
          {
              return $this->hasMany(ProjectImage::className(), [ 'project_id' => 'project_id' ]);
          }
          
          public function getImage()
          {
              return $this->hasOne(ProjectImage::className(), [ 'project_id' => 'project_id' ]);
          }
          
          public function getImagesHTML()
          {
              $op = [];
              if($this->images) {
                  foreach($this->images as $image) {
                      $op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb');
                  }
36d1807a   Yarik   Big commit.
123
              }
cc658b4c   Yarik   Big commit
124
              return $op;
36d1807a   Yarik   Big commit.
125
          }
cc658b4c   Yarik   Big commit
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
          
          public function getImagesConfig()
          {
              $op = [];
              if($this->images) {
                  foreach($this->images as $image) {
                      $op[] = [
                          'caption' => $image->image,
                          'width'   => '120px',
                          'url'     => \yii\helpers\Url::to([
                              '/project/delimg',
                              'id' => $image->project_image_id,
                          ]),
                          'key'     => $image->project_image_id,
                          'extra'   => [
                              'id' => $image->project_image_id,
                          ],
                      ];
36d1807a   Yarik   Big commit.
144
                  }
cc658b4c   Yarik   Big commit
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
              }
              return $op;
          }
          
          public function imagesUpload()
          {
              if($this->validate()) {
                  $images = [];
                  foreach($this->imagesUpload as $image) {
                      $imageName = $image->baseName . '.' . $image->extension;
                      $i = 0;
                      while(file_exists(\Yii::getAlias('@storage/projects/' . $imageName))) {
                          $i++;
                          $imageName = $image->baseName . '_' . $i . '.' . $image->extension;
                      }
                      $imgDir = \Yii::getAlias('@storage/projects/');
                      if(!is_dir($imgDir)) {
                          mkdir($imgDir, 0755, true);
                      }
                      
                      $image->saveAs($imgDir . $imageName);
                      $images[] = $imageName;
36d1807a   Yarik   Big commit.
167
                  }
cc658b4c   Yarik   Big commit
168
169
170
                  return $images;
              } else {
                  return false;
36d1807a   Yarik   Big commit.
171
              }
cc658b4c   Yarik   Big commit
172
173
174
175
176
177
178
179
180
181
182
183
184
          }
          
          public function getProductToProject()
          {
              return $this->hasMany(ProductToProject::className(), [
                  'project_id' => 'project_id',
              ]);
          }
          
          public function getVariants()
          {
              return $this->hasMany(ProductVariant::className(), [ 'product_variant_id' => 'product_variant_id' ])
                          ->viaTable('product_to_project', [ 'project_id' => 'project_id' ]);
36d1807a   Yarik   Big commit.
185
186
          }
      }