diff --git a/Module.php b/Module.php new file mode 100755 index 0000000..73adc8f --- /dev/null +++ b/Module.php @@ -0,0 +1,12 @@ +=5.4", + "yiisoft/yii2": "*", + "developeruz/yii2-db-rbac": "*", + "artweb/artbox": "dev-master", + "artweb/artbox-language": "dev-master" + }, + "autoload": { + "psr-4": { + "artweb\\artbox\\event\\": "" + } + } +} \ No newline at end of file diff --git a/controllers/EventController.php b/controllers/EventController.php new file mode 100755 index 0000000..387bb36 --- /dev/null +++ b/controllers/EventController.php @@ -0,0 +1,173 @@ +[ + 'class' => AccessBehavior::className(), + 'rules' => + ['site' => + [ + [ + 'actions' => ['login', 'error'], + 'allow' => true, + ] + ] + ] + ], + 'verbs' => [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all Event models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new EventSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Event model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Event model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Event(); + $model->generateLangs(); + if ($model->load(Yii::$app->request->post())) { + $model->loadLangs(\Yii::$app->request); + if ($model->save() && $model->transactionStatus) { + return $this->redirect([ + 'view', + 'id' => $model->id, + ]); + } + + } + return $this->render('create', [ + 'model' => $model, + 'modelLangs' => $model->modelLangs, + ]); + + } + + /** + * Updates an existing Event model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + $model->generateLangs(); + if ($model->load(Yii::$app->request->post())) { + $model->loadLangs(\Yii::$app->request); + if ($model->save() && $model->transactionStatus) { + + if ( ($file = UploadedFile::getInstance($model, 'products_file')) ) { + if(!empty($file)){ + $file->saveAs(Yii::getAlias('@uploadDir/' . $file->name)); + $model->goEvent(Yii::getAlias('@uploadDir/' . $file->name)); + } + + } + + return $this->redirect([ + 'view', + 'id' => $model->id, + ]); + } + } + + return $this->render('update', [ + 'model' => $model, + 'modelLangs' => $model->modelLangs, + ]); + + } + + /** + * Deletes an existing Event model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + public function actionDelimg($id,$field){ + $model = $this->findModel($id); + $model->detachBehavior('img'); + $model->$field = ''; + $model->save(); + return true; + } + + + /** + * Finds the Event model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Event the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Event::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/models/Event.php b/models/Event.php new file mode 100755 index 0000000..c9495f4 --- /dev/null +++ b/models/Event.php @@ -0,0 +1,203 @@ +[ + 'class' => SaveImgBehavior::className(), + 'fields' => [ + ['name'=>'image','directory' => 'event' ], + ['name'=>'banner','directory' => 'event' ], + ] + ], + TimestampBehavior::className(), + 'language' => [ + 'class' => LanguageBehavior::className(), + 'objectLang' => EventLang::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','sale','is_event','percent' ], 'integer'], + [['image', 'end_at','banner'], 'string', 'max' => 255], + [['imageUpload','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', 'Загрузка файла'), + '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 goEvent($file) { + + set_time_limit(0); + + + $handle = fopen($file, 'r'); + + + while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { + if(isset($data[0]) && isset($data[1])){ + $product = ProductVariant::find()->where(['sku' => $data[1]])->joinWith('product')->one(); + if($product instanceof ProductVariant){ + $model= EventsToProducts::find()->where(['event_id' =>$data[0], 'product_id' => $product->product->id ])->one(); + if(!$model instanceof EventsToProducts){ + $model = new EventsToProducts; + $model->event_id = $data[0]; + $model->product_id = $product->product->id; + $model->save(); + } + } + } + + } + fclose($handle); + unlink($file); + + } + + public function getProducts(){ + return $this->hasMany(Product::className(),['product_id' => 'product_id'] )->viaTable('events_to_products', ['id' => 'event_id']); + } + + public static function getSaleEvents(){ + return ArrayHelper::toArray(self::find()->select('percent')->distinct('percent')->where('sale=true AND percent IS NOT NULL')->orderBy('percent')->all()); + } + +} diff --git a/models/EventLang.php b/models/EventLang.php new file mode 100644 index 0000000..263c59c --- /dev/null +++ b/models/EventLang.php @@ -0,0 +1,93 @@ + [ + 'class' => 'artweb\artbox\behaviors\Slug', + ], + ]; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [['event_id', 'language_id', 'title', 'body'], 'required'], + [['event_id', 'language_id'], 'integer'], + [['body', 'seo_text'], 'string'], + [['title', 'meta_title', 'meta_description', 'h1', 'alias'], 'string', 'max' => 255], + [['alias'], 'unique'], + [['event_id', 'language_id'], 'unique', 'targetAttribute' => ['event_id', 'language_id'], 'message' => 'The combination of Event ID and Language ID has already been taken.'], + [['event_id'], 'exist', 'skipOnError' => true, 'targetClass' => Event::className(), 'targetAttribute' => ['event_id' => 'id']], + [['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => Language::className(), 'targetAttribute' => ['language_id' => 'id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'event_id' => 'Event ID', + 'language_id' => 'Language ID', + 'title' => 'Title', + 'body' => 'Body', + 'meta_title' => 'Meta Title', + 'meta_description' => 'Meta Description', + 'seo_text' => 'Seo Text', + 'h1' => 'H1', + 'alias' => 'Alias', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getEvent() + { + return $this->hasOne(Event::className(), ['id' => 'event_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguage() + { + return $this->hasOne(Language::className(), ['id' => 'language_id']); + } +} diff --git a/models/EventSearch.php b/models/EventSearch.php new file mode 100755 index 0000000..7675828 --- /dev/null +++ b/models/EventSearch.php @@ -0,0 +1,86 @@ + $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + $query->joinWith('lang'); + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + 'end_at' => $this->end_at, + ]); + + if($this->lang !== null){ + $query->andFilterWhere(['like', 'title', $this->lang->title]) + ->andFilterWhere(['like', 'alias', $this->lang->alias]) + ->andFilterWhere(['like', 'body', $this->lang->body]) + ->andFilterWhere(['like', 'image', $this->image]) + ->andFilterWhere(['like', 'meta_title', $this->lang->meta_title]) + ->andFilterWhere(['like', 'description', $this->lang->meta_description]) + ->andFilterWhere(['like', 'h1', $this->lang->h1]) + ->andFilterWhere(['like', 'seo_text', $this->lang->seo_text]); + } + + + return $dataProvider; + } +} diff --git a/models/EventsToProducts.php b/models/EventsToProducts.php new file mode 100755 index 0000000..d96372f --- /dev/null +++ b/models/EventsToProducts.php @@ -0,0 +1,68 @@ + true, 'targetClass' => Event::className(), 'targetAttribute' => ['event_id' => 'event_id']], + [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'events_to_products_id' => 'Events To Products ID', + 'event_id' => 'Event ID', + 'product_id' => 'Product ID', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getEvent() + { + return $this->hasOne(Event::className(), ['event_id' => 'event_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getProduct() + { + return $this->hasOne(Product::className(), ['product_id' => 'product_id']); + } +} diff --git a/views/event/_form.php b/views/event/_form.php new file mode 100755 index 0000000..a6a6490 --- /dev/null +++ b/views/event/_form.php @@ -0,0 +1,103 @@ + + +
+ + false, + 'options' => ['enctype' => 'multipart/form-data'] + ]); ?> + + field($model, 'id')->textInput(['maxlength' => true, 'disabled'=>true]) ?> + + field($model, 'end_at') + ->widget(DatePicker::className(), [ + 'pluginOptions' => [ + 'todayHighlight' => true, + 'format' => 'yyyy-mm-dd', + ]]) ?> + + field($model, 'image')->widget(\kartik\file\FileInput::classname(), [ + 'language' => 'ru', + 'options' => [ + 'accept' => 'image/*', + 'multiple' => false, + ], + 'pluginOptions' => [ + 'allowedFileExtensions' => ['jpg', 'gif', 'png'], + 'initialPreview' => !empty($model->imageUrl) ? ArtboxImageHelper::getImage($model->imageUrl, 'list') : '', + 'deleteUrl' => \yii\helpers\Url::to(['/product/manage/delimg', 'id' => $model->primaryKey]), + 'initialPreviewConfig' => $model->getImagesConfig(), + 'overwriteInitial' => true, + 'showUpload' => false, + 'previewFileType' => 'image', + ], + ]); ?> + + + + + field($model, 'banner')->widget(\kartik\file\FileInput::classname(), [ + 'language' => 'ru', + 'options' => [ + 'accept' => 'image/*', + 'multiple' => false, + ], + 'pluginOptions' => [ + 'allowedFileExtensions' => ['jpg', 'gif', 'png'], + 'initialPreview' => !empty($model->getImageUrl(1)) ? ArtboxImageHelper::getImage($model->getImageUrl(1), 'list') : '', + 'deleteUrl' => \yii\helpers\Url::to(['/product/manage/delimg', 'id' => $model->primaryKey]), + 'initialPreviewConfig' => $model->getImagesConfig('banner'), + 'overwriteInitial' => true, + 'showUpload' => false, + 'previewFileType' => 'image', + ], + ]); ?> + + + field($model, 'products_file')->widget(\kartik\file\FileInput::classname(), [ + 'language' => 'ru' + ]); ?> + + + + field($model, 'status')->checkbox() ?> + + field($model, 'sale')->checkbox() ?> + + field($model, 'is_event')->checkbox() ?> + + field($model, 'percent')->textInput() ?> + + $modelLangs, + 'formView' => '@artweb/artbox/event/views/event/_form_language', + 'form' => $form, + ]) ?> + + + + + + + +
+ isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> +
+ + + +
diff --git a/views/event/_form_language.php b/views/event/_form_language.php new file mode 100755 index 0000000..487e791 --- /dev/null +++ b/views/event/_form_language.php @@ -0,0 +1,46 @@ + + + + + + +field($model_lang, '[' . $language->id . ']title')->textInput(['maxlength' => true]) ?> + +field($model_lang, '[' . $language->id . ']alias')->textInput(['maxlength' => true]) ?> + +field($model_lang, '[' . $language->id . ']body')->widget(CKEditor::className(), + [ + 'editorOptions' => ElFinder::ckeditorOptions('elfinder',[ + 'preset' => 'full', //разработанны стандартные настройки basic, standard, full данную возможность не обязательно использовать + 'inline' => false, //по умолчанию false]), + 'filebrowserUploadUrl'=>Yii::$app->getUrlManager()->createUrl('file/uploader/images-upload') + ] + ) + ]) ?> + +field($model_lang, '[' . $language->id . ']meta_title')->textInput(['maxlength' => true]) ?> + +field($model_lang, '[' . $language->id . ']meta_description')->textInput(['maxlength' => true]) ?> + +field($model_lang, '[' . $language->id . ']h1')->textInput(['maxlength' => true]) ?> + +field($model_lang, '[' . $language->id . ']seo_text')->textarea(['rows' => 6]) ?> + + + diff --git a/views/event/_search.php b/views/event/_search.php new file mode 100755 index 0000000..4185226 --- /dev/null +++ b/views/event/_search.php @@ -0,0 +1,49 @@ + + + diff --git a/views/event/create.php b/views/event/create.php new file mode 100755 index 0000000..cc2931f --- /dev/null +++ b/views/event/create.php @@ -0,0 +1,24 @@ +title = Yii::t('app', 'Create Event'); +$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Events'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + 'modelLangs' => $modelLangs + ]) ?> + +
diff --git a/views/event/index.php b/views/event/index.php new file mode 100755 index 0000000..3c0e12b --- /dev/null +++ b/views/event/index.php @@ -0,0 +1,44 @@ +title = Yii::t('app', 'Events'); +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ render('_search', ['model' => $searchModel]); ?> + +

+ 'btn btn-success']) ?> +

+ $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'id', + 'lang.title', + 'lang.alias', + 'imageUrl:image', + // 'meta_title', + // 'description', + // 'h1', + // 'seo_text:ntext', + // 'created_at', + // 'updated_at', + // 'end_at', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> +
diff --git a/views/event/update.php b/views/event/update.php new file mode 100755 index 0000000..f257309 --- /dev/null +++ b/views/event/update.php @@ -0,0 +1,26 @@ +title = Yii::t('app', 'Update {modelClass}: ', [ + 'modelClass' => 'Event', +]) . $model->lang->title; +$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Events'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->lang->title, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = Yii::t('app', 'Update'); +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + 'modelLangs' => $modelLangs + ]) ?> + +
diff --git a/views/event/view.php b/views/event/view.php new file mode 100755 index 0000000..314317a --- /dev/null +++ b/views/event/view.php @@ -0,0 +1,40 @@ +title = $model->lang->title; +$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Events'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id', + 'lang.title', + 'lang.alias', + 'lang.meta_title', + 'lang.meta_description', + 'lang.h1', + ], + ]) ?> + +
-- libgit2 0.21.4