PageRenderTime 64ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/adminmap/helpers/adminmap_helper.php

https://github.com/fayazv/Taarifa_Web
PHP | 1988 lines | 1378 code | 328 blank | 282 comment | 225 complexity | aedfb847b56e7943235e8994e058d2fb MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, BSD-3-Clause, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Admin helper
  4. *
  5. */
  6. class adminmap_helper_Core {
  7. // Table Prefix
  8. protected static $table_prefix;
  9. static function init()
  10. {
  11. // Set Table Prefix
  12. self::$table_prefix = Kohana::config('database.default.table_prefix');
  13. }
  14. /**************************************************************************************************************
  15. * Given all the parameters returns a list of incidents that meet the search criteria
  16. */
  17. public static function setup_adminmap($map_controller, $map_view = "adminmap/mapview", $map_css = "adminmap/css/adminmap")
  18. {
  19. //set the CSS for this
  20. if($map_css != null)
  21. {
  22. plugin::add_stylesheet($map_css);
  23. }
  24. plugin::add_javascript("adminmap/js/jquery.flot");
  25. plugin::add_javascript("adminmap/js/excanvas.min");
  26. plugin::add_javascript("adminmap/js/timeline");
  27. plugin::add_javascript("adminmap/js/jquery.hovertip-1.0");
  28. plugin::add_javascript("simplegroups/js/groups.js");
  29. $map_controller->template->content = new View($map_view);
  30. // Get Default Color
  31. $map_controller->template->content->default_map_all = Kohana::config('settings.default_map_all');
  32. }
  33. /****
  34. * Sets up the overlays and shares
  35. */
  36. public static function set_overlays_shares($map_controller)
  37. {
  38. // Get all active Layers (KMZ/KML)
  39. $layers = array();
  40. $config_layers = Kohana::config('map.layers'); // use config/map layers if set
  41. if ($config_layers == $layers) {
  42. foreach (ORM::factory('layer')
  43. ->where('layer_visible', 1)
  44. ->find_all() as $layer)
  45. {
  46. $layers[$layer->id] = array($layer->layer_name, $layer->layer_color,
  47. $layer->layer_url, $layer->layer_file);
  48. }
  49. } else {
  50. $layers = $config_layers;
  51. }
  52. $map_controller->template->content->layers = $layers;
  53. // Get all active Shares
  54. $shares = array();
  55. foreach (ORM::factory('sharing')
  56. ->where('sharing_active', 1)
  57. ->find_all() as $share)
  58. {
  59. $shares[$share->id] = array($share->sharing_name, $share->sharing_color);
  60. }
  61. $map_controller->template->content->shares = $shares;
  62. }
  63. /*
  64. * this makes the map for this plugin
  65. */
  66. public static function set_map($template, $themes, $json_url, $json_timeline_url, $javascript_view = 'adminmap/mapview_js',
  67. $div_map_view = 'adminmap/main_map', $div_timeline_view = 'adminmap/main_timeline')
  68. {
  69. ////////////////////////////////////////////////////////////////Map and Slider Blocks////////////////////////////////////////////////////////////////////////////
  70. $div_map = new View($div_map_view);
  71. $div_timeline = new View($div_timeline_view);
  72. // Filter::map_main - Modify Main Map Block
  73. Event::run('ushahidi_filter.map_main', $div_map);
  74. // Filter::map_timeline - Modify Main Map Block
  75. Event::run('ushahidi_filter.map_timeline', $div_timeline);
  76. $template->content->div_map = $div_map;
  77. $template->content->div_timeline = $div_timeline;
  78. ///////////////////////////////////////////////////////////////SETUP THE DATES////////////////////////////////////////////////////////////////////////////
  79. // Get The START, END and Incident Dates
  80. $startDate = "";
  81. $endDate = "";
  82. $display_startDate = 0;
  83. $display_endDate = 0;
  84. $db = new Database();
  85. // Next, Get the Range of Years
  86. $query = $db->query('SELECT DATE_FORMAT(incident_date, \'%Y-%c\') AS dates FROM '.self::$table_prefix.'incident WHERE incident_active = 1 GROUP BY DATE_FORMAT(incident_date, \'%Y-%c\') ORDER BY incident_date');
  87. $first_year = date('Y');
  88. $last_year = date('Y');
  89. $first_month = 1;
  90. $last_month = 12;
  91. $i = 0;
  92. foreach ($query as $data)
  93. {
  94. $date = explode('-',$data->dates);
  95. $year = $date[0];
  96. $month = $date[1];
  97. // Set first year
  98. if($i == 0)
  99. {
  100. $first_year = $year;
  101. $first_month = $month;
  102. }
  103. // Set last dates
  104. $last_year = $year;
  105. $last_month = $month;
  106. $i++;
  107. }
  108. $show_year = $first_year;
  109. $selected_start_flag = TRUE;
  110. while($show_year <= $last_year)
  111. {
  112. $startDate .= "<optgroup label=\"".$show_year."\">";
  113. $s_m = 1;
  114. if($show_year == $first_year)
  115. {
  116. // If we are showing the first year, the starting month may not be January
  117. $s_m = $first_month;
  118. }
  119. $l_m = 12;
  120. if($show_year == $last_year)
  121. {
  122. // If we are showing the last year, the ending month may not be December
  123. $l_m = $last_month;
  124. }
  125. for ( $i=$s_m; $i <= $l_m; $i++ )
  126. {
  127. if ( $i < 10 )
  128. {
  129. // All months need to be two digits
  130. $i = "0".$i;
  131. }
  132. $startDate .= "<option value=\"".strtotime($show_year."-".$i."-01")."\"";
  133. if($selected_start_flag == TRUE)
  134. {
  135. $display_startDate = strtotime($show_year."-".$i."-01");
  136. $startDate .= " selected=\"selected\" ";
  137. $selected_start_flag = FALSE;
  138. }
  139. $startDate .= ">".date('M', mktime(0,0,0,$i,1))." ".$show_year."</option>";
  140. }
  141. $startDate .= "</optgroup>";
  142. $endDate .= "<optgroup label=\"".$show_year."\">";
  143. for ( $i=$s_m; $i <= $l_m; $i++ )
  144. {
  145. if ( $i < 10 )
  146. {
  147. // All months need to be two digits
  148. $i = "0".$i;
  149. }
  150. $endDate .= "<option value=\"".strtotime($show_year."-".$i."-".date('t', mktime(0,0,0,$i,1))." 23:59:59")."\"";
  151. if($i == $l_m AND $show_year == $last_year)
  152. {
  153. $display_endDate = strtotime($show_year."-".$i."-".date('t', mktime(0,0,0,$i,1))." 23:59:59");
  154. $endDate .= " selected=\"selected\" ";
  155. }
  156. $endDate .= ">".date('M', mktime(0,0,0,$i,1))." ".$show_year."</option>";
  157. }
  158. $endDate .= "</optgroup>";
  159. // Show next year
  160. $show_year++;
  161. }
  162. Event::run('ushahidi_filter.active_startDate', $display_startDate);
  163. Event::run('ushahidi_filter.active_endDate', $display_endDate);
  164. Event::run('ushahidi_filter.startDate', $startDate);
  165. Event::run('ushahidi_filter.endDate', $endDate);
  166. $template->content->div_timeline->startDate = $startDate;
  167. $template->content->div_timeline->endDate = $endDate;
  168. ///////////////////////////////////////////////////////////////MAP JAVA SCRIPT////////////////////////////////////////////////////////////////////////////
  169. //turn the map on, also turn on the timeline
  170. //$template->flot_enabled = TRUE; //this is done using our own custom .js files in the adminmap/js folder.
  171. $themes->map_enabled = true;
  172. //check if we're on the front end, if we are then the template and themese will be different
  173. if($themes != $template)
  174. {
  175. $themes->main_page = true;
  176. }
  177. $themes->js = new View($javascript_view);
  178. $themes->js->default_map = Kohana::config('settings.default_map');
  179. $themes->js->default_zoom = Kohana::config('settings.default_zoom');
  180. // Map Settings
  181. $clustering = Kohana::config('settings.allow_clustering');
  182. $marker_radius = Kohana::config('map.marker_radius');
  183. $marker_opacity = Kohana::config('map.marker_opacity');
  184. $marker_stroke_width = Kohana::config('map.marker_stroke_width');
  185. $marker_stroke_opacity = Kohana::config('map.marker_stroke_opacity');
  186. // pdestefanis - allows to restrict the number of zoomlevels available
  187. $numZoomLevels = Kohana::config('map.numZoomLevels');
  188. $minZoomLevel = Kohana::config('map.minZoomLevel');
  189. $maxZoomLevel = Kohana::config('map.maxZoomLevel');
  190. // pdestefanis - allows to limit the extents of the map
  191. $lonFrom = Kohana::config('map.lonFrom');
  192. $latFrom = Kohana::config('map.latFrom');
  193. $lonTo = Kohana::config('map.lonTo');
  194. $latTo = Kohana::config('map.latTo');
  195. $themes->js->json_url = $json_url;
  196. $themes->js->json_timeline_url = $json_timeline_url;
  197. $themes->js->marker_radius =
  198. ($marker_radius >=1 && $marker_radius <= 10 ) ? $marker_radius : 5;
  199. $themes->js->marker_opacity =
  200. ($marker_opacity >=1 && $marker_opacity <= 10 )
  201. ? $marker_opacity * 0.1 : 0.9;
  202. $themes->js->marker_stroke_width =
  203. ($marker_stroke_width >=1 && $marker_stroke_width <= 5 ) ? $marker_stroke_width : 2;
  204. $themes->js->marker_stroke_opacity =
  205. ($marker_stroke_opacity >=1 && $marker_stroke_opacity <= 10 )
  206. ? $marker_stroke_opacity * 0.1 : 0.9;
  207. // pdestefanis - allows to restrict the number of zoomlevels available
  208. $themes->js->numZoomLevels = $numZoomLevels;
  209. $themes->js->minZoomLevel = $minZoomLevel;
  210. $themes->js->maxZoomLevel = $maxZoomLevel;
  211. // pdestefanis - allows to limit the extents of the map
  212. $themes->js->lonFrom = $lonFrom;
  213. $themes->js->latFrom = $latFrom;
  214. $themes->js->lonTo = $lonTo;
  215. $themes->js->latTo = $latTo;
  216. $themes->js->default_map = Kohana::config('settings.default_map');
  217. $themes->js->default_zoom = Kohana::config('settings.default_zoom');
  218. $themes->js->latitude = Kohana::config('settings.default_lat');
  219. $themes->js->longitude = Kohana::config('settings.default_lon');
  220. $themes->js->default_map_all = Kohana::config('settings.default_map_all');
  221. $themes->js->active_startDate = $display_startDate;
  222. $themes->js->active_endDate = $display_endDate;
  223. }
  224. public static function set_categories($map_controller, $on_backend = false, $group = false)
  225. {
  226. // Check for localization of parent category
  227. // Get locale
  228. $l = Kohana::config('locale.language.0');
  229. $parent_categories = array();
  230. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  231. //Check to see if we're dealing with a group, and thus
  232. //should show group specific categories
  233. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  234. if($group != false)
  235. {
  236. //check and make sure the simpel groups category is installed
  237. $plugin = ORM::factory('plugin')
  238. ->where('plugin_name', 'simplegroups')
  239. ->where('plugin_active', '1')
  240. ->find();
  241. if(!$plugin)
  242. {
  243. throw new Exception("A group was set in adminmap_helper::set_categories() when the SimpleGroupl plugin is not installed");
  244. }
  245. $cats = ORM::factory('simplegroups_category');
  246. if(!$on_backend)
  247. {
  248. $cats = $cats->where('category_visible', '1');
  249. }
  250. $cats = $cats->where('parent_id', '0');
  251. $cats = $cats->where('applies_to_report', 1);
  252. $cats = $cats->where('simplegroups_groups_id', $group->id)
  253. ->find_all() ;
  254. foreach ($cats as $category)
  255. {
  256. /////////////////////////////////////////////////////////////////////////////////////////////
  257. // Get the children
  258. /////////////////////////////////////////////////////////////////////////////////////////////
  259. $children = array();
  260. foreach ($category->children as $child)
  261. {
  262. // Check for localization of child category
  263. $translated_title = Simplegroups_category_lang_Model::simplegroups_category_title($child->id,$l);
  264. if($translated_title)
  265. {
  266. $display_title = $translated_title;
  267. }
  268. else
  269. {
  270. $display_title = $child->category_title;
  271. }
  272. $children["sg_".$child->id] = array(
  273. $display_title,
  274. $child->category_color,
  275. $child->category_image
  276. );
  277. }
  278. $translated_title = Simplegroups_category_lang_Model::simplegroups_category_title($category->id,$l);
  279. if($translated_title)
  280. {
  281. $display_title = $translated_title;
  282. }else{
  283. $display_title = $category->category_title;
  284. }
  285. // Put it all together
  286. $parent_categories["sg_".$category->id] = array(
  287. $display_title,
  288. $category->category_color,
  289. $category->category_image,
  290. $children
  291. );
  292. }
  293. }
  294. /////////////////////////////////////////////////////////////////////////////////////////////
  295. // Get all active top level categories
  296. /////////////////////////////////////////////////////////////////////////////////////////////
  297. $cats = ORM::factory('category');
  298. if(!$on_backend)
  299. {
  300. $cats = $cats->where('category_visible', '1');
  301. }
  302. $cats = $cats->where('parent_id', '0')
  303. ->find_all() ;
  304. foreach ($cats as $category)
  305. {
  306. /////////////////////////////////////////////////////////////////////////////////////////////
  307. // Get the children
  308. /////////////////////////////////////////////////////////////////////////////////////////////
  309. $children = array();
  310. foreach ($category->children as $child)
  311. {
  312. // Check for localization of child category
  313. $translated_title = Category_Lang_Model::category_title($child->id,$l);
  314. if($translated_title)
  315. {
  316. $display_title = $translated_title;
  317. }
  318. else
  319. {
  320. $display_title = $child->category_title;
  321. }
  322. $children[$child->id] = array(
  323. $display_title,
  324. $child->category_color,
  325. $child->category_image
  326. );
  327. if ($child->category_trusted)
  328. { // Get Trusted Category Count
  329. $trusted = ORM::factory("incident")
  330. ->join("incident_category","incident.id","incident_category.incident_id")
  331. ->where("category_id",$child->id);
  332. if ( ! $trusted->count_all())
  333. {
  334. unset($children[$child->id]);
  335. }
  336. }
  337. }
  338. $translated_title = Category_Lang_Model::category_title($category->id,$l);
  339. if($translated_title)
  340. {
  341. $display_title = $translated_title;
  342. }else{
  343. $display_title = $category->category_title;
  344. }
  345. // Put it all together
  346. $parent_categories[$category->id] = array(
  347. $display_title,
  348. $category->category_color,
  349. $category->category_image,
  350. $children
  351. );
  352. if ($category->category_trusted)
  353. { // Get Trusted Category Count
  354. $trusted = ORM::factory("incident")
  355. ->join("incident_category","incident.id","incident_category.incident_id")
  356. ->where("category_id",$category->id);
  357. if ( ! $trusted->count_all())
  358. {
  359. unset($parent_categories[$category->id]);
  360. }
  361. }
  362. }
  363. $map_controller->template->content->categories = $parent_categories;
  364. }//end method
  365. /////////////////////////////////////////////////////////////////////////////////////////////////////
  366. /////////////////METHODS FOR the JSON CONTROLLER
  367. ///////////////////////////////////////////////////////////////////////////////////////////////////
  368. /**
  369. * Generate JSON in NON-CLUSTER mode
  370. * $edit_report_path is used to set where the link to edit/view a report should be set to
  371. * $on_the_back_end sets whether or not this user is viewing this data from the backend
  372. */
  373. public static function json_index($json_controller, $edit_report_path = 'admin/reports/edit/', $on_the_back_end = true,
  374. $extra_where_text = "",
  375. $joins = array(),
  376. $custom_category_to_table_mapping = array(),
  377. $link_target = "_self")
  378. {
  379. $json = "";
  380. $json_item = "";
  381. $json_array = array();
  382. $cat_array = array();
  383. $color = Kohana::config('settings.default_map_all');
  384. $default_color = Kohana::config('settings.default_map_all');
  385. $icon = "";
  386. $category_ids = array();
  387. $incident_id = "";
  388. $neighboring = "";
  389. $media_type = "";
  390. $show_unapproved="3"; //1 show only approved, 2 show only unapproved, 3 show all
  391. $logical_operator = "or";
  392. if( isset($_GET['c']) AND ! empty($_GET['c']) )
  393. {
  394. //check if there are any ',' in the category
  395. if((strpos($_GET['c'], ",")===false) && is_numeric($_GET['c']))
  396. {
  397. $category_ids = array($_GET['c']);
  398. }
  399. else
  400. {
  401. $category_ids = explode(",", $_GET['c'],-1); //get rid of that trailing ";"
  402. }
  403. }
  404. else
  405. {
  406. $category_ids = array("0");
  407. }
  408. $is_all_categories = false;
  409. If(count($category_ids) == 0 || $category_ids[0] == '0')
  410. {
  411. $is_all_categories = true;
  412. }
  413. $approved_text = "";
  414. if( $on_the_back_end)
  415. {
  416. //figure out if we're showing unapproved stuff or what.
  417. if (isset($_GET['u']) AND !empty($_GET['u']))
  418. {
  419. $show_unapproved = (int) $_GET['u'];
  420. }
  421. if($show_unapproved == 1)
  422. {
  423. $approved_text = "incident.incident_active = 1 ";
  424. }
  425. else if ($show_unapproved == 2)
  426. {
  427. $approved_text = "incident.incident_active = 0 ";
  428. }
  429. else if ($show_unapproved == 3)
  430. {
  431. $approved_text = " (incident.incident_active = 0 OR incident.incident_active = 1) ";
  432. }
  433. }
  434. else
  435. {
  436. $approved_text = "incident.incident_active = 1 ";
  437. }
  438. //should we color unapproved reports a different color?
  439. $color_unapproved = 2;
  440. if (isset($_GET['uc']) AND !empty($_GET['uc']))
  441. {
  442. $color_unapproved = (int) $_GET['uc'];
  443. }
  444. if (isset($_GET['lo']) AND !empty($_GET['lo']))
  445. {
  446. $logical_operator = $_GET['lo'];
  447. }
  448. if (isset($_GET['i']) AND !empty($_GET['i']))
  449. {
  450. $incident_id = (int) $_GET['i'];
  451. }
  452. if (isset($_GET['n']) AND !empty($_GET['n']))
  453. {
  454. $neighboring = (int) $_GET['n'];
  455. }
  456. $where_text = '';
  457. // Do we have a media id to filter by?
  458. if (isset($_GET['m']) AND !empty($_GET['m']) AND $_GET['m'] != '0')
  459. {
  460. $media_type = (int) $_GET['m'];
  461. $where_text .= " AND ".self::$table_prefix."media.media_type = " . $media_type;
  462. }
  463. if (isset($_GET['s']) AND !empty($_GET['s']))
  464. {
  465. $start_date = (int) $_GET['s'];
  466. $where_text .= " AND UNIX_TIMESTAMP(".self::$table_prefix."incident.incident_date) >= '" . $start_date . "'";
  467. }
  468. if (isset($_GET['e']) AND !empty($_GET['e']))
  469. {
  470. $end_date = (int) $_GET['e'];
  471. $where_text .= " AND UNIX_TIMESTAMP(".self::$table_prefix."incident.incident_date) <= '" . $end_date . "'";
  472. }
  473. //get our new custom color based on the categories we're working with
  474. $color = self::merge_colors($category_ids, $custom_category_to_table_mapping);
  475. $incidents = adminmap_reports::get_reports_list_by_cat($category_ids,
  476. $approved_text,
  477. $where_text. " ". $extra_where_text,
  478. $logical_operator,
  479. "incident.id",
  480. "asc",
  481. $joins,
  482. $custom_category_to_table_mapping);
  483. $curr_id = "not a number";
  484. $cat_names = array();
  485. $colors = array();
  486. $last_marker = null;
  487. $isnt_first = false;
  488. $json_item_first = ""; // Variable to store individual item for report detail page
  489. foreach ($incidents as $marker)
  490. {
  491. //if the is a new incident, or the same incident we looked at last time, then skip all of this
  492. if($isnt_first && ($curr_id != $marker->id))
  493. {
  494. $json_item = "{";
  495. $json_item .= "\"type\":\"Feature\",";
  496. $json_item .= "\"properties\": {";
  497. $json_item .= "\"id\": \"".$last_marker->id."\", \n";
  498. $cat_names_txt = "";
  499. $count = 0;
  500. If(!$is_all_categories && $logical_operator != "and")
  501. {
  502. $cat_names_txt = "<br/><br/>Falls under categories:<br/> ";
  503. foreach($cat_names as $cat_name)
  504. {
  505. $count++;
  506. if($count>1)
  507. {
  508. $cat_names_txt .= ", ";
  509. }
  510. $cat_names_txt .= $cat_name;
  511. }
  512. }
  513. $json_item .= "\"name\":\"" .date("n/j/Y", strtotime($marker->incident_date)).":<br/>". str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a target='".$link_target."' href='" . url::base() . $edit_report_path . $last_marker->id . "'>" . htmlentities($last_marker->incident_title) . "</a>".$cat_names_txt)) . "\",";
  514. //for compatiblity with the InfoWindows plugin
  515. $json_item .= "\"link\":\"" .url::base(). "$edit_report_path{$last_marker->id}\",";
  516. if (isset($category))
  517. {
  518. $json_item .= "\"category\":[" . $category_id . "], ";
  519. }
  520. else
  521. {
  522. $json_item .= "\"category\":[0], ";
  523. }
  524. //check if it's a unapproved/unactive report
  525. if($last_marker->incident_active == 0 && $color_unapproved==2)
  526. {
  527. $item_color = "000000";
  528. $json_item .= "\"color\": \"000000\", \n";
  529. $json_item .= "\"icon\": \"".$icon."\", \n";
  530. }
  531. //check if we're looking at all categories
  532. elseif(count($category_ids) == 0 || $category_ids[0] == '0')
  533. {
  534. $item_color = $default_color;
  535. $json_item .= "\"color\": \"".$default_color."\", \n";
  536. $json_item .= "\"icon\": \"".$icon."\", \n";
  537. }
  538. //check if we're using AND
  539. elseif($logical_operator=="and")
  540. {
  541. $item_color = $color;
  542. $json_item .= "\"color\": \"".$color."\", \n";
  543. $json_item .= "\"icon\": \"".$icon."\", \n";
  544. }
  545. //else we're using OR to combine categories
  546. else
  547. {
  548. $color = self::merge_colors_for_dots($colors);
  549. $item_color = $color;
  550. $json_item .= "\"color\": \"".$color."\", \n";
  551. $json_item .= "\"icon\": \"".$icon."\", \n";
  552. }
  553. $json_item .= "\"timestamp\": \"" . strtotime($last_marker->incident_date) . "\"";
  554. $json_item .= "},";
  555. $json_item .= "\"geometry\": {";
  556. $json_item .= "\"type\":\"Point\", ";
  557. $json_item .= "\"coordinates\":[" . $last_marker->location->longitude . ", " . $last_marker->location->latitude . "]";
  558. $json_item .= "}";
  559. $json_item .= "}";
  560. // Get Incident Geometries
  561. $geometry = self::_get_geometry($last_marker->id, $last_marker->incident_title, $last_marker->incident_date, $on_the_back_end, $item_color, $link_target);
  562. if (count($geometry))
  563. {
  564. $json_item = implode(",", $geometry);
  565. array_push($json_array, $json_item);
  566. }
  567. else //if there are markers then don't draw the central dot, it's just an aproximation
  568. {
  569. if ($last_marker->id == $incident_id)
  570. {
  571. $json_item_first = $json_item;
  572. }
  573. else
  574. {
  575. array_push($json_array, $json_item);
  576. }
  577. }
  578. $cat_array = array();
  579. //reset the variables
  580. $cat_names = array();
  581. $colors = array();
  582. }//end if
  583. //keep track how many categories we've found for this report
  584. $last_marker = $marker;
  585. $curr_id = $marker->id;
  586. $isnt_first = true;
  587. $incident_array = $marker->as_array();
  588. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  589. //Now we try and figure out which category this report was matched to
  590. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  591. if(isset($incident_array["is_parent"]) && $incident_array["is_parent"] != 0)
  592. {
  593. //echo $incident->incident_title." parent matched\r\n";
  594. $cat_names[$incident_array["parent_id"]] = $incident_array["parent_title"];
  595. $colors[$incident_array["parent_id"]] = $incident_array["parent_color"];
  596. }
  597. elseif(isset($incident_array["is_child"]) && $incident_array["is_child"] != 0)
  598. {
  599. //echo $incident->incident_title." kid matched\r\n";
  600. $cat_names[$incident_array["cat_id"]] = $incident_array["category_title"];
  601. $colors[$incident_array["cat_id"]] = $incident_array["color"];
  602. }
  603. //now the fun part, loop through all the custom categories
  604. foreach($custom_category_to_table_mapping as $name=>$custom_cat)
  605. {
  606. if(isset($incident_array["is_".$name."_parent"]) && $incident_array["is_".$name."_parent"] != 0)
  607. {
  608. //echo $incident->incident_title." $name parent matched\r\n";
  609. $cat_names[$name."_".$incident_array[$name."_parent_cat_id"]] = $incident_array[$name."_parent_title"];
  610. $colors[$name."_".$incident_array[$name."_parent_cat_id"]] = $incident_array[$name."_parent_color"];
  611. }
  612. elseif(isset($incident_array["is_".$name."_child"]) && $incident_array["is_".$name."_child"] != 0)
  613. {
  614. //echo $incident->incident_title." $name kid matched. With color: ".$incident_array[$name."_color"]."\r\n";
  615. $cat_names[$name."_".$incident_array[$name."_cat_id"]] = $incident_array[$name."_title"];
  616. $colors[$name."_".$incident_array[$name."_cat_id"]] = $incident_array[$name."_color"];
  617. }
  618. }
  619. }//end loop
  620. //catch the last one
  621. if($last_marker != null)
  622. {
  623. $json_item = "{";
  624. $json_item .= "\"type\":\"Feature\",";
  625. $json_item .= "\"properties\": {";
  626. $json_item .= "\"id\": \"".$last_marker->id."\", \n";
  627. $cat_names_txt = "";
  628. $count = 0;
  629. If(!$is_all_categories && $logical_operator != "and")
  630. {
  631. $cat_names_txt = "<br/><br/>Falls under categories:<br/> ";
  632. foreach($cat_names as $cat_name)
  633. {
  634. $count++;
  635. if($count>1)
  636. {
  637. $cat_names_txt .= ", ";
  638. }
  639. $cat_names_txt .= $cat_name;
  640. }
  641. }
  642. $json_item .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a target='".$link_target."' href='" . url::base() . $edit_report_path . $last_marker->id . "'>" . htmlentities($last_marker->incident_title) . "</a>".$cat_names_txt)) . "\",";
  643. //compatibility with the InfoWindow Plugin
  644. $json_item .= "\"link\":\"" .url::base(). "$edit_report_path{$last_marker->id}\",";
  645. if (isset($category))
  646. {
  647. $json_item .= "\"category\":[" . $category_id . "], ";
  648. }
  649. else
  650. {
  651. $json_item .= "\"category\":[0], ";
  652. }
  653. //check if it's a unapproved/unactive report
  654. if($last_marker->incident_active == 0 && $color_unapproved==2)
  655. {
  656. $item_color = "000000";
  657. $json_item .= "\"color\": \"000000\", \n";
  658. $json_item .= "\"icon\": \"".$icon."\", \n";
  659. }
  660. //check if we're looking at all categories
  661. elseif(count($category_ids) == 0 || $category_ids[0] == '0')
  662. {
  663. $item_color = $default_color;
  664. $json_item .= "\"color\": \"".$default_color."\", \n";
  665. $json_item .= "\"icon\": \"".$icon."\", \n";
  666. }
  667. //check if we're using AND
  668. elseif($logical_operator=="and")
  669. {
  670. $item_color = $color;
  671. $json_item .= "\"color\": \"".$color."\", \n";
  672. $json_item .= "\"icon\": \"".$icon."\", \n";
  673. }
  674. //else we're using OR to combine categories
  675. else
  676. {
  677. $color = self::merge_colors_for_dots($colors);
  678. $item_color = $color;
  679. $json_item .= "\"color\": \"".$color."\", \n";
  680. $json_item .= "\"icon\": \"".$icon."\", \n";
  681. }
  682. $json_item .= "\"timestamp\": \"" . strtotime($last_marker->incident_date) . "\"";
  683. $json_item .= "},";
  684. $json_item .= "\"geometry\": {";
  685. $json_item .= "\"type\":\"Point\", ";
  686. $json_item .= "\"coordinates\":[" . $last_marker->location->longitude . ", " . $last_marker->location->latitude . "]";
  687. $json_item .= "}";
  688. $json_item .= "}";
  689. // Get Incident Geometries
  690. $geometry = self::_get_geometry($last_marker->id, $last_marker->incident_title, $last_marker->incident_date, $on_the_back_end, $item_color, $link_target);
  691. if (count($geometry))
  692. {
  693. $json_item = implode(",", $geometry);
  694. array_push($json_array, $json_item);
  695. }
  696. else //if there are markers then don't draw the central dot, it's just an aproximation
  697. {
  698. if ($last_marker->id == $incident_id)
  699. {
  700. $json_item_first = $json_item;
  701. }
  702. else
  703. {
  704. array_push($json_array, $json_item);
  705. }
  706. }
  707. $cat_array = array();
  708. }//end catching the last one
  709. if ($json_item_first)
  710. { // Push individual marker in last so that it is layered on top when pulled into map
  711. array_push($json_array, $json_item_first);
  712. }
  713. $json = implode(",", $json_array);
  714. header('Content-type: application/json');
  715. $json_controller->template->json = $json;
  716. }
  717. /************************************************************************************************
  718. * Function, this'll merge colors. Given an array of category IDs it'll return a hex string
  719. * of all the colors merged together
  720. */
  721. public static function merge_colors($category_ids_temp, $custom_category_to_table_mapping = array())
  722. {
  723. //because I might unset some of the values in the $category_ids array
  724. $category_ids = adminmap_reports::array_copy($category_ids_temp);
  725. //check if we're looking at category 0
  726. if(count($category_ids) == 0 || $category_ids[0] == '0')
  727. {
  728. return Kohana::config('settings.default_map_all');
  729. }
  730. $red = 0;
  731. $green = 0;
  732. $blue = 0;
  733. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  734. //Lets handle custom categories
  735. foreach($custom_category_to_table_mapping as $cat_name=>$custom_cats)
  736. {
  737. $where_str_color = ""; //to get the colors we're gonna use
  738. $i = 0;
  739. foreach($category_ids as $key=>$id)
  740. {
  741. //check if we have a custom cateogry ID
  742. $delimiter_pos = strpos($id, "_");
  743. if($delimiter_pos !== false)
  744. {
  745. //get the custom category name
  746. $custom_cat_name = substr($id, 0, $delimiter_pos);
  747. //get the custom category's numeric id
  748. $custom_cat_id = substr($id,$delimiter_pos + 1);
  749. //does the custom_cat_name match our current custom cat
  750. if($cat_name == $custom_cat_name)
  751. {
  752. $i++;
  753. if($i > 1)
  754. {
  755. $where_str_color = $where_str_color . " OR ";
  756. }
  757. $where_str_color = $where_str_color . "id = ".$custom_cat_id;
  758. unset($category_ids[$key]);
  759. }
  760. }
  761. }
  762. if($where_str_color != "")
  763. {
  764. //get the custom categories themselves and add up their colors:
  765. // Retrieve all the categories with their colors
  766. $categories = ORM::factory($custom_cats['child'])
  767. ->where($where_str_color)
  768. ->find_all();
  769. //now for each color break it into RGB, add them up, then normalize
  770. foreach($categories as $category)
  771. {
  772. $color = $category->category_color;
  773. $numeric_colors = self::_hex2RGB($color);
  774. $red = $red + $numeric_colors['red'];
  775. $green = $green + $numeric_colors['green'];
  776. $blue = $blue + $numeric_colors['blue'];
  777. }
  778. }
  779. }//end loop through all custom categorie sources
  780. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  781. //Next lets handle regular categories
  782. //first lets figure out the composite color that we're gonna usehere
  783. if(count($category_ids) > 0)
  784. {
  785. $where_str_color = ""; //to get the colors we're gonna use
  786. $i = 0;
  787. foreach($category_ids as $id)
  788. {
  789. $i++;
  790. if($i > 1)
  791. {
  792. $where_str_color = $where_str_color . " OR ";
  793. }
  794. $where_str_color = $where_str_color . "id = ".$id;
  795. }
  796. // Retrieve all the categories with their colors
  797. $categories = ORM::factory('category')
  798. ->where($where_str_color)
  799. ->find_all();
  800. //now for each color break it into RGB, add them up, then normalize
  801. foreach($categories as $category)
  802. {
  803. $color = $category->category_color;
  804. $numeric_colors = self::_hex2RGB($color);
  805. $red = $red + $numeric_colors['red'];
  806. $green = $green + $numeric_colors['green'];
  807. $blue = $blue + $numeric_colors['blue'];
  808. }
  809. }
  810. //now normalize
  811. $color_length = sqrt( ($red*$red) + ($green*$green) + ($blue*$blue));
  812. //make sure there's no divide by zero
  813. if($color_length == 0)
  814. {
  815. $color_length = 255;
  816. }
  817. $red = ($red / $color_length) * 255;
  818. $green = ($green / $color_length) * 255;
  819. $blue = ($blue / $color_length) * 255;
  820. //pad with zeros if there's too much space
  821. $red = dechex($red);
  822. if(strlen($red) < 2)
  823. {
  824. $red = "0".$red;
  825. }
  826. $green = dechex($green);
  827. if(strlen($green) < 2)
  828. {
  829. $green = "0".$green;
  830. }
  831. $blue = dechex($blue);
  832. if(strlen($blue) < 2)
  833. {
  834. $blue = "0".$blue;
  835. }
  836. //now put the color back together and return it
  837. return $red.$green.$blue;
  838. }//end method merge colors
  839. /************************************************************************************************
  840. * Function, this'll merge colors. Given an array of category IDs it'll return a hex string
  841. * of all the colors merged together
  842. */
  843. public static function merge_colors_for_dots($colors)
  844. {
  845. //check if we're dealing with just one color
  846. if(count($colors)==1)
  847. {
  848. foreach($colors as $color)
  849. {
  850. return $color;
  851. }
  852. }
  853. //now for each color break it into RGB, add them up, then normalize
  854. $red = 0;
  855. $green = 0;
  856. $blue = 0;
  857. foreach($colors as $color)
  858. {
  859. $numeric_colors = self::_hex2RGB($color);
  860. $red = $red + $numeric_colors['red'];
  861. $green = $green + $numeric_colors['green'];
  862. $blue = $blue + $numeric_colors['blue'];
  863. }
  864. //now normalize
  865. $color_length = sqrt( ($red*$red) + ($green*$green) + ($blue*$blue));
  866. //make sure there's no divide by zero
  867. if($color_length == 0)
  868. {
  869. $color_length = 255;
  870. }
  871. $red = ($red / $color_length) * 255;
  872. $green = ($green / $color_length) * 255;
  873. $blue = ($blue / $color_length) * 255;
  874. //pad with zeros if there's too much space
  875. $red = dechex($red);
  876. if(strlen($red) < 2)
  877. {
  878. $red = "0".$red;
  879. }
  880. $green = dechex($green);
  881. if(strlen($green) < 2)
  882. {
  883. $green = "0".$green;
  884. }
  885. $blue = dechex($blue);
  886. if(strlen($blue) < 2)
  887. {
  888. $blue = "0".$blue;
  889. }
  890. //now put the color back together and return it
  891. return $red.$green.$blue;
  892. }//end method merge colors
  893. private static function _hex2RGB($hexStr, $returnAsString = false, $seperator = ',')
  894. {
  895. $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
  896. $rgbArray = array();
  897. if (strlen($hexStr) == 6)
  898. { //If a proper hex code, convert using bitwise operation. No overhead... faster
  899. $colorVal = hexdec($hexStr);
  900. $rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
  901. $rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
  902. $rgbArray['blue'] = 0xFF & $colorVal;
  903. }
  904. elseif (strlen($hexStr) == 3)
  905. { //if shorthand notation, need some string manipulations
  906. $rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
  907. $rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
  908. $rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
  909. }
  910. else
  911. {
  912. return false; //Invalid hex color code
  913. }
  914. return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array
  915. }
  916. /***************************************************************************************************************
  917. * Generate JSON in CLUSTER mode
  918. * $edit_report_path sets the path to the link to edit/view a report
  919. * $list_report_path sets the path to view a cluster of reports
  920. * $on_the_back_end sets whether or not this user is looking at this from the front end or back end
  921. */
  922. public static function json_cluster($controller,
  923. $edit_report_path = 'admin/reports/edit/',
  924. $list_reports_path = "admin/adminmap_reports/index/",
  925. $on_the_back_end = true,
  926. $extra_where_text = "",
  927. $joins = array(),
  928. $custom_category_to_table_mapping = array(),
  929. $link_target = "_self")
  930. {
  931. //check to see how we're adding GET params
  932. $url_param_join_character = (strpos($list_reports_path, "?") === false) ? "?" : "&";
  933. // Database
  934. $db = new Database();
  935. $json = "";
  936. $json_item = "";
  937. $json_array = array();
  938. $geometry_array = array();
  939. $color = Kohana::config('settings.default_map_all');
  940. $default_color = Kohana::config('settings.default_map_all');
  941. $icon = "";
  942. $logical_operator = "or";
  943. $show_unapproved="3"; //1 show only approved, 2 show only unapproved, 3 show all
  944. if($on_the_back_end)
  945. {
  946. //figure out if we're showing unapproved stuff or what.
  947. if (isset($_GET['u']) AND !empty($_GET['u']))
  948. {
  949. $show_unapproved = (int) $_GET['u'];
  950. }
  951. $approved_text = "";
  952. if($show_unapproved == 1)
  953. {
  954. $approved_text = "incident.incident_active = 1 ";
  955. }
  956. else if ($show_unapproved == 2)
  957. {
  958. $approved_text = "incident.incident_active = 0 ";
  959. }
  960. else if ($show_unapproved == 3)
  961. {
  962. $approved_text = " (incident.incident_active = 0 OR incident.incident_active = 1) ";
  963. }
  964. }
  965. else
  966. {
  967. $approved_text = "incident.incident_active = 1 ";
  968. $show_unapproved = 1;
  969. }
  970. //should we color unapproved reports a different color?
  971. $color_unapproved = 2;
  972. if (isset($_GET['uc']) AND !empty($_GET['uc']))
  973. {
  974. $color_unapproved = (int) $_GET['uc'];
  975. }
  976. if (isset($_GET['lo']) AND !empty($_GET['lo']))
  977. {
  978. $logical_operator = $_GET['lo'];
  979. }
  980. // Get Zoom Level
  981. $zoomLevel = (isset($_GET['z']) AND !empty($_GET['z'])) ?
  982. (int) $_GET['z'] : 8;
  983. //$distance = 60;
  984. $distance = ((10000000 >> $zoomLevel) / 100000) / 2.5;
  985. // Category ID
  986. $is_all_categories = false;
  987. $category_ids=array();
  988. if( isset($_GET['c']) AND ! empty($_GET['c']) )
  989. {
  990. //check if there are any ',' in the category
  991. if((strpos($_GET['c'], ",")===false) && is_numeric($_GET['c']))
  992. {
  993. $category_ids = array($_GET['c']);
  994. }
  995. else
  996. {
  997. $category_ids = explode(",", $_GET['c'],-1); //get rid of that trailing ";"
  998. }
  999. }
  1000. else
  1001. {
  1002. $category_ids = array("0");
  1003. }
  1004. If(count($category_ids) == 0 || $category_ids[0] == '0')
  1005. {
  1006. $is_all_categories = true;
  1007. }
  1008. // Start Date
  1009. $start_date = (isset($_GET['s']) AND !empty($_GET['s'])) ?
  1010. (int) $_GET['s'] : "0";
  1011. // End Date
  1012. $end_date = (isset($_GET['e']) AND !empty($_GET['e'])) ?
  1013. (int) $_GET['e'] : "0";
  1014. // SouthWest Bound
  1015. $southwest = (isset($_GET['sw']) AND !empty($_GET['sw'])) ?
  1016. $_GET['sw'] : "0";
  1017. $northeast = (isset($_GET['ne']) AND !empty($_GET['ne'])) ?
  1018. $_GET['ne'] : "0";
  1019. $filter = "";
  1020. $filter .= ($start_date) ?
  1021. " AND incident.incident_date >= '" . date("Y-m-d H:i:s", $start_date) . "'" : "";
  1022. $filter .= ($end_date) ?
  1023. " AND incident.incident_date <= '" . date("Y-m-d H:i:s", $end_date) . "'" : "";
  1024. if ($southwest AND $northeast)
  1025. {
  1026. list($latitude_min, $longitude_min) = explode(',', $southwest);
  1027. list($latitude_max, $longitude_max) = explode(',', $northeast);
  1028. $filter .= " AND location.latitude >=".(float) $latitude_min.
  1029. " AND location.latitude <=".(float) $latitude_max;
  1030. $filter .= " AND location.longitude >=".(float) $longitude_min.
  1031. " AND location.longitude <=".(float) $longitude_max;
  1032. }
  1033. //stuff john just added
  1034. $color = self::merge_colors($category_ids, $custom_category_to_table_mapping);
  1035. //$incidents = reports::get_reports($category_ids, $approved_text, $filter, $logical_operator);
  1036. $incidents = adminmap_reports::get_reports_list_by_cat($category_ids,
  1037. $approved_text,
  1038. $filter. " ". $extra_where_text,
  1039. $logical_operator,
  1040. "incident.id",
  1041. "asc",
  1042. $joins,
  1043. $custom_category_to_table_mapping);
  1044. /**
  1045. * **********************************************************************************************************************
  1046. * **********************************************************************************************************************
  1047. * We have the incidents, now process them
  1048. */
  1049. // Create markers by marrying the the stuff together
  1050. $markers = array();
  1051. $last_incident = null;
  1052. $curr_id = "no idea";
  1053. $isnt_first = false;
  1054. $cat_names = array();
  1055. $colors = array();
  1056. foreach($incidents as $incident)
  1057. {
  1058. if($isnt_first && $curr_id != $incident->id)
  1059. {
  1060. //echo $last_incident->incident_title."\n\r".Kohana::debug($cat_names)."\r\n\r\n";
  1061. $incident_info = array("incident" => $last_incident,
  1062. "colors"=>$colors,
  1063. "cat_names"=>$cat_names);
  1064. //array_push($markers, $incident_info);
  1065. $markers[] = $incident_info;
  1066. //reset the arrays
  1067. $cat_names = array();
  1068. $colors = array();
  1069. }
  1070. //things to keep us moving forward
  1071. $last_incident = $incident;
  1072. $curr_id = $incident->id;
  1073. $isnt_first = true;
  1074. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1075. //Now we try and figure out which category this report was matched to
  1076. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1077. //if we're looking at all categories don't bother doing all this crazyness
  1078. if(!$is_all_categories)
  1079. {
  1080. $incident_array = $incident->as_array();
  1081. if(isset($incident_array["is_parent"]) && $incident_array["is_parent"] != 0)
  1082. {
  1083. //echo $incident->incident_title." parent matched\r\n";
  1084. $cat_names[$incident->parent_id] = $incident->parent_title;
  1085. $colors[$incident->parent_id] = $incident->parent_color;
  1086. }
  1087. elseif(isset($incident_array["is_child"]) && $incident_array["is_child"] != 0)
  1088. {
  1089. //echo $incident->incident_title." kid matched\r\n";
  1090. $cat_names[$incident->cat_id] = $incident->category_title;
  1091. $colors[$incident->cat_id] = $incident->color;
  1092. }
  1093. //now the fun part, loop through all the custom categories
  1094. foreach($custom_category_to_table_mapping as $name=>$custom_cat)
  1095. {
  1096. if(isset($incident_array["is_".$name."_parent"]) && $incident_array["is_".$name."_parent"] != 0)
  1097. {
  1098. //echo $incident->incident_title." $name parent matched\r\n";
  1099. $cat_names[$name."_".$incident_array[$name."_parent_cat_id"]] = $incident_array[$name."_parent_title"];
  1100. $colors[$name."_".$incident_array[$name."_parent_cat_id"]] = $incident_array[$name."_parent_color"];
  1101. }
  1102. elseif(isset($incident_array["is_".$name."_child"]) && $incident_array["is_".$name."_child"] != 0)
  1103. {
  1104. //echo $incident->incident_title." $name kid matched. With color: ".$incident_array[$name."_color"]."\r\n";
  1105. $cat_names[$name."_".$incident_array[$name."_cat_id"]] = $incident_array[$name."_title"];
  1106. $colors[$name."_".$incident_array[$name."_cat_id"]] = $incident_array[$name."_color"];
  1107. }
  1108. }
  1109. }//end if not is_all_categories
  1110. unset($incident_array);
  1111. }//end loop
  1112. //catch the last report
  1113. if($last_incident != null)
  1114. {
  1115. //echo "last one ".$last_incident->incident_title."\n\r".Kohana::debug($cat_names)."\r\n\r\n";
  1116. $incident_info = array("incident" => $last_incident, "colors"=>$colors, "cat_names"=>$cat_names);
  1117. $markers[] = $incident_info;
  1118. }
  1119. //echo "___________________________________________________\r\n";
  1120. $clusters = array(); // Clustered
  1121. $singles = array(); // Non Clustered
  1122. // Loop until all markers have been compared
  1123. while (count($markers))
  1124. {
  1125. $marker_info = array_pop($markers);
  1126. $colors = $marker_info["colors"];
  1127. $cat_names = $marker_info["cat_names"];
  1128. $marker = $marker_info["incident"];
  1129. //echo "\r\nLooking for clusters around ". $marker->incident_title. "\r\n";
  1130. $cluster = array();
  1131. $category_colors = array(); //all the colors that were seen while making a cluster
  1132. $category_count = array(); //how many times we've seen a category while making this cluster
  1133. $category_names = array(); //all the names
  1134. $contains_nonactive = false;
  1135. // Compare marker against all remaining markers.
  1136. foreach ($markers as $key => $target_info)
  1137. {
  1138. $target = $target_info["incident"];
  1139. $pixels = abs($marker->location->longitude - $target->location->longitude) + abs($marker->location->latitude - $target->location->latitude);
  1140. // If two markers are closer than defined distance, remove compareMarker from array and add to cluster.
  1141. if ($pixels < $distance)
  1142. {
  1143. unset($markers[$key]);
  1144. $target_colors = $target_info["colors"];
  1145. $target_cat_names = $target_info["cat_names"];
  1146. // Get Incident Geometries
  1147. // are we using all categories red, or a specific blend of colors and goodness?
  1148. $geo_color = $is_all_categories || $logical_operator=="and" ? $color : self::merge_colors_for_dots($target_colors);
  1149. /** This is too slow
  1150. /*$geometry = self::_get_geometry($target->id,
  1151. $target->incident_title,
  1152. $target->incident_date,
  1153. $on_the_back_end,
  1154. $geo_color,
  1155. $link_target);
  1156. $geometry = array();
  1157. if (count($geometry))
  1158. {
  1159. $json_item = implode(",", $geometry);
  1160. array_push($geometry_array, $json_item);
  1161. }
  1162. else // we don't want any average markers, just the orignals
  1163. */
  1164. {
  1165. $cluster[] = $target;
  1166. }
  1167. //check if the colors and category names have been accounted for
  1168. if(!$is_all_categories)
  1169. {
  1170. foreach($target_colors as $cat_id => $target_color)
  1171. {
  1172. //echo "\t\t".$target->incident_title. " has category: ".$target_cat_names[$cat_id]."\r\n";
  1173. //colors
  1174. $category_colors[$cat_id] = $target_color;
  1175. //name
  1176. $category_names[$cat_id] = $target_cat_names[$cat_id];
  1177. //count
  1178. if(isset($category_count[$cat_id]))
  1179. {
  1180. $category_count[$cat_id] = $category_count[$cat_id] + 1;
  1181. }
  1182. else
  1183. {
  1184. $category_count[$cat_id] = 1;
  1185. }
  1186. }//end loop
  1187. } //end if
  1188. //check if this is a unapproved report
  1189. if($target->incident_active == 0)
  1190. {
  1191. $contains_nonactive = true;
  1192. }
  1193. }//end if the the two points are close
  1194. //trying to minizmie memory use
  1195. unset($target_colors);
  1196. unset($target_cat_names);
  1197. unset($target);
  1198. }//end for loop
  1199. // If a marker was added to cluster, also add the marker we were comparing to.
  1200. if (count($cluster) > 0)
  1201. {
  1202. // Get Incident Geometries
  1203. // are we using all categories red, or a specific blend of colors and goodness?
  1204. /**
  1205. /**This is too slow
  1206. $geo_color = $is_all_categories || $logical_operator=="and" ? $color : self::merge_colors_for_dots($colors);
  1207. /*$geometry = self::_get_geometry($marker->id,
  1208. $marker->incident_title,
  1209. $marker->incident_date,
  1210. $on_the_back_end,
  1211. $geo_color,
  1212. $link_target);
  1213. $geometry = array();
  1214. if (count($geometry))
  1215. {
  1216. $json_item = implode(",", $geometry);
  1217. array_push($geometry_array, $json_item);
  1218. }
  1219. else*/
  1220. {
  1221. $cluster[] = $marker;
  1222. }
  1223. //check if the colors and category names have been accounted for
  1224. if(!$is_all_categories)
  1225. {
  1226. foreach($colors as $cat_id2 => $marker_color)
  1227. {
  1228. //echo "\t\t".$marker->incident_title. " has category: ".$cat_names[$cat_id2]."\r\n";
  1229. //colors
  1230. $category_colors[$cat_id2] = $marker_color;
  1231. //name
  1232. $category_names[$cat_id2] = $cat_names[$cat_id2];
  1233. //count
  1234. if(isset($category_count[$cat_id2]))
  1235. {
  1236. $category_count[$cat_id2] = $category_count[$cat_id2] + 1;
  1237. }
  1238. else
  1239. {
  1240. $category_count[$cat_id2] = 1;
  1241. }
  1242. }//end loop
  1243. }//end if
  1244. //check if this is an unapproved report
  1245. if($marker->incident_active == 0)
  1246. {
  1247. $contains_nonactive = true;
  1248. }
  1249. if($contains_nonactive)
  1250. {
  1251. $clusters[] = array( 'contains_nonactive' => TRUE, 'cluster'=> $cluster, 'category_count'=>$category_count,
  1252. 'category_names'=>$category_names, 'category_colors'=>$category_colors);
  1253. }
  1254. else
  1255. {
  1256. $clusters[] = array( 'contains_nonactive' => FALSE, 'cluster'=> $cluster, 'category_count'=>$category_count,
  1257. 'category_names'=>$category_names, 'category_colors'=>$category_colors);
  1258. }
  1259. }
  1260. else
  1261. {
  1262. // Get Incident Geometries
  1263. // are we using all categories red, or a specific blend of colors and goodness?
  1264. /** THIS IS TOO SLOW
  1265. $geo_color = $is_all_categories || $logical_operator=="and" ? $color : self::merge_colors_for_dots($marker_info["colors"]);
  1266. $geometry = self::_get_geometry($marker_info["incident"]->id,
  1267. $marker_info["incident"]->incident_title,
  1268. $marker_info["incident"]->incident_date,
  1269. $on_the_back_end,
  1270. $geo_color,
  1271. $link_target);
  1272. if (count($geometry))
  1273. {
  1274. $json_item = implode(",", $geometry);
  1275. array_push($geometry_array, $json_item);
  1276. }
  1277. else //if there are markers we don't want any average dots, we just want original dots
  1278. */
  1279. {
  1280. $singles[] = $marker_info;
  1281. }
  1282. }
  1283. }
  1284. //echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n\r\n";
  1285. $i = 0;
  1286. // Create Json
  1287. foreach ($clusters as $cluster_alpha)
  1288. {
  1289. $cluster = $cluster_alpha['cluster'];
  1290. $cluster_cat_colors = $cluster_alpha['category_colors'];
  1291. $cluster_cat_names = $cluster_alpha['category_names'];
  1292. $cluster_cat_count = $cluster_alpha['category_count'];
  1293. //make category description string
  1294. $category_str = "";
  1295. if( (!$is_all_categories) && ($logical_operator!="and"))
  1296. {
  1297. foreach($cluster_cat_count as $cat_id => $cat_count)
  1298. {
  1299. $category_str .= "<li>".$cluster_cat_names[$cat_id]." (".$cat_count.")</li>";
  1300. }
  1301. $category_str = "<br/><br/> Categories in this cluster (number of reports):<ul>". $category_str."</ul>";
  1302. }
  1303. //make the categories string for the URL
  1304. //if we're on the backend do it the old fashioned way
  1305. $categories_str = "";
  1306. if($on_the_back_end)
  1307. {
  1308. $categories_str = implode(",", $category_ids);
  1309. $categories_str = "c=" . $categories_str;
  1310. }
  1311. //otherwise do it the new fangled fancy way, which is better, but it's a pain to change the way i do everything.
  1312. else
  1313. {
  1314. foreach($category_ids as $c)
  1315. {
  1316. $categories_str .= "&c%5B%5D=" . mysql_real_escape_string($c);
  1317. }
  1318. }
  1319. $contains_nonactive = $cluster_alpha['contains_nonactive'];
  1320. // Calculate cluster center
  1321. $bounds = self::_calculateCenter($cluster);
  1322. $cluster_center = $bounds['center'];
  1323. $southwest = $bounds['sw'];
  1324. $northeast = $bounds['ne'];
  1325. $time_filter = "";
  1326. if($start_date != "0" && $end_date != "0")
  1327. {
  1328. $time_filter = "&s=".$start_date."&e=".$end_date;
  1329. }
  1330. // Number of Items in Cluster
  1331. $cluster_count = count($cluster);
  1332. $json_item = "{\"type\":\"Featur…

Large files files are truncated, but you can click here to view the full file