예제 #1
0
파일: api.json.php 프로젝트: viru48/imc
 private function getTop($type)
 {
     $result = null;
     $app = JFactory::getApplication();
     try {
         self::validateRequest();
         if ($app->input->getMethod() != 'GET') {
             throw new Exception('You cannot use other method than GET to fetch top users');
         }
         //get necessary arguments
         $ts = $app->input->getString('ts', null);
         $prior_to = $app->input->getString('prior_to', null);
         $lim = $app->input->getInt('lim', null);
         $ids = $app->input->getString('ids', null);
         if (!is_null($ts) && !ImcFrontendHelper::isValidTimeStamp($ts)) {
             throw new Exception('Invalid timestamp ts');
         }
         if (!is_null($prior_to) && !ImcFrontendHelper::isValidTimeStamp($prior_to)) {
             throw new Exception('Invalid timestamp prior_to');
         }
         //get date from ts
         if (!is_null($ts)) {
             $ts = gmdate('Y-m-d H:i:s', $ts);
         }
         if (!is_null($prior_to)) {
             $prior_to = gmdate('Y-m-d H:i:s', $prior_to);
         }
         //handle unexpected warnings from model
         set_error_handler(array($this, 'exception_error_handler'));
         switch ($type) {
             case 'users':
                 $result = ImcFrontendHelper::getTopUsers($lim, $ts, $prior_to, $ids);
                 break;
             case 'categories':
                 $result = ImcFrontendHelper::getTopCategories($lim, $ts, $prior_to, $ids);
                 break;
             case 'steps':
                 $result = ImcFrontendHelper::getTopSteps($lim, $ts, $prior_to, $ids);
                 break;
             case 'voters':
                 $result = ImcFrontendHelper::getTopVoters($lim, $ts, $prior_to, $ids);
                 break;
             case 'commenters':
                 $result = ImcFrontendHelper::getTopCommenters($lim, $ts, $prior_to, $ids);
                 break;
         }
         restore_error_handler();
         $app->enqueueMessage('size: ' . sizeof($result), 'info');
         echo new JResponseJson($result, 'Top fetched successfully');
     } catch (Exception $e) {
         header("HTTP/1.0 202 Accepted");
         echo new JResponseJson($e);
     }
 }