public static function setCommentLike($id, $val) { $val = $val >= 0 ? 1 : -1; $rate = self::model()->findByAttributes(array('comment_id' => $id, 'user_id' => Yii::app()->user->id)); $comment = BooksComments::model()->findByPk($id); if (empty($rate)) { $rate = new CommentLike(); $rate->comment_id = $id; $rate->user_id = Yii::app()->user->id; } else { if ($rate->val == $val) { $rate->delete(); if (!empty($comment)) { $comment->updateRate(); } return 0; } } $rate->val = $val; $rate->save(); if (!empty($comment)) { $comment->updateRate(); } return $val; }
public function like($id) { $type = Input::get('type'); if ($type) { if ($type == 1) { $like = Like::wherePostId($id)->whereUserId(Auth::user()->id)->get()->count(); $dislike = Dislike::wherePostId($id)->whereUserId(Auth::user()->id)->get()->count(); if ($like || $dislike) { return 'false'; } $like = new Like(); $like->post_id = $id; $like->user_id = Auth::user()->id; try { if ($like->save()) { DB::table('notifications')->where('type', '1')->where('post_id', $like->post_id)->update(array('seen' => 0, 'updated_at' => Carbon::now())); return 'true'; } } catch (Exception $e) { } } elseif ($type == 2) { $like = CommentLike::whereCommentId($id)->whereUserId(Auth::user()->id)->get()->count(); $dislike = CommentDislike::whereCommentId($id)->whereUserId(Auth::user()->id)->get()->count(); if ($like || $dislike) { return 'false'; } $like = new CommentLike(); $like->comment_id = $id; $like->user_id = Auth::user()->id; try { if ($like->save()) { return 'true'; } } catch (Exception $e) { } } } return 'false'; }