Commit 767d56734db4c867f3251784542d45e5ef2343bf
1 parent
c00502bf
event, blog, social
Showing
3 changed files
with
80 additions
and
1 deletions
Show diff stats
controllers/EventController.php
| @@ -3,9 +3,11 @@ | @@ -3,9 +3,11 @@ | ||
| 3 | namespace artweb\artbox\event\controllers; | 3 | namespace artweb\artbox\event\controllers; |
| 4 | 4 | ||
| 5 | 5 | ||
| 6 | +use artweb\artbox\ecommerce\models\Product; | ||
| 6 | use Yii; | 7 | use Yii; |
| 7 | use artweb\artbox\event\models\Event; | 8 | use artweb\artbox\event\models\Event; |
| 8 | use artweb\artbox\event\models\EventSearch; | 9 | use artweb\artbox\event\models\EventSearch; |
| 10 | +use yii\helpers\ArrayHelper; | ||
| 9 | use yii\web\Controller; | 11 | use yii\web\Controller; |
| 10 | use yii\web\NotFoundHttpException; | 12 | use yii\web\NotFoundHttpException; |
| 11 | use yii\filters\VerbFilter; | 13 | use yii\filters\VerbFilter; |
| @@ -80,9 +82,24 @@ class EventController extends Controller | @@ -80,9 +82,24 @@ class EventController extends Controller | ||
| 80 | { | 82 | { |
| 81 | $model = new Event(); | 83 | $model = new Event(); |
| 82 | $model->generateLangs(); | 84 | $model->generateLangs(); |
| 85 | + $model->productsIds = []; | ||
| 83 | if ($model->load(Yii::$app->request->post())) { | 86 | if ($model->load(Yii::$app->request->post())) { |
| 84 | $model->loadLangs(\Yii::$app->request); | 87 | $model->loadLangs(\Yii::$app->request); |
| 85 | if ($model->save() && $model->transactionStatus) { | 88 | if ($model->save() && $model->transactionStatus) { |
| 89 | + if ( ($file = UploadedFile::getInstance($model, 'products_file')) ) { | ||
| 90 | + if(!empty($file)){ | ||
| 91 | + | ||
| 92 | + $file->saveAs(Yii::getAlias('@uploadDir/' . $file->name)); | ||
| 93 | + $model->goEvent(Yii::getAlias('@uploadDir/' . $file->name)); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + }else{ | ||
| 97 | + $products = Product::find() | ||
| 98 | + ->where([ 'id' => \Yii::$app->request->post('productIds') ]) | ||
| 99 | + ->all(); | ||
| 100 | + $model->linkMany('products', $products); | ||
| 101 | + } | ||
| 102 | + | ||
| 86 | return $this->redirect([ | 103 | return $this->redirect([ |
| 87 | 'view', | 104 | 'view', |
| 88 | 'id' => $model->id, | 105 | 'id' => $model->id, |
| @@ -107,6 +124,9 @@ class EventController extends Controller | @@ -107,6 +124,9 @@ class EventController extends Controller | ||
| 107 | { | 124 | { |
| 108 | $model = $this->findModel($id); | 125 | $model = $this->findModel($id); |
| 109 | $model->generateLangs(); | 126 | $model->generateLangs(); |
| 127 | + $model->productsIds = ArrayHelper::map($model->products, | ||
| 128 | + 'id', | ||
| 129 | + 'lang.title'); | ||
| 110 | if ($model->load(Yii::$app->request->post())) { | 130 | if ($model->load(Yii::$app->request->post())) { |
| 111 | $model->loadLangs(\Yii::$app->request); | 131 | $model->loadLangs(\Yii::$app->request); |
| 112 | if ($model->save() && $model->transactionStatus) { | 132 | if ($model->save() && $model->transactionStatus) { |
| @@ -118,6 +138,11 @@ class EventController extends Controller | @@ -118,6 +138,11 @@ class EventController extends Controller | ||
| 118 | $model->goEvent(Yii::getAlias('@uploadDir/' . $file->name)); | 138 | $model->goEvent(Yii::getAlias('@uploadDir/' . $file->name)); |
| 119 | } | 139 | } |
| 120 | 140 | ||
| 141 | + }else{ | ||
| 142 | + $products = Product::find() | ||
| 143 | + ->where([ 'id' => \Yii::$app->request->post('productIds') ]) | ||
| 144 | + ->all(); | ||
| 145 | + $model->linkMany('products', $products); | ||
| 121 | } | 146 | } |
| 122 | 147 | ||
| 123 | return $this->redirect([ | 148 | return $this->redirect([ |
models/Event.php
| @@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
| 2 | 2 | ||
| 3 | namespace artweb\artbox\event\models; | 3 | namespace artweb\artbox\event\models; |
| 4 | 4 | ||
| 5 | +use artweb\artbox\behaviors\ManyToManyBehavior; | ||
| 5 | use artweb\artbox\behaviors\SaveImgBehavior; | 6 | use artweb\artbox\behaviors\SaveImgBehavior; |
| 6 | use artweb\artbox\ecommerce\models\Product; | 7 | use artweb\artbox\ecommerce\models\Product; |
| 7 | use artweb\artbox\ecommerce\models\ProductVariant; | 8 | use artweb\artbox\ecommerce\models\ProductVariant; |
| @@ -26,6 +27,7 @@ use yii\web\Request; | @@ -26,6 +27,7 @@ use yii\web\Request; | ||
| 26 | * @property integer $percent | 27 | * @property integer $percent |
| 27 | * @property integer $banner | 28 | * @property integer $banner |
| 28 | * @property integer $type | 29 | * @property integer $type |
| 30 | + * @property Product[] $products | ||
| 29 | * * From language behavior * | 31 | * * From language behavior * |
| 30 | * @property EventLang $lang | 32 | * @property EventLang $lang |
| 31 | * @property EventLang[] $langs | 33 | * @property EventLang[] $langs |
| @@ -60,6 +62,7 @@ class Event extends \yii\db\ActiveRecord | @@ -60,6 +62,7 @@ class Event extends \yii\db\ActiveRecord | ||
| 60 | public $products_file; | 62 | public $products_file; |
| 61 | const ACTIVE = 1; | 63 | const ACTIVE = 1; |
| 62 | const INACTIVE = 2; | 64 | const INACTIVE = 2; |
| 65 | + public $productsIds = array(); | ||
| 63 | /** | 66 | /** |
| 64 | * @inheritdoc | 67 | * @inheritdoc |
| 65 | */ | 68 | */ |
| @@ -89,6 +92,9 @@ class Event extends \yii\db\ActiveRecord | @@ -89,6 +92,9 @@ class Event extends \yii\db\ActiveRecord | ||
| 89 | 'class' => LanguageBehavior::className(), | 92 | 'class' => LanguageBehavior::className(), |
| 90 | 'objectLang' => EventLang::className() | 93 | 'objectLang' => EventLang::className() |
| 91 | ], | 94 | ], |
| 95 | + [ | ||
| 96 | + 'class' => ManyToManyBehavior::className(), | ||
| 97 | + ], | ||
| 92 | ]; | 98 | ]; |
| 93 | } | 99 | } |
| 94 | 100 |
views/event/_form.php
| @@ -9,6 +9,7 @@ use kartik\select2\Select2; | @@ -9,6 +9,7 @@ use kartik\select2\Select2; | ||
| 9 | use yii\helpers\Html; | 9 | use yii\helpers\Html; |
| 10 | use yii\helpers\Url; | 10 | use yii\helpers\Url; |
| 11 | use yii\widgets\ActiveForm; | 11 | use yii\widgets\ActiveForm; |
| 12 | + use yii\web\JsExpression; | ||
| 12 | 13 | ||
| 13 | /* @var $this yii\web\View */ | 14 | /* @var $this yii\web\View */ |
| 14 | /* @var $model Event */ | 15 | /* @var $model Event */ |
| @@ -112,7 +113,54 @@ use yii\helpers\Html; | @@ -112,7 +113,54 @@ use yii\helpers\Html; | ||
| 112 | 'form' => $form, | 113 | 'form' => $form, |
| 113 | ]) ?> | 114 | ]) ?> |
| 114 | 115 | ||
| 115 | - | 116 | + <?php $condition = ''; |
| 117 | + echo Select2::widget( | ||
| 118 | + [ | ||
| 119 | + 'name' => 'productIds', | ||
| 120 | + 'options' => [ | ||
| 121 | + 'placeholder' => \Yii::t('app', 'Search for products ...'), | ||
| 122 | + 'multiple' => true, | ||
| 123 | + ], | ||
| 124 | + 'value' => array_keys($model->productsIds), | ||
| 125 | + 'data' => $model->productsIds, | ||
| 126 | + 'pluginOptions' => [ | ||
| 127 | + 'allowClear' => true, | ||
| 128 | + 'minimumInputLength' => 3, | ||
| 129 | + 'language' => [ | ||
| 130 | + 'errorLoading' => new JsExpression( | ||
| 131 | + "function () { return 'Waiting for results...'; }" | ||
| 132 | + ), | ||
| 133 | + ], | ||
| 134 | + 'ajax' => [ | ||
| 135 | + 'url' => Url::to([ '/ecommerce/manage/list' ]), | ||
| 136 | + 'dataType' => 'json', | ||
| 137 | + 'data' => new JsExpression( | ||
| 138 | + 'function(params) { | ||
| 139 | + return { | ||
| 140 | + q:params.term' . $condition . ' | ||
| 141 | + }; | ||
| 142 | + }' | ||
| 143 | + ), | ||
| 144 | + ], | ||
| 145 | + 'escapeMarkup' => new JsExpression( | ||
| 146 | + 'function (markup) { | ||
| 147 | + return markup; | ||
| 148 | + }' | ||
| 149 | + ), | ||
| 150 | + 'templateResult' => new JsExpression( | ||
| 151 | + 'function (product) { | ||
| 152 | + return product.text; | ||
| 153 | + }' | ||
| 154 | + ), | ||
| 155 | + 'templateSelection' => new JsExpression( | ||
| 156 | + 'function (product) { | ||
| 157 | + return product.text; | ||
| 158 | + }' | ||
| 159 | + ), | ||
| 160 | + ], | ||
| 161 | + ] | ||
| 162 | + ); | ||
| 163 | + ?> | ||
| 116 | 164 | ||
| 117 | 165 | ||
| 118 | 166 |