Guest User

Untitled

a guest
Jan 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. public function action_like()
  2. {
  3. $response = Response::forge();
  4. $like = Model_Like::forge();
  5. $like->user_id = Input::post('user_id');
  6. $like->liked_by = Session::get('sentry_user');
  7. // find if the user already liked
  8. $row = Model_Like::find(array($like->user_id, $like->liked_by));
  9. //if liked remove from database
  10. if($row):
  11. $response->body(json_encode(array(
  12. 'status' => 'no',
  13. )));
  14. $row->delete();
  15. else:
  16. $response->body(json_encode(array(
  17. 'status' => 'yes',
  18. )));
  19. $like->save();
  20. endif;
  21.  
  22. return $response;
  23. }
  24.  
  25. $('button.like').on('click', function(){
  26. var likeId = $(this).data('like')
  27. valPlus = parseInt($('.like-total').text()) + 1;;
  28. $.ajax({
  29. type: "POST",
  30. url: site_url + 'profile/like/',
  31. data: {user_id: likeId},
  32. dataType: "json",
  33. context: this,
  34. //async: false,
  35. beforeSend: function(data) {
  36. $(this).attr('disabled', 'disabled');
  37. },
  38. success: function(data) {
  39. console.debug(data);
  40. if(data.status == "yes") {
  41.  
  42. $('.like-total').text(valPlus);
  43. }
  44. },
  45. complete: function(data) {
  46. $(this).removeAttr('disabled', 'disabled');
  47. }
  48. })
  49.  
  50.  
  51. });
  52.  
  53. $row = Model_Like::find()->where(array(
  54. array('user_id', $like->user_id),
  55. array('liked_id', $like->liked_by)
  56. ));
Add Comment
Please, Sign In to add comment