From 8e468b8605eb1a85d32dbba4d41a9821daaa4016 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 +++++++++++++++++++++++++++++++++++++++ models/Friends.php | 32 +++++++++++++ models/User.php | 23 ++------- views/friends/add.html.php | 4 ++ views/friends/index.html.php | 7 +++ 5 files changed, 145 insertions(+), 19 deletions(-) create mode 100644 controllers/FriendsController.php create mode 100644 models/Friends.php create mode 100644 views/friends/add.html.php create mode 100644 views/friends/index.html.php 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 diff --git a/models/Friends.php b/models/Friends.php new file mode 100644 index 0000000..67d747a --- /dev/null +++ b/models/Friends.php @@ -0,0 +1,32 @@ + array('keys' => array('FromUserId' => 'id'))); + + /** + * Returns the opposite value of whatever is passed in (like an enum) + * @param Mixed $input + * @return String or Int; + */ + public static function status($input) { + if (is_int($input)) { + switch($input) { + case "rejected": return 0; + case "accepted": return 1; + case "open": return 2; + } + } + else { + switch($input) { + case 0: return "rejected"; + case 1: return "accepted"; + case 2: return "open"; + } + } + } +} +?> \ No newline at end of file diff --git a/models/User.php b/models/User.php index cacda49..6bfa423 100644 --- a/models/User.php +++ b/models/User.php @@ -10,7 +10,7 @@ use \lithium\security\Password; class User extends \lithium\data\Model { - public $hasMany = array('Post'); + public $hasMany = array('Post', 'Friends' => array('keys' => array('id' => 'ToUserId'))); public static function __init() { @@ -223,27 +223,12 @@ class User extends \lithium\data\Model { return $posts; } - /** - * Increments the amount profile views for the user. - * This is the command we are going to give to Mongo, which breaks down to db.users.update({_id : $id}, {$inc: {profileViews : 1}} ) - * Which says, find the user by their mongo ID, then increment their profile views by one. - * @param User $entity The instance of user to increment - * @param string $type Not implemented but in the future will allow you to increment anime manga and kdrama list views :TODO: - * @return null - */ - public function incrementViews($entity, $type = null) + public function myFriends($entity) { - if ($type = null) - { - $views = 'profileViews'; - } - $updateData = array('$inc' => array('profileViews' => 1)); - $conditions = array('_id' => $entity['_id']); - $result = User::update($updateData, $conditions, array('atomic' => false)); - return $result; + return self::find(array('conditions' => array('id' => $entity->friends->map(function ($f) { return $f->FromUserId;})))); + //return static::FindAllById($entity->friends->map(function ($f) { return $f->FromUserId; })); } - //Overrides save so we can do some stuff before the data is commited to the database. public function save($entity, $data = null, array $options = array()) { diff --git a/views/friends/add.html.php b/views/friends/add.html.php new file mode 100644 index 0000000..079c5d7 --- /dev/null +++ b/views/friends/add.html.php @@ -0,0 +1,4 @@ +form->create(); ?> + form->textbox('userid'); ?> + form->submit('request'); ?> +form->end(); ?> \ No newline at end of file diff --git a/views/friends/index.html.php b/views/friends/index.html.php new file mode 100644 index 0000000..41fbeb7 --- /dev/null +++ b/views/friends/index.html.php @@ -0,0 +1,7 @@ +myFriends() as $friend) { +echo $friend->username; +} + +?> \ No newline at end of file -- cgit v1.2.3