Commit c2648e6567513a9d9882d0fc00f5b705caac57b3

Authored by Anastasia
1 parent b60a88b8

- prices

backend/assets/AppAsset.php
@@ -16,9 +16,8 @@ @@ -16,9 +16,8 @@
16 // 'css/site.css', 16 // 'css/site.css',
17 // 'css/table-new.css', 17 // 'css/table-new.css',
18 ]; 18 ];
19 -// public $js = ['/js/admin-script.js']; 19 + public $js = ['js/backend.js'];
20 public $depends = [ 20 public $depends = [
21 - 'yii\web\YiiAsset',  
22 - 'yii\bootstrap\BootstrapAsset', 21 + 'artbox\core\assets\ArtboxCoreAsset',
23 ]; 22 ];
24 } 23 }
backend/controllers/PriceController.php
@@ -8,6 +8,9 @@ @@ -8,6 +8,9 @@
8 8
9 namespace backend\controllers; 9 namespace backend\controllers;
10 10
  11 + use artbox\core\models\Language;
  12 + use common\models\Price;
  13 + use common\models\PriceLang;
11 use common\models\Service; 14 use common\models\Service;
12 use yii\helpers\ArrayHelper; 15 use yii\helpers\ArrayHelper;
13 use yii\web\Controller; 16 use yii\web\Controller;
@@ -15,12 +18,78 @@ @@ -15,12 +18,78 @@
15 class PriceController extends Controller 18 class PriceController extends Controller
16 { 19 {
17 public function actionIndex($service_id = null){ 20 public function actionIndex($service_id = null){
18 - 21 +
  22 + if (\Yii::$app->request->post()){
  23 + $price = \Yii::$app->request->post('Price');
  24 + $price_lang = \Yii::$app->request->post('PriceLang');
  25 + $transaction = \Yii::$app->db->beginTransaction();
  26 + try {
  27 + Price::deleteAll(['service_id' => $service_id]);
  28 + foreach ($price as $key => $value){
  29 + $model = new Price();
  30 + $model->load($value, '');
  31 + $model->id = $key;
  32 + if ($model->save()){
  33 + $languages = [];
  34 + foreach ($price_lang[$key] as $k => $item){
  35 + $languages[] = [
  36 + 'language_id' => $k,
  37 + 'price_id' => $model->id,
  38 + 'title' => $item['title']
  39 + ];
  40 + }
  41 + \Yii::$app->db->createCommand()
  42 + ->batchInsert(
  43 + 'price_lang',
  44 + [
  45 + 'language_id',
  46 + 'price_id',
  47 + 'title',
  48 + ],
  49 + $languages
  50 + )
  51 + ->execute();
  52 + }else{
  53 + continue;
  54 + }
  55 + }
  56 + $transaction->commit();
  57 +
  58 + } catch (\Exception $e) {
  59 + $transaction->rollBack();
  60 + throw $e;
  61 +
  62 + } catch (\Throwable $e) {
  63 + $transaction->rollBack();
  64 + return false;
  65 + }
  66 +
  67 + }
19 $services = Service::find()->where(['is not','parent_id', null])->with('language')->all(); 68 $services = Service::find()->where(['is not','parent_id', null])->with('language')->all();
  69 + $lastId = Price::find()->max('id');
  70 + $prices = Price::find()->where(['service_id' => $service_id])->with('languages')->all();
20 $data = ArrayHelper::map($services, 'id', 'title'); 71 $data = ArrayHelper::map($services, 'id', 'title');
  72 +
21 return $this->render('index',[ 73 return $this->render('index',[
22 'services' => $data, 74 'services' => $data,
23 - 'service_id' => $service_id 75 + 'service_id' => $service_id,
  76 + 'lastId' => $lastId,
  77 + 'prices' => $prices
24 ]); 78 ]);
  79 +
  80 + }
  81 +
  82 + public function actionNewPrice($lastId, $service_id = null){
  83 + $languages = Language::getActive();
  84 + $price = New Price();
  85 + $price->service_id = $service_id;
  86 + $price_lang = new PriceLang();
  87 + return $this->renderPartial('_add_price',[
  88 + 'lastId' => $lastId,
  89 + 'price' => $price,
  90 + 'price_lang' => $price_lang,
  91 + 'service_id' => $service_id,
  92 + 'languages' => $languages
  93 + ]);
25 } 94 }
26 } 95 }
27 \ No newline at end of file 96 \ No newline at end of file
backend/views/layouts/main.php
@@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
9 use artbox\core\models\User; 9 use artbox\core\models\User;
10 use artbox\core\widgets\FeedbackWidget; 10 use artbox\core\widgets\FeedbackWidget;
11 use artbox\core\widgets\FlashWidget; 11 use artbox\core\widgets\FlashWidget;
  12 + use backend\assets\AppAsset;
12 use yii\bootstrap\Html; 13 use yii\bootstrap\Html;
13 use yii\helpers\Url; 14 use yii\helpers\Url;
14 use yii\web\View; 15 use yii\web\View;
@@ -16,7 +17,7 @@ @@ -16,7 +17,7 @@
16 use yiister\gentelella\widgets\Menu; 17 use yiister\gentelella\widgets\Menu;
17 18
18 ArtboxCoreAsset::register($this); 19 ArtboxCoreAsset::register($this);
19 -// AppAsset::register($this); 20 + AppAsset::register($this);
20 21
21 /** 22 /**
22 * @var User $user 23 * @var User $user
backend/views/price/_add_price.php 0 → 100644
  1 +<?php
  2 + /**
  3 + * @var \common\models\PriceLang $price_lang;
  4 + * @var \common\models\Price $price
  5 + * @var integer $lastId;
  6 + * @var integer $service_id
  7 + * @var array $languages
  8 + */
  9 + use yii\widgets\ActiveForm;
  10 +
  11 + $form = new ActiveForm();
  12 + ?>
  13 +<div class="form-group-33-wr">
  14 +<div class="form-group-33">
  15 + <?php echo $form->field($price, '['.$lastId.']service_id')->hiddenInput()->label(false)?>
  16 + <?php echo $form->field($price, '['.$lastId.']price')?>
  17 + <?php
  18 + foreach ($languages as $key => $value){
  19 + echo $form->field($price_lang, '['.$lastId.']['.$key.']title')->label('Название '.$value['url']);
  20 + }
  21 + ?>
  22 + <a class="delete-variant delete-url remove-img-2 btn-danger btn" href="#"></a>
  23 +</div>
  24 +</div>
backend/views/price/index.php
1 <?php 1 <?php
2 /** 2 /**
3 - * @var array $services 3 + * @var array $services
4 * @var integer $service_id 4 * @var integer $service_id
  5 + * @var integer $lastId
  6 + * @var \common\models\Price[] $prices;
5 */ 7 */
6 8
7 use artbox\core\admin\assets\Select2; 9 use artbox\core\admin\assets\Select2;
8 use yii\helpers\Html; 10 use yii\helpers\Html;
9 use yii\web\View; 11 use yii\web\View;
10 use yii\widgets\ActiveForm; 12 use yii\widgets\ActiveForm;
11 -  
12 - ActiveForm::begin(['method' => 'GET', 'action' => [ 'price/index' ],]);  
13 - echo Html::dropDownList(  
14 - 'service_id',$service_id, $services, [  
15 - 'prompt' => \Yii::t('core', '- All -'),  
16 - 'id' => 'service_id'  
17 - ] 13 + use yiister\gentelella\widgets\Panel;
  14 +
  15 +?>
  16 +<div class='adm-index'>
  17 + <?php
  18 + $panel = Panel::begin(
  19 + [
  20 + 'header' => $this->title,
  21 + 'options' => [
  22 + 'class' => 'x_panel feedback-update',
  23 + ],
  24 + ]
  25 + );
  26 + ?>
  27 + <div class='form-admin-sort-wr'>
  28 + <div class='form-admin-sort'>
  29 + <?php ActiveForm::begin(
  30 + [
  31 + 'method' => 'GET',
  32 + 'action' => [ 'price/index' ],
  33 + ]
  34 + ) ?>
  35 + <div class='form-group'>
  36 + <?php echo Html::label('Услуга');
  37 + echo Html::dropDownList(
  38 + 'service_id',
  39 + $service_id,
  40 + $services,
  41 + [
  42 + 'prompt' => \Yii::t('core', '- All -'),
  43 + 'id' => 'service_id',
  44 + 'class' => 'form-control',
  45 + ]
18 ); 46 );
19 - echo Html::submitButton('Select');  
20 - ActiveForm::end();  
21 - Select2::register($this);  
22 -  
23 - $js = <<<JS 47 + ?>
  48 + </div>
  49 + <div class='form-search-btn-wr'>
  50 + <?php echo Html::submitButton(
  51 + 'Select',
  52 + [
  53 + 'class' => 'btn btn-success',
  54 + ]
  55 + );
  56 + ?>
  57 + </div>
  58 + <?php ActiveForm::end(); ?>
  59 + </div>
  60 + </div>
  61 + <?php
  62 + Select2::register($this);
  63 +
  64 + $js = <<<JS
24 $('#service_id').select2(); 65 $('#service_id').select2();
25 JS; 66 JS;
26 - $this->registerJs($js, View::POS_READY);  
27 \ No newline at end of file 67 \ No newline at end of file
  68 + $this->registerJs($js, View::POS_READY);
  69 + $panel::end();
  70 + ?>
  71 +</div>
  72 +
  73 +<?php
  74 + $panel = Panel::begin(
  75 + [
  76 + 'header' => $this->title,
  77 + 'options' => [
  78 + 'class' => 'x_panel feedback-update',
  79 + ],
  80 + ]
  81 + );
  82 + $form = ActiveForm::begin(
  83 + [
  84 + 'method' => 'POST',
  85 + 'action' => [ 'price/index', 'service_id' => $service_id ],
  86 + ]
  87 +) ?>
  88 +<?php if (!empty($prices)){
  89 + foreach ($prices as $price){?>
  90 +<div class="form-group-33-wr">
  91 + <div class="form-group-33">
  92 + <?php echo $form->field($price, '['.$price->id.']service_id')->hiddenInput()->label(false)?>
  93 + <?php echo $form->field($price, '['.$price->id.']price')?>
  94 + <?php
  95 + foreach ($price->languages as $key => $value){
  96 + echo $form->field($value, '['.$price->id.']['.$value->language_id.']title')->label('Название '.$value->language->url);
  97 + }
  98 + ?>
  99 + </div>
  100 +</div>
  101 +<?php }
  102 + }?>
  103 +<div class="form-group-33-wr">
  104 + <div class="form-group-33" style="height: 292px">
  105 + <a href="#" data-last="<?=(!empty($lastId) ? $lastId : 0)?>" data-service="<?=$service_id?>" id="add-price" class="add-url-pluse btn btn-success add-gallery-btn-new"><i class="fa fa-plus"></i></a>
  106 + </div>
  107 +</div>
  108 +<div class="style buttons-page-wr">
  109 + <?= Html::submitButton(
  110 + 'Save', ['class' => 'btn btn-success',]
  111 + ) ?>
  112 +</div>
  113 +
  114 +<?php
  115 + ActiveForm::end();
  116 + $panel::end()?>
backend/web/js/backend.js 0 → 100644
  1 +$(document).on('click', '#add-price', function(e) {
  2 + e.preventDefault();
  3 + var button = $(this);
  4 + var lastId = parseInt(button.data('last'))+1;
  5 + var serviceId = button.data('service');
  6 +
  7 + $.ajax({
  8 + url: '/admin/price/new-price',
  9 + type: "GET",
  10 + data: {
  11 + lastId: lastId,
  12 + service_id: serviceId
  13 + },
  14 + success: function(data) {
  15 + console.log(lastId);
  16 + console.log( button.data('last', lastId));
  17 + lastId = button.attr('data-last', lastId);
  18 + button.parent().parent().before(data);
  19 +
  20 + },
  21 + error: function(data) {
  22 + }
  23 + });
  24 +});
common/models/Price.php
@@ -2,7 +2,11 @@ @@ -2,7 +2,11 @@
2 2
3 namespace common\models; 3 namespace common\models;
4 4
  5 +use artbox\core\models\Language;
5 use Yii; 6 use Yii;
  7 +use yii\db\ActiveQuery;
  8 +use yii2tech\ar\position\PositionBehavior;
  9 +use yii2tech\ar\variation\VariationBehavior;
6 10
7 /** 11 /**
8 * This is the model class for table "price". 12 * This is the model class for table "price".
@@ -15,7 +19,9 @@ use Yii; @@ -15,7 +19,9 @@ use Yii;
15 * 19 *
16 * @property Service $service 20 * @property Service $service
17 * @property PriceLang[] $priceLangs 21 * @property PriceLang[] $priceLangs
18 - * @property Language[] $languages 22 + * @property \artbox\core\models\Language[] $languages
  23 + * * from VariationBehavior
  24 + * @method ActiveQuery hasDefaultVariationRelation();
19 */ 25 */
20 class Price extends \yii\db\ActiveRecord 26 class Price extends \yii\db\ActiveRecord
21 { 27 {
@@ -26,6 +32,32 @@ class Price extends \yii\db\ActiveRecord @@ -26,6 +32,32 @@ class Price extends \yii\db\ActiveRecord
26 { 32 {
27 return 'price'; 33 return 'price';
28 } 34 }
  35 + public function behaviors()
  36 + {
  37 + return [
  38 + 'translations' => [
  39 + 'class' => VariationBehavior::className(),
  40 + 'variationsRelation' => 'languages',
  41 + 'defaultVariationRelation' => 'language',
  42 + 'variationOptionReferenceAttribute' => 'language_id',
  43 + 'optionModelClass' => Language::className(),
  44 + 'defaultVariationOptionReference' => function () {
  45 + return Language::getCurrent()->id;
  46 + },
  47 + 'optionQueryFilter' => function (ActiveQuery $query) {
  48 + $query->where(
  49 + [
  50 + 'status' => true,
  51 + ]
  52 + );
  53 + },
  54 + ],
  55 + 'positionBehavior' => [
  56 + 'class' => PositionBehavior::className(),
  57 + 'positionAttribute' => 'sort',
  58 + ],
  59 + ];
  60 + }
29 61
30 /** 62 /**
31 * {@inheritdoc} 63 * {@inheritdoc}
@@ -64,16 +96,12 @@ class Price extends \yii\db\ActiveRecord @@ -64,16 +96,12 @@ class Price extends \yii\db\ActiveRecord
64 /** 96 /**
65 * @return \yii\db\ActiveQuery 97 * @return \yii\db\ActiveQuery
66 */ 98 */
67 - public function getPriceLangs() 99 + public function getLanguages()
68 { 100 {
69 return $this->hasMany(PriceLang::className(), ['price_id' => 'id']); 101 return $this->hasMany(PriceLang::className(), ['price_id' => 'id']);
70 } 102 }
71 -  
72 - /**  
73 - * @return \yii\db\ActiveQuery  
74 - */  
75 - public function getLanguages()  
76 - {  
77 - return $this->hasMany(Language::className(), ['id' => 'language_id'])->viaTable('price_lang', ['price_id' => 'id']); 103 +
  104 + public function getLanguage(){
  105 + return $this->hasDefaultVariationRelation();
78 } 106 }
79 } 107 }
common/models/PriceLang.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\models;
  4 +
  5 +use artbox\core\models\Language;
  6 +use Yii;
  7 +
  8 +/**
  9 + * This is the model class for table "price_lang".
  10 + *
  11 + * @property int $language_id
  12 + * @property int $price_id
  13 + * @property string $title
  14 + * @property string $description
  15 + *
  16 + * @property Language $language
  17 + * @property Price $price
  18 + */
  19 +class PriceLang extends \yii\db\ActiveRecord
  20 +{
  21 + /**
  22 + * {@inheritdoc}
  23 + */
  24 + public static function tableName()
  25 + {
  26 + return 'price_lang';
  27 + }
  28 +
  29 + /**
  30 + * {@inheritdoc}
  31 + */
  32 + public function rules()
  33 + {
  34 + return [
  35 + [['language_id', 'price_id'], 'required'],
  36 + [['language_id', 'price_id'], 'default', 'value' => null],
  37 + [['language_id', 'price_id'], 'integer'],
  38 + [['description'], 'string'],
  39 + [['title'], 'string', 'max' => 255],
  40 + [['language_id', 'price_id'], 'unique', 'targetAttribute' => ['language_id', 'price_id']],
  41 + [['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => Language::className(), 'targetAttribute' => ['language_id' => 'id']],
  42 + [['price_id'], 'exist', 'skipOnError' => true, 'targetClass' => Price::className(), 'targetAttribute' => ['price_id' => 'id']],
  43 + ];
  44 + }
  45 +
  46 + /**
  47 + * {@inheritdoc}
  48 + */
  49 + public function attributeLabels()
  50 + {
  51 + return [
  52 + 'language_id' => Yii::t('app', 'Language ID'),
  53 + 'price_id' => Yii::t('app', 'Price ID'),
  54 + 'title' => Yii::t('app', 'Title'),
  55 + 'description' => Yii::t('app', 'Description'),
  56 + ];
  57 + }
  58 +
  59 + /**
  60 + * @return \yii\db\ActiveQuery
  61 + */
  62 + public function getLanguage()
  63 + {
  64 + return $this->hasOne(Language::className(), ['id' => 'language_id']);
  65 + }
  66 +
  67 + /**
  68 + * @return \yii\db\ActiveQuery
  69 + */
  70 + public function getPrice()
  71 + {
  72 + return $this->hasOne(Price::className(), ['id' => 'price_id']);
  73 + }
  74 +}