Campustream 1.0
A social network MQP for WPI
application/controllers/notification.php
Go to the documentation of this file.
00001 <?
00002 
00006 class Notification_Controller extends Controller implements REST {
00007         public $enable_session = true;
00008         
00012         public function mark_as_read() {
00013                 if (!$this->session->get('authenticated')) {
00014                         return Hub::http_error(403, "Not authorized");
00015                 }
00016                 
00017                 $type = trim($_POST['type']);
00018                 if (!$type) {
00019                         return Hub::http_error(401, "Must specify which type of notifications to set as read");
00020                 }
00021                 
00022                 $n = notifications::load();
00023                 if (!$n->markAsRead($type)) {
00024                         return Hub::http_error(401, "Invalid type given or error marking as read");
00025                 }
00026                 
00027                 View::respond_to(array('json', 'xml'), function ($format) use($n) {
00028                         $num = array(
00029                                 'news' => $n->numUnread('news'),
00030                                 'collaborate' => $n->numUnread('collaborate'),
00031                                 'reply' => $n->numUnread('reply')
00032                         );
00033                         
00034                         if ($format == 'json') {
00035                                 echo json_encode($num);
00036                         }
00037                 });
00038         }
00039         
00043         public function show() {
00044                 if (!$this->session->get('authenticated')) {
00045                         return Hub::http_error(403, "Not authorized");
00046                 }
00047                 
00048                 $n = notifications::load();
00049                 
00050                 $msgs = array();
00051                 $msgs['news'] = array_merge($n->get('news'), $n->get('newscomment'));
00052                 $msgs['collaborate'] = $n->get('collaborate');
00053                 $msgs['reply'] = $n->get('reply');
00054                 
00055                 
00056                 View::respond_to(array('xml', 'json'), function ($format) use($msgs) {
00057                         if ($format == 'json') {
00058                                 echo json_encode($msgs);
00059                         }
00060                 });
00061         }
00062 }