[ '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 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; } 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); } 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() ); } }