Campustream 1.0
A social network MQP for WPI
application/controllers/search.php
Go to the documentation of this file.
00001 <?
00002 
00007 class Search_Controller extends Controller implements REST {
00008         public $enable_session = true;
00009         
00013         public function statuses() {
00014                 $q = trim($_GET['q']);
00015 
00016                 if (!$q || strlen($q) < 3) {
00017                         return Hub::http_error(401, "Invalid search, or search too short");
00018                 }
00019                 
00020                 $sphinx = SphinxManager::connection();
00021                 $res = $sphinx->Query($q . '*', 'statuses');
00022                 if ($res['total'] > 0) {
00023                         $statuses = array();
00024                         foreach ($res['matches'] as $match) {
00025                                 $status = ActiveCache::find('Status_Model', $match['id'], 43200)->sql(
00026                                         "SELECT * FROM statuses WHERE id = {$match['id']} LIMIT 1"
00027                                 );
00028                                 
00029                                 if ($status->is_loaded() && $status->deleted == 0) {
00030                                         $statuses[] = $status->limited_object();
00031                                 }
00032                         }
00033                         
00034                         View::respond_to(array('json', 'xml'), function ($format) use($statuses) {
00035                                 if ($format == 'json') {
00036                                         echo json_encode($statuses);
00037                                 } elseif ($format == 'xml') {
00038                                         echo xml::encode_array($statuses, "search", "result");
00039                                 }
00040                         });
00041                 }
00042         }
00043         
00047         public function users() {
00048                 $q = trim($_GET['q']);
00049 
00050                 if (!$q || strlen($q) < 3) {
00051                         return Hub::http_error(401, "Invalid search, or search too short");
00052                 }
00053                 
00054                 $sphinx = SphinxManager::connection();
00055                 $res = $sphinx->Query($q . '*', 'users');
00056                 if ($res['total'] > 0) {
00057                         $users = array();
00058                         foreach ($res['matches'] as $match) {
00059                                 $user = ActiveCache::find('User_Model', $match['id'], 43200)->sql(
00060                                         "SELECT * FROM users WHERE id = {$match['id']} LIMIT 1"
00061                                 );
00062                                 
00063                                 if ($user->is_loaded()) {
00064                                         $users[] = $user->limited_object();
00065                                 }
00066                         }
00067                         
00068                         View::respond_to(array('json', 'xml'), function ($format) use($users) {
00069                                 if ($format == 'json') {
00070                                         echo json_encode($users);
00071                                 } elseif ($format == 'xml') {
00072                                         echo xml::encode_array($users, "search", "result");
00073                                 }
00074                         });
00075                 }
00076         }
00077         
00081         public function tags() {
00082                 $q = trim($_GET['q']);
00083                 
00084                 if (!$q || strlen($q) < 3) {
00085                         return Hub::http_error(401, "Invalid search, or search too short");
00086                 }
00087                 
00088                 $sphinx = SphinxManager::connection();
00089                 $res = $sphinx->Query($q . '*', 'tags');
00090                 if ($res['total'] > 0) {
00091                         $questions = array();
00092                         foreach ($res['matches'] as $match) {
00093                                 $question_id = $match['attrs']['question_id'];
00094                                 $question = ActiveCache::find('Question_Model', $question_id, 43200)->sql(
00095                                         "SELECT * FROM questions WHERE id = $question_id LIMIT 1"
00096                                 );
00097                                 
00098                                 if($question->is_loaded() && !$question->deleted) {
00099                                         $question->load_tags();
00100                                         $question->load_user();
00101                                         $questions[] = $question->limited_object();
00102                                 }
00103                         }
00104                         
00105                         util::objectSort($questions, 'id');
00106                         
00107                         View::respond_to(array('json','xml'), function ($format) use($questions) {
00108                                 if ($format == 'json') {
00109                                         echo json_encode($questions);
00110                                 } else {
00111                                         echo xml::encode_array($questions, 'search', 'result');
00112                                 }
00113                         });
00114                 }
00115         }
00116 }