Campustream 1.0
A social network MQP for WPI
application/controllers/messaging.php
Go to the documentation of this file.
00001 <?
00002 
00010 class Messaging_Controller extends Controller {
00011         public $enable_session = true;
00012         public $template = 'template/main';
00013         
00017         public function index() {
00018                 if (!$this->session->get('authenticated')) {
00019                         View::respond_to('html', function () {
00020                                 return Hub::redirect('/login');
00021                         });
00022                         
00023                         View::respond_to(array('json', 'xml'), function () {
00024                                 return Hub::http_error(403, "Not authorized");
00025                         });
00026                 }
00027                 
00028                 $r = RedisManager::connection();
00029                 
00030                 $active_user = sess::getUser();
00031                 
00032                 // Get # of users we've interacted with
00033                 $num = $r->zcard("messages:user:{$active_user->id}");
00034                 $users = array();
00035                 $msg_count = array();
00036                 
00037                 if ($num) {
00038                         $result = $r->zrevrange("messages:user:{$active_user->id}", 0, $num);
00039                         
00040                         foreach ($result as $user) {
00041                                 $_user = ActiveCache::find('User_Model', $user, 43200)->sql(
00042                                         "SELECT * FROM users WHERE id = $user LIMIT 1"
00043                                 );
00044                                 
00045                                 if ($_user->is_loaded()) {
00046                                         $users[] = $_user;
00047                                         
00048                                         $msg_count[$_user->id] = $r->llen("messages:user:{$active_user->id}:withuser:{$_user->id}");
00049                                 }
00050                         }
00051                 }
00052                 
00053                 $template = $this->template;
00054                 View::respond_to('html', function () use($template, $users, $msg_count) {
00055                         $view = new View('messages/index');
00056                         $view->msg_users = $users;
00057                         $view->msg_count = $msg_count;
00058                         
00059                         $template->content = $view->render();
00060                         echo $template->render();
00061                 });
00062                 
00063                 View::respond_to(array('json', 'xml'), function ($format) use($users, $msg_count) {
00064                 
00065                 });
00066         }
00067 }