PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/FuChi/ushahidi/application/controllers/admin/messages.php

https://github.com/dannyrealfox/Fu-Chi--Future-Chinatown
PHP | 365 lines | 247 code | 47 blank | 71 comment | 30 complexity | 512bc46fe3078961266500852f0197a9 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Messages Controller.
  4. *
  5. * PHP version 5
  6. * LICENSE: This source file is subject to LGPL license
  7. * that is available through the world-wide-web at the following URI:
  8. * http://www.gnu.org/copyleft/lesser.html
  9. * @author Ushahidi Team <team@ushahidi.com>
  10. * @package Ushahidi - http://source.ushahididev.com
  11. * @module Admin Messages Controller
  12. * @copyright Ushahidi - http://www.ushahidi.com
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  14. */
  15. class Messages_Controller extends Admin_Controller
  16. {
  17. function __construct()
  18. {
  19. parent::__construct();
  20. $this->template->this_page = 'messages';
  21. // If user doesn't have access, redirect to dashboard
  22. if ( ! admin::permissions($this->user, "messages"))
  23. {
  24. url::redirect(url::site().'admin/dashboard');
  25. }
  26. }
  27. /**
  28. * Lists the messages.
  29. * @param int $service_id
  30. */
  31. function index($service_id = 1)
  32. {
  33. $this->template->content = new View('admin/messages');
  34. // Get Title
  35. $service = ORM::factory('service', $service_id);
  36. $this->template->content->title = $service->service_name;
  37. // Display Reply to Option?
  38. $this->template->content->reply_to = TRUE;
  39. if ( ! Kohana::config("settings.sms_provider"))
  40. {
  41. // Hide Reply to option
  42. $this->template->content->reply_to = FALSE;
  43. }
  44. // Is this an Inbox or Outbox Filter?
  45. if (!empty($_GET['type']))
  46. {
  47. $type = $_GET['type'];
  48. if ($type == '2')
  49. { // OUTBOX
  50. $filter = 'message_type = 2';
  51. }
  52. else
  53. { // INBOX
  54. $type = "1";
  55. $filter = 'message_type = 1';
  56. }
  57. }
  58. else
  59. {
  60. $type = "1";
  61. $filter = 'message_type = 1';
  62. }
  63. // Do we have a reporter ID?
  64. if (isset($_GET['rid']) AND !empty($_GET['rid']))
  65. {
  66. $filter .= ' AND reporter_id=\''.$_GET['rid'].'\'';
  67. }
  68. // ALL / Trusted / Spam
  69. $level = '0';
  70. if (isset($_GET['level']) AND !empty($_GET['level']))
  71. {
  72. $level = $_GET['level'];
  73. if ($level == 4)
  74. {
  75. $filter .= " AND ( reporter.level_id = '4' OR reporter.level_id = '5' ) AND ( message.message_level != '99' ) ";
  76. }
  77. elseif ($level == 2)
  78. {
  79. $filter .= " AND ( message.message_level = '99' ) ";
  80. }
  81. }
  82. // check, has the form been submitted?
  83. $form_error = FALSE;
  84. $form_saved = FALSE;
  85. $form_action = "";
  86. // check, has the form been submitted, if so, setup validation
  87. if ($_POST)
  88. {
  89. // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
  90. $post = Validation::factory($_POST);
  91. // Add some filters
  92. $post->pre_filter('trim', TRUE);
  93. // Add some rules, the input field, followed by a list of checks, carried out in order
  94. $post->add_rules('action','required', 'alpha', 'length[1,1]');
  95. $post->add_rules('message_id.*','required','numeric');
  96. // Test to see if things passed the rule checks
  97. if ($post->validate())
  98. {
  99. if( $post->action == 'd' ) // Delete Action
  100. {
  101. foreach($post->message_id as $item)
  102. {
  103. // Delete Message
  104. $message = ORM::factory('message')->find($item);
  105. $message->delete( $item );
  106. }
  107. $form_saved = TRUE;
  108. $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
  109. }
  110. elseif( $post->action == 'n' ) // Not Spam
  111. {
  112. foreach($post->message_id as $item)
  113. {
  114. // Update Message Level
  115. $message = ORM::factory('message')->find($item);
  116. if ($message->loaded)
  117. {
  118. $message->message_level = '1';
  119. $message->save();
  120. }
  121. }
  122. $form_saved = TRUE;
  123. $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
  124. }
  125. elseif( $post->action == 's' ) // Spam
  126. {
  127. foreach($post->message_id as $item)
  128. {
  129. // Update Message Level
  130. $message = ORM::factory('message')->find($item);
  131. if ($message->loaded)
  132. {
  133. $message->message_level = '99';
  134. $message->save();
  135. }
  136. }
  137. $form_saved = TRUE;
  138. $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
  139. }
  140. }
  141. // No! We have validation errors, we need to show the form again, with the errors
  142. else
  143. {
  144. // repopulate the form fields
  145. $form = arr::overwrite($form, $post->as_array());
  146. // populate the error fields, if any
  147. $errors = arr::overwrite($errors, $post->errors('message'));
  148. $form_error = TRUE;
  149. }
  150. }
  151. // Pagination
  152. $pagination = new Pagination(array(
  153. 'query_string' => 'page',
  154. 'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'),
  155. 'total_items' => ORM::factory('message')
  156. ->join('reporter','message.reporter_id','reporter.id')
  157. ->where($filter)
  158. ->where('service_id', $service_id)
  159. ->count_all()
  160. ));
  161. $messages = ORM::factory('message')
  162. ->join('reporter','message.reporter_id','reporter.id')
  163. ->where('service_id', $service_id)
  164. ->where($filter)
  165. ->orderby('message_date','desc')
  166. ->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset);
  167. // Get Message Count
  168. // ALL
  169. $this->template->content->count_all = ORM::factory('message')
  170. ->join('reporter','message.reporter_id','reporter.id')
  171. ->where('service_id', $service_id)
  172. ->where('message_type', 1)
  173. ->count_all();
  174. // Trusted
  175. $this->template->content->count_trusted = ORM::factory('message')
  176. ->join('reporter','message.reporter_id','reporter.id')
  177. ->where('service_id', $service_id)
  178. ->where("( reporter.level_id = '4' OR reporter.level_id = '5' ) AND ( message.message_level != '99' )")
  179. ->where('message_type', 1)
  180. ->count_all();
  181. // Spam
  182. $this->template->content->count_spam = ORM::factory('message')
  183. ->join('reporter','message.reporter_id','reporter.id')
  184. ->where('service_id', $service_id)
  185. ->where('message_type', 1)
  186. ->where("message.message_level = '99'")
  187. ->count_all();
  188. $this->template->content->messages = $messages;
  189. $this->template->content->service_id = $service_id;
  190. $this->template->content->services = ORM::factory('service')->find_all();
  191. $this->template->content->pagination = $pagination;
  192. $this->template->content->form_error = $form_error;
  193. $this->template->content->form_saved = $form_saved;
  194. $this->template->content->form_action = $form_action;
  195. $levels = ORM::factory('level')->orderby('level_weight')->find_all();
  196. $this->template->content->levels = $levels;
  197. // Total Reports
  198. $this->template->content->total_items = $pagination->total_items;
  199. // Message Type Tab - Inbox/Outbox
  200. $this->template->content->type = $type;
  201. $this->template->content->level = $level;
  202. // Javascript Header
  203. $this->template->js = new View('admin/messages_js');
  204. }
  205. /**
  206. * Send A New Message Using Default SMS Provider
  207. */
  208. function send()
  209. {
  210. $this->template = "";
  211. $this->auto_render = FALSE;
  212. // setup and initialize form field names
  213. $form = array
  214. (
  215. 'to_id' => '',
  216. 'message' => ''
  217. );
  218. // Copy the form as errors, so the errors will be stored with keys
  219. // corresponding to the form field names
  220. $errors = $form;
  221. $form_error = FALSE;
  222. // check, has the form been submitted, if so, setup validation
  223. if ($_POST)
  224. {
  225. // Instantiate Validation, use $post, so we don't overwrite $_POST
  226. // fields with our own things
  227. $post = new Validation($_POST);
  228. // Add some filters
  229. $post->pre_filter('trim', TRUE);
  230. // Add some rules, the input field, followed by a list of checks, carried out in order
  231. $post->add_rules('to_id', 'required', 'numeric');
  232. $post->add_rules('message', 'required', 'length[1,160]');
  233. // Test to see if things passed the rule checks
  234. if ($post->validate())
  235. {
  236. // Yes! everything is valid
  237. $reply_to = ORM::factory('message', $post->to_id);
  238. if ($reply_to->loaded == true)
  239. {
  240. // Yes! Replyto Exists
  241. // This is the message we're replying to
  242. $sms_to = intval($reply_to->message_from);
  243. // Load Users Settings
  244. $settings = new Settings_Model(1);
  245. if ($settings->loaded == true) {
  246. // Get SMS Numbers
  247. if ( ! empty($settings->sms_no3))
  248. {
  249. $sms_from = $settings->sms_no3;
  250. }
  251. elseif ( ! empty($settings->sms_no2))
  252. {
  253. $sms_from = $settings->sms_no2;
  254. }
  255. elseif ( ! empty($settings->sms_no1))
  256. {
  257. $sms_from = $settings->sms_no1;
  258. }
  259. else
  260. {
  261. $sms_from = "000"; // User needs to set up an SMS number
  262. }
  263. // Send Message
  264. $response = sms::send($sms_to, $sms_from, $post->message);
  265. // Message Went Through??
  266. if ($response === true)
  267. {
  268. $newmessage = ORM::factory('message');
  269. $newmessage->parent_id = $post->to_id; // The parent message
  270. $newmessage->message_from = $sms_from;
  271. $newmessage->message_to = $sms_to;
  272. $newmessage->message = $post->message;
  273. $newmessage->message_type = 2; // This is an outgoing message
  274. $newmessage->reporter_id = $reply_to->reporter_id;
  275. $newmessage->message_date = date("Y-m-d H:i:s",time());
  276. $newmessage->save();
  277. echo json_encode(array("status"=>"sent", "message"=>Kohana::lang('ui_admin.message_sent')));
  278. }
  279. else // Message Failed
  280. {
  281. echo json_encode(array("status"=>"error", "message"=>Kohana::lang('ui_admin.error')." - " . $response));
  282. }
  283. }
  284. else
  285. {
  286. echo json_encode(array("status"=>"error", "message"=>Kohana::lang('ui_admin.error').Kohana::lang('ui_admin.check_sms_settings')));
  287. }
  288. }
  289. // Send_To Mobile Number Doesn't Exist
  290. else {
  291. echo json_encode(array("status"=>"error", "message"=>Kohana::lang('ui_admin.error').Kohana::lang('ui_admin.check_number')));
  292. }
  293. }
  294. // No! We have validation errors, we need to show the form again,
  295. // with the errors
  296. else
  297. {
  298. // populate the error fields, if any
  299. $errors = arr::overwrite($errors, $post->errors('messages'));
  300. echo json_encode(array("status"=>"error", "message"=>Kohana::lang('ui_admin.error').Kohana::lang('ui_admin.check_message_valid')));
  301. }
  302. }
  303. }
  304. /**
  305. * setup simplepie
  306. * @param string $raw_data
  307. */
  308. private function _setup_simplepie( $raw_data )
  309. {
  310. $data = new SimplePie();
  311. $data->set_raw_data( $raw_data );
  312. $data->enable_cache(false);
  313. $data->enable_order_by_date(true);
  314. $data->init();
  315. $data->handle_content_type();
  316. return $data;
  317. }
  318. }