summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Francis <edude03@gmail.com>2011-07-16 00:50:36 -0400
committerMichael Francis <edude03@gmail.com>2011-07-16 00:51:12 -0400
commit8e468b8605eb1a85d32dbba4d41a9821daaa4016 (patch)
tree1b596a89ce0c8362883ebd3727a216155a0cf74a
parenta277c978e66c970231b3cf987d220f3476fe456e (diff)
downloadotakuhub-8e468b8605eb1a85d32dbba4d41a9821daaa4016.tar.xz
Implemented mySQL based friending
-rw-r--r--controllers/FriendsController.php98
-rw-r--r--models/Friends.php32
-rw-r--r--models/User.php23
-rw-r--r--views/friends/add.html.php4
-rw-r--r--views/friends/index.html.php7
5 files changed, 145 insertions, 19 deletions
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 @@
+<?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');
+ }
+}
+
+?> \ 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 @@
+<?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";
+ }
+ }
+ }
+}
+?> \ 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 @@
+<?= $this->form->create(); ?>
+ <?= $this->form->textbox('userid'); ?>
+ <?= $this->form->submit('request'); ?>
+<?= $this->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 @@
+<?php
+
+foreach($user->myFriends() as $friend) {
+echo $friend->username;
+}
+
+?> \ No newline at end of file