diff --git a/controllers/EventController.php b/controllers/EventController.php index 55fef90..155dd40 100755 --- a/controllers/EventController.php +++ b/controllers/EventController.php @@ -4,6 +4,7 @@ namespace artweb\artbox\event\controllers; use artweb\artbox\ecommerce\models\Product; +use artweb\artbox\ecommerce\models\ProductVariant; use Yii; use artweb\artbox\event\models\Event; use artweb\artbox\event\models\EventSearch; @@ -82,7 +83,7 @@ class EventController extends Controller { $model = new Event(); $model->generateLangs(); - $model->productsIds = []; + $model->variantsIds = []; if ($model->load(Yii::$app->request->post())) { $model->loadLangs(\Yii::$app->request); if ($model->save() && $model->transactionStatus) { @@ -94,10 +95,10 @@ class EventController extends Controller } }else{ - $products = Product::find() - ->where([ 'id' => \Yii::$app->request->post('productIds') ]) - ->all(); - $model->linkMany('products', $products); + $variants = ProductVariant::find() + ->where([ 'id' => \Yii::$app->request->post('variantsIds') ]) + ->all(); + $model->linkMany('variants', $variants); } return $this->redirect([ @@ -124,9 +125,10 @@ class EventController extends Controller { $model = $this->findModel($id); $model->generateLangs(); - $model->productsIds = ArrayHelper::map($model->products, - 'id', - 'lang.title'); +// $model->productsIds = ArrayHelper::map($model->products, +// 'id', +// 'lang.title'); + $model->variantsIds = ArrayHelper::map($model->variants,'id', 'sku'); if ($model->load(Yii::$app->request->post())) { $model->loadLangs(\Yii::$app->request); if ($model->save() && $model->transactionStatus) { @@ -139,10 +141,10 @@ class EventController extends Controller } }else{ - $products = Product::find() - ->where([ 'id' => \Yii::$app->request->post('productIds') ]) + $variants = ProductVariant::find() + ->where([ 'id' => \Yii::$app->request->post('variantsIds') ]) ->all(); - $model->linkMany('products', $products); + $model->linkMany('variants', $variants); } return $this->redirect([ diff --git a/models/Event.php b/models/Event.php index a02a047..adbfa90 100755 --- a/models/Event.php +++ b/models/Event.php @@ -1,218 +1,314 @@ [ - 'class' => SaveImgBehavior::className(), - 'fields' => [ - ['name'=>'image','directory' => 'event' ], - ['name'=>'banner','directory' => 'event' ], - ] - ], - TimestampBehavior::className(), - 'language' => [ - 'class' => LanguageBehavior::className(), - 'objectLang' => EventLang::className() - ], - [ - 'class' => ManyToManyBehavior::className(), - ], - ]; - } - - - public function beforeSave($insert) - { - if (parent::beforeSave($insert)) { - - $this->end_at = !empty($this->end_at) ? (string)strtotime($this->end_at) : ''; - return true; + public $imageUpload; + public $products_file; + const ACTIVE = 1; + const INACTIVE = 2; + public $productsIds = []; + public $variantsIds = []; + /** + * @inheritdoc + */ + public static function tableName() + { + return 'event'; } - return false; - - } - - public function afterFind(){ - $this->end_at = !empty($this->end_at) ? date("Y-m-d", $this->end_at) : ''; - } - - - /** - * @inheritdoc - */ - public function rules() - { - return [ - [['created_at', 'updated_at','percent','status','is_sale','is_event','percent' ], 'integer'], - [['image', 'end_at','banner'], 'string', 'max' => 255], - [['imageUpload','is_sale','is_event'], 'safe'], - [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'], - [['products_file'], 'file'], - ]; - } - - /** - * @inheritdoc - */ - public function attributeLabels() - { - return [ - 'id' => Yii::t('app', 'ID акции'), - 'name' => Yii::t('app', 'name'), - 'alias' => Yii::t('app', 'alias'), - 'body' => Yii::t('app', 'body'), - 'image' => Yii::t('app', 'image'), - 'meta_title' => Yii::t('app', 'meta_title'), - 'description' => Yii::t('app', 'description'), - 'h1' => Yii::t('app', 'h1'), - 'seo_text' => Yii::t('app', 'seo_text'), - 'created_at' => Yii::t('app', 'created_at'), - 'updated_at' => Yii::t('app', 'updated_at'), - 'end_at' => Yii::t('app', 'end_at'), - 'status' => Yii::t('app', 'Статус акции'), - 'products_file' => Yii::t('app', 'Загрузка файла'), - 'is_sale' => Yii::t('app', 'Распродажа'), - 'percent' => Yii::t('app', 'Процент'), - 'is_event' => Yii::t('app', 'Акция'), - ]; - } - - - public function isActive(){ - if($this->status){ - - if(!empty($this->end_at) && (strtotime($this->end_at) <= strtotime(date("Y-m-d")))){ - return false; + + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + 'img' => [ + 'class' => SaveImgBehavior::className(), + 'fields' => [ + [ + 'name' => 'image', + 'directory' => 'event', + ], + [ + 'name' => 'banner', + 'directory' => 'event', + ], + ], + ], + TimestampBehavior::className(), + 'language' => [ + 'class' => LanguageBehavior::className(), + 'objectLang' => EventLang::className(), + ], + [ + 'class' => ManyToManyBehavior::className(), + ], + ]; + } + + public function beforeSave($insert) + { + if (parent::beforeSave($insert)) { + + $this->end_at = !empty($this->end_at) ? (string) strtotime($this->end_at) : ''; + return true; } - return true; + return false; + } - return false; - } - - - public function getType(){ - if($this->is_event){ - return "promo"; - } else if($this->is_sale){ - return "sale"; - } else { - return "promo"; + + public function afterFind() + { + $this->end_at = !empty($this->end_at) ? date("Y-m-d", $this->end_at) : ''; } - } - - - public function goEvent($file) { - - set_time_limit(0); - - - $handle = fopen($file, 'r'); - - - while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { - if(isset($data[0])){ - $product = ProductVariant::find()->where(['sku' => $data[0]])->joinWith('product')->one(); - if($product instanceof ProductVariant){ - $model= EventsToProducts::find()->where(['event_id' =>$this->id, 'product_id' => $product->product->id ])->one(); - if(!$model instanceof EventsToProducts){ - $model = new EventsToProducts; - $model->event_id = $this->id; - $model->product_id = $product->product->id; - $model->save(); + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ + 'created_at', + 'updated_at', + 'percent', + 'status', + 'is_sale', + 'is_event', + 'percent', + ], + 'integer', + ], + [ + [ + 'image', + 'end_at', + 'banner', + ], + 'string', + 'max' => 255, + ], + [ + [ + 'imageUpload', + 'is_sale', + 'is_event', + ], + 'safe', + ], + [ + [ 'imageUpload' ], + 'file', + 'extensions' => 'jpg, gif, png', + ], + [ + [ 'products_file' ], + 'file', + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID акции'), + 'name' => Yii::t('app', 'name'), + 'alias' => Yii::t('app', 'alias'), + 'body' => Yii::t('app', 'body'), + 'image' => Yii::t('app', 'image'), + 'meta_title' => Yii::t('app', 'meta_title'), + 'description' => Yii::t('app', 'description'), + 'h1' => Yii::t('app', 'h1'), + 'seo_text' => Yii::t('app', 'seo_text'), + 'created_at' => Yii::t('app', 'created_at'), + 'updated_at' => Yii::t('app', 'updated_at'), + 'end_at' => Yii::t('app', 'end_at'), + 'status' => Yii::t('app', 'Статус акции'), + 'products_file' => Yii::t('app', 'Загрузка файла'), + 'is_sale' => Yii::t('app', 'Распродажа'), + 'percent' => Yii::t('app', 'Процент'), + 'is_event' => Yii::t('app', 'Акция'), + ]; + } + + public function isActive() + { + if ($this->status) { + + if (!empty($this->end_at) && ( strtotime($this->end_at) <= strtotime(date("Y-m-d")) )) { + return false; + } + return true; + } + return false; + } + + public function getType() + { + if ($this->is_event) { + return "promo"; + } else if ($this->is_sale) { + return "sale"; + } else { + return "promo"; + } + } + +// public function goEvent($file) +// { +// +// set_time_limit(0); +// +// $handle = fopen($file, 'r'); +// +// while (( $data = fgetcsv($handle, 1000, ";") ) !== false) { +// if (isset($data[ 0 ])) { +// $product = ProductVariant::find() +// ->where([ 'sku' => $data[ 0 ] ]) +// ->joinWith('product') +// ->one(); +// if ($product instanceof ProductVariant) { +// $model = EventsToProducts::find() +// ->where( +// [ +// 'event_id' => $this->id, +// 'product_id' => $product->product->id, +// ] +// ) +// ->one(); +// if (!$model instanceof EventsToProducts) { +// $model = new EventsToProducts; +// $model->event_id = $this->id; +// $model->product_id = $product->product->id; +// $model->save(); +// } +// } +// } +// +// } +// fclose($handle); +// unlink($file); +// +// } + + public function goEvent($file) + { + + set_time_limit(0); + + $handle = fopen($file, 'r'); + + while (( $data = fgetcsv($handle, 1000, ";") ) !== false) { + if (isset($data[ 0 ])) { + $product = ProductVariant::find() + ->where([ 'sku' => $data[ 0 ] ]) + ->one(); + if ($product instanceof ProductVariant) { + $model = EventToVariant::find() + ->where( + [ + 'event_id' => $this->id, + 'variant_id' => $product->id, + ] + ) + ->one(); + if (!$model instanceof EventToVariant) { + $model = new EventToVariant(); + $model->event_id = $this->id; + $model->variant_id = $product->id; + $model->save(); + } } } + } - + fclose($handle); + unlink($file); + } - fclose($handle); - unlink($file); - - } - - public function getProducts(){ - return $this->hasMany(Product::className(),['id' => 'product_id'] )->viaTable('events_to_products', ['event_id' => 'id']); - } - - public static function getSaleEvents(){ - return ArrayHelper::toArray(self::find()->select('percent')->distinct('percent')->where('is_sale=true AND percent IS NOT NULL')->orderBy('percent')->all()); + + public function getProducts() + { + return $this->hasMany(Product::className(), [ 'id' => 'product_id' ]) + ->viaTable('events_to_products', [ 'event_id' => 'id' ]); + } + + public function getVariants() + { + return $this->hasMany(ProductVariant::className(), [ 'id' => 'variant_id' ]) + ->viaTable('event_to_variant', [ 'event_id' => 'id' ]); + } + + public static function getSaleEvents() + { + return ArrayHelper::toArray( + self::find() + ->select('percent') + ->distinct('percent') + ->where('is_sale=true AND percent IS NOT NULL') + ->orderBy('percent') + ->all() + ); + } + } - -} diff --git a/models/EventToVariant.php b/models/EventToVariant.php new file mode 100644 index 0000000..8b6a795 --- /dev/null +++ b/models/EventToVariant.php @@ -0,0 +1,67 @@ + true, 'targetClass' => Event::className(), 'targetAttribute' => ['event_id' => 'id']], + [['variant_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductVariant::className(), 'targetAttribute' => ['variant_id' => 'id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'event_id' => Yii::t('app', 'Event ID'), + 'variant_id' => Yii::t('app', 'Variant ID'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getEvent() + { + return $this->hasOne(Event::className(), ['id' => 'event_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getVariant() + { + return $this->hasOne(ProductVariant::className(), ['id' => 'variant_id']); + } +} diff --git a/views/event/_form.php b/views/event/_form.php index 8e7e62d..463b9ce 100755 --- a/views/event/_form.php +++ b/views/event/_form.php @@ -116,13 +116,13 @@ use yii\helpers\Html; 'productIds', + 'name' => 'variantsIds', 'options' => [ - 'placeholder' => \Yii::t('app', 'Search for products ...'), + 'placeholder' => \Yii::t('app', 'Search for variant ...'), 'multiple' => true, ], - 'value' => array_keys($model->productsIds), - 'data' => $model->productsIds, + 'value' => array_keys($model->variantsIds), + 'data' => $model->variantsIds, 'pluginOptions' => [ 'allowClear' => true, 'minimumInputLength' => 3, @@ -132,7 +132,7 @@ use yii\helpers\Html; ), ], 'ajax' => [ - 'url' => Url::to([ '/ecommerce/manage/list' ]), + 'url' => Url::to([ '/ecommerce/variant/list' ]), 'dataType' => 'json', 'data' => new JsExpression( 'function(params) { -- libgit2 0.21.4