Commit 3bd3d96c17fff80262ada9d206cb523dcdb5dc34

Authored by Anastasia
1 parent 767d5673

commit before grand update

controllers/EventController.php
... ... @@ -4,6 +4,7 @@ namespace artweb\artbox\event\controllers;
4 4  
5 5  
6 6 use artweb\artbox\ecommerce\models\Product;
  7 +use artweb\artbox\ecommerce\models\ProductVariant;
7 8 use Yii;
8 9 use artweb\artbox\event\models\Event;
9 10 use artweb\artbox\event\models\EventSearch;
... ... @@ -82,7 +83,7 @@ class EventController extends Controller
82 83 {
83 84 $model = new Event();
84 85 $model->generateLangs();
85   - $model->productsIds = [];
  86 + $model->variantsIds = [];
86 87 if ($model->load(Yii::$app->request->post())) {
87 88 $model->loadLangs(\Yii::$app->request);
88 89 if ($model->save() && $model->transactionStatus) {
... ... @@ -94,10 +95,10 @@ class EventController extends Controller
94 95 }
95 96  
96 97 }else{
97   - $products = Product::find()
98   - ->where([ 'id' => \Yii::$app->request->post('productIds') ])
99   - ->all();
100   - $model->linkMany('products', $products);
  98 + $variants = ProductVariant::find()
  99 + ->where([ 'id' => \Yii::$app->request->post('variantsIds') ])
  100 + ->all();
  101 + $model->linkMany('variants', $variants);
101 102 }
102 103  
103 104 return $this->redirect([
... ... @@ -124,9 +125,10 @@ class EventController extends Controller
124 125 {
125 126 $model = $this->findModel($id);
126 127 $model->generateLangs();
127   - $model->productsIds = ArrayHelper::map($model->products,
128   - 'id',
129   - 'lang.title');
  128 +// $model->productsIds = ArrayHelper::map($model->products,
  129 +// 'id',
  130 +// 'lang.title');
  131 + $model->variantsIds = ArrayHelper::map($model->variants,'id', 'sku');
130 132 if ($model->load(Yii::$app->request->post())) {
131 133 $model->loadLangs(\Yii::$app->request);
132 134 if ($model->save() && $model->transactionStatus) {
... ... @@ -139,10 +141,10 @@ class EventController extends Controller
139 141 }
140 142  
141 143 }else{
142   - $products = Product::find()
143   - ->where([ 'id' => \Yii::$app->request->post('productIds') ])
  144 + $variants = ProductVariant::find()
  145 + ->where([ 'id' => \Yii::$app->request->post('variantsIds') ])
144 146 ->all();
145   - $model->linkMany('products', $products);
  147 + $model->linkMany('variants', $variants);
146 148 }
147 149  
148 150 return $this->redirect([
... ...
models/Event.php
1 1 <?php
2   -
3   -namespace artweb\artbox\event\models;
4   -
5   -use artweb\artbox\behaviors\ManyToManyBehavior;
6   -use artweb\artbox\behaviors\SaveImgBehavior;
7   -use artweb\artbox\ecommerce\models\Product;
8   -use artweb\artbox\ecommerce\models\ProductVariant;
9   -use artweb\artbox\language\behaviors\LanguageBehavior;
10   -use Yii;
11   -use yii\behaviors\TimestampBehavior;
12   -use yii\helpers\ArrayHelper;
13   -use yii\db\ActiveQuery;
14   -use yii\web\Request;
15   -
16   -/**
17   - * This is the model class for table "event".
18   - *
19   - * @property integer $id
20   - * @property string $image
21   - * @property integer $created_at
22   - * @property integer $updated_at
23   - * @property integer $end_at
24   - * @property integer $status
25   - * @property integer $is_sale
26   - * @property integer $is_event
27   - * @property integer $percent
28   - * @property integer $banner
29   - * @property integer $type
30   - * @property Product[] $products
31   - * * From language behavior *
32   - * @property EventLang $lang
33   - * @property EventLang[] $langs
34   - * @property EventLang $objectLang
35   - * @property string $ownerKey
36   - * @property string $langKey
37   - * @property EventLang[] $modelLangs
38   - * @property bool $transactionStatus
39   - * @method string getOwnerKey()
40   - * @method void setOwnerKey( string $value )
41   - * @method string getLangKey()
42   - * @method void setLangKey( string $value )
43   - * @method ActiveQuery getLangs()
44   - * @method ActiveQuery getLang( integer $language_id )
45   - * @method EventLang[] generateLangs()
46   - * @method void loadLangs( Request $request )
47   - * @method bool linkLangs()
48   - * @method bool saveLangs()
49   - * @method bool getTransactionStatus()
50   - * * End language behavior *
51   - * * From SaveImgBehavior
52   - * @property string|null $imageFile
53   - * @property string|null $imageUrl
54   - * @method string|null getImageFile( int $field )
55   - * @method string|null getImageUrl( int $field )
56   - * * End SaveImgBehavior
57   - *
58   - */
59   -class Event extends \yii\db\ActiveRecord
60   -{
61   - public $imageUpload;
62   - public $products_file;
63   - const ACTIVE = 1;
64   - const INACTIVE = 2;
65   - public $productsIds = array();
  2 +
  3 + namespace artweb\artbox\event\models;
  4 +
  5 + use artweb\artbox\behaviors\ManyToManyBehavior;
  6 + use artweb\artbox\behaviors\SaveImgBehavior;
  7 + use artweb\artbox\ecommerce\models\Product;
  8 + use artweb\artbox\ecommerce\models\ProductVariant;
  9 + use artweb\artbox\language\behaviors\LanguageBehavior;
  10 + use Yii;
  11 + use yii\behaviors\TimestampBehavior;
  12 + use yii\helpers\ArrayHelper;
  13 + use yii\db\ActiveQuery;
  14 + use yii\web\Request;
  15 +
66 16 /**
67   - * @inheritdoc
  17 + * This is the model class for table "event".
  18 + *
  19 + * @property integer $id
  20 + * @property string $image
  21 + * @property integer $created_at
  22 + * @property integer $updated_at
  23 + * @property integer $end_at
  24 + * @property integer $status
  25 + * @property integer $is_sale
  26 + * @property integer $is_event
  27 + * @property integer $percent
  28 + * @property integer $banner
  29 + * @property integer $type
  30 + * @property Product[] $products
  31 + * @property ProductVariant[] $variants;
  32 + * * From language behavior *
  33 + * @property EventLang $lang
  34 + * @property EventLang[] $langs
  35 + * @property EventLang $objectLang
  36 + * @property string $ownerKey
  37 + * @property string $langKey
  38 + * @property EventLang[] $modelLangs
  39 + * @property bool $transactionStatus
  40 + * @method string getOwnerKey()
  41 + * @method void setOwnerKey( string $value )
  42 + * @method string getLangKey()
  43 + * @method void setLangKey( string $value )
  44 + * @method ActiveQuery getLangs()
  45 + * @method ActiveQuery getLang( integer $language_id )
  46 + * @method EventLang[] generateLangs()
  47 + * @method void loadLangs( Request $request )
  48 + * @method bool linkLangs()
  49 + * @method bool saveLangs()
  50 + * @method bool getTransactionStatus()
  51 + * * End language behavior *
  52 + * * From SaveImgBehavior
  53 + * @property string|null $imageFile
  54 + * @property string|null $imageUrl
  55 + * @method string|null getImageFile( int $field )
  56 + * @method string|null getImageUrl( int $field )
  57 + * * End SaveImgBehavior
68 58 */
69   - public static function tableName()
  59 + class Event extends \yii\db\ActiveRecord
70 60 {
71   - return 'event';
72   - }
73   -
74   -
75   -
76   -
77   - /**
78   - * @inheritdoc
79   - */
80   - public function behaviors()
81   - {
82   - return [
83   - 'img'=>[
84   - 'class' => SaveImgBehavior::className(),
85   - 'fields' => [
86   - ['name'=>'image','directory' => 'event' ],
87   - ['name'=>'banner','directory' => 'event' ],
88   - ]
89   - ],
90   - TimestampBehavior::className(),
91   - 'language' => [
92   - 'class' => LanguageBehavior::className(),
93   - 'objectLang' => EventLang::className()
94   - ],
95   - [
96   - 'class' => ManyToManyBehavior::className(),
97   - ],
98   - ];
99   - }
100   -
101   -
102   - public function beforeSave($insert)
103   - {
104   - if (parent::beforeSave($insert)) {
105   -
106   - $this->end_at = !empty($this->end_at) ? (string)strtotime($this->end_at) : '';
107   - return true;
  61 + public $imageUpload;
  62 + public $products_file;
  63 + const ACTIVE = 1;
  64 + const INACTIVE = 2;
  65 + public $productsIds = [];
  66 + public $variantsIds = [];
  67 + /**
  68 + * @inheritdoc
  69 + */
  70 + public static function tableName()
  71 + {
  72 + return 'event';
108 73 }
109   - return false;
110   -
111   - }
112   -
113   - public function afterFind(){
114   - $this->end_at = !empty($this->end_at) ? date("Y-m-d", $this->end_at) : '';
115   - }
116   -
117   -
118   - /**
119   - * @inheritdoc
120   - */
121   - public function rules()
122   - {
123   - return [
124   - [['created_at', 'updated_at','percent','status','is_sale','is_event','percent' ], 'integer'],
125   - [['image', 'end_at','banner'], 'string', 'max' => 255],
126   - [['imageUpload','is_sale','is_event'], 'safe'],
127   - [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'],
128   - [['products_file'], 'file'],
129   - ];
130   - }
131   -
132   - /**
133   - * @inheritdoc
134   - */
135   - public function attributeLabels()
136   - {
137   - return [
138   - 'id' => Yii::t('app', 'ID ะฐะบั†ะธะธ'),
139   - 'name' => Yii::t('app', 'name'),
140   - 'alias' => Yii::t('app', 'alias'),
141   - 'body' => Yii::t('app', 'body'),
142   - 'image' => Yii::t('app', 'image'),
143   - 'meta_title' => Yii::t('app', 'meta_title'),
144   - 'description' => Yii::t('app', 'description'),
145   - 'h1' => Yii::t('app', 'h1'),
146   - 'seo_text' => Yii::t('app', 'seo_text'),
147   - 'created_at' => Yii::t('app', 'created_at'),
148   - 'updated_at' => Yii::t('app', 'updated_at'),
149   - 'end_at' => Yii::t('app', 'end_at'),
150   - 'status' => Yii::t('app', 'ะกั‚ะฐั‚ัƒั ะฐะบั†ะธะธ'),
151   - 'products_file' => Yii::t('app', 'ะ—ะฐะณั€ัƒะทะบะฐ ั„ะฐะนะปะฐ'),
152   - 'is_sale' => Yii::t('app', 'ะ ะฐัะฟั€ะพะดะฐะถะฐ'),
153   - 'percent' => Yii::t('app', 'ะŸั€ะพั†ะตะฝั‚'),
154   - 'is_event' => Yii::t('app', 'ะะบั†ะธั'),
155   - ];
156   - }
157   -
158   -
159   - public function isActive(){
160   - if($this->status){
161   -
162   - if(!empty($this->end_at) && (strtotime($this->end_at) <= strtotime(date("Y-m-d")))){
163   - return false;
  74 +
  75 + /**
  76 + * @inheritdoc
  77 + */
  78 + public function behaviors()
  79 + {
  80 + return [
  81 + 'img' => [
  82 + 'class' => SaveImgBehavior::className(),
  83 + 'fields' => [
  84 + [
  85 + 'name' => 'image',
  86 + 'directory' => 'event',
  87 + ],
  88 + [
  89 + 'name' => 'banner',
  90 + 'directory' => 'event',
  91 + ],
  92 + ],
  93 + ],
  94 + TimestampBehavior::className(),
  95 + 'language' => [
  96 + 'class' => LanguageBehavior::className(),
  97 + 'objectLang' => EventLang::className(),
  98 + ],
  99 + [
  100 + 'class' => ManyToManyBehavior::className(),
  101 + ],
  102 + ];
  103 + }
  104 +
  105 + public function beforeSave($insert)
  106 + {
  107 + if (parent::beforeSave($insert)) {
  108 +
  109 + $this->end_at = !empty($this->end_at) ? (string) strtotime($this->end_at) : '';
  110 + return true;
164 111 }
165   - return true;
  112 + return false;
  113 +
166 114 }
167   - return false;
168   - }
169   -
170   -
171   - public function getType(){
172   - if($this->is_event){
173   - return "promo";
174   - } else if($this->is_sale){
175   - return "sale";
176   - } else {
177   - return "promo";
  115 +
  116 + public function afterFind()
  117 + {
  118 + $this->end_at = !empty($this->end_at) ? date("Y-m-d", $this->end_at) : '';
178 119 }
179   - }
180   -
181   -
182   - public function goEvent($file) {
183   -
184   - set_time_limit(0);
185   -
186   -
187   - $handle = fopen($file, 'r');
188   -
189   -
190   - while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
191   - if(isset($data[0])){
192   - $product = ProductVariant::find()->where(['sku' => $data[0]])->joinWith('product')->one();
193   - if($product instanceof ProductVariant){
194   - $model= EventsToProducts::find()->where(['event_id' =>$this->id, 'product_id' => $product->product->id ])->one();
195   - if(!$model instanceof EventsToProducts){
196   - $model = new EventsToProducts;
197   - $model->event_id = $this->id;
198   - $model->product_id = $product->product->id;
199   - $model->save();
  120 +
  121 + /**
  122 + * @inheritdoc
  123 + */
  124 + public function rules()
  125 + {
  126 + return [
  127 + [
  128 + [
  129 + 'created_at',
  130 + 'updated_at',
  131 + 'percent',
  132 + 'status',
  133 + 'is_sale',
  134 + 'is_event',
  135 + 'percent',
  136 + ],
  137 + 'integer',
  138 + ],
  139 + [
  140 + [
  141 + 'image',
  142 + 'end_at',
  143 + 'banner',
  144 + ],
  145 + 'string',
  146 + 'max' => 255,
  147 + ],
  148 + [
  149 + [
  150 + 'imageUpload',
  151 + 'is_sale',
  152 + 'is_event',
  153 + ],
  154 + 'safe',
  155 + ],
  156 + [
  157 + [ 'imageUpload' ],
  158 + 'file',
  159 + 'extensions' => 'jpg, gif, png',
  160 + ],
  161 + [
  162 + [ 'products_file' ],
  163 + 'file',
  164 + ],
  165 + ];
  166 + }
  167 +
  168 + /**
  169 + * @inheritdoc
  170 + */
  171 + public function attributeLabels()
  172 + {
  173 + return [
  174 + 'id' => Yii::t('app', 'ID ะฐะบั†ะธะธ'),
  175 + 'name' => Yii::t('app', 'name'),
  176 + 'alias' => Yii::t('app', 'alias'),
  177 + 'body' => Yii::t('app', 'body'),
  178 + 'image' => Yii::t('app', 'image'),
  179 + 'meta_title' => Yii::t('app', 'meta_title'),
  180 + 'description' => Yii::t('app', 'description'),
  181 + 'h1' => Yii::t('app', 'h1'),
  182 + 'seo_text' => Yii::t('app', 'seo_text'),
  183 + 'created_at' => Yii::t('app', 'created_at'),
  184 + 'updated_at' => Yii::t('app', 'updated_at'),
  185 + 'end_at' => Yii::t('app', 'end_at'),
  186 + 'status' => Yii::t('app', 'ะกั‚ะฐั‚ัƒั ะฐะบั†ะธะธ'),
  187 + 'products_file' => Yii::t('app', 'ะ—ะฐะณั€ัƒะทะบะฐ ั„ะฐะนะปะฐ'),
  188 + 'is_sale' => Yii::t('app', 'ะ ะฐัะฟั€ะพะดะฐะถะฐ'),
  189 + 'percent' => Yii::t('app', 'ะŸั€ะพั†ะตะฝั‚'),
  190 + 'is_event' => Yii::t('app', 'ะะบั†ะธั'),
  191 + ];
  192 + }
  193 +
  194 + public function isActive()
  195 + {
  196 + if ($this->status) {
  197 +
  198 + if (!empty($this->end_at) && ( strtotime($this->end_at) <= strtotime(date("Y-m-d")) )) {
  199 + return false;
  200 + }
  201 + return true;
  202 + }
  203 + return false;
  204 + }
  205 +
  206 + public function getType()
  207 + {
  208 + if ($this->is_event) {
  209 + return "promo";
  210 + } else if ($this->is_sale) {
  211 + return "sale";
  212 + } else {
  213 + return "promo";
  214 + }
  215 + }
  216 +
  217 +// public function goEvent($file)
  218 +// {
  219 +//
  220 +// set_time_limit(0);
  221 +//
  222 +// $handle = fopen($file, 'r');
  223 +//
  224 +// while (( $data = fgetcsv($handle, 1000, ";") ) !== false) {
  225 +// if (isset($data[ 0 ])) {
  226 +// $product = ProductVariant::find()
  227 +// ->where([ 'sku' => $data[ 0 ] ])
  228 +// ->joinWith('product')
  229 +// ->one();
  230 +// if ($product instanceof ProductVariant) {
  231 +// $model = EventsToProducts::find()
  232 +// ->where(
  233 +// [
  234 +// 'event_id' => $this->id,
  235 +// 'product_id' => $product->product->id,
  236 +// ]
  237 +// )
  238 +// ->one();
  239 +// if (!$model instanceof EventsToProducts) {
  240 +// $model = new EventsToProducts;
  241 +// $model->event_id = $this->id;
  242 +// $model->product_id = $product->product->id;
  243 +// $model->save();
  244 +// }
  245 +// }
  246 +// }
  247 +//
  248 +// }
  249 +// fclose($handle);
  250 +// unlink($file);
  251 +//
  252 +// }
  253 +
  254 + public function goEvent($file)
  255 + {
  256 +
  257 + set_time_limit(0);
  258 +
  259 + $handle = fopen($file, 'r');
  260 +
  261 + while (( $data = fgetcsv($handle, 1000, ";") ) !== false) {
  262 + if (isset($data[ 0 ])) {
  263 + $product = ProductVariant::find()
  264 + ->where([ 'sku' => $data[ 0 ] ])
  265 + ->one();
  266 + if ($product instanceof ProductVariant) {
  267 + $model = EventToVariant::find()
  268 + ->where(
  269 + [
  270 + 'event_id' => $this->id,
  271 + 'variant_id' => $product->id,
  272 + ]
  273 + )
  274 + ->one();
  275 + if (!$model instanceof EventToVariant) {
  276 + $model = new EventToVariant();
  277 + $model->event_id = $this->id;
  278 + $model->variant_id = $product->id;
  279 + $model->save();
  280 + }
200 281 }
201 282 }
  283 +
202 284 }
203   -
  285 + fclose($handle);
  286 + unlink($file);
  287 +
204 288 }
205   - fclose($handle);
206   - unlink($file);
207   -
208   - }
209   -
210   - public function getProducts(){
211   - return $this->hasMany(Product::className(),['id' => 'product_id'] )->viaTable('events_to_products', ['event_id' => 'id']);
212   - }
213   -
214   - public static function getSaleEvents(){
215   - return ArrayHelper::toArray(self::find()->select('percent')->distinct('percent')->where('is_sale=true AND percent IS NOT NULL')->orderBy('percent')->all());
  289 +
  290 + public function getProducts()
  291 + {
  292 + return $this->hasMany(Product::className(), [ 'id' => 'product_id' ])
  293 + ->viaTable('events_to_products', [ 'event_id' => 'id' ]);
  294 + }
  295 +
  296 + public function getVariants()
  297 + {
  298 + return $this->hasMany(ProductVariant::className(), [ 'id' => 'variant_id' ])
  299 + ->viaTable('event_to_variant', [ 'event_id' => 'id' ]);
  300 + }
  301 +
  302 + public static function getSaleEvents()
  303 + {
  304 + return ArrayHelper::toArray(
  305 + self::find()
  306 + ->select('percent')
  307 + ->distinct('percent')
  308 + ->where('is_sale=true AND percent IS NOT NULL')
  309 + ->orderBy('percent')
  310 + ->all()
  311 + );
  312 + }
  313 +
216 314 }
217   -
218   -}
... ...
models/EventToVariant.php 0 โ†’ 100644
  1 +<?php
  2 +
  3 +namespace artweb\artbox\event\models;
  4 +
  5 +use artweb\artbox\ecommerce\models\ProductVariant;
  6 +use Yii;
  7 +
  8 +/**
  9 + * This is the model class for table "event_to_variant".
  10 + *
  11 + * @property integer $id
  12 + * @property integer $event_id
  13 + * @property integer $variant_id
  14 + *
  15 + * @property Event $event
  16 + * @property ProductVariant $variant
  17 + */
  18 +class EventToVariant extends \yii\db\ActiveRecord
  19 +{
  20 + /**
  21 + * @inheritdoc
  22 + */
  23 + public static function tableName()
  24 + {
  25 + return 'event_to_variant';
  26 + }
  27 +
  28 + /**
  29 + * @inheritdoc
  30 + */
  31 + public function rules()
  32 + {
  33 + return [
  34 + [['event_id', 'variant_id'], 'integer'],
  35 + [['event_id'], 'exist', 'skipOnError' => true, 'targetClass' => Event::className(), 'targetAttribute' => ['event_id' => 'id']],
  36 + [['variant_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductVariant::className(), 'targetAttribute' => ['variant_id' => 'id']],
  37 + ];
  38 + }
  39 +
  40 + /**
  41 + * @inheritdoc
  42 + */
  43 + public function attributeLabels()
  44 + {
  45 + return [
  46 + 'id' => Yii::t('app', 'ID'),
  47 + 'event_id' => Yii::t('app', 'Event ID'),
  48 + 'variant_id' => Yii::t('app', 'Variant ID'),
  49 + ];
  50 + }
  51 +
  52 + /**
  53 + * @return \yii\db\ActiveQuery
  54 + */
  55 + public function getEvent()
  56 + {
  57 + return $this->hasOne(Event::className(), ['id' => 'event_id']);
  58 + }
  59 +
  60 + /**
  61 + * @return \yii\db\ActiveQuery
  62 + */
  63 + public function getVariant()
  64 + {
  65 + return $this->hasOne(ProductVariant::className(), ['id' => 'variant_id']);
  66 + }
  67 +}
... ...
views/event/_form.php
... ... @@ -116,13 +116,13 @@ use yii\helpers\Html;
116 116 <?php $condition = '';
117 117 echo Select2::widget(
118 118 [
119   - 'name' => 'productIds',
  119 + 'name' => 'variantsIds',
120 120 'options' => [
121   - 'placeholder' => \Yii::t('app', 'Search for products ...'),
  121 + 'placeholder' => \Yii::t('app', 'Search for variant ...'),
122 122 'multiple' => true,
123 123 ],
124   - 'value' => array_keys($model->productsIds),
125   - 'data' => $model->productsIds,
  124 + 'value' => array_keys($model->variantsIds),
  125 + 'data' => $model->variantsIds,
126 126 'pluginOptions' => [
127 127 'allowClear' => true,
128 128 'minimumInputLength' => 3,
... ... @@ -132,7 +132,7 @@ use yii\helpers\Html;
132 132 ),
133 133 ],
134 134 'ajax' => [
135   - 'url' => Url::to([ '/ecommerce/manage/list' ]),
  135 + 'url' => Url::to([ '/ecommerce/variant/list' ]),
136 136 'dataType' => 'json',
137 137 'data' => new JsExpression(
138 138 'function(params) {
... ...