Example #1
0
 public function index()
 {
     $this->template->header->this_page = 'home';
     $this->template->content = new View('main');
     //get images from flickr
     $flickr = new PhpFlickr('7ffd3c4b9d9f3a486b67124d5b530f11');
     $haiti_photos = $flickr->photos_search(array('text' => 'earthquake in haiti', 'per_page' => 12));
     $this->template->content->flickr = $flickr;
     $this->template->content->haiti_photos = $haiti_photos;
     // Get all active top level categories
     $parent_categories = array();
     foreach (ORM::factory('category')->where('category_visible', '1')->where('parent_id', '0')->find_all() as $category) {
         // Get The Children
         $children = array();
         foreach ($category->children as $child) {
             $children[$child->id] = array($child->category_title, $child->category_color, $child->category_image);
         }
         // Put it all together
         $parent_categories[$category->id] = array($category->category_title, $category->category_color, $category->category_image, $children);
     }
     $this->template->content->categories = $parent_categories;
     // Get all active Layers (KMZ/KML)
     $layers = array();
     foreach (ORM::factory('layer')->where('layer_visible', 1)->find_all() as $layer) {
         $layers[$layer->id] = array($layer->layer_name, $layer->layer_color, $layer->layer_url, $layer->layer_file);
     }
     $this->template->content->layers = $layers;
     // Get all active Shares
     $shares = array();
     foreach (ORM::factory('sharing')->where('sharing_active', 1)->where('sharing_type', 1)->find_all() as $share) {
         $shares[$share->id] = array($share->sharing_site_name, $share->sharing_color);
     }
     $this->template->content->shares = $shares;
     // Get Reports
     // XXX: Might need to replace magic no. 8 with a constant
     $this->template->content->total_items = ORM::factory('incident')->where('incident_active', '1')->limit('8')->count_all();
     $this->template->content->incidents = ORM::factory('incident')->where('incident_active', '1')->limit('14')->orderby('incident_date', 'desc')->with('location')->find_all();
     // Get quick stats for "Latest Activity"
     // note: Kohana ORM doesn't support these fancy date search features
     $this->template->content->latest_activity_today = count($this->db->query('SELECT id FROM incident WHERE incident_dateadd >= DATE_SUB(CURDATE(),INTERVAL 0 DAY);'));
     $this->template->content->latest_activity_yesterday = count($this->db->query('SELECT id FROM incident WHERE incident_dateadd >= DATE_SUB(CURDATE(),INTERVAL 1 DAY) AND incident_dateadd < DATE_SUB(CURDATE(),INTERVAL 0 DAY);'));
     $this->template->content->latest_activity_week = count($this->db->query('SELECT id FROM incident WHERE incident_dateadd >= DATE_SUB(CURDATE(),INTERVAL 1 WEEK);'));
     $this->template->content->latest_activity_total_from_sms = Incident_Model::num_incidents_by_sms();
     // End getting of quick stats for "Latest Activity"
     // Get Default Color
     $this->template->content->default_map_all = Kohana::config('settings.default_map_all');
     // Get Twitter Hashtags
     $this->template->content->twitter_hashtag_array = array_filter(array_map('trim', explode(',', Kohana::config('settings.twitter_hashtags'))));
     // Get Report-To-Email
     $this->template->content->report_email = Kohana::config('settings.site_email');
     // Get SMS Numbers
     $phone_array = array();
     $sms_no1 = Kohana::config('settings.sms_no1');
     $sms_no2 = Kohana::config('settings.sms_no2');
     $sms_no3 = Kohana::config('settings.sms_no3');
     if (!empty($sms_no1)) {
         $phone_array[] = $sms_no1;
     }
     if (!empty($sms_no2)) {
         $phone_array[] = $sms_no2;
     }
     if (!empty($sms_no3)) {
         $phone_array[] = $sms_no3;
     }
     $this->template->content->phone_array = $phone_array;
     $this->template->header->phone_array = $phone_array;
     // Because we need some custom language around these numbers,
     // I'm just sending them straight to the template for "hardcoding"
     $this->template->content->sms_no1 = $sms_no1;
     $this->template->content->sms_no2 = $sms_no2;
     $this->template->header->sms_no1 = $sms_no1;
     $this->template->header->sms_no2 = $sms_no2;
     // Get RSS News Feeds but don't include Global Voices (8)
     $this->template->content->feeds = ORM::factory('feed_item')->limit('10')->where('feed_id !=', '8')->orderby('item_date', 'desc')->find_all();
     // Get Global Voices Feed
     $this->template->content->gvfeeds = ORM::factory('feed_item')->limit('10')->where('feed_id', '8')->orderby('item_date', 'desc')->find_all();
     // Get The START, END and most ACTIVE Incident Dates
     $startDate = "";
     $endDate = "";
     $active_month = 0;
     $active_startDate = 0;
     $active_endDate = 0;
     $db = new Database();
     // First Get The Most Active Month
     $query = $db->query('SELECT incident_date, count(*) AS incident_count FROM incident WHERE incident_active = 1 GROUP BY DATE_FORMAT(incident_date, \'%Y-%m\') ORDER BY incident_count DESC LIMIT 1');
     foreach ($query as $query_active) {
         $active_month = date('n', strtotime($query_active->incident_date));
         $active_year = date('Y', strtotime($query_active->incident_date));
         $active_startDate = strtotime($active_year . "-" . $active_month . "-01");
         $active_endDate = strtotime($active_year . "-" . $active_month . "-" . date('t', mktime(0, 0, 0, $active_month, 1)) . " 23:59:59");
     }
     /** HARDCODED SLIDER SET UP **/
     // We'll Hardcode in the Start/End Dates
     $timeframe_start = strtotime("2010-01-12", 0);
     $timeframe_stop = strtotime(date("Y-m-d"), 0);
     $active_startDate = $timeframe_start;
     $active_endDate = $timeframe_stop + 86399;
     $days = floor(($timeframe_stop - $timeframe_start) / 86400);
     $startDate = "<optgroup label=\"2010\">";
     $endDate = "<optgroup label=\"2010\">";
     for ($i = 0; $i <= $days; $i++) {
         $startDate .= "<option value=\"" . $timeframe_start . "\"";
         if ($i == 0) {
             $startDate .= " selected=\"selected\" ";
         }
         $startDate .= ">" . date('M j', $timeframe_start) . " 2010</option>";
         $timeframe_stop = $timeframe_start + 86399;
         $endDate .= "<option value=\"" . $timeframe_stop . "\"";
         if ($i == $days) {
             $endDate .= " selected=\"selected\" ";
         }
         $endDate .= ">" . date('M j', $timeframe_stop) . " 2010</option>";
         $timeframe_start = $timeframe_start + 86400;
     }
     $startDate .= "</optgroup>";
     $endDate .= "</optgroup>";
     /** OLD SLIDER SET UP **/
     // Next, Get the Range of Years
     //        $query = $db->query('SELECT DATE_FORMAT(incident_date, \'%Y\') AS incident_date FROM incident WHERE incident_active = 1 GROUP BY DATE_FORMAT(incident_date, \'%Y\') ORDER BY incident_date');
     $query = array();
     foreach ($query as $slider_date) {
         $years = $slider_date->incident_date;
         $startDate .= "<optgroup label=\"" . $years . "\">";
         for ($i = 1; $i <= 12; $i++) {
             if ($i < 10) {
                 $i = "0" . $i;
             }
             $startDate .= "<option value=\"" . strtotime($years . "-" . $i . "-01") . "\"";
             if ($active_month && (int) $i == $active_month - 1) {
                 $startDate .= " selected=\"selected\" ";
             }
             $startDate .= ">" . date('M', mktime(0, 0, 0, $i, 1)) . " " . $years . "</option>";
         }
         $startDate .= "</optgroup>";
         $endDate .= "<optgroup label=\"" . $years . "\">";
         for ($i = 1; $i <= 12; $i++) {
             if ($i < 10) {
                 $i = "0" . $i;
             }
             $endDate .= "<option value=\"" . strtotime($years . "-" . $i . "-" . date('t', mktime(0, 0, 0, $i, 1)) . " 23:59:59") . "\"";
             if ($active_month && (int) $i == $active_month + 1 || $i == 12) {
                 $endDate .= " selected=\"selected\" ";
             }
             $endDate .= ">" . date('M', mktime(0, 0, 0, $i, 1)) . " " . $years . "</option>";
         }
         $endDate .= "</optgroup>";
     }
     $this->template->content->startDate = $startDate;
     $this->template->content->endDate = $endDate;
     //$this->template->content->startDate = '<select name="startDate" id="startDate"><optgroup label="2010"><option value="1262325600">Jan 2010</option></optgroup></select>';
     //$this->template->content->endDate = '<select name="endDate" id="endDate"><optgroup label="2010"><option value="1265003999">Jan 2010</option><option value="1267423199" selected="selected" >Feb 2010</option></optgroup></select>';
     // get graph data
     // could not use DB query builder. It does not support parentheses yet
     $graph_data = array();
     $all_graphs = Incident_Model::get_incidents_by_interval('month');
     $daily_graphs = Incident_Model::get_incidents_by_interval('day');
     $weekly_graphs = Incident_Model::get_incidents_by_interval('week');
     $hourly_graphs = Incident_Model::get_incidents_by_interval('hour');
     $this->template->content->all_graphs = $all_graphs;
     $this->template->content->daily_graphs = $daily_graphs;
     // If we are looking at the standard street map set by user
     if (!isset($_GET['3dmap'])) {
         //echo 'STREET MAP';
         // Javascript Header
         $this->template->header->map_enabled = 'streetmap';
         $this->template->content->map_enabled = 'streetmap';
         $this->template->content->map_container = 'map';
         $this->template->header->main_page = TRUE;
         $this->template->header->validator_enabled = TRUE;
         // Map Settings
         $clustering = Kohana::config('settings.allow_clustering');
         $marker_radius = Kohana::config('map.marker_radius');
         $marker_opacity = Kohana::config('map.marker_opacity');
         $marker_stroke_width = Kohana::config('map.marker_stroke_width');
         $marker_stroke_opacity = Kohana::config('map.marker_stroke_opacity');
         $this->template->header->js = $clustering ? new View('main_cluster_js') : new View('main_js');
         $this->template->header->js->cluster = $clustering == 1 ? "true" : "false";
         $this->template->header->js->marker_radius = $marker_radius >= 1 && $marker_radius <= 10 ? $marker_radius : 5;
         $this->template->header->js->marker_opacity = $marker_opacity >= 1 && $marker_opacity <= 10 ? $marker_opacity * 0.1 : 0.9;
         $this->template->header->js->marker_stroke_width = $marker_stroke_width >= 1 && $marker_stroke_width <= 5 ? $marker_stroke_width : 2;
         $this->template->header->js->marker_stroke_opacity = $marker_stroke_opacity >= 1 && $marker_stroke_opacity <= 10 ? $marker_stroke_opacity * 0.1 : 0.9;
         $this->template->header->js->default_map = Kohana::config('settings.default_map');
         $this->template->header->js->default_zoom = Kohana::config('settings.default_zoom');
         $this->template->header->js->latitude = Kohana::config('settings.default_lat');
         $this->template->header->js->longitude = Kohana::config('settings.default_lon');
         $this->template->header->js->graph_data = $graph_data;
         $this->template->header->js->all_graphs = $all_graphs;
         $this->template->header->js->daily_graphs = $daily_graphs;
         $this->template->header->js->hourly_graphs = $hourly_graphs;
         $this->template->header->js->weekly_graphs = $weekly_graphs;
         $this->template->header->js->default_map_all = Kohana::config('settings.default_map_all');
         //
         $this->template->header->js->active_startDate = $active_startDate;
         $this->template->header->js->active_endDate = $active_endDate;
         // If we are viewing the 3D map
     } else {
         //echo '3D MAP';
         // Javascript Header
         $this->template->header->map_enabled = '3dmap';
         $this->template->content->map_enabled = '3dmap';
         $this->template->content->map_container = 'map3d';
         $this->template->header->main_page = FALSE;
         // Setting to false because we don't want all the external controls that the street map has
         $this->template->header->js = new View('main_3d_js');
         $this->template->header->js->default_zoom = Kohana::config('settings.default_zoom');
         $this->template->header->js->latitude = Kohana::config('settings.default_lat');
         $this->template->header->js->longitude = Kohana::config('settings.default_lon');
         // Override API URL
         $this->template->header->api_url = '<script src="http://www.google.com/jsapi?key=' . Kohana::config('settings.api_google') . '"> </script>';
     }
     $footerjs = new View('footer_form_js');
     // Pack the javascript using the javascriptpacker helper
     $this->template->header->js .= $footerjs;
     $myPacker = new javascriptpacker($this->template->header->js, 'Normal', true, false);
     $this->template->header->js = $myPacker->pack();
 }