Blame view

models/TaxGroup.php 7.28 KB
2f25da09   Yarik   first commit
1
2
3
4
  <?php

      

      namespace artweb\artbox\ecommerce\models;

      

8a7e6ecf   Yarik   Namespaces
5
6
      use artweb\artbox\language\behaviors\LanguageBehavior;

      use artweb\artbox\language\models\Language;

2f25da09   Yarik   first commit
7
8
9
10
11
12
13
14
15
16
17
18
      use yii\base\InvalidValueException;

      use yii\db\ActiveQuery;

      use yii\db\ActiveRecord;

      use yii\web\Request;

      

      /**

       * This is the model class for table "{{%tax_group}}".

       *

       * @property integer        $id

       * @property boolean        $is_filter

       * @property integer        $level

       * @property integer        $sort

a1e889f7   Administrator   add variantSku
19
20
       * @property integer        $use_in_name

       * @property string         $meta_robots

2f25da09   Yarik   first commit
21
22
23
24
25
26
27
28
       * @property boolean        $display

       * @property boolean        $is_menu

       * @property string         $remote_id

       * @property TaxOption[]    $taxOptions

       * @property Category[]     $categories

       * @property TaxOption[]    $options

       * @property TaxOption[]    $customOptions

       * @property string         $alias

2b315849   Alexey Boroda   -Product card (wi...
29
       * @property integer        $position

2f25da09   Yarik   first commit
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
       * * From language behavior *

       * @property TaxGroupLang   $lang

       * @property TaxGroupLang[] $langs

       * @property TaxGroupLang   $objectLang

       * @property string         $ownerKey

       * @property string         $langKey

       * @property TaxGroupLang[] $modelLangs

       * @property bool           $transactionStatus

       * @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 TaxGroupLang[]    generateLangs()

       * @method void             loadLangs( Request $request )

       * @method bool             linkLangs()

       * @method bool             saveLangs()

       * @method bool             getTransactionStatus()

       * * End language behavior *

       */

      class TaxGroup extends ActiveRecord

      {

          

          const GROUP_PRODUCT = 0;

          const GROUP_VARIANT = 1;

          

          /**

           * @var TaxOption[] $options

           */

          protected $customOptions = [];

bd98f6d3   Administrator   full commit
61
          protected $categories = [];

2f25da09   Yarik   first commit
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
          

          /**

           * @inheritdoc

           */

          public function behaviors()

          {

              return [

                  'language' => [

                      'class' => LanguageBehavior::className(),

                  ],

              ];

          }

          

          /**

           * @inheritdoc

           */

          public static function tableName()

          {

              return 'tax_group';

          }

          

          /**

           * @inheritdoc

           */

          public function rules()

          {

              return [

                  [

                      [

                          'is_filter',

                          'display',

                          'is_menu',

                      ],

                      'boolean',

                  ],

                  [

                      [

                          'level',

                          'sort',

2b315849   Alexey Boroda   -Product card (wi...
101
                          'position',

a1e889f7   Administrator   add variantSku
102
                          'use_in_name',

2f25da09   Yarik   first commit
103
104
105
106
                      ],

                      'integer',

                  ],

                  [

a1e889f7   Administrator   add variantSku
107
108
109
110
                      ['meta_robots'],

                      'string'

                  ],

                  [

2f25da09   Yarik   first commit
111
112
113
114
115
116
117
118
119
120
121
122
                      [ 'categories' ],

                      'safe',

                  ],

              ];

          }

          

          /**

           * @inheritdoc

           */

          public function attributeLabels()

          {

              return [

a1e889f7   Administrator   add variantSku
123
124
125
126
127
128
129
130
131
                  'id'          => 'Tax Group ID',

                  'is_filter'   => 'Use in filter',

                  'sort'        => 'Sort',

                  'display'     => 'Display',

                  'is_menu'     => 'Отображать в характеристиках',

                  'level'       => 'уровень',

                  'position'    => 'Позиция',

                  'meta_robots' => 'Meta Robots',

                  'use_in_name' => 'Использовать в названии',

2f25da09   Yarik   first commit
132
133
134
135
136
137
138
139
140
              ];

          }

          

          /**

           * @return ActiveQuery

           */

          public function getCategories()

          {

              return $this->hasMany(Category::className(), [ 'id' => 'category_id' ])

89e1e18c   Administrator   add variantSku
141
                          ->viaTable('tax_group_to_category', [ 'tax_group_id' => 'id' ]);

2f25da09   Yarik   first commit
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
          }

          

          /**

           * Set categories to override tax_group_to_category table data

           *

           * @param int[] $values

           */

          public function setCategories(array $values)

          {

              $this->categories = $values;

          }

          

          /**

           * @inheritdoc

           */

          public function afterSave($insert, $changedAttributes)

          {

              parent::afterSave($insert, $changedAttributes);

              $this->unlinkAll('categories', true);

              $categories = [];

              if (!empty( $this->categories )) {

                  $categories = Category::findAll($this->categories);

              }

              foreach ($categories as $category) {

                  $this->link('categories', $category);

              }

          }

          

          /**

           * @return ActiveQuery

           */

          public function getTaxOptions()

          {

89e1e18c   Administrator   add variantSku
175
              return $this->hasMany(TaxOption::className(), [ 'tax_group_id' => 'id' ]);

2f25da09   Yarik   first commit
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
          }

          

          /**

           * Synonim for getTaxOptions()

           *

           * @see TaxGroup::getTaxOptions()

           * @return \yii\db\ActiveQuery

           */

          public function getOptions()

          {

              return $this->getTaxOptions();

          }

          

          /**

           * Get customOptins that were filled dynamically.

           * If $fillDefault is true then fill $customOptions with TaxOptions for current TaxGroup

           *

           * @param bool $fillDefault

           *

           * @return TaxOption[]

           */

          public function getCustomOptions(bool $fillDefault = false): array

          {

              if ($fillDefault && empty( $this->custom_options )) {

                  $this->customOptions = $this->getTaxOptions()

                                              ->with('lang')

                                              ->all();

              }

              return $this->customOptions;

          }

          

          /**

           * Set customOptions

           *

           * @param TaxOption[] $value

           */

          public function setCustomOptions(array $value)

          {

              foreach ($value as $item) {

                  if (!( $item instanceof TaxOption )) {

                      throw new InvalidValueException('All elements must be instances of ' . TaxOption::className());

                  }

              }

              $this->customOptions = $value;

          }

          

          /**

           * Get default lang alias

           *

           * @return string

           */

          public function getAlias()

          {

              $default_lang = Language::getDefaultLanguage();

              /**

               * @var TaxGroupLang $lang

               */

              $lang = $this->getLang($default_lang->id)

                           ->one();

              return $lang->alias;

          }

      }