Beispiel #1
0
 public function actionFavorite($id)
 {
     if (!Yii::$app->user->isGuest) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         $favorite = Favorite::find()->where(['user_id' => Yii::$app->user->id, 'product_id' => $id])->one();
         if (!$favorite) {
             $model = new Favorite(['user_id' => Yii::$app->user->id, 'product_id' => $id]);
             if ($model->save()) {
                 return ['status' => 1];
             }
         }
         return ['status' => 2];
     } else {
         return $this->redirect('site/login');
     }
 }
Beispiel #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Favorite::find();
     $query->orderBy(['created_at' => SORT_DESC]);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if ($this->load($params) && !$this->validate()) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'product_id' => $this->product_id, 'attention' => $this->attention, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     return $dataProvider;
 }
 /**
  * Finds the Favorite model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Favorite the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Favorite::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Beispiel #4
0
 public function actionAjaxDeleteFavorite($id)
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     if ($id) {
         Favorite::deleteAll(['user_id' => Yii::$app->user->id, 'product_id' => $id]);
         return ['status' => 1];
     }
     return ['status' => -1];
 }
Beispiel #5
0
                    <?php 
}
?>
                </ul>
            </div>
        </div>
    </div>
</div>
</div>
</div>

</div>
<?php 
$isFavorite = 0;
if (!Yii::$app->user->isGuest) {
    $favorite = \common\models\Favorite::find()->where(['user_id' => Yii::$app->user->id, 'product_id' => $model->id])->one();
    if ($favorite) {
        $isFavorite = 1;
    }
}
$urlAddToCart = Yii::$app->urlManager->createUrl(['cart/add-to-cart']);
$urlFavorite = Yii::$app->urlManager->createUrl(['product/favorite']);
$urlLogin = Yii::$app->urlManager->createUrl(['site/login']);
$urlConsultationAdd = Yii::$app->urlManager->createUrl(['consultation/ajax-add']);
$urlComment = Yii::$app->urlManager->createUrl(['product/comment']);
$urlConsultation = Yii::$app->urlManager->createUrl(['product/consultation']);
$this->registerJs('
var product = {productId:' . $model->id . ', stock:' . $model->stock . ', csrf:"' . Yii::$app->request->getCsrfToken() . '"};
var user = {id:' . (Yii::$app->user->isGuest ? 0 : Yii::$app->user->id) . ', favorite:' . $isFavorite . '};
var urlCartAdd = "' . Yii::$app->urlManager->createUrl(['cart/ajax-add']) . '";
');