FriendsController.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\controllers;
  3. use app\models\Friends;
  4. use lithium\action\DispatchException;
  5. use lithium\security\Auth;
  6. use app\models\User;
  7. class FriendsController extends \lithium\action\Controller {
  8. public function index() {
  9. $id = Auth::check('default');
  10. $id = $id['id'];
  11. $conditions = array('User.id' => $id);
  12. $with = array('Friends');
  13. $user = User::find('first', compact('conditions', 'with'));
  14. return compact('user');
  15. }
  16. public function view() {
  17. $friend = Friends::first($this->request->id);
  18. return compact('friend');
  19. }
  20. public function add() {
  21. $friend = Friends::create();
  22. if ($this->request->data) {
  23. $user = Auth::check('default');
  24. $ToUserId = $this->request->data['userid'];
  25. //If the user exists
  26. if(User::count($ToUserId) == 1)
  27. {
  28. $friendship = Friends::create(array('FromUserId' => $user['id'],
  29. 'ToUserId' => $ToUserId,
  30. 'StatusId' => Friends::status("open"),
  31. 'SentTime' => date('Y-m-d H:i:s', time())));
  32. $friendship->save();
  33. }
  34. }
  35. return compact('user');
  36. }
  37. public function accept() {
  38. $user == Auth::check('default');
  39. $id = $this->request->data['id'];
  40. $relationship = Friends::find($id);
  41. //Ensure the user is accepting their own request
  42. if ($relationship->ToUserId == $user->id)
  43. {
  44. $relationship->ResponseTime = date('Y-m-d H:i:s', time());
  45. $relationship->StatusId = Friends::status('accepted');
  46. $relationship->save();
  47. }
  48. //Return them to their friends page (with ajax return the status I guess)
  49. return $this->redirect("Friendships::view");
  50. }
  51. //Copied and pasted the code from above but it really should be handled inside the friendship
  52. //model;
  53. public function decline() {
  54. $user == Auth::check('default');
  55. $id = $this->request->data['id'];
  56. $relationship = Friendships::find($id);
  57. //Ensure the user is accepting their own request
  58. if ($relationship->ToUserId == $user->id)
  59. {
  60. $relationship->ResponseTime = date('Y-m-d H:i:s', time());
  61. $relationship->StatusId = Friendship::status('rejected');
  62. $relationship->save();
  63. }
  64. //Return them to their friends page (with ajax return the status I guess)
  65. return $this->redirect("Friendships::view");
  66. }
  67. public function delete() {
  68. if (!$this->request->is('post') && !$this->request->is('delete')) {
  69. $msg = "Friends::delete can only be called with http:post or http:delete.";
  70. throw new DispatchException($msg);
  71. }
  72. Friends::find($this->request->id)->delete();
  73. $this->redirect('Friends::index');
  74. }
  75. }
  76. ?>