From c2648e6567513a9d9882d0fc00f5b705caac57b3 Mon Sep 17 00:00:00 2001 From: Anastasia Date: Fri, 25 May 2018 16:47:58 +0300 Subject: [PATCH] - prices --- backend/assets/AppAsset.php | 5 ++--- backend/controllers/PriceController.php | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- backend/views/layouts/main.php | 3 ++- backend/views/price/_add_price.php | 24 ++++++++++++++++++++++++ backend/views/price/index.php | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- backend/web/js/backend.js | 24 ++++++++++++++++++++++++ common/models/Price.php | 46 +++++++++++++++++++++++++++++++++++++--------- common/models/PriceLang.php | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 337 insertions(+), 29 deletions(-) create mode 100644 backend/views/price/_add_price.php create mode 100644 backend/web/js/backend.js create mode 100644 common/models/PriceLang.php diff --git a/backend/assets/AppAsset.php b/backend/assets/AppAsset.php index d5cd961..91a1313 100755 --- a/backend/assets/AppAsset.php +++ b/backend/assets/AppAsset.php @@ -16,9 +16,8 @@ // 'css/site.css', // 'css/table-new.css', ]; -// public $js = ['/js/admin-script.js']; + public $js = ['js/backend.js']; public $depends = [ - 'yii\web\YiiAsset', - 'yii\bootstrap\BootstrapAsset', + 'artbox\core\assets\ArtboxCoreAsset', ]; } diff --git a/backend/controllers/PriceController.php b/backend/controllers/PriceController.php index 66e325e..ccb1544 100644 --- a/backend/controllers/PriceController.php +++ b/backend/controllers/PriceController.php @@ -8,6 +8,9 @@ namespace backend\controllers; + use artbox\core\models\Language; + use common\models\Price; + use common\models\PriceLang; use common\models\Service; use yii\helpers\ArrayHelper; use yii\web\Controller; @@ -15,12 +18,78 @@ class PriceController extends Controller { public function actionIndex($service_id = null){ - + + if (\Yii::$app->request->post()){ + $price = \Yii::$app->request->post('Price'); + $price_lang = \Yii::$app->request->post('PriceLang'); + $transaction = \Yii::$app->db->beginTransaction(); + try { + Price::deleteAll(['service_id' => $service_id]); + foreach ($price as $key => $value){ + $model = new Price(); + $model->load($value, ''); + $model->id = $key; + if ($model->save()){ + $languages = []; + foreach ($price_lang[$key] as $k => $item){ + $languages[] = [ + 'language_id' => $k, + 'price_id' => $model->id, + 'title' => $item['title'] + ]; + } + \Yii::$app->db->createCommand() + ->batchInsert( + 'price_lang', + [ + 'language_id', + 'price_id', + 'title', + ], + $languages + ) + ->execute(); + }else{ + continue; + } + } + $transaction->commit(); + + } catch (\Exception $e) { + $transaction->rollBack(); + throw $e; + + } catch (\Throwable $e) { + $transaction->rollBack(); + return false; + } + + } $services = Service::find()->where(['is not','parent_id', null])->with('language')->all(); + $lastId = Price::find()->max('id'); + $prices = Price::find()->where(['service_id' => $service_id])->with('languages')->all(); $data = ArrayHelper::map($services, 'id', 'title'); + return $this->render('index',[ 'services' => $data, - 'service_id' => $service_id + 'service_id' => $service_id, + 'lastId' => $lastId, + 'prices' => $prices ]); + + } + + public function actionNewPrice($lastId, $service_id = null){ + $languages = Language::getActive(); + $price = New Price(); + $price->service_id = $service_id; + $price_lang = new PriceLang(); + return $this->renderPartial('_add_price',[ + 'lastId' => $lastId, + 'price' => $price, + 'price_lang' => $price_lang, + 'service_id' => $service_id, + 'languages' => $languages + ]); } } \ No newline at end of file diff --git a/backend/views/layouts/main.php b/backend/views/layouts/main.php index 7c46fd6..3dbb92f 100755 --- a/backend/views/layouts/main.php +++ b/backend/views/layouts/main.php @@ -9,6 +9,7 @@ use artbox\core\models\User; use artbox\core\widgets\FeedbackWidget; use artbox\core\widgets\FlashWidget; + use backend\assets\AppAsset; use yii\bootstrap\Html; use yii\helpers\Url; use yii\web\View; @@ -16,7 +17,7 @@ use yiister\gentelella\widgets\Menu; ArtboxCoreAsset::register($this); -// AppAsset::register($this); + AppAsset::register($this); /** * @var User $user diff --git a/backend/views/price/_add_price.php b/backend/views/price/_add_price.php new file mode 100644 index 0000000..ce33c0c --- /dev/null +++ b/backend/views/price/_add_price.php @@ -0,0 +1,24 @@ + +
+
+ field($price, '['.$lastId.']service_id')->hiddenInput()->label(false)?> + field($price, '['.$lastId.']price')?> + $value){ + echo $form->field($price_lang, '['.$lastId.']['.$key.']title')->label('Название '.$value['url']); + } + ?> + +
+
diff --git a/backend/views/price/index.php b/backend/views/price/index.php index 4789f2a..98b7fcc 100644 --- a/backend/views/price/index.php +++ b/backend/views/price/index.php @@ -1,26 +1,115 @@ 'GET', 'action' => [ 'price/index' ],]); - echo Html::dropDownList( - 'service_id',$service_id, $services, [ - 'prompt' => \Yii::t('core', '- All -'), - 'id' => 'service_id' - ] + use yiister\gentelella\widgets\Panel; + +?> +
+ $this->title, + 'options' => [ + 'class' => 'x_panel feedback-update', + ], + ] + ); + ?> +
+
+ 'GET', + 'action' => [ 'price/index' ], + ] + ) ?> +
+ \Yii::t('core', '- All -'), + 'id' => 'service_id', + 'class' => 'form-control', + ] ); - echo Html::submitButton('Select'); - ActiveForm::end(); - Select2::register($this); - - $js = << +
+
+ 'btn btn-success', + ] + ); + ?> +
+ +
+
+ registerJs($js, View::POS_READY); \ No newline at end of file + $this->registerJs($js, View::POS_READY); + $panel::end(); + ?> +
+ + $this->title, + 'options' => [ + 'class' => 'x_panel feedback-update', + ], + ] + ); + $form = ActiveForm::begin( + [ + 'method' => 'POST', + 'action' => [ 'price/index', 'service_id' => $service_id ], + ] +) ?> + +
+
+ field($price, '['.$price->id.']service_id')->hiddenInput()->label(false)?> + field($price, '['.$price->id.']price')?> + languages as $key => $value){ + echo $form->field($value, '['.$price->id.']['.$value->language_id.']title')->label('Название '.$value->language->url); + } + ?> +
+
+ +
+
+ +
+
+
+ 'btn btn-success',] + ) ?> +
+ + diff --git a/backend/web/js/backend.js b/backend/web/js/backend.js new file mode 100644 index 0000000..83df877 --- /dev/null +++ b/backend/web/js/backend.js @@ -0,0 +1,24 @@ +$(document).on('click', '#add-price', function(e) { + e.preventDefault(); + var button = $(this); + var lastId = parseInt(button.data('last'))+1; + var serviceId = button.data('service'); + + $.ajax({ + url: '/admin/price/new-price', + type: "GET", + data: { + lastId: lastId, + service_id: serviceId + }, + success: function(data) { + console.log(lastId); + console.log( button.data('last', lastId)); + lastId = button.attr('data-last', lastId); + button.parent().parent().before(data); + + }, + error: function(data) { + } + }); +}); diff --git a/common/models/Price.php b/common/models/Price.php index 4e3851f..844d6aa 100644 --- a/common/models/Price.php +++ b/common/models/Price.php @@ -2,7 +2,11 @@ namespace common\models; +use artbox\core\models\Language; use Yii; +use yii\db\ActiveQuery; +use yii2tech\ar\position\PositionBehavior; +use yii2tech\ar\variation\VariationBehavior; /** * This is the model class for table "price". @@ -15,7 +19,9 @@ use Yii; * * @property Service $service * @property PriceLang[] $priceLangs - * @property Language[] $languages + * @property \artbox\core\models\Language[] $languages + * * from VariationBehavior + * @method ActiveQuery hasDefaultVariationRelation(); */ class Price extends \yii\db\ActiveRecord { @@ -26,6 +32,32 @@ class Price extends \yii\db\ActiveRecord { return 'price'; } + public function behaviors() + { + return [ + 'translations' => [ + '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', + ], + ]; + } /** * {@inheritdoc} @@ -64,16 +96,12 @@ class Price extends \yii\db\ActiveRecord /** * @return \yii\db\ActiveQuery */ - public function getPriceLangs() + public function getLanguages() { return $this->hasMany(PriceLang::className(), ['price_id' => 'id']); } - - /** - * @return \yii\db\ActiveQuery - */ - public function getLanguages() - { - return $this->hasMany(Language::className(), ['id' => 'language_id'])->viaTable('price_lang', ['price_id' => 'id']); + + public function getLanguage(){ + return $this->hasDefaultVariationRelation(); } } diff --git a/common/models/PriceLang.php b/common/models/PriceLang.php new file mode 100644 index 0000000..1ea3efd --- /dev/null +++ b/common/models/PriceLang.php @@ -0,0 +1,74 @@ + null], + [['language_id', 'price_id'], 'integer'], + [['description'], 'string'], + [['title'], 'string', 'max' => 255], + [['language_id', 'price_id'], 'unique', 'targetAttribute' => ['language_id', 'price_id']], + [['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => Language::className(), 'targetAttribute' => ['language_id' => 'id']], + [['price_id'], 'exist', 'skipOnError' => true, 'targetClass' => Price::className(), 'targetAttribute' => ['price_id' => 'id']], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'language_id' => Yii::t('app', 'Language ID'), + 'price_id' => Yii::t('app', 'Price ID'), + 'title' => Yii::t('app', 'Title'), + 'description' => Yii::t('app', 'Description'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguage() + { + return $this->hasOne(Language::className(), ['id' => 'language_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getPrice() + { + return $this->hasOne(Price::className(), ['id' => 'price_id']); + } +} -- libgit2 0.21.4