Example #1
0
 public function applyVote($user, $type, $type_id, $updown)
 {
     //deal with item table
     $item = "";
     switch ($type) {
         case Constant::POST_TYPE:
             $item = Post::findOrFail($type_id);
             break;
         case Constant::COMMENT_TYPE:
             $item = Comment::findOrFail($type_id);
             Cache::forget(Constant::COMMENT_CACHE_NEWLIST_NAME . $item->post_id);
             break;
         case Constant::SECTION_TYPE:
             $item = Section::findOrFail($type_id);
             break;
         default:
             throw new UnexpectedValueException("type: {$type} not enumerated");
     }
     //increment our total vote counter
     $user->increment('votes');
     //decrement one point for voting
     $user->decrement('points');
     //double decrement for self upvote
     if ($type == Constant::POST_TYPE || $type == Constant::COMMENT_TYPE) {
         if ($item->user_id == Auth::user()->id && $updown == Constant::VOTE_UP) {
             $user->decrement('points');
         }
     }
     //upvote/downvote the item itself
     if ($updown == Constant::VOTE_UP) {
         $item->increment('upvotes');
     } else {
         if ($updown == Constant::VOTE_DOWN) {
             $item->increment('downvotes');
         }
     }
     //upvote/downvote user who posted (ignore for sections)
     if ($type == Constant::POST_TYPE || $type == Constant::COMMENT_TYPE) {
         $rec_user = User::findOrFail($item->user_id);
         if ($updown == Constant::VOTE_UP) {
             $rec_user->increment('points');
         } else {
             if ($updown == Constant::VOTE_DOWN) {
                 $rec_user->decrement('points');
             }
         }
     }
     //deal with votes table
     $vote = new Vote(array('type' => $type, 'user_id' => Auth::user()->id, 'item_id' => $type_id, 'updown' => $updown));
     $vote->save();
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $section = Section::findOrFail($id);
     $section->delete();
     return Redirect::route('admin.sections.index');
 }
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show($id)
 {
     $section = $this->section->findOrFail($id);
     return View::make('sections.show', compact('section'));
 }