PageRenderTime 32ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/api/PostReport.php

https://github.com/error10/Ushahidi_Web
PHP | 373 lines | 277 code | 41 blank | 55 comment | 39 complexity | 396a875270228376996603fd5b397c97 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * This class handles posting report to ushahidi via the API.
  4. *
  5. * @version 22 - David Kobia 2010-08-30
  6. *
  7. * PHP version 5
  8. * LICENSE: This source file is subject to LGPL license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.gnu.org/copyleft/lesser.html
  11. * @author Ushahidi Team <team@ushahidi.com>
  12. * @package Ushahidi - http://source.ushahididev.com
  13. * @module API Controller
  14. * @copyright Ushahidi - http://www.ushahidi.com
  15. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  16. */
  17. require_once('ApiActions.php');
  18. class PostReport
  19. {
  20. private $data; // items to parse to JSON.
  21. private $items; // categories to parse to JSON.
  22. private $query; // Holds the SQL query
  23. private $replar; // assists in proper XML generation.
  24. private $db;
  25. private $domain;
  26. private $table_prefix;
  27. private $list_limit;
  28. private $ret_json_or_xml;
  29. private $api_actions;
  30. public function __construct()
  31. {
  32. $this->api_actions = new ApiActions;
  33. $this->data = array();
  34. $this->items = array();
  35. $this->ret_json_or_xml = '';
  36. $this->query = '';
  37. $this->replar = array();
  38. $this->domain = $this->api_actions->_get_domain();
  39. $this->db = $this->api_actions->_get_db();
  40. }
  41. /**
  42. * Submit a report
  43. *
  44. * @param string response_type - JSON or XML
  45. *
  46. * @return Array
  47. */
  48. public function _report($response_type)
  49. {
  50. $reponse = array();
  51. $ret_value = $this->_submit();
  52. if($ret_value == 0 )
  53. {
  54. $reponse = array(
  55. "payload" => array(
  56. "domain" => $this->domain,
  57. "success" => "true"
  58. ),
  59. "error" => $this->api_actions->_get_error_msg(0)
  60. );
  61. }
  62. else if( $ret_value == 1 )
  63. {
  64. $reponse = array(
  65. "payload" => array(
  66. "domain" => $this->domain,
  67. "success" => "false"
  68. ),
  69. "error" => $this->api_actions->
  70. _get_error_msg(003,'',$this->error_messages)
  71. );
  72. }
  73. else
  74. {
  75. $reponse = array(
  76. "payload" => array(
  77. "domain" => $this->domain,
  78. "success" => "false"
  79. ),
  80. "error" => $this->api_actions->_get_error_msg(004)
  81. );
  82. }
  83. if($response_type == 'json')
  84. {
  85. $this->ret_json_or_xml = $this->api_actions->
  86. _array_as_JSON($reponse);
  87. }
  88. else
  89. {
  90. $this->ret_json_or_xml = $this->api_actions->
  91. _array_as_XML($reponse, array());
  92. }
  93. return $this->ret_json_or_xml;
  94. }
  95. /**
  96. * The actual reporting -
  97. *
  98. * @return int
  99. */
  100. private function _submit()
  101. {
  102. // setup and initialize form field names
  103. $form = array
  104. (
  105. 'incident_title' => '',
  106. 'incident_description' => '',
  107. 'incident_date' => '',
  108. 'incident_hour' => '',
  109. 'incident_minute' => '',
  110. 'incident_ampm' => '',
  111. 'latitude' => '',
  112. 'longitude' => '',
  113. 'location_name' => '',
  114. 'country_id' => '',
  115. 'incident_category' => '',
  116. 'incident_news' => array(),
  117. 'incident_video' => array(),
  118. 'incident_photo' => array(),
  119. 'person_first' => '',
  120. 'person_last' => '',
  121. 'person_email' => ''
  122. );
  123. $this->messages = $form;
  124. // check, has the form been submitted, if so, setup validation
  125. if ($_POST)
  126. {
  127. // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
  128. $post = Validation::factory(array_merge($_POST,$_FILES));
  129. // Add some filters
  130. $post->pre_filter('trim', TRUE);
  131. // Add some rules, the input field, followed by a list of checks, carried out in order
  132. $post->add_rules('incident_title','required', 'length[3,200]');
  133. $post->add_rules('incident_description','required');
  134. $post->add_rules('incident_date','required','date_mmddyyyy');
  135. $post->add_rules('incident_hour','required','between[0,23]');
  136. //$post->add_rules('incident_minute','required','between[0,59]');
  137. if($this->api_actions->_verify_array_index(
  138. $_POST, 'incident_ampm'))
  139. {
  140. if ($_POST['incident_ampm'] != "am" &&
  141. $_POST['incident_ampm'] != "pm")
  142. {
  143. $post->add_error('incident_ampm','values');
  144. }
  145. }
  146. $post->add_rules('latitude','required','between[-90,90]');
  147. $post->add_rules('longitude','required','between[-180,180]');
  148. $post->add_rules('location_name','required', 'length[3,200]');
  149. $post->add_rules('incident_category','required',
  150. 'length[1,100]');
  151. // Validate Personal Information
  152. if (!empty($post->person_first))
  153. {
  154. $post->add_rules('person_first', 'length[3,100]');
  155. }
  156. if (!empty($post->person_last))
  157. {
  158. $post->add_rules('person_last', 'length[3,100]');
  159. }
  160. if (!empty($post->person_email))
  161. {
  162. $post->add_rules('person_email', 'email', 'length[3,100]');
  163. }
  164. // Test to see if things passed the rule checks
  165. if ($post->validate())
  166. {
  167. // SAVE LOCATION (***IF IT DOES NOT EXIST***)
  168. $location = new Location_Model();
  169. $location->location_name = $post->location_name;
  170. $location->latitude = $post->latitude;
  171. $location->longitude = $post->longitude;
  172. $location->location_date = date("Y-m-d H:i:s",time());
  173. $location->save();
  174. // SAVE INCIDENT
  175. $incident = new Incident_Model();
  176. $incident->location_id = $location->id;
  177. $incident->user_id = 0;
  178. $incident->incident_title = $post->incident_title;
  179. $incident->incident_description = $post->incident_description;
  180. $incident_date=explode("/",$post->incident_date);
  181. /**
  182. * where the $_POST['date'] is a value posted by form in
  183. * mm/dd/yyyy format
  184. */
  185. $incident_date=$incident_date[2]."-".$incident_date[0]."-".
  186. $incident_date[1];
  187. $incident_time = $post->incident_hour . ":" .
  188. $post->incident_minute . ":00 " . $post->incident_ampm;
  189. $incident->incident_date = $incident_date . " " .
  190. $incident_time;
  191. $incident->incident_dateadd = date("Y-m-d H:i:s",time());
  192. $incident->save();
  193. // SAVE CATEGORIES
  194. //check if data is csv or a single value.
  195. $pos = strpos($post->incident_category,",");
  196. if($pos === false)
  197. {
  198. //for backward compactibility. will drop support for it in the future.
  199. if(@unserialize($post->incident_category))
  200. {
  201. $categories = unserialize($post->incident_category);
  202. }
  203. else
  204. {
  205. $categories = array($post->incident_category);
  206. }
  207. }
  208. else
  209. {
  210. $categories = explode(",",$post->incident_category);
  211. }
  212. if(!empty($categories) && is_array($categories))
  213. {
  214. foreach($categories as $item)
  215. {
  216. $incident_category = new Incident_Category_Model();
  217. $incident_category->incident_id = $incident->id;
  218. $incident_category->category_id = $item;
  219. $incident_category->save();
  220. }
  221. }
  222. // STEP 4: SAVE MEDIA
  223. // a. News
  224. if(!empty( $post->incident_news ) && is_array(
  225. $post->incident_news)) {
  226. foreach($post->incident_news as $item)
  227. {
  228. if(!empty($item))
  229. {
  230. $news = new Media_Model();
  231. $news->location_id = $location->id;
  232. $news->incident_id = $incident->id;
  233. $news->media_type = 4; // News
  234. $news->media_link = $item;
  235. $news->media_date = date("Y-m-d H:i:s",time());
  236. $news->save();
  237. }
  238. }
  239. }
  240. // b. Video
  241. if( !empty( $post->incident_video) && is_array(
  242. $post->incident_video))
  243. {
  244. foreach($post->incident_video as $item)
  245. {
  246. if(!empty($item))
  247. {
  248. $video = new Media_Model();
  249. $video->location_id = $location->id;
  250. $video->incident_id = $incident->id;
  251. $video->media_type = 2; // Video
  252. $video->media_link = $item;
  253. $video->media_date = date("Y-m-d H:i:s",time());
  254. $video->save();
  255. }
  256. }
  257. }
  258. // c. Photos
  259. if(!empty($post->incident_photo))
  260. {
  261. $filenames = upload::save('incident_photo');
  262. $i = 1;
  263. foreach ($filenames as $filename)
  264. {
  265. $new_filename = $incident->id . "_" . $i . "_" .
  266. time();
  267. // Resize original file... make sure its max 408px wide
  268. Image::factory($filename)->resize(408,248,
  269. Image::AUTO)->save(
  270. Kohana::config('upload.directory',
  271. TRUE) . $new_filename . ".jpg");
  272. // Create thumbnail
  273. Image::factory($filename)->resize(70,41,
  274. Image::HEIGHT)->save(
  275. Kohana::config('upload.directory',
  276. TRUE) . $new_filename . "_t.jpg");
  277. // Remove the temporary file
  278. unlink($filename);
  279. // Save to DB
  280. $photo = new Media_Model();
  281. $photo->location_id = $location->id;
  282. $photo->incident_id = $incident->id;
  283. $photo->media_type = 1; // Images
  284. $photo->media_link = $new_filename . ".jpg";
  285. $photo->media_thumb = $new_filename . "_t.jpg";
  286. $photo->media_date = date("Y-m-d H:i:s",time());
  287. $photo->save();
  288. $i++;
  289. }
  290. }
  291. // SAVE PERSONAL INFORMATION IF ITS FILLED UP
  292. if(!empty($post->person_first) || !empty(
  293. $post->person_last))
  294. {
  295. $person = new Incident_Person_Model();
  296. $person->location_id = $location->id;
  297. $person->incident_id = $incident->id;
  298. $person->person_first = $post->person_first;
  299. $person->person_last = $post->person_last;
  300. $person->person_email = $post->person_email;
  301. $person->person_date = date("Y-m-d H:i:s",time());
  302. $person->save();
  303. }
  304. return 0; //success
  305. }
  306. else
  307. {
  308. // populate the error fields, if any
  309. $this->messages = arr::overwrite($this->messages,
  310. $post->errors('report'));
  311. foreach ($this->messages as $error_item =>
  312. $error_description)
  313. {
  314. if(!is_array($error_description))
  315. {
  316. $this->error_messages .= $error_description;
  317. if($error_description != end($this->messages))
  318. {
  319. $this->error_messages .= " - ";
  320. }
  321. }
  322. }
  323. //FAILED!!!
  324. return 1; //validation error
  325. }
  326. }
  327. else
  328. {
  329. return 2; // Not sent by post method.
  330. }
  331. }
  332. }