Campustream 1.0
A social network MQP for WPI
application/controllers/news.php
Go to the documentation of this file.
00001 <?
00002 
00007 class News_Controller extends Controller implements REST {
00008         public $enable_session = true;
00009         public $template = 'template/main';
00010         
00014         public function index($args) {
00015                 if (isset($_GET['slug'])) {
00016                         $args['slug'] = $_GET['slug'];
00017                 }
00018                 if (isset($_GET['type'])) {
00019                         $args['type'] = $_GET['type'];
00020                 }
00021                 
00022                 if ($args['type'] == 'news' && $args['slug'] == 'imported') {
00023                         $args['type'] = 'imported';
00024                 }
00025         
00026                 $cat = 0;
00027                 if (isset($args['slug'])) {
00028                         $cat = Newseventcategory_Model::getBySlug($args['type'], $args['slug']);
00029                 }
00030                 
00031                 $news = null;
00032                 if (isset($args['slug'])) {
00033                         // Type & Category
00034                         $news = Newsevent_Model::load_popular(array('cat_id' => $cat->id));
00035                 } elseif (isset($args['type']) && !isset($args['slug'])) {
00036                         // Type & All Categories
00037                         $news = Newsevent_Model::load_popular(array('type' => $args['type']));
00038                 } else {
00039                         // All Types & All Categories
00040                         $news = Newsevent_Model::load_popular();
00041                 }
00042                 
00043                 for ($i = 0; $i < count($news); $i++) {
00044                         $news[$i]->load_category();
00045                         $news[$i]->load_user();
00046                 }
00047 
00048                 $template = $this->template;    
00049                 View::respond_to('html', function () use($news, $cat, $args, $template) {                       
00050                         $view = new View('news/index');
00051                         $view->posts = $news;
00052                         $view->type = (isset($args['type']) ? $args['type'] : 'all');
00053                         $view->category = $cat;
00054                         
00055                         $template->active = 'newsevents';
00056                         $template->content = $view->render();
00057                         echo $template->render();
00058                 });
00059                 
00060                 View::respond_to(array('json', 'jsonp', 'xml'), function ($format) use($news) {
00061                         for ($i = 0; $i < count($news); $i++) {
00062                                 $news[$i] = $news[$i]->limited_object();
00063                         }
00064                         
00065                         if ($format == 'json') {
00066                                 echo json_encode($news);
00067                         } elseif ($format == 'jsonp') {
00068                                 return;
00069                         } else {
00070                                 echo xml::encode_array($news, 'posts', 'post');
00071                         }
00072                 });
00073                 
00074                 View::respond_to('rss', function () use($news) {
00075                   $stories = array();
00076                   foreach ($news as $story) {
00077                     $stories[] = array(
00078                       'title' => $story->title,
00079                       'link' => 'http://campustream.com/ne/' . $story->short_id,
00080                       'description' => $story->post
00081                     );
00082                   }
00083 
00084                   echo xml::encode_rss($stories);
00085                 });
00086         }
00087         
00091         public function new_action() {
00092                 sess::require_login();
00093                 
00094                 $view = new View('news/new');
00095                 $this->template->active = 'newsevents';
00096                 $this->template->content = $view->render();
00097                 echo $this->template->render();
00098         }
00099         
00103         public function show($args) {
00104                 $id = null;
00105                 if (isset($args['short_id'])) {
00106                         $id = base_convert($args['short_id'], 36, 10);
00107                 } elseif (isset($_GET['id']) && is_numeric($_GET['id'])) {
00108                         $id = $args['id'];
00109                 } elseif (isset($_GET['short_id'])) {
00110                         $id = base_convert($_GET['short_id'], 36, 10);
00111                 } else {
00112                         View::respond_to('html', function () {
00113                                 Hub::redirect('/news');
00114                         });
00115                         
00116                         View::respond_to(array('json', 'jsonp', 'xml'), function () {
00117                                 Hub::http_error(400, "Missing or invalid ID");
00118                         });
00119                 }
00120                 
00121                 $news = ActiveCache::find('Newsevent_Model', $id, 43200)->sql(
00122                         "SELECT * FROM newsevents WHERE id = $id LIMIT 1"
00123                 );
00124                 
00125                 if (!$news->is_loaded()) {
00126                         View::respond_to('html', function () {
00127                                 Hub::redirect('/news');
00128                         });
00129                         
00130                         View::respond_to(array('json', 'jsonp', 'xml'), function () {
00131                                 Hub::http_error(400, "Missing or invalid ID");
00132                         });
00133                 }
00134                 
00135                 $news->load_category();
00136                 $news->load_user();
00137                 $news->load_comments();
00138                 $news->load_eventmeta();
00139                 
00140                 $template = $this->template;
00141                 View::respond_to('html', function () use($news, $template) {
00142                         $view = new View('news/show');
00143                         $view->news = $news;
00144                         
00145                         $template->active = 'newsevents';
00146                         $template->content = $view->render();
00147                         echo $template->render();
00148                 });
00149                 
00150                 View::respond_to(array('json', 'jsonp', 'xml'), function ($format) use($news) {
00151                         if ($news->type == 'text') {
00152                                 $news->post = bbparser::bb2html($news->post);
00153                         }
00154                         
00155                         echo $news->{"to_$format"}();
00156                 });
00157         }
00158         
00162         public function create() {
00163                 if (!$this->session->get('authenticated')) {
00164                         return Hub::http_error(403, "Not authorized");
00165                 }
00166                 
00167                 $title = htmlspecialchars(trim($_POST['title']), ENT_QUOTES, 'UTF-8');
00168                 $content = htmlspecialchars(trim($_POST['content']), ENT_QUOTES, 'UTF-8');
00169                 $category = $_POST['category'];
00170                 $type = strtolower(trim($_POST['type']));
00171                 
00172                 /* Data validation */
00173                 if (strlen($title) == 0 || strlen($content) == 0) {
00174                         return Hub::http_error(400, "Missing title or content");
00175                 }
00176                 
00177                 if (!is_numeric($category) || $category <= 0) {
00178                         return Hub::http_error(400, "Invalid or missing category");
00179                 }
00180                 
00181                 if ($type != 'text' && $type != 'link') {
00182                         return Hub::http_error(400, "Invalid post type");
00183                 }
00184                 
00185                 $cat = new Newseventcategory_Model();
00186                 $cat->load($category);
00187                 
00188                 $news = new Newsevent_Model();
00189                 $news->type = $type;
00190                 $news->category_id = $cat->id;
00191                 $news->newsevent = $cat->type;
00192                 $news->title = $title;
00193                 $news->post = $content;
00194                 $news->posted_by = sess::getUserID();
00195                 $news->time_posted = ActiveRecord::NOW();
00196                 $news->votes = 0;
00197                 
00198                 $news->save(true);
00199 
00200                 // Now that we've saved the news item, we can also save the event metadata if applicable
00201                 if ($cat->type == 'event') {
00202                         $meta = new Eventmeta_Model();
00203                         $meta->event_id = $news->id;
00204                         $meta->location = htmlspecialchars(trim($_POST['event_location']), ENT_QUOTES, 'UTF-8');
00205                         $meta->location_area = htmlspecialchars(trim($_POST['event_location_area']), ENT_QUOTES, 'UTF-8');
00206 
00207                         if ($_POST['event_all_day']) {
00208                                 $meta->all_day = 1;
00209                         } else {
00210                                 $meta->all_day = 0;
00211                                 
00212                                 $from_min = str_pad($_POST['event_start_from_minute'], 2, 0, STR_PAD_LEFT);
00213                                 $to_min = str_pad($_POST['event_start_to_minute'], 2, 0, STR_PAD_LEFT);
00214                                 
00215                                 $start_date = strtotime("{$_POST['event_start_from_date']} {$_POST['event_start_from_hour']}:{$from_min} {$_POST['event_start_from_ampm']}");
00216                                 $end_date = strtotime("{$_POST['event_start_to_date']} {$_POST['event_start_to_hour']}:{$to_min} {$_POST['event_start_to_ampm']}");
00217                                 
00218                                 $meta->start_date = date('Y-m-d H:i:s', $start_date);
00219                                 $meta->end_date = date('Y-m-d H:i:s', $end_date);
00220                         }
00221 
00222                         $meta->save();
00223                 }
00224                 
00225                 View::respond_to(array('json', 'xml'), function ($format) use($news) {
00226                         echo $news->{"to_$format"}();
00227                 });
00228         }
00229         
00233         public function vote() {
00234                 if (!$this->session->get('authenticated')) {
00235                         return Hub::http_error(403, "Not authorized");
00236                 }
00237                 
00238                 $id = $_POST['id'];
00239                 $dir = $_POST['dir'];
00240                 
00241                 if (!is_numeric($id)) {
00242                         return Hub::http_error(400, "Invalid post ID");
00243                 }
00244                 
00245                 $news = ActiveCache::find('Newsevent_Model', $id, 43200)->sql(
00246                         "SELECT * FROM newsevents WHERE id = $id LIMIT 1"
00247                 );
00248                 
00249                 if (!$news->is_loaded()) {
00250                         return Hub::http_error(404, "Post ID not found");
00251                 }
00252                 
00253                 $news->vote($dir);
00254 
00255                 View::respond_to(array('json', 'xml'), function ($format) use($news) {
00256                         echo $news->{"to_$format"}();
00257                 });
00258         }
00259         
00263         public function create_comment() {
00264                 if (!$this->session->get('authenticated')) {
00265                         return Hub::http_error(403, "Unauthorized");
00266                 }
00267                 
00268                 $news_id = null;
00269                 if (isset($_POST['short_news_id'])) {
00270                         $news_id = base_convert($_POST['short_news_id'], 36, 10);
00271                 } elseif (isset($_POST['news_id']) && is_numeric($_POST['news_id'])) {
00272                         $news_id = $_POST['news_id'];
00273                 } else {
00274                         return Hub::http_error(400, "Invalid news ID");
00275                 }
00276                 
00277                 $reply_to = isset($_POST['reply_to']) ? $_POST['reply_to'] : 0;
00278                 $content = htmlspecialchars(trim($_POST['content']), ENT_QUOTES, 'UTF-8');
00279                 
00280                 if (strlen($content) == 0) {
00281                         return Hub::http_error(400, "Missing comment content");
00282                 }
00283                 
00284                 if (!is_numeric($reply_to)) {
00285                         return Hub::http_error(400, "Invalid reply to ID");
00286                 }
00287                 
00288                 $comment = new Newscomment_Model();
00289                 $comment->posted_by = sess::getUserID();
00290                 $comment->news_id = $news_id;
00291                 $comment->parent_id = $reply_to;
00292                 $comment->content = $content;
00293                 $comment->time_posted = ActiveRecord::NOW();
00294                 
00295                 $comment->save();
00296                 
00297                 View::respond_to(array('json', 'xml'), function ($format) use($comment) {
00298                         echo $comment->{"to_$format"}();
00299                 });
00300         }
00301         
00308         public function delete_comment() {
00309                 if (!$this->session->get('authenticated')) {
00310                         return Hub::http_error(403, "Not authorized");
00311                 }
00312                 
00313                 $id = $_POST['id'];
00314                 if (!$id || !is_numeric($id)) {
00315                         return Hub::http_error(401, "Invalid or missing ID");
00316                 }
00317                 
00318                 $comment = ActiveCache::find('Newscomment_Model', $id, 43200)->sql(
00319                         "SELECT * FROM newscomments WHERE id = $id LIMIT 1"
00320                 );
00321                 
00322                 if (!$comment->is_loaded()) {
00323                         return Hub::http_error(404, "Comment not found");
00324                 }
00325                 
00326                 if ($comment->posted_by != sess::getUserID()) {
00327                         return Hub::http_error(403, "Not authorized");
00328                 }
00329                 
00330                 $comment->delete();
00331                 
00332                 View::respond_to(array('json', 'xml'), function () {
00333                         return false;
00334                 });
00335         }
00336         
00341         public function location_search() {
00342                 $q = trim($_GET['q']);
00343                 if (!$q || strlen($q) == 0) {
00344                         return Hub::http_error(400, "Missing query parameter");
00345                 }
00346                 
00347                 $results = Eventlocation_Model::findByName($_GET['q']);
00348                 
00349                 $data = array();
00350                 foreach ($results as $result) {
00351                         $data[] = array(
00352                                 'id' => $result['id'],
00353                                 'label' => $result['name'],
00354                                 'value' => $result['name']
00355                         );
00356                 }
00357                 
00358                 echo json_encode($data);
00359         }
00360 }