Skip to content
Browse files

Backend / OccupySandyBackend accepts more than one remote data source…

…. We determine which we are using by doing some wack sniffing on the URL provided.
  • Loading branch information...
1 parent 7abe935 commit f1d40017eb774e41a29a55c255e464487188a487 @radgeek committed Dec 14, 2012
View
71 extensions/fusion/occupysandybackend.php
@@ -2,22 +2,32 @@
require_once('fusiontable.class.php');
require_once('sahanageojsonp.class.php');
+define('GOOGLE_FUSIONTABLE_API_URL', 'https://www.googleapis.com/fusiontables/v1/query');
+
class OccupySandyBackend {
- var $ft;
+ private $source;
+
function __construct () {
add_action( 'admin_menu', array(&$this, 'admin_menu') );
add_action( 'admin_init', array(&$this, 'admin_init') );
- $apiKey = get_option('os_fusiontables_apikey', null);
- $tableId = get_option('os_fusiontables_tableid', null);
-
- if (!is_null($apiKey) and (strlen($apiKey) > 0)) :
- $this->ft = new FusionTable($apiKey, $tableId);
- endif;
+ switch ($this->data_source_class()) :
+ case 'SahanaGeoJSONP' :
+ $this->source = new SahanaGeoJSONP($this->api_url());
+ break;
+ case 'FusionTable' :
+ $apiKey = get_option('os_fusiontables_apikey', null);
+ $tableId = get_option('os_fusiontables_tableid', null);
+
+ if (!is_null($apiKey) and (strlen($apiKey) > 0)) :
+ $this->source = new FusionTable($apiKey, $tableId);
+ endif;
+ break;
+ endswitch;
}
function admin_menu () {
- add_submenu_page('options-general.php', 'Settings', 'FusionTables Backend', 0, 'os_options_fusiontables_backend', array(&$this, 'options_page'));
+ add_submenu_page('options-general.php', 'Settings', 'Locations Data Backend', 0, 'os_options_fusiontables_backend', array(&$this, 'options_page'));
}
function admin_init () {
@@ -28,13 +38,31 @@ function admin_init () {
include(dirname(__FILE__).'/occupysandytests.php');
endif;
- // Register FusionTables Backend settings
+ // Register Data Source Backend settings
register_setting( 'os-fusiontables-settings', 'os_fusiontables_apikey' );
register_setting( 'os-fusiontables-settings', 'os_fusiontables_tableid' );
register_setting( 'os-fusiontables-settings', 'os_fusiontables_wipe_querycache' );
+ register_setting( 'os-fusiontables-settings', 'os_data_source_url' );
} /* if */
+ public function api_url () {
+ return get_option('os_data_source_url', GOOGLE_FUSIONTABLE_API_URL);
+ }
+ public function data_source_class () {
+ $url = $this->api_url();
+ if (preg_match('|sahanafoundation\.org/.*\.geojsonp$|i', $url)) :
+ $ret = 'SahanaGeoJSONP';
+ elseif (preg_match('|/fusiontables/v1|i', $url)) :
+ $ret = 'FusionTable';
+ else :
+ // Default to FusionTable.
+ $ret = 'FusionTable';
+ endif;
+ return $ret;
+ }
+
function options_page () {
+ $dataSourceURL = $this->api_url();
$apiKey = get_option('os_fusiontables_apikey', null);
$tableId = get_option('os_fusiontables_tableid', null);
$wipeCache = get_option('os_fusiontables_wipe_querycache', null);
@@ -43,25 +71,31 @@ function options_page () {
if (strlen($wipeCache) > 0) :
update_option('os_fusiontables_wipe_querycache', false);
- if ($this->ft->has_cache()) :
- $this->ft->cache()->wipe(null, $wipeCache);
+ if ($this->source->has_cache()) :
+ $this->source->cache()->wipe(null, $wipeCache);
endif;
endif;
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br/></div>
- <h2>FusionTables Backend Settings</h2>
+ <h2>Locations Data Backend Settings</h2>
<form method="post" action="options.php">
<?php settings_fields('os-fusiontables-settings'); ?>
<table class="form-table">
<tbody>
+ <tr style="vertical-align: top"><th>Data Source:</th> <td><input type="url" name="os_data_source_url" value="<?php echo esc_attr($dataSourceURL); ?>" size="127" placeholder="URL" /></td></tr>
+ </tbody>
+ </table>
+ <h3>FusionTables Credentials. (If applicable.)</h3>
+ <table class="form-table">
+ <tbody>
<tr style="vertical-align: top"><th>API Key:</th> <td><input type="text" name="os_fusiontables_apikey" value="<?php echo esc_attr($apiKey); ?>" placeholder="API key" size="127" />
<p style="font-size: smaller; color: #333; max-width: 50em; font-style: italic;">From <a href="https://developers.google.com/fusiontables/docs/v1/using#auth">Fusion Tables documentation:</a> <q>Requests to the Fusion Tables API for public data must be accompanied by an identifier, which can be an API key or an auth token. To acquire an API key, visit the APIs Console. In the Services pane, activate the Fusion Tables API; if the Terms of Service appear, read and accept them. Next, go to the API Access pane. The API key is near the bottom of that pane, in the section titled <q>Simple API Access.</q></q></p></tr>
<tr><th>Default Table:</th> <td><input type="text" name="os_fusiontables_tableid" value="<?php echo esc_attr($tableId); ?>" placeholder="table_name" size="127" /></tr>
<?php
- if ($this->ft->has_cache()) :
+ if ($this->source->has_cache()) :
?>
<tr><th>Wipe Cache:</th> <td><input type="checkbox" name="os_fusiontables_wipe_querycache" value="<?php echo esc_attr($apiKey); ?>" /> Wipe cache</td></tr>
<?php
@@ -72,9 +106,9 @@ function options_page () {
<p class="submit"><input class="button-primary" type="submit" name="Submit" value="<?php print __('Save Changes'); ?>" /></p>
</form>
- <?php if ($this->ft->has_cache()) : ?>
+ <?php if ($this->source->has_cache()) : ?>
<h3>Cache Status</h3>
- <?php $this->ft->cache()->dump(); ?>
+ <?php $this->source->cache()->dump(); ?>
<?php endif; ?>
<h3>Test Output</h3>
@@ -96,7 +130,8 @@ function options_page () {
foreach ($data->rows as $row) :
?>
<tr style="vertical-align: top"><?php
- foreach ($row as $cell) :
+ foreach ($data->columns as $col => $cname) :
+ $cell = (isset($row[$col]) ? $row[$col] : NULL);
?><td><?php ob_start(); var_dump($cell); $html = ob_get_clean(); print esc_html($html); ?></td><?php
endforeach;
endforeach; ?></tr>
@@ -123,13 +158,13 @@ function options_page () {
}
function has_data () {
- return ($this->ft InstanceOf FusionTable);
+ return ($this->source InstanceOf OccupySandyDataSource);
}
function get_data ($params = array()) {
$ret = NULL;
if ($this->has_data()) :
- $ret = $this->ft->data($params);
+ $ret = $this->source->data($params);
endif;
return $ret;
}
View
8 extensions/fusion/occupysandydatasource.class.php
@@ -9,6 +9,14 @@ public function data ($params) {
return NULL;
}
+ public function has_cache () {
+ return false;
+ }
+
+ public function cache () {
+ return NULL;
+ }
+
public function to_table_hash ($data) {
$ret = array();
foreach ($data->rows as $row) :
View
2 extensions/fusion/occupysandyfrontend.php
@@ -82,7 +82,7 @@ function the_occupy_sandy_cards ($params = array()) {
"matches" => null,
"template-class" => null,
));
-
+
$cards = get_occupy_sandy_cards($params);
if (is_wp_error($cards)) :
$cards = array($cards);

0 comments on commit f1d4001

Please sign in to comment.
Something went wrong with that request. Please try again.