/**
  * Creates data provider instance with search query applied
  * @param array $params
  * @param $ids
  * @return ActiveDataProvider
  */
 public function catalogSearch($params, $ids)
 {
     $query = Product::find()->select(['{{product}}.*', 'IF(round(sum(product_review.rating) / count(product.reviews_count)) IS NULL, 0, rating) as rating'])->withFilerProperties()->with(['brand' => function (ActiveQuery $query) {
         $query->productsCount();
     }])->joinWith('productReviews')->groupBy('{{product}}.id')->where(['product.category_id' => $ids]);
     $cloneQuery = clone $query;
     $products = $cloneQuery->all();
     $brands = ArrayHelper::map($products, 'brand.id', 'brand.title');
     $brandsProductsCount = ArrayHelper::map($products, 'brand.id', 'brand.productCount');
     $sort = new Sort(['defaultOrder' => ['rating' => SORT_ASC], 'attributes' => Product::getSortAttributes()]);
     $viewSort = new Sort(['sortParam' => 'view', 'attributes' => [Product::VIEW_SORT_LIST, Product::VIEW_SORT_BLOCK]]);
     $this->load($params);
     $sortParam = Yii::$app->request->getQueryParam('view');
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => $sort, 'pagination' => ['pageSize' => $sortParam === Product::VIEW_SORT_BLOCK ? self::PAGINATION_SIZE_BLOCK : self::PAGINATION_SIZE_LIST]]);
     Yii::$app->controller->view->params['viewSort'] = $viewSort;
     Yii::$app->controller->view->params['sort'] = $sort;
     Yii::$app->controller->view->params['searchModel'] = $this;
     Yii::$app->controller->view->params['brands'] = $brands;
     Yii::$app->controller->view->params['brandsProductCount'] = $brandsProductsCount;
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'active' => $this->active, 'reviews_count' => $this->reviews_count, 'brand_id' => $this->brand_id, 'comment_count' => $this->comment_count, 'category_id' => $this->category_id, 'UI' => $this->ui]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'full_description', $this->full_description])->andFilterWhere(['like', 'short_description', $this->short_description])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'ui', $this->ui]);
     Yii::$app->controller->view->params['propertiesProvider'] = new PropertiesProvider(['query' => clone $query]);
     Yii::$app->controller->view->params['dataProvider'] = $dataProvider;
     return $dataProvider;
 }
Exemple #2
0
 /**
  * Creates and populates a set of models.
  *
  * @param string $modelClass
  * @param array $multipleModels
  * @return array
  */
 public static function createMultiple($modelClass, $multipleModels = [])
 {
     $model = new $modelClass();
     $formName = $model->formName();
     $post = Yii::$app->request->post($formName);
     $models = [];
     if (!empty($multipleModels)) {
         $keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));
         $multipleModels = array_combine($keys, $multipleModels);
     }
     if ($post && is_array($post)) {
         foreach ($post as $i => $item) {
             if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']])) {
                 $models[] = $multipleModels[$item['id']];
             } else {
                 $models[] = new $modelClass();
             }
         }
     }
     unset($model, $formName, $post);
     return $models;
 }
 /**
  * Updates an existing Product model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  * @throws ErrorException
  * @throws Exception
  * @throws NotFoundHttpException
  * @throws \yii\base\Exception
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $model->scenario = $model::SCENARIO_UPDATE;
     $videos = $model->videos;
     Model::setScenarios($videos, ProductVideo::SCENARIO_PRODUCT_UPDATE);
     $properties = $model->productProperties;
     Model::setScenarios($properties, ProductProperty::SCENARIO_PRODUCT_UPDATE);
     if ($model->load($post = Yii::$app->request->post())) {
         $oldVideoIds = ArrayHelper::map($videos, 'id', 'id');
         $oldPropertyIds = ArrayHelper::map($properties, 'id', 'id');
         $videos = Model::createMultiple(ProductVideo::classname(), $videos);
         $properties = Model::createMultiple(ProductProperty::classname(), $properties);
         Model::setScenarios($videos, ProductVideo::SCENARIO_PRODUCT_UPDATE);
         Model::setScenarios($properties, ProductProperty::SCENARIO_PRODUCT_UPDATE);
         Model::loadMultiple($videos, Yii::$app->request->post());
         Model::loadMultiple($properties, Yii::$app->request->post());
         $deletedVideoIds = array_diff($oldVideoIds, array_filter(ArrayHelper::map($videos, 'id', 'id')));
         $deletedPropertyIds = array_diff($oldPropertyIds, array_filter(ArrayHelper::map($properties, 'id', 'id')));
         Model::validateDuplicates($properties, 'property_id');
         // ajax validation
         if (Yii::$app->request->isAjax) {
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ArrayHelper::merge(ActiveForm::validate($model), ActiveForm::validateMultiple($properties, false), ActiveForm::validateMultiple($videos));
         }
         if ($model->validate() && Model::validateMultiple($properties) && Model::validateMultiple($videos)) {
             $transaction = $model->getDb()->beginTransaction();
             $oldMainPhoto = $model->getOldAttribute('main_photo');
             $model->main_photo = UploadedFile::getInstance($model, 'main_photo');
             try {
                 if (!($category = Category::findOne(['id' => $model->category_id]))) {
                     throw new \yii\base\Exception();
                 }
                 if ($model->main_photo) {
                     if (!$model->save(false)) {
                         throw new ErrorException();
                     }
                     $model->removeMainPhotoFromFolder($oldMainPhoto);
                     if (!$model->upload()) {
                         throw new \yii\base\Exception();
                     }
                 } else {
                     $model->setAttribute('main_photo', $oldMainPhoto);
                     if (!$model->save(false)) {
                         throw new \yii\base\Exception();
                     }
                 }
                 ProductVideo::deleteAll(['id' => $deletedVideoIds]);
                 ProductProperty::deleteAll(['id' => $deletedPropertyIds]);
                 Model::linkAll($model, $properties, 'productProperties');
                 Model::unsetEmpty($videos, 'video');
                 Model::linkAll($model, $videos, 'videos');
                 $model->savePhotos();
                 $transaction->commit();
                 Yii::$app->session->setFlash('success', 'Product have been updated.');
                 return $this->redirect('index');
             } catch (Exception $e) {
                 Yii::$app->session->setFlash('error', 'Failed to update product.');
                 $transaction->rollBack();
                 return $this->redirect('index');
             }
         }
     }
     $model->clearTmpPhotos();
     return $this->render('update', ['model' => $model, 'videos' => empty($videos) ? [new ProductVideo()] : $videos, 'properties' => empty($properties) ? [new ProductProperty()] : $properties, 'category' => Category::findOne(['id' => $model->category_id])]);
 }