From fda3b17b032b67e0ccfbaa3aa6ec6e4ee5a17f70 Mon Sep 17 00:00:00 2001 From: Michael Francis Date: Sat, 16 Jul 2011 00:50:36 -0400 Subject: Implemented mySQL based friending --- controllers/FriendsController.php | 98 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 controllers/FriendsController.php (limited to 'controllers') diff --git a/controllers/FriendsController.php b/controllers/FriendsController.php new file mode 100644 index 0000000..6d7922f --- /dev/null +++ b/controllers/FriendsController.php @@ -0,0 +1,98 @@ + $id); + $with = array('Friends'); + $user = User::find('first', compact('conditions', 'with')); + + return compact('user'); + } + + public function view() { + $friend = Friends::first($this->request->id); + return compact('friend'); + } + + public function add() { + $friend = Friends::create(); + if ($this->request->data) { + $user = Auth::check('default'); + $ToUserId = $this->request->data['userid']; + + //If the user exists + if(User::count($ToUserId) == 1) + { + + $friendship = Friends::create(array('FromUserId' => $user['id'], + 'ToUserId' => $ToUserId, + 'StatusId' => Friends::status("open"), + 'SentTime' => date('Y-m-d H:i:s', time()))); + + $friendship->save(); + + } + } + return compact('user'); + } + + public function accept() { + $user == Auth::check('default'); + $id = $this->request->data['id']; + + $relationship = Friends::find($id); + + //Ensure the user is accepting their own request + if ($relationship->ToUserId == $user->id) + { + $relationship->ResponseTime = date('Y-m-d H:i:s', time()); + $relationship->StatusId = Friends::status('accepted'); + $relationship->save(); + } + + //Return them to their friends page (with ajax return the status I guess) + return $this->redirect("Friendships::view"); + } + + //Copied and pasted the code from above but it really should be handled inside the friendship + //model; + public function decline() { + $user == Auth::check('default'); + $id = $this->request->data['id']; + + $relationship = Friendships::find($id); + + //Ensure the user is accepting their own request + if ($relationship->ToUserId == $user->id) + { + $relationship->ResponseTime = date('Y-m-d H:i:s', time()); + $relationship->StatusId = Friendship::status('rejected'); + $relationship->save(); + } + + //Return them to their friends page (with ajax return the status I guess) + return $this->redirect("Friendships::view"); + } + + public function delete() { + if (!$this->request->is('post') && !$this->request->is('delete')) { + $msg = "Friends::delete can only be called with http:post or http:delete."; + throw new DispatchException($msg); + } + Friends::find($this->request->id)->delete(); + $this->redirect('Friends::index'); + } +} + +?> \ No newline at end of file -- cgit v1.2.3