Permalink
Browse files

Fixes #259 - "Error on Install - missing sharing table"

Signed-off-by: Linda Kamau <kamaulynder@gmail.com>
  • Loading branch information...
1 parent a33cf23 commit ef308a48a02770e627cd64f77165d88e66a30547 @kamaulynder kamaulynder committed Feb 19, 2012
@@ -0,0 +1,130 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+/**
+ * Sharing Scheduler Controller
+ *
+ * PHP version 5
+ * LICENSE: This source file is subject to LGPL license
+ * that is available through the world-wide-web at the following URI:
+ * http://www.gnu.org/copyleft/lesser.html
+ * @author Ushahidi Team <team@ushahidi.com>
+ * @package Ushahidi - http://source.ushahididev.com
+ * @subpackage Scheduler
+ * @copyright Ushahidi - http://www.ushahidi.com
+ * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
+*/
+
+class S_Sharing_Controller extends Controller {
+
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ public function index()
+ {
+ // Get all currently active shares
+ $shares = ORM::factory('sharing')
+ ->where('sharing_active', 1)
+ ->find_all();
+
+ foreach ($shares as $share)
+ {
+ $sharing_url = "http://".$share->sharing_url;
+
+ $this->_parse_json($share->id, $sharing_url);
+ }
+ }
+
+ /**
+ * Use remote Ushahidi deployments API to get Incident Data
+ * Limit to 20 not to kill remote server
+ */
+ private function _parse_json($sharing_id = NULL, $sharing_url = NULL)
+ {
+ if ( ! $sharing_id OR ! $sharing_url)
+ {
+ return false;
+ }
+
+ $timeout = 5;
+ $limit = 20;
+ $since_id = 0;
+ $modified_ids = array(); // this is an array of our primary keys
+ $more_reports_to_pull = TRUE;
+
+ while($more_reports_to_pull == TRUE)
+ {
+ $api_url = "/api?task=incidents&limit=".$limit."&resp=json&orderfield=incidentid&sort=0&by=sinceid&id=".$since_id;
+
+ $ch = curl_init();
+ curl_setopt($ch,CURLOPT_URL,$sharing_url.$api_url);
+ curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
+ curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
+ curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
+ $json = curl_exec($ch);
+ curl_close($ch);
+
+ $all_data = json_decode($json, false);
+ if ( ! $all_data)
+ {
+ return false;
+ }
+
+ if ( ! isset($all_data->payload->incidents))
+ {
+ return false;
+ }
+
+ // Parse Incidents Into Database
+
+ $count = 0;
+ foreach($all_data->payload->incidents as $incident)
+ {
+ // See if this incident already exists so we can edit it
+
+ $item_check = ORM::factory('sharing_incident')
+ ->where('sharing_id', $sharing_id)
+ ->where('incident_id',$incident->incident->incidentid)
+ ->find();
+
+ if ($item_check->loaded==TRUE)
+ {
+ $item = ORM::factory('sharing_incident',$item_check->id);
+ }else{
+ $item = ORM::factory('sharing_incident');
+ }
+ $item->sharing_id = $sharing_id;
+ $item->incident_id = $incident->incident->incidentid;
+ $item->incident_title = $incident->incident->incidenttitle;
+ $item->latitude = $incident->incident->locationlatitude;
+ $item->longitude = $incident->incident->locationlongitude;
+ $item->incident_date = $incident->incident->incidentdate;
+ $item->save();
+
+ // Save the primary key of the row we touched. We will be deleting ones that weren't touched.
+
+ $modified_ids[] = $item->id;
+
+ // Save the highest pulled incident id so we can grab the next set from that id on
+
+ $since_id = $incident->incident->incidentid;
+
+ // Save count so we know if we need to pull any more reports or not
+
+ $count++;
+ }
+
+ if($count < $limit)
+ {
+ $more_reports_to_pull = FALSE;
+ }
+ }
+
+ // Delete the reports that are no longer being displayed on the shared site
+
+ ORM::factory('sharing_incident')
+ ->notin('id',$modified_ids)
+ ->where('sharing_id', $sharing_id)
+ ->delete_all();
+ }
+}
@@ -36,12 +36,35 @@ public function add()
{
Event::add('ushahidi_action.nav_admin_manage', array($this,'_sharing'));
}
+ elseif (strripos(Router::$current_uri, "main") !== false)
+ {
+ Event::add('ushahidi_action.main_sidebar', array($this, '_display'));
+ }
}
public function _sharing()
{
$this_sub_page = Event::$data;
echo ($this_sub_page == "sharing") ? "Sharing" : "<a href=\"".url::site()."admin/manage/sharing\">Sharing</a>";
}
+
+ public function _display()
+ {
+ // Get all active Shares
+ $shares = array();
+ foreach (ORM::factory('sharing')
+ ->where('sharing_active', 1)
+ ->find_all() as $share)
+ {
+ $shares[$share->id] = array($share->sharing_name, $share->sharing_color);
+ }
+
+ $sharing_bar = View::factory('sharing/sharing_bar');
+
+ $sharing_bar->shares = $shares;
+ $sharing_bar->render(TRUE);
+ $this->template->js = new View('js/sharing_bar_js');
+
+ }
}
new sharing;
@@ -58,6 +58,19 @@ public function run_install()
PRIMARY KEY (`id`)
);");
+ //Dump the sharing scheduler item from bundled SQL dump file
+ $this->db->query("DELETE FROM `".Kohana::config('database.default.table_prefix')."scheduler` where scheduler_name = 'Sharing' ");
+ $db_insert = fopen (dirname(dirname(__FILE__)).'/sql/s_sharing.sql', 'r');
+ $rows = fread ($db_insert, filesize(dirname(dirname(__FILE__)).'/sql/s_sharing.sql'));
+
+ //split by ; to get the sql statement for inserting each row
+ $rows = explode(';\n',$rows);
+
+ foreach($rows as $query)
+ {
+ $this->db->query($query);
+ }
+
}
View
@@ -963,8 +963,7 @@ INSERT INTO `scheduler` (`id`, `scheduler_name`, `scheduler_last`, `scheduler_we
(2, 'Alerts', 0, -1, -1, -1, -1, 's_alerts', 1),
(3, 'Email', 0, -1, -1, -1, 0, 's_email', 1),
(4, 'Twitter', 0, -1, -1, -1, 0, 's_twitter', 1),
-(5, 'Sharing', 0, -1, -1, -1, 0, 's_sharing', 1),
-(6, 'Cleanup', 0, -1, -1, -1, 0, 's_cleanup', 1);
+(5, 'Cleanup', 0, -1, -1, -1, 0, 's_cleanup', 1);
/**
@@ -91,30 +91,6 @@
}
?>
- <?php
- if ($shares)
- {
- ?>
- <!-- Layers (Other Ushahidi Layers) -->
- <div class="cat-filters clearingfix" style="margin-top:20px;">
- <strong><?php echo Kohana::lang('ui_main.other_ushahidi_instances');?> <span>[<a href="javascript:toggleLayer('sharing_switch_link', 'sharing_switch')" id="sharing_switch_link"><?php echo Kohana::lang('ui_main.hide'); ?></a>]</span></strong>
- </div>
- <ul id="sharing_switch" class="category-filters">
- <?php
- foreach ($shares as $share => $share_info)
- {
- $sharing_name = $share_info[0];
- $sharing_color = $share_info[1];
- echo '<li><a href="#" id="share_'. $share .'"><div class="swatch" style="background-color:#'.$sharing_color.'"></div>
- <div>'.$sharing_name.'</div></a></li>';
- }
- ?>
- </ul>
- <!-- /Layers -->
- <?php
- }
- ?>
-
<br />
<!-- additional content -->

0 comments on commit ef308a4

Please sign in to comment.