diff --git a/controllers/EventController.php b/controllers/EventController.php index 6e77d96..55fef90 100755 --- a/controllers/EventController.php +++ b/controllers/EventController.php @@ -3,9 +3,11 @@ namespace artweb\artbox\event\controllers; +use artweb\artbox\ecommerce\models\Product; use Yii; use artweb\artbox\event\models\Event; use artweb\artbox\event\models\EventSearch; +use yii\helpers\ArrayHelper; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; @@ -80,9 +82,24 @@ class EventController extends Controller { $model = new Event(); $model->generateLangs(); + $model->productsIds = []; 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)); + } + + }else{ + $products = Product::find() + ->where([ 'id' => \Yii::$app->request->post('productIds') ]) + ->all(); + $model->linkMany('products', $products); + } + return $this->redirect([ 'view', 'id' => $model->id, @@ -107,6 +124,9 @@ class EventController extends Controller { $model = $this->findModel($id); $model->generateLangs(); + $model->productsIds = ArrayHelper::map($model->products, + 'id', + 'lang.title'); if ($model->load(Yii::$app->request->post())) { $model->loadLangs(\Yii::$app->request); if ($model->save() && $model->transactionStatus) { @@ -118,6 +138,11 @@ class EventController extends Controller $model->goEvent(Yii::getAlias('@uploadDir/' . $file->name)); } + }else{ + $products = Product::find() + ->where([ 'id' => \Yii::$app->request->post('productIds') ]) + ->all(); + $model->linkMany('products', $products); } return $this->redirect([ diff --git a/models/Event.php b/models/Event.php index 462594e..a02a047 100755 --- a/models/Event.php +++ b/models/Event.php @@ -2,6 +2,7 @@ namespace artweb\artbox\event\models; +use artweb\artbox\behaviors\ManyToManyBehavior; use artweb\artbox\behaviors\SaveImgBehavior; use artweb\artbox\ecommerce\models\Product; use artweb\artbox\ecommerce\models\ProductVariant; @@ -26,6 +27,7 @@ use yii\web\Request; * @property integer $percent * @property integer $banner * @property integer $type + * @property Product[] $products * * From language behavior * * @property EventLang $lang * @property EventLang[] $langs @@ -60,6 +62,7 @@ class Event extends \yii\db\ActiveRecord public $products_file; const ACTIVE = 1; const INACTIVE = 2; + public $productsIds = array(); /** * @inheritdoc */ @@ -89,6 +92,9 @@ class Event extends \yii\db\ActiveRecord 'class' => LanguageBehavior::className(), 'objectLang' => EventLang::className() ], + [ + 'class' => ManyToManyBehavior::className(), + ], ]; } diff --git a/views/event/_form.php b/views/event/_form.php index f01ff90..8e7e62d 100755 --- a/views/event/_form.php +++ b/views/event/_form.php @@ -9,6 +9,7 @@ use kartik\select2\Select2; use yii\helpers\Html; use yii\helpers\Url; use yii\widgets\ActiveForm; + use yii\web\JsExpression; /* @var $this yii\web\View */ /* @var $model Event */ @@ -112,7 +113,54 @@ use yii\helpers\Html; 'form' => $form, ]) ?> - + 'productIds', + 'options' => [ + 'placeholder' => \Yii::t('app', 'Search for products ...'), + 'multiple' => true, + ], + 'value' => array_keys($model->productsIds), + 'data' => $model->productsIds, + 'pluginOptions' => [ + 'allowClear' => true, + 'minimumInputLength' => 3, + 'language' => [ + 'errorLoading' => new JsExpression( + "function () { return 'Waiting for results...'; }" + ), + ], + 'ajax' => [ + 'url' => Url::to([ '/ecommerce/manage/list' ]), + 'dataType' => 'json', + 'data' => new JsExpression( + 'function(params) { + return { + q:params.term' . $condition . ' + }; + }' + ), + ], + 'escapeMarkup' => new JsExpression( + 'function (markup) { + return markup; + }' + ), + 'templateResult' => new JsExpression( + 'function (product) { + return product.text; + }' + ), + 'templateSelection' => new JsExpression( + 'function (product) { + return product.text; + }' + ), + ], + ] + ); + ?> -- libgit2 0.21.4