PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/api.php

https://bitbucket.org/mike_kelly/omnicaster
PHP | 251 lines | 201 code | 36 blank | 14 comment | 10 complexity | 5ebc46269950e9f82ce8dd959936488a MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Api extends MY_Controller
  3. {
  4. public function index()
  5. {
  6. show_error('Sorry you are not Authorized to view this page!', 403);
  7. }
  8. public function linkGen()
  9. {
  10. $action = $this->uri->segment(3);
  11. $feedName = $this->uri->segment(4);
  12. switch ($action) {
  13. case 'manage':
  14. $baseURL = base_url().'manage/';
  15. $feedName = urldecode($feedName);
  16. $result = $this->api_model->get_feedID($feedName);
  17. $returnedData = $baseURL.'entries/'.$result[0]['id'];
  18. echo $returnedData;
  19. break;
  20. case 'create':
  21. $baseURL = base_url().'create/';
  22. if($feedName == "new")
  23. {
  24. echo $baseURL.'feed';
  25. break;
  26. }
  27. else
  28. {
  29. $feedName = urldecode($feedName);
  30. $result = $this->api_model->get_feedID($feedName);
  31. $returnedData = $baseURL.'entry/'.$result[0]['id'];
  32. echo $returnedData;
  33. break;
  34. }
  35. }
  36. }
  37. function feedEntries()
  38. {
  39. $returnedData = $this->feeds_model->get_entries($this->uri->segment(3));
  40. $data = array(
  41. 'code' => 200,
  42. 'data' => $returnedData,
  43. 'format' => $this->uri->segment(4),
  44. 'counter' => 0
  45. );
  46. $this->response($data);
  47. }
  48. function subscribersSuggest()
  49. {
  50. $email = urldecode($this->uri->segment(3));
  51. $returnedData = $this->subscribers_model->subscribers_suggest($email);
  52. $data = array(
  53. 'code' => 200,
  54. 'data' => $returnedData,
  55. 'format' => $this->uri->segment(4),
  56. 'counter' => 0
  57. );
  58. $this->response($data);
  59. }
  60. function mobile2()
  61. {
  62. $authorID = $this->uri->segment(3);
  63. $feedID = $this->uri->segment(4);
  64. if(!$this->uri->segment(5))
  65. {
  66. $queryResponse = $this->api_model->get_feedEntries($authorID, $feedID);
  67. $formattedResponse = array();
  68. foreach($queryResponse as $posts)
  69. {
  70. $authorName = $this->api_model->get_authorName($posts['author_id']);
  71. $temp = array(
  72. 'id' => $posts['id'],
  73. 'url' => base_url('api/mobile').'/'.$posts['author_id'].'/'.$posts['feed_id'].'/'.$posts['id'],
  74. 'title' => $posts['title'],
  75. 'date' => $posts['publish_date'],
  76. 'author' => $authorName->first_name.' '.$authorName->last_name,
  77. 'thumbnail' => 'http://placehold.it/150x150'
  78. );
  79. array_push($formattedResponse, $temp);
  80. }
  81. $returnedData = array(
  82. 'status' => 'ok',
  83. 'count' => 10,
  84. 'count_total' => 1470,
  85. 'pages' => 147,
  86. 'posts' => $formattedResponse
  87. );
  88. $this->output
  89. ->set_content_type('application/json')
  90. ->set_output(json_encode($returnedData));
  91. }
  92. else //a specific article is requested and must be delivered via a view
  93. {
  94. //look up article based on authorID, feedID, and articleID
  95. $authorName = $this->api_model->get_article($authorID,$feedID,$this->uri->segment(5));
  96. $this->output->set_output('<br /><br /><br /><h2>'.$authorName->title.'</h2>'.$authorName->body);
  97. }
  98. }
  99. function mobile()
  100. {
  101. date_default_timezone_set('America/Detroit');
  102. //2010-10-15 10:00:00
  103. $dateTimeNow = date('Y-m-d G:i:s');
  104. $formattedResponse = array();
  105. switch($this->uri->segment(3))
  106. {
  107. case "feed":
  108. // get articles for a feed (limit 10)
  109. $queryResponse = $this->api_model->feed_EntriesByID($this->uri->segment(4), $dateTimeNow);
  110. foreach($queryResponse as $posts)
  111. {
  112. $authorName = $this->api_model->get_authorName($posts['author_id']);
  113. $temp = array(
  114. 'id' => $posts['id'],
  115. 'url' => base_url('api/mobile').'/'.$posts['id'],
  116. 'title' => $posts['title'],
  117. 'date' => $posts['publish_date'],
  118. 'author' => $authorName->first_name.' '.$authorName->last_name,
  119. 'thumbnail' => 'http://placehold.it/150x150'
  120. );
  121. array_push($formattedResponse, $temp);
  122. }
  123. $returnedData = array(
  124. 'status' => 'ok',
  125. 'count' => count($formattedResponse),
  126. 'count_total' => 1470,
  127. 'pages' => 147,
  128. 'posts' => $formattedResponse
  129. );
  130. $this->output
  131. ->set_content_type('application/json')
  132. ->set_output(json_encode($returnedData));
  133. break;
  134. case "author":
  135. // get all articles from author (limit 10)
  136. $queryResponse = $this->api_model->feed_EntriesByAuthor($this->uri->segment(4), $dateTimeNow);
  137. foreach($queryResponse as $posts)
  138. {
  139. $authorName = $this->api_model->get_authorName($posts['author_id']);
  140. $temp = array(
  141. 'id' => $posts['id'],
  142. 'url' => base_url('api/mobile').'/'.$posts['id'],
  143. 'title' => $posts['title'],
  144. 'date' => $posts['publish_date'],
  145. 'author' => $authorName->first_name.' '.$authorName->last_name,
  146. 'thumbnail' => 'http://placehold.it/150x150'
  147. );
  148. array_push($formattedResponse, $temp);
  149. }
  150. $returnedData = array(
  151. 'status' => 'ok',
  152. 'count' => count($formattedResponse),
  153. 'count_total' => 1470,
  154. 'pages' => 147,
  155. 'posts' => $formattedResponse
  156. );
  157. $this->output
  158. ->set_content_type('application/json')
  159. ->set_output(json_encode($returnedData));
  160. break;
  161. default:
  162. //look up article based on entryID and publish date
  163. $template =
  164. '<!DOCTYPE html>'
  165. .'<html>'
  166. .' <head>'
  167. .' <style>'
  168. .' body {background-image:url("'.base_url().'/resource/img/post-pg.png"); margin-left: 50px;}'
  169. .' </style>'
  170. .' </head>'
  171. .' <body>';
  172. $article = $this->api_model->feed_EntryByID($this->uri->segment(3), $dateTimeNow);
  173. $this->output->set_output($template.'<br /><br /><br /><h2>'.$article->title.'</h2>'.$article->body.'</body></html>');
  174. }
  175. }
  176. function response($response)
  177. {
  178. //if there is a response
  179. if(isset($response) && $response['code'] == 200)
  180. {
  181. if(isset($response['data']))
  182. {
  183. switch($response['format'])
  184. {
  185. //Encodes and outputs in xml
  186. case 'xml':
  187. $this->load->dbutil();
  188. $xmlOutput = $this->dbutil->xml_from_result($response['data']);
  189. $this->output
  190. ->set_content_type('text/xml')
  191. ->set_output($xmlOutput);
  192. break;
  193. //Encodes and outputs a html table
  194. case 'html':
  195. //send data array to a view()
  196. //create an html table out of the data array
  197. //print_r($response['data']->result_array());
  198. $this->load->view('api-view',$response);
  199. break;
  200. //Encodes and outputs in json - This is the default action
  201. default:
  202. $this->output
  203. ->set_content_type('application/json')
  204. ->set_output(json_encode($response['data']->result_array()));
  205. }
  206. }
  207. //send appropriate response code 200, 201, ect...
  208. }
  209. else
  210. {
  211. //Generic error - requested object not found
  212. show_404();
  213. }
  214. }
  215. }