예제 #1
0
 /**
  * Thumbs relation.
  * @return PostThumb
  */
 public function getThumb()
 {
     return $this->hasOne(PostThumb::className(), ['post_id' => 'id'])->where(['user_id' => User::loggedId()]);
 }
예제 #2
0
파일: Post.php 프로젝트: pezisc/yii2-podium
 /**
  * Performs vote processing.
  * @param boolean $up whether this is up or downvote
  * @param integer $count number of user's cached votes
  * @return boolean
  * @since 0.2
  */
 public function podiumThumb($up = true, $count = 0)
 {
     try {
         if ($this->thumb) {
             if ($this->thumb->thumb == 1 && !$up) {
                 $this->thumb->thumb = -1;
                 if ($this->thumb->save()) {
                     $this->updateCounters(['likes' => -1, 'dislikes' => 1]);
                 }
             } elseif ($this->thumb->thumb == -1 && $up) {
                 $this->thumb->thumb = 1;
                 if ($this->thumb->save()) {
                     $this->updateCounters(['likes' => 1, 'dislikes' => -1]);
                 }
             }
         } else {
             $postThumb = new PostThumb();
             $postThumb->post_id = $this->id;
             $postThumb->user_id = User::loggedId();
             $postThumb->thumb = $up ? 1 : -1;
             if ($postThumb->save()) {
                 if ($postThumb->thumb) {
                     $this->updateCounters(['likes' => 1]);
                 } else {
                     $this->updateCounters(['dislikes' => 1]);
                 }
             }
         }
         if ($count == 0) {
             Cache::getInstance()->set('user.votes.' . User::loggedId(), ['count' => 1, 'expire' => time() + 3600]);
         } else {
             Cache::getInstance()->setElement('user.votes.' . User::loggedId(), 'count', $count + 1);
         }
         return true;
     } catch (Exception $e) {
         Log::error($e->getMessage(), null, __METHOD__);
     }
     return false;
 }
 /**
  * Voting on the post.
  * @return string|\yii\web\Response
  */
 public function actionThumb()
 {
     if (Yii::$app->request->isAjax) {
         $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Error while voting on this post!'), ['class' => 'text-danger'])];
         if (!Yii::$app->user->isGuest) {
             $postId = Yii::$app->request->post('post');
             $thumb = Yii::$app->request->post('thumb');
             if (is_numeric($postId) && $postId > 0 && in_array($thumb, ['up', 'down'])) {
                 $post = Post::findOne((int) $postId);
                 if ($post) {
                     if ($post->thread->locked) {
                         $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'This thread is locked.'), ['class' => 'text-info'])];
                     } else {
                         if ($post->author_id == User::loggedId()) {
                             return Json::encode(['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You can not vote on your own post!'), ['class' => 'text-info'])]);
                         }
                         $count = 0;
                         $votes = Cache::getInstance()->get('user.votes.' . User::loggedId());
                         if ($votes !== false) {
                             if ($votes['expire'] < time()) {
                                 $votes = false;
                             } elseif ($votes['count'] >= 10) {
                                 return Json::encode(['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', '{max} votes per hour limit reached!', ['max' => 10]), ['class' => 'text-danger'])]);
                             } else {
                                 $count = $votes['count'];
                             }
                         }
                         if ($post->thumb) {
                             if ($post->thumb->thumb == 1 && $thumb == 'down') {
                                 $post->thumb->thumb = -1;
                                 if ($post->thumb->save()) {
                                     $post->updateCounters(['likes' => -1, 'dislikes' => 1]);
                                 }
                             } elseif ($post->thumb->thumb == -1 && $thumb == 'up') {
                                 $post->thumb->thumb = 1;
                                 if ($post->thumb->save()) {
                                     $post->updateCounters(['likes' => 1, 'dislikes' => -1]);
                                 }
                             }
                         } else {
                             $postThumb = new PostThumb();
                             $postThumb->post_id = $post->id;
                             $postThumb->user_id = User::loggedId();
                             $postThumb->thumb = $thumb == 'up' ? 1 : -1;
                             if ($postThumb->save()) {
                                 if ($thumb == 'up') {
                                     $post->updateCounters(['likes' => 1]);
                                 } else {
                                     $post->updateCounters(['dislikes' => 1]);
                                 }
                             }
                         }
                         $data = ['error' => 0, 'likes' => '+' . $post->likes, 'dislikes' => '-' . $post->dislikes, 'summ' => $post->likes - $post->dislikes, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', 'Your vote has been saved!'), ['class' => 'text-success'])];
                         if ($count == 0) {
                             Cache::getInstance()->set('user.votes.' . User::loggedId(), ['count' => 1, 'expire' => time() + 3600]);
                         } else {
                             Cache::getInstance()->setElement('user.votes.' . User::loggedId(), 'count', $count + 1);
                         }
                     }
                 }
             }
         } else {
             $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Please sign in to vote on this post'), ['class' => 'text-info'])];
         }
         return Json::encode($data);
     } else {
         return $this->redirect(['default/index']);
     }
 }
예제 #4
0
 public function getThumb()
 {
     return $this->hasOne(PostThumb::className(), ['post_id' => 'id'])->where(['user_id' => Yii::$app->user->id]);
 }