public function actionApply($id) { if (Yii::$app->user->can('couponGet')) { $model = $this->findModel($id); $score = User::getScore(); if ($score >= $model->requireScore) { User::reduceScore($model->requireScore); $hasCoupon = new ParticipantHasCoupon(); $hasCoupon->Participant_id = Yii::$app->user->id; $hasCoupon->coupon_id = $id; $hasCoupon->is_read = 0; $hasCoupon->is_used = 0; if ($hasCoupon->save()) { $this->redirect(['index', 'apply' => 1]); } } $this->redirect(['index', 'apply' => 2]); } else { if (Yii::$app->user->isGuest) { Yii::$app->user->loginRequired(); } else { throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); } } }
public function checkUserHasCoupon($id) { if (ParticipantHasCoupon::findOne(['Participant_id' => Yii::$app->user->id, 'coupon_id' => $id]) !== null) { return true; } return false; }
/** * Finds the ParticipantHasCoupon model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $Participant_id * @param integer $coupon_id * @return ParticipantHasCoupon the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($Participant_id, $coupon_id) { if (($model = ParticipantHasCoupon::findOne(['Participant_id' => $Participant_id, 'coupon_id' => $coupon_id])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
if (Yii::$app->user->can('couponEdit')) { ?> <p> <?php echo Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']); ?> <?php echo Html::a('Delete', ['delete', 'id' => $model->id], ['class' => 'btn btn-danger', 'data' => ['confirm' => 'Are you sure you want to delete this item?', 'method' => 'post']]); ?> </p> <?php } ?> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['Coupon_name', 'Coupon_description', 'expireDatetime', 'requireScore']]); ?> <?php if (\app\models\ParticipantHasCoupon::checkUserHasCoupon($model->id)) { echo Html::a('Taken', ['apply', 'id' => $model->id], ['class' => 'btn btn-warning', 'disabled' => 'disabled']); } else { if (\app\models\User::getScore() < $model->requireScore) { echo Html::a('Not enough score', ['apply', 'id' => $model->id], ['class' => 'btn btn-danger', 'disabled' => 'disabled']); } else { echo Html::a('Get it', ['apply', 'id' => $model->id], ['class' => 'btn btn-success']); } } ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getParticipantHasCoupons() { return $this->hasMany(ParticipantHasCoupon::className(), ['coupon_id' => 'id']); }
</h3> <span>Expire at <?php echo $row['expireDatetime']; ?> </span> </div> <div class="col-sm-3"> <span class="score"><?php echo $row['requireScore']; ?> </span> <span>score to get</span> </div> <div class="col-sm-2 text-center"> <?php if (\app\models\ParticipantHasCoupon::checkUserHasCoupon($row['id'])) { echo Html::a('Taken', ['apply', 'id' => $row['id']], ['class' => 'btn btn-warning', 'disabled' => 'disabled']); } else { if (\app\models\User::getScore() < $row['requireScore']) { echo Html::a('Not enough score', ['apply', 'id' => $row['id']], ['class' => 'btn btn-danger', 'disabled' => 'disabled']); } else { echo Html::a('Get it', ['apply', 'id' => $row['id']], ['class' => 'btn btn-success']); } } ?> </div> </div> <hr> </a> <?php }