/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Ip::find(); $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; } $query->andFilterWhere(['id' => $this->id, 'product_id' => $this->product_id]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
<?php use yii\helpers\Html; use kartik\rating\StarRating; use yii\widgets\ActiveForm; use backend\models\Ip; use backend\models\Product; use frontend\helpers\ProductHelper; $this->title = $model->title; /* @var $this yii\web\View */ $ip = Yii::$app->request->userIP; $ipModel = Ip::find()->where(['name' => $ip, 'product_id' => $model->id])->one(); if ($ipModel) { $isRatingDisabled = true; } ?> <div class="row"> <div class="col-sm-4"> <?php require Yii::getAlias('@partials') . '/_slider.php'; echo $slider; ?> </div> <div class="col-sm-8"> <article> <h2 class="product-view"><?php echo $model->title; ?> </h2> <p><?php
/** * Finds the Ip model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Ip the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Ip::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionProductRating() { if (Yii::$app->request->isAjax and $rating = Yii::$app->request->post('rating') and $id = Yii::$app->request->post('id')) { if ($product = Product::findOne($id)) { $ip = Yii::$app->request->userIP; $ipModel = Ip::find()->where(['name' => $ip, 'product_id' => $id])->one(); if ($ipModel === null) { $currentRating = $product->rating; $currentRatingCount = $product->rating_count; $resultRating = round(($currentRating + $rating) / 2, 1); $product->rating = $resultRating; $product->rating_count = $currentRatingCount + 1; $product->save(); $model = new Ip(); $model->name = $ip; $model->product_id = $id; $model->save(); return true; } else { return 'forbidden'; // $this->refresh(); } } else { return false; } // return json_encode([$rating, $id]); } else { throw new NotFoundHttpException('The requested page does not exist.'); } }