summaryrefslogtreecommitdiffstats
path: root/controllers
diff options
context:
space:
mode:
authorMichael Francis <edude03@gmail.com>2011-05-28 13:28:16 -0400
committerMichael Francis <edude03@gmail.com>2011-05-28 13:28:16 -0400
commit2389d66da849798f8d4ec5f10e3b07c11da49185 (patch)
treee22556d12982395b469a23420c662662e3e064cc /controllers
downloadotakuhub-2389d66da849798f8d4ec5f10e3b07c11da49185.tar.xz
Initial Commit
Diffstat (limited to 'controllers')
-rw-r--r--controllers/AdminController.php100
-rw-r--r--controllers/AnimelistController.php77
-rw-r--r--controllers/ContentController.php62
-rw-r--r--controllers/FeedsController.php79
-rw-r--r--controllers/PagesController.php36
-rw-r--r--controllers/PhotosController.php41
-rw-r--r--controllers/ProfileController.php75
-rw-r--r--controllers/ProfilesController.php41
-rw-r--r--controllers/SearchController.php37
-rw-r--r--controllers/TopicController.php25
-rw-r--r--controllers/UsersController.php473
11 files changed, 1046 insertions, 0 deletions
diff --git a/controllers/AdminController.php b/controllers/AdminController.php
new file mode 100644
index 0000000..3e4dfab
--- /dev/null
+++ b/controllers/AdminController.php
@@ -0,0 +1,100 @@
+<?php
+namespace app\controllers;
+
+use app\models\User;
+use lithium\security\Auth;
+use lithium\util\String;
+use li3_access\security\Access;
+use li3_flash_message\extensions\storage\FlashMessage;
+use lithium\action\Dispatcher;
+
+class AdminController extends \lithium\action\Controller {
+ public function index()
+ {
+ $limit = 5;
+ $page = $this->request->page ?: 1;
+ //$order = array('created' => 'DESC');
+ $total = User::count();
+ $users = User::all(compact('limit','page'));
+ $this->render(array('layout' => 'admin', 'data' => compact('users', 'total', 'page', 'limit')));
+ }
+
+ public function users()
+ {
+ $users = User::all();
+ $this->render(array('layout' => 'admin', 'data' => compact('users')));
+ //Should have paginate for when there is more users.
+ }
+
+
+ //This is basically admins version of signup
+ public function addUser()
+ {
+ $sucsess = false;
+
+ //If the request isn't empty
+ if($this->request->data) {
+ //Does admins data need to be validated?
+ $user = User::Create($this->request->data);
+ $sucsess = $user->save();
+ }
+ if ($sucsess) {
+ return $this->redirect('Users');
+ }
+
+ FlashMessage::set($user->name . "was added sucessfully.");
+ }
+
+ public function editUser($username = NULL)
+ {
+ if ($username != NULL)
+ {
+ $user = User::find('first', array('conditions' => compact('username')));
+
+ if($this->request->data)
+ {
+ $user->set($this->request->data);
+ if ($user->save(null, array('validate' => false)))
+ {
+ FlashMessage::set('User updated sucsessfully');
+ $this->redirect('Admin::index');
+ }
+ else
+ {
+ FlashMessage::set('There was an error');
+ $this->redirect('Admin::index');
+
+ }
+ }
+ else
+ {
+ //unset($user->password);
+ return compact('user');
+ }
+ }
+ }
+
+ public function removeUser($username)
+ {
+ /*
+ //Form data needs to have $username and $confirm = true to do the delete.
+ if($this->request->data)
+ {
+ //If the user has confirmed the deletion of the user.
+ if($this->request->data->confirm)
+ { */
+ $user = User::find('first', array('conditions' => compact('username')));
+ $user->delete();
+ FlashMessage::set("User was deleted sucsessfully.");
+ $this->redirect('Admin');
+ //}
+ }
+ /*else
+ {
+ //Render the form
+ $this->render(array('layout' => 'form', 'data' => compact('users')));
+
+ }*/
+ //}
+}
+?> \ No newline at end of file
diff --git a/controllers/AnimelistController.php b/controllers/AnimelistController.php
new file mode 100644
index 0000000..c61577a
--- /dev/null
+++ b/controllers/AnimelistController.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace app\controllers;
+
+use app\models\contentList;
+use app\models\User;
+use app\models\Anime;
+
+
+class AnimeListController extends \lithium\action\Controller {
+ public $publicActions = array('view');
+ public function view($username, $sort = "all")
+ {
+ $user = User::find('first', array('conditions' => compact('username')));
+
+
+ $watching = array();
+ $paused = array();
+ $dropped = array();
+ $planning = array();
+ $finished = array();
+
+
+ foreach($user->animelist as $entry)
+ {
+ switch($entry->my_status)
+ {
+ case "Completed": $finished[] = $entry; break;
+ case "Watching": $watching[] = $entry; break;
+ case "On-Hold" : $paused[] = $entry; break;
+ case "Dropped" : $dropped[] = $entry; break;
+ case "Plan to Watch": $planning[] = $entry; break;
+ }
+ }
+
+
+ //In the future we can use set or something
+ switch($sort)
+ {
+ case "planning" : return compact('user', 'planning'); break;
+ case "completed" : return compact('user', 'finished'); break;
+ case "onhold": return compact('user', 'paused'); break;
+ case "watching" : return compact('user', 'watching'); break;
+ case "dropped": return compact('user', 'dropped');
+ default: return compact('user', 'watching', 'paused', 'dropped', 'planning', 'finished'); break;
+ }
+ }
+
+ public function add($id)
+ {
+ if (empty($this->request->data))
+ {
+ $anime = Anime::find('first', array('conditions' => array('special_id' => $id)));
+ $entry = null;
+ return compact('anime', 'entry');
+ }
+
+ $entry = Entry::create($this->request->data);
+
+ if (isset($this->request->data['tags']))
+ {
+ $entry->my_tags = explode(' ', $this->request->data['tags']);
+ unset($this->request->data['tags']);
+ }
+
+ if ($entry->validates()) {
+ $entry->add($username);
+
+ return $this->redirects('Animelist::Index');
+ }
+
+ return $entry;
+
+ }
+
+
+} \ No newline at end of file
diff --git a/controllers/ContentController.php b/controllers/ContentController.php
new file mode 100644
index 0000000..688c2a8
--- /dev/null
+++ b/controllers/ContentController.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace app\controllers;
+use app\models\anime;
+
+class ContentController extends \lithium\action\Controller {
+ public $publicActions = array('anime');
+
+ public function index($type) //type has to equal something
+ {
+
+
+
+ switch($type) {
+ case "anime": $content = Anime::all(compact('limit','page','order'));
+ $total = Anime::count();
+ break;
+
+ case "manga":
+
+ case "kdrama": $content = Kdrama::all(compact('limit', 'page', 'order'));
+ $total = Kdrama::count();
+ break;
+ }
+
+ return compact('content', 'total', 'page', 'limit');
+ }
+
+ public function manga($id = null)
+ {
+ if ($id != null)
+ {
+
+ }
+ else
+ {
+ $content = Manga::all(compact('limit', 'page', 'order'));
+ $total = Manga::count();
+ }
+ }
+
+ public function anime($id = null)
+ {
+ $limit = 20;
+ $page = $this->request->page ?: 1;
+ $order = array('title' => 'ASC');
+ $content;
+ $total;
+
+ if ($id != null)
+ {
+ $content = Anime::find('first', array('conditions' => array('special_id' => $id), 'order' => array('title' => 'ASC')));
+ return compact('content');
+ }
+ else
+ {
+ $content = Anime::all(compact('limit','page','order'));
+ $total = Anime::count();
+ $this->render(array('template' => 'index', 'data' => compact('limit', 'page', 'content', 'total')));
+ }
+ }
+} \ No newline at end of file
diff --git a/controllers/FeedsController.php b/controllers/FeedsController.php
new file mode 100644
index 0000000..dfbd5f3
--- /dev/null
+++ b/controllers/FeedsController.php
@@ -0,0 +1,79 @@
+<?php
+
+namespace app\FeedsController;
+
+
+use lithium\security\Auth;
+use app\models\Post;
+
+class FeedsController extends \lithium\action\Controller {
+ //Class Variables
+ //public $validates =
+
+
+ public function index()
+ {
+ //If the user isn't authenticated
+ if(!Auth::check('default'))
+ {
+ //Redirect them to the login/welcome page
+ $this->redirect('Users::login');
+ }
+
+ //else
+ //Figure out what user is logged in (from their session cookie presumably)
+ /* Display the users last 20 posts in decending order. */
+ $user = Session::read('username');
+
+ //Since there can only be one of each username, getting the first occurence of $user should be fine
+ $query = User::find('first', array('conditions' => array('username' => $user)));
+ $feed = $query->posts;
+
+
+ return compact($feed); //Return the feed array to the
+ }
+
+ /**
+ * New needs to do a few things
+ * 1) Validation,
+ * Ensure that the post is unique,
+ * Flood protection
+ * Spam Protection at some point
+ * 2) Storage
+ * The post needs to be stored in the users feed as well as users who are friends with them
+ *
+ */
+ public function new()
+ {
+
+ }
+
+ /**
+ * Hide needs to put a "HIDES" edge on the graph
+ * By getting the users document from the database, querying its id
+ *then doing something like $thisUser->hides($username)
+ */
+ public function hide($username)
+ {
+
+ }
+
+
+ /**
+ *Does the same sort of validation as new, but deletes a post obviously :P
+ */
+ public function delete($id)
+ {
+ $user = Auth::check('default');
+ $post = Post::find($id);
+
+ if ($post->username == $user['username']) {
+ $post->delete();
+ }
+
+ return $this->redirect('Feed::Index');
+
+ }
+}
+
+?> \ No newline at end of file
diff --git a/controllers/PagesController.php b/controllers/PagesController.php
new file mode 100644
index 0000000..47b88d2
--- /dev/null
+++ b/controllers/PagesController.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Lithium: the most rad php framework
+ *
+ * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ */
+
+namespace app\controllers;
+
+/**
+ * This controller is used for serving static pages by name, which are located in the `/views/pages`
+ * folder.
+ *
+ * A Lithium application's default routing provides for automatically routing and rendering
+ * static pages using this controller. The default route (`/`) will render the `home` template, as
+ * specified in the `view()` action.
+ *
+ * Additionally, any other static templates in `/views/pages` can be called by name in the URL. For
+ * example, browsing to `/pages/about` will render `/views/pages/about.html.php`, if it exists.
+ *
+ * Templates can be nested within directories as well, which will automatically be accounted for.
+ * For example, browsing to `/pages/about/company` will render
+ * `/views/pages/about/company.html.php`.
+ */
+class PagesController extends \lithium\action\Controller {
+ // !--Waring--! this makes all pages accessibe to non-logged in uses.
+ // :TODO: Fix this.
+ public $publicActions = array('view');
+ public function view() {
+ $path = func_get_args() ?: array('home');
+ return $this->render(array('template' => join('/', $path)));
+ }
+}
+
+?> \ No newline at end of file
diff --git a/controllers/PhotosController.php b/controllers/PhotosController.php
new file mode 100644
index 0000000..ea41234
--- /dev/null
+++ b/controllers/PhotosController.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace app\controllers;
+
+use app\models\Photo;
+
+class PhotosController extends \lithium\action\Controller {
+
+ public function index() {
+ $photos = Photo::all();
+ return compact('photos');
+ }
+
+ public function view() {
+ $photo = Photo::first($this->request->id);
+ return compact('photo');
+ }
+
+ public function add() {
+ $photo = Photo::create();
+
+ if (($this->request->data) && $photo->save($this->request->data)) {
+ $this->redirect(array('Photos::view', 'id' => $photo->id));
+ }
+ return compact('photo');
+ }
+
+ public function edit() {
+ $photo = Photo::find($this->request->id);
+
+ if (!$photo) {
+ $this->redirect('Photos::index');
+ }
+ if (($this->request->data) && $photo->save($this->request->data)) {
+ $this->redirect(array('Photos::view', 'args' => array($photo->id)));
+ }
+ return compact('photo');
+ }
+}
+
+?> \ No newline at end of file
diff --git a/controllers/ProfileController.php b/controllers/ProfileController.php
new file mode 100644
index 0000000..8e5d487
--- /dev/null
+++ b/controllers/ProfileController.php
@@ -0,0 +1,75 @@
+<?php
+
+namespace app\controllers;
+use app\models\User;
+use \lithium\security\Auth;
+
+class ProfileController extends \lithium\action\Controller {
+ public $publicActions = array("view");
+
+ public function view($username)
+ {
+ //Find the user profile
+ $user = User::find('first', array('conditions' => compact('username')));
+ //If the user variable isn't empty a user was found.
+ if (!empty($user))
+ {
+
+ //The only issue(?) is that this will update the profile views even if the user views their own profile, maybe we should fix that.
+ $user->incrementViews();
+
+ $photo = null;
+ $profile = $user->profile;
+ return compact('user', 'photo', 'profile');
+ }
+ else
+ {
+ //Tell the user that user wasn't found.
+ }
+ }
+
+ public function create()
+ {
+
+ }
+
+ public function edit($username)
+ {
+ $user = Auth::check('default');
+
+ if ($user) {
+ $user = User::find('first', array('conditions' => array('username' => $user['username'])));
+ $profile = $user->profile;
+ $photo;
+
+
+ if (empty($this->request->data)) {
+ return compact('profile', 'user');
+ }
+
+ //If a photo was uploaded
+ if (isset($this->request->data['photo']))
+ {
+ /* Update / Add the profile picture */
+ //Store the image in grid FS
+ $photo = Photo::create($this->request->data["photo"]);
+
+ //We don't to resave the photo in the profile data
+ unset($this->request->data["photo"]);
+
+ //Save the photo
+ $photo->save();
+
+ //Since images are accessed via /image/mongoid we just store the mongo id so we
+ //can substitue it in the <img src=""> tag.
+ $user->profilepic = $photo->_id->__toString();
+ }
+
+ $user->profile = $this->request->data;
+
+ if ($user->save(null, array('validate' =>false))) {
+ return $this->redirect("/profile/view/$user->username");
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/controllers/ProfilesController.php b/controllers/ProfilesController.php
new file mode 100644
index 0000000..022b540
--- /dev/null
+++ b/controllers/ProfilesController.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace app\controllers;
+
+use app\models\Profile;
+
+class ProfilesController extends \lithium\action\Controller {
+
+ public function index() {
+ $profiles = Profile::all();
+ return compact('profiles');
+ }
+
+ public function view() {
+ $profile = Profile::first($this->request->id);
+ return compact('profile');
+ }
+
+ public function add() {
+ $profile = Profile::create();
+
+ if (($this->request->data) && $profile->save($this->request->data)) {
+ $this->redirect(array('Profiles::view', 'args' => array($profile->id)));
+ }
+ return compact('profile');
+ }
+
+ public function edit() {
+ $profile = Profile::find($this->request->id);
+
+ if (!$profile) {
+ $this->redirect('Profiles::index');
+ }
+ if (($this->request->data) && $profile->save($this->request->data)) {
+ $this->redirect(array('Profiles::view', 'args' => array($profile->id)));
+ }
+ return compact('profile');
+ }
+}
+
+?> \ No newline at end of file
diff --git a/controllers/SearchController.php b/controllers/SearchController.php
new file mode 100644
index 0000000..fea8e41
--- /dev/null
+++ b/controllers/SearchController.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace app\controllers;
+
+use app\models\Anime;
+use \MongoRegex;
+
+class SearchController extends \lithium\action\Controller {
+ public function index($type, $by = "series_title")
+ {
+
+ if (empty($this->request->query['search'])) {
+ //Redirect them or something
+ }
+
+
+ $searchParam = '/' . $this->request->query['search'] . '/i';
+
+ $content;
+ $limit = 20;
+ $page = $this->request->page ?: 1;
+ $total; //<-- number of search results
+
+
+ switch($type)
+ {
+ case "anime": $content = Anime::find('all', array('conditions' => array('title' => array('like' => $searchParam)), $limit, $page));
+ $total = Anime::count(array('title' => array('like' => $searchParam)));
+ break;
+ case "kdrama": break;
+ case "manga": break;
+ }
+ return compact('content', 'type', 'by', 'limit', 'total', 'page');
+ }
+
+
+} \ No newline at end of file
diff --git a/controllers/TopicController.php b/controllers/TopicController.php
new file mode 100644
index 0000000..827c188
--- /dev/null
+++ b/controllers/TopicController.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace app\controllers;
+use app\models\Topic;
+
+class TopicController extends \lithium\action\Controller {
+ public function index()
+ {
+ //We'll write this eventually
+ }
+
+ public function view($topicname)
+ {
+ $topic = Topic::find('first', array('conditions' => compact('topicname')));
+ }
+
+ public function new()
+ {
+ if ($this->request->data)
+ {
+ $topic = Topic::create($this->request->data);
+ $topic->save();
+ }
+ }
+} \ No newline at end of file
diff --git a/controllers/UsersController.php b/controllers/UsersController.php
new file mode 100644
index 0000000..15da52b
--- /dev/null
+++ b/controllers/UsersController.php
@@ -0,0 +1,473 @@
+<?php
+
+namespace app\controllers;
+
+use lithium\storage\Session;
+use app\models\User;
+use app\models\confirmKey;
+use app\models\ProfilePic;
+use app\models\Post;
+use lithium\security\Auth;
+use lithium\util\String;
+use \MongoDate;
+use li3_flash_message\extensions\storage\FlashMessage;
+use app\libraries\openID\LightOpenID;
+
+class UsersController extends \lithium\action\Controller {
+ public $secret = "marshmellows"; //I don't know why either?
+
+ //Make login a public action.
+ public $publicActions = array('login', 'logout', 'signup', 'confirm');
+
+ /* User profile implementation */
+ /* So, like twitter if you friend someone but they don't friend you,
+ * You can see their posts, but they don't see yours. Furthermore, posts that start with a @username
+ * shouldn't be shown to the user either.
+ *
+ * Now say for instance the the user isn't logged in but visits the profile of another user. It should show the users posts
+ * that aren't set to private. this should be as easy as Posts::find('all', array('conditions' => array('level' != private)))
+ * However, we need to differentiate posts that are Hidden (posts that begin with a @username), Friends only (set by user) or
+ * Public (by default all posts.) there should be a method that filters the new post method, that takes a post, determines
+ * what access level it should be then passes that back to the calling method (in save?)
+ *
+ * Of course, for logged in users, we need to do a multiselect I guess, something like find posts where access level is hidden
+ * friends only, and public, ordered by date (descending) limit 20 or so (use pagination plugin)
+ *
+ * Finally, there should be an an option to make all posts private, this can be done by the postlevel() method, it can check i
+ * the user has the private option set, then if true return private for all posts :)
+ */
+
+ public function index($username = null)
+ {
+ //If no username was entered,
+ if ($username == null)
+ {
+ //Show a user list?
+ // TODO: Pagination
+ //Show a list of all users
+ $users = User::all();
+ return compact('users');
+ //maybe we can use the endpoint hook and show a "network activity feed"
+ //For example show all public posts as they get posted using ajax ?
+ //Maybe we should have a route for this? /activity maybe?
+ }
+ //Otherwise get that user
+ $user = User::find('first', array('conditions' => compact('username')));
+
+ //find all posts, sort by date, order descending, limit to 20
+ $feed;
+
+ //Dont know if php will keep counts return on the stack so I'm storing it to avoid calling it 3 times.
+ $tmpCount = count($user->feed);
+
+ //If the user has 20 or more posts then query 20, otherwise query the number of posts the user has
+ $count = ($tmpCount >= 20) ? 20 : $tmpCount;
+
+ //If the user is logged in
+ $user = Auth::check('default');
+ if ($user)
+ {
+ if ($user->username == $username)
+ {
+ $feed = $user->getPosts(array('level' => null));
+ return compact($feed, $user);
+ }
+ //if the user is logged in and the users are friends
+ if (User::areFriend(Auth::check('default'), $user))
+ {
+ $feed = $user->getPosts(array('level' => null));
+ return compact($feed, $user);
+ }
+ } //If they aren't friend, or if the user isn't logged in it defaults to the public version (filter private posts)
+ //If the user isn't logged in, then we should show a signup partial like twitter
+ else
+ {
+ //find all non-private posts, limit 20, order descending (decending was taken care of with array_unshift btw)
+ $feed = $user->getPost(array('level' => array('$ne' => "private")));
+ return compact($feed, $user);
+ }
+ }
+
+
+ /**
+ * How this method should work:
+ * Since this network is default open like twitter, there is three types of relationships:
+ * 1) Mutually Oblivious: Neither user knows each other.
+ * 2) You Follow: You follow a user and sees their non-private posts
+ * 3) Friends: being friends allows you to see each others privates posts
+ * (@see app\models\post) for an example of how this works
+ *
+ * Now, for now there are two types of requests: Friend requests which causes both users to automatically follow eachother
+ * And follows. Follows are unidirectional and thus don't require the followees permission
+ */
+ public function follow($username = null)
+ {
+ $status;
+ $message;
+ if($this->request->data)
+ {
+ $user = Auth::check('default');
+ if ($user)
+ {
+ $user = User::find('first', array('conditions' => array('username' => $user['username'])));
+ if ($user->follow($username))
+ {
+ $status = true;
+
+ return array('sucseeded' => true, 'message' => 'You are now following $username');
+ }
+ }
+ }
+ }
+
+ /**
+ * Calls the user to addFriend method, then returns the status.
+ * @param username The name of the user to add as a friend
+ */
+ public function addfriend($username = null)
+ {
+
+ }
+
+ public function reconfirm()
+ {
+ //Search the database for email address
+ if (ConfirmKey::count(array('conditions' => array('email' => $this->request->data['email']))) != 0)
+ {
+ //Resend the confirmation email
+ }
+ else
+ {
+ //Show them a message
+ }
+
+ //Send a new key
+ }
+
+ public function profile($username)
+ {
+ //If no username is passed in.
+ if (empty($username))
+ {
+ $this->redirect('/');
+ }
+
+ //Otherwise
+ else
+ {
+ //Find that user, and go to their profile page.
+ $user = User::find('first', array('conditions' => compact('username')));
+ //$photo = (empty($user->profilepic) ? ProfilePic::create() : $user->profilepic);
+ return compact('user', 'photo');
+ //Render the profile layout
+
+ }
+ }
+
+ public function post()
+ {
+ if ($this->request->data)
+ {
+ $user = Auth::check('default');
+ if ($user) {
+ $user = User::create($user, array('exists' => true));
+ $user->post($this->request->data);
+ }
+
+ /* :TODO: Need to return a status here */
+ $this->redirect('Users::feed');
+ }
+ }
+
+
+ public function feed()
+ {
+ //Get the currently logged in user
+ $user = Auth::check('default');
+
+ //If there is a user logged in (There should be since feed isn't a public function)
+ if ($user)
+ {
+ //Get that user from the database
+ $user = User::find('first', array('conditions' => array('username' => $user['username'])));
+
+ //Set the feed variable scope (since we are going to use it outside the loop)
+ $feed;
+
+ //For each post ID in $users feed,
+ foreach ($user->feed as $post)
+ {
+ //Find the post by it's ID
+ $post = Post::find($post);
+
+ //If a post was found,
+ if (!empty($post))
+ {
+ //Add it to the feed
+ $feed[] = $post;
+ }
+ //Else we should remove the the ID from the users feed.
+ }
+
+ /* new posts are appended to the end of the feed array, therefore new posts naturally end up at the bottom of the feed
+ * therefore, we reverse the order of the array so that new posts end up at the top of the feed.
+ * This is probably faster than doing sorting by date at the database level, though it for some reason
+ * posts don't get inserted in the right order it could cause them to come out wrong in the view */
+ $feed = array_reverse($feed);
+
+ //This renders a custom layout we use for the feed, then passes user and feed to the view for the variables.
+ $this->render(array('layout' => 'untitled', 'data' => compact('user', 'feed')));
+ }
+
+ }
+
+ public function openid()
+ {
+ if ($this->request->data)
+ {
+ if (!empty($this->request->query))
+ {
+ var_dump($this->request->query);
+ }
+ else
+ {
+ $openid = new LightOpenID;
+ echo $openid->validates();
+ }
+ }
+ }
+
+ public function signup()
+ {
+ //If the request isn't empty
+ if($this->request->data)
+ {
+ //Create a user from the data
+ $user = User::Create($this->request->data);
+
+ //Until the save bug is fixed
+ $results = $user->validates();
+
+ if ($results)
+ {
+ //The user isn't active until after they confirm.
+ $user->confirmed = false;
+ $user->active = false;
+ $user->joinedOn = new MongoDate();
+
+ //Generate a confirmation key for the user
+ $key = confirmKey::Create(array('key' => confirmKey::generate($user->email), 'username' => $user->username));
+
+ //Save it to the database
+ $key->save();
+
+ //If everything goes ok
+ if ($user->save(null, array('validates' => false)))
+ {
+ //Store some session information
+ //Session::write('username', $user->username);
+ //Session::write('email', $user->email);
+
+ //For the debug version, send the key to the front page
+ $link = "/users/confirm";
+ return compact('key', 'link');
+
+ // /*
+ // //Send them to the confirmation page.
+ // $this->redirect('users/confirm');
+
+ }
+ }
+ else
+ {
+ return compact('user');
+ }
+ }
+ }
+
+ public function login($location = null, $args = null)
+ {
+ //Put in a check to make sure the user has confirmed their account
+ //The check should probably happen below after the auth check.
+ /*
+ If the user is valid, but not confirmed,
+ tell the user they haven't confirmed,
+ offer to resend the confirmation email or changed their email address.
+ */
+ if (!empty($this->request->data)) {
+ $user = Auth::check('default', $this->request);
+ if ($user)
+ {
+ $user = User::find('first', array('conditions' => array('username' => $user['username'])));
+ $user->lastLogin = new MongoDate();
+ $user->save(null, array('validate' => false));
+
+
+ //If the user hasn't confirmed their account
+ if(!$user->confirmed)
+ {
+ //Redirect them to the confirmation page.
+ return $this->redirect('Users::confirm');
+ }
+
+ //If the user's account is not active they are probably banned
+ if (!$user->active)
+ {
+ return $this->redirect('/pages/banned');
+ }
+
+ //If the user was trying to go somewhere, redirect them there
+ if ($location != null)
+ {
+
+ }
+ //Otherwise send them to the hompa
+ return $this->redirect('Users::feed');
+ }
+ else
+ {
+ FlashMessage::set('Username or Password Incorrect.');
+ }
+ }
+ }
+
+ //Logout
+ public function logout()
+ {
+ //If the user logs out
+ //Clear their auth cookie
+ Auth::Clear('default');
+
+ //Redirect them to the homepage.
+ return $this->redirect('/');
+ }
+
+ private function changePassword()
+ {
+ //Get the user to verify their current password
+ $input = $this->request->data;
+
+ //If there is inputfrom the form
+ if ($input)
+ {
+ //Get the user from auth
+ $user = Auth::check('default');
+ if(!empty($user) && ($data['newpass'] == $data['confirm']))
+ {
+ //find the user by their ID
+ $user = User::find($user['_id']);
+
+ //Set the newpassword, this triggers the hash function in ->save()
+ $user->newpass = $data['newpass'];
+
+ //Save the data
+ if ($user->save(null, array('validate' => false))) {
+ //Tell the user their password was updated sucsessfully
+ }
+
+ //Else there was an error, so send them away
+ /* If the compare is changed to a validator
+ * returning the user object will show the error in the view.*/
+ return compact('user');
+
+ }
+ }
+ }
+
+ public function requestFriend($username) {
+ //If the user isn't blocking this user,
+
+ //And the user doesn't have private set
+
+ //Send them a DM with a confirm key
+ $key = confirmKey::create();
+ $thisUser = auth::check('default');
+ $link = Html::link('here', "/users/confirmFriend/$this->username/$key->key");
+ $post = Post::create(array('body' => "$thisUser->username want's to be your friend. Click $link to confirm"));
+
+ $post->directMessage($username);
+
+ }
+
+ /* Potential hack here, in theory, a user could sign up a new account
+ * then send a direct message manually using the confirm key from the email.
+ */
+ public function confirmFriend($username, $key) {
+ /* Normally we could try and find the cKey, then if it doesn't exist
+ * Do something about it,
+ * However, ConfirmKey::validates basically counts the number of keys that
+ * match $key, this is probably better than find since ->validates() may be a bit
+ * more efficent */
+
+ $cKey = confirmKey::create(compact('key'));
+ if ($cKey->validates())
+ {
+ $thisUser = Auth::check('default');
+ $thisUser = User::find('first', array('conditions' => array('username' => $thisUser['username'])));
+ $requestingUser = User::find('first', array('conditions' => compact('username')));
+ $requestingUser->addFriend($thisUser->username);
+ $thisUser->addFriend($requetingUser->username);
+
+ //Some action here if true :TODO:
+ }
+
+ //Some action here if false
+ }
+
+
+ public function confirm($key = null)
+ {
+ //Situation one
+ //They have a key
+ if (!(empty($key)))
+ {
+ //Find the key in the database
+ $foundKey = confirmKey::find('first', array('conditions' => compact('key')));
+
+ //If the key exists
+ if($foundKey != NULL)
+ {
+ /* Note: foundKey->validates() does the same check, but it was added incase more validation is needed */
+ //Find that user in the database
+ $foundUser = User::find('first', array('conditions' => array("username" => $foundKey->username)));
+ $valid = ($foundUser != NULL);
+
+ //Set the users account active and confirmed.
+ $foundUser->confirmed = true;
+ $foundUser->active = true;
+
+ //If the user is saved sucsessfully,
+ if($foundUser->save(null, array('validate' => false)))
+ {
+ /* If the save is sucsessful we are done */
+ //Delete their key,
+ $foundKey->delete();
+
+ //Send them to the homepage (probably login though)
+ $this->redirect("/");
+
+ }
+ else
+ {
+ FlashMessage::set("There was an error.");
+ }
+
+ }
+ else
+ {
+ //Otherwise
+ FlashMessage::set("There was an error finding the key.");
+ return;
+ }
+ }
+ }
+
+
+ public function step2()
+ {
+ //Check that step1 is completed sucsessfully,
+ //Then take them to their profile in edit mode
+ }
+}
+
+?> \ No newline at end of file