Commit 767d56734db4c867f3251784542d45e5ef2343bf

Authored by Anastasia
1 parent c00502bf

event, blog, social

controllers/EventController.php
... ... @@ -3,9 +3,11 @@
3 3 namespace artweb\artbox\event\controllers;
4 4  
5 5  
  6 +use artweb\artbox\ecommerce\models\Product;
6 7 use Yii;
7 8 use artweb\artbox\event\models\Event;
8 9 use artweb\artbox\event\models\EventSearch;
  10 +use yii\helpers\ArrayHelper;
9 11 use yii\web\Controller;
10 12 use yii\web\NotFoundHttpException;
11 13 use yii\filters\VerbFilter;
... ... @@ -80,9 +82,24 @@ class EventController extends Controller
80 82 {
81 83 $model = new Event();
82 84 $model->generateLangs();
  85 + $model->productsIds = [];
83 86 if ($model->load(Yii::$app->request->post())) {
84 87 $model->loadLangs(\Yii::$app->request);
85 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 103 return $this->redirect([
87 104 'view',
88 105 'id' => $model->id,
... ... @@ -107,6 +124,9 @@ class EventController extends Controller
107 124 {
108 125 $model = $this->findModel($id);
109 126 $model->generateLangs();
  127 + $model->productsIds = ArrayHelper::map($model->products,
  128 + 'id',
  129 + 'lang.title');
110 130 if ($model->load(Yii::$app->request->post())) {
111 131 $model->loadLangs(\Yii::$app->request);
112 132 if ($model->save() && $model->transactionStatus) {
... ... @@ -118,6 +138,11 @@ class EventController extends Controller
118 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 148 return $this->redirect([
... ...
models/Event.php
... ... @@ -2,6 +2,7 @@
2 2  
3 3 namespace artweb\artbox\event\models;
4 4  
  5 +use artweb\artbox\behaviors\ManyToManyBehavior;
5 6 use artweb\artbox\behaviors\SaveImgBehavior;
6 7 use artweb\artbox\ecommerce\models\Product;
7 8 use artweb\artbox\ecommerce\models\ProductVariant;
... ... @@ -26,6 +27,7 @@ use yii\web\Request;
26 27 * @property integer $percent
27 28 * @property integer $banner
28 29 * @property integer $type
  30 + * @property Product[] $products
29 31 * * From language behavior *
30 32 * @property EventLang $lang
31 33 * @property EventLang[] $langs
... ... @@ -60,6 +62,7 @@ class Event extends \yii\db\ActiveRecord
60 62 public $products_file;
61 63 const ACTIVE = 1;
62 64 const INACTIVE = 2;
  65 + public $productsIds = array();
63 66 /**
64 67 * @inheritdoc
65 68 */
... ... @@ -89,6 +92,9 @@ class Event extends \yii\db\ActiveRecord
89 92 'class' => LanguageBehavior::className(),
90 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 9 use yii\helpers\Html;
10 10 use yii\helpers\Url;
11 11 use yii\widgets\ActiveForm;
  12 + use yii\web\JsExpression;
12 13  
13 14 /* @var $this yii\web\View */
14 15 /* @var $model Event */
... ... @@ -112,7 +113,54 @@ use yii\helpers\Html;
112 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  
... ...