From c4f4776ae0bbaab0a9b5e926a736ba607e4d7e49 Mon Sep 17 00:00:00 2001 From: Anastasia Date: Fri, 25 May 2018 17:24:44 +0300 Subject: [PATCH] - package offers --- backend/controllers/PackageController.php | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ backend/views/layouts/menu_items.php | 5 +++++ common/models/Package.php | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/models/PackageLang.php | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ console/migrations/m180525_140759_create_package_table.php | 31 +++++++++++++++++++++++++++++++ console/migrations/m180525_141010_create_package_lang_table.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 531 insertions(+), 0 deletions(-) create mode 100644 backend/controllers/PackageController.php create mode 100644 common/models/Package.php create mode 100644 common/models/PackageLang.php create mode 100644 console/migrations/m180525_140759_create_package_table.php create mode 100644 console/migrations/m180525_141010_create_package_lang_table.php diff --git a/backend/controllers/PackageController.php b/backend/controllers/PackageController.php new file mode 100644 index 0000000..b1fa3fa --- /dev/null +++ b/backend/controllers/PackageController.php @@ -0,0 +1,170 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => [ 'POST' ], + ], + ], + 'access' => [ + 'class' => AccessControl::className(), + 'rules' => [ + [ + 'allow' => true, + 'roles' => [ '@' ], + ], + ], + ], + ]; + } + + public function actions() + { + return [ + 'index' => [ + 'class' => Index::className(), + 'columns' => [ + 'title' => [ + 'type' => Index::ACTION_COL, + ], + 'updated_at' => [ + 'type' => Index::DATETIME_COL, + ], + 'sort' => [ + 'type' => Index::POSITION_COL, + ], + 'status' => [ + 'type' => Index::STATUS_COL, + ], + ], + 'model' => Package::className(), + 'hasLanguage' => true, + 'enableMassDelete' => true, + 'modelPrimaryKey' => 'id', + ], + 'create' => array_merge([ 'class' => Create::className() ], self::fieldsConfig()), + 'update' => array_merge([ 'class' => Update::className() ], self::fieldsConfig()), + 'view' => [ + 'class' => View::className(), + 'model' => Package::className(), + 'hasAlias' => true, + 'languageFields' => [ + [ + 'name' => 'title', + 'type' => Form::STRING, + ], + [ + 'name' => 'body', + 'type' => Form::WYSIWYG, + ], + ], + 'fields' => [ + ], + ], + 'delete' => [ + 'class' => Delete::className(), + ], + ]; + } + + public function findModel($id) + { + $model = Package::find() + ->with('languages') + ->where([ 'id' => $id ]) + ->one(); + if ($model !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } + + public function newModel() + { + return new Package(); + } + + public function deleteModel($id) + { + $page = Package::find() + ->with('languages.alias') + ->where( + [ + 'id' => $id, + ] + ) + ->one(); + $langs = call_user_func( + [ + $page, + 'getVariationModels', + ] + ); + foreach ($langs as $lang) { + if ($lang->alias !== null) { + $lang->alias->delete(); + } + } + + return $page->delete(); + } + + protected static function fieldsConfig() + { + return [ + 'model' => Package::className(), + 'hasAlias' => true, + 'languageFields' => [ + [ + 'name' => 'title', + 'type' => Form::STRING, + ], + [ + 'name' => 'body', + 'type' => Form::WYSIWYG, + ], + ], + 'fields' => [ + [ + 'name' => 'image_id', + 'type' => Form::IMAGE + ], + [ + 'name' => 'status', + 'type' => Form::BOOL, + ], + [ + 'name' => 'sort', + 'type' => Form::NUMBER, + ], + ], + ]; + } + } \ No newline at end of file diff --git a/backend/views/layouts/menu_items.php b/backend/views/layouts/menu_items.php index 78d0237..abb5f8f 100755 --- a/backend/views/layouts/menu_items.php +++ b/backend/views/layouts/menu_items.php @@ -60,6 +60,11 @@ 'url' => [ '/price/index' ], ], + [ + 'label' => \Yii::t('app', 'Package Offers'), + 'url' => [ '/package/index' ], + + ], ], ], diff --git a/common/models/Package.php b/common/models/Package.php new file mode 100644 index 0000000..b15972d --- /dev/null +++ b/common/models/Package.php @@ -0,0 +1,133 @@ + [ + 'class' => VariationBehavior::className(), + 'variationsRelation' => 'languages', + 'defaultVariationRelation' => 'language', + 'variationOptionReferenceAttribute' => 'language_id', + 'optionModelClass' => Language::className(), + 'defaultVariationOptionReference' => function () { + return Language::getCurrent()->id; + }, + 'optionQueryFilter' => function (ActiveQuery $query) { + $query->where( + [ + 'status' => true, + ] + ); + }, + ], + 'positionBehavior' => [ + 'class' => PositionBehavior::className(), + 'positionAttribute' => 'sort', + ], + 'timestamp' => [ + 'class' => TimestampBehavior::className(), + ], + ]; + } + /** + * {@inheritdoc} + */ + public function rules() + { + return [ + [['sort', 'created_at', 'updated_at'], 'default', 'value' => null], + [['sort', 'created_at', 'updated_at', 'image_id'], 'integer'], + [['status'], 'boolean'], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'sort' => Yii::t('app', 'Sort'), + 'status' => Yii::t('app', 'Status'), + 'created_at' => Yii::t('app', 'Created At'), + 'updated_at' => Yii::t('app', 'Updated At'), + 'image_id' => Yii::t('app', 'Image'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguages() + { + return $this->hasMany(PackageLang::className(), ['package_id' => 'id']); + } + + + + public function getLanguage(){ + return $this->hasDefaultVariationRelation(); + } + + /** + * @return string + */ + public function getRoute() + { + return Json::encode( + [ + 'service/view', + 'id' => $this->id, + ] + ); + } + + public function getImage(){ + return $this->hasOne(Image::className(), ['id' => 'image_id']); + } + +} diff --git a/common/models/PackageLang.php b/common/models/PackageLang.php new file mode 100644 index 0000000..b0d2113 --- /dev/null +++ b/common/models/PackageLang.php @@ -0,0 +1,135 @@ + null, + ], + [ + [ + 'package_id', + 'language_id', + 'alias_id', + ], + 'integer', + ], + [ + [ 'body' ], + 'string', + ], + [ + [ 'title' ], + 'string', + 'max' => 255, + ], + [ + [ + 'package_id', + 'language_id', + ], + 'unique', + 'targetAttribute' => [ + 'package_id', + 'language_id', + ], + ], + [ + [ 'alias_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Alias::className(), + 'targetAttribute' => [ 'alias_id' => 'id' ], + ], + [ + [ 'language_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Language::className(), + 'targetAttribute' => [ 'language_id' => 'id' ], + ], + [ + [ 'package_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Service::className(), + 'targetAttribute' => [ 'package_id' => 'id' ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'package_id' => Yii::t('app', 'Service ID'), + 'language_id' => Yii::t('app', 'Language ID'), + 'title' => Yii::t('app', 'Title'), + 'body' => Yii::t('app', 'Body'), + 'alias_id' => Yii::t('app', 'Alias ID'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getAlias() + { + return $this->hasOne(Alias::className(), [ 'id' => 'alias_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguage() + { + return $this->hasOne(Language::className(), [ 'id' => 'language_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getService() + { + return $this->hasOne(Package::className(), [ 'id' => 'service_id' ]); + } + } diff --git a/console/migrations/m180525_140759_create_package_table.php b/console/migrations/m180525_140759_create_package_table.php new file mode 100644 index 0000000..d6f573e --- /dev/null +++ b/console/migrations/m180525_140759_create_package_table.php @@ -0,0 +1,31 @@ +createTable('package', [ + 'id' => $this->primaryKey(), + 'sort' => $this->integer(), + 'status' => $this->boolean(), + 'created_at' => $this->integer(), + 'updated_at' => $this->integer() + ]); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropTable('package'); + } +} diff --git a/console/migrations/m180525_141010_create_package_lang_table.php b/console/migrations/m180525_141010_create_package_lang_table.php new file mode 100644 index 0000000..e2b9c0c --- /dev/null +++ b/console/migrations/m180525_141010_create_package_lang_table.php @@ -0,0 +1,57 @@ +createTable('package_lang', [ + 'package_id'=> $this->integer(32)->notNull(), + 'language_id'=> $this->integer(32)->notNull(), + 'title'=> $this->string(255)->notNull(), + 'body'=> $this->text()->notNull(), + 'alias_id'=> $this->integer(), + 'PRIMARY KEY(package_id, language_id)', + ]); + + $this->addForeignKey('package_lang_package_fk', + 'package_lang', + 'package_id', + 'package', + 'id', + 'CASCADE', + 'CASCADE'); + $this->addForeignKey('package_lang_language_fk', + 'package_lang', + 'language_id', + 'language', + 'id', + 'CASCADE', + 'CASCADE'); + $this->addForeignKey('package_lang_alias_fk', + 'package_lang', + 'alias_id', + 'alias', + 'id', + 'SET NULL', + 'CASCADE'); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropForeignKey('package_lang_package_fk', 'package_lang'); + $this->dropForeignKey('package_lang_language_fk', 'package_lang'); + $this->dropForeignKey('package_lang_alias_fk', 'package_lang'); + $this->dropTable('package_lang'); + } +} -- libgit2 0.21.4