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 +++++++++++++++++++++++++++++++++++ models/Friends.php | 32 ++++++++++++ models/User.php | 23 ++------ setup/otakuhub_friends_2011-07-16.sql | 48 +++++++++++++++++ views/friends/add.html.php | 4 ++ views/friends/index.html.php | 7 +++ 6 files changed, 193 insertions(+), 19 deletions(-) create mode 100644 controllers/FriendsController.php create mode 100644 models/Friends.php create mode 100644 setup/otakuhub_friends_2011-07-16.sql 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/setup/otakuhub_friends_2011-07-16.sql b/setup/otakuhub_friends_2011-07-16.sql new file mode 100644 index 0000000..9409825 --- /dev/null +++ b/setup/otakuhub_friends_2011-07-16.sql @@ -0,0 +1,48 @@ +# ************************************************************ +# Sequel Pro SQL dump +# Version 3348 +# +# http://www.sequelpro.com/ +# http://code.google.com/p/sequel-pro/ +# +# Host: localhost (MySQL 5.5.14) +# Database: otakuhub +# Generation Time: 2011-07-16 00:52:59 -0400 +# ************************************************************ + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + + +# Dump of table friends +# ------------------------------------------------------------ + +CREATE TABLE `friends` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `FromUserId` int(11) unsigned NOT NULL, + `ToUserId` int(11) unsigned NOT NULL, + `StatusId` tinyint(4) DEFAULT NULL, + `SentTime` datetime DEFAULT NULL, + `ResponseTime` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `FromUserId` (`FromUserId`), + KEY `ToUserId` (`ToUserId`), + CONSTRAINT `friends_ibfk_1` FOREIGN KEY (`FromUserId`) REFERENCES `users` (`id`), + CONSTRAINT `friends_ibfk_2` FOREIGN KEY (`ToUserId`) REFERENCES `users` (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; + + + + +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 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