/**
  * Removes product from list of favorites.
  *
  * @param integer $productId
  * @return mixed
  * @throws ForbiddenHttpException
  * @throws NotFoundHttpException
  */
 public function actionRemove($productId)
 {
     if (!empty($productId)) {
         if (!Yii::$app->user->isGuest) {
             $model = FavoriteProduct::find()->where(['product_id' => $productId, 'user_id' => Yii::$app->user->id])->one();
             if (!empty($model)) {
                 $model->delete();
                 if (Yii::$app->request->isAjax) {
                     return Yii::t('shop', 'You have successfully deleted this product from favorites.');
                 } else {
                     Yii::$app->session->setFlash('success', Yii::t('shop', 'You have successfully deleted this product from favorites.'));
                 }
             } else {
                 if (Yii::$app->request->isAjax) {
                     return Yii::t('shop', 'Error has occurred.');
                 } else {
                     Yii::$app->session->setFlash('error', Yii::t('shop', 'Error has occurred.'));
                 }
             }
             return $this->redirect(Yii::$app->request->referrer);
         }
         throw new ForbiddenHttpException();
     } else {
         throw new NotFoundHttpException();
     }
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = FavoriteProduct::find()->where(['user_id' => \Yii::$app->user->id]);
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     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;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'product_id' => $this->product_id, 'user_id' => $this->user_id]);
     return $dataProvider;
 }
Beispiel #3
0
 /**
  * Return true if product has added to favorites already or false if not.
  * @return boolean
  */
 public function isFavorite()
 {
     $favoriteProduct = FavoriteProduct::find()->where(['product_id' => $this->id, 'user_id' => \Yii::$app->user->id])->one();
     if (!empty($favoriteProduct)) {
         return true;
     } else {
         return false;
     }
 }