Bläddra i källkod

Implemented mySQL based friending

Michael Francis 14 år sedan
förälder
incheckning
fda3b17b03

+ 98 - 0
controllers/FriendsController.php

@@ -0,0 +1,98 @@
+<?php
+
+namespace app\controllers;
+
+use app\models\Friends;
+use lithium\action\DispatchException;
+use lithium\security\Auth;
+
+use app\models\User;
+
+class FriendsController extends \lithium\action\Controller {
+
+	public function index() {
+		$id = Auth::check('default');
+		$id = $id['id'];
+		$conditions = array('User.id' => $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');
+	}
+}
+
+?>

+ 32 - 0
models/Friends.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace app\models;
+
+class Friends extends \lithium\data\Model {
+
+	public $validates = array();
+	public $belongsTo = array('User' => 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";     
+			}
+		}
+	}
+}
+?>

+ 4 - 19
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()) 
 	{

+ 48 - 0
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 */;

+ 4 - 0
views/friends/add.html.php

@@ -0,0 +1,4 @@
+<?= $this->form->create(); ?>
+	<?= $this->form->textbox('userid'); ?>
+	<?= $this->form->submit('request'); ?>
+<?= $this->form->end(); ?>

+ 7 - 0
views/friends/index.html.php

@@ -0,0 +1,7 @@
+<?php
+
+foreach($user->myFriends() as $friend) {
+echo $friend->username; 
+}
+
+?>