Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit f1d40017eb774e41a29a55c255e464487188a487 1 parent 7abe935
Charles Johnson authored
71 extensions/fusion/occupysandybackend.php
@@ -2,22 +2,32 @@
2 2 require_once('fusiontable.class.php');
3 3 require_once('sahanageojsonp.class.php');
4 4
  5 +define('GOOGLE_FUSIONTABLE_API_URL', 'https://www.googleapis.com/fusiontables/v1/query');
  6 +
5 7 class OccupySandyBackend {
6   - var $ft;
  8 + private $source;
  9 +
7 10 function __construct () {
8 11 add_action( 'admin_menu', array(&$this, 'admin_menu') );
9 12 add_action( 'admin_init', array(&$this, 'admin_init') );
10 13
11   - $apiKey = get_option('os_fusiontables_apikey', null);
12   - $tableId = get_option('os_fusiontables_tableid', null);
13   -
14   - if (!is_null($apiKey) and (strlen($apiKey) > 0)) :
15   - $this->ft = new FusionTable($apiKey, $tableId);
16   - endif;
  14 + switch ($this->data_source_class()) :
  15 + case 'SahanaGeoJSONP' :
  16 + $this->source = new SahanaGeoJSONP($this->api_url());
  17 + break;
  18 + case 'FusionTable' :
  19 + $apiKey = get_option('os_fusiontables_apikey', null);
  20 + $tableId = get_option('os_fusiontables_tableid', null);
  21 +
  22 + if (!is_null($apiKey) and (strlen($apiKey) > 0)) :
  23 + $this->source = new FusionTable($apiKey, $tableId);
  24 + endif;
  25 + break;
  26 + endswitch;
17 27 }
18 28
19 29 function admin_menu () {
20   - add_submenu_page('options-general.php', 'Settings', 'FusionTables Backend', 0, 'os_options_fusiontables_backend', array(&$this, 'options_page'));
  30 + add_submenu_page('options-general.php', 'Settings', 'Locations Data Backend', 0, 'os_options_fusiontables_backend', array(&$this, 'options_page'));
21 31 }
22 32
23 33 function admin_init () {
@@ -28,13 +38,31 @@ function admin_init () {
28 38 include(dirname(__FILE__).'/occupysandytests.php');
29 39 endif;
30 40
31   - // Register FusionTables Backend settings
  41 + // Register Data Source Backend settings
32 42 register_setting( 'os-fusiontables-settings', 'os_fusiontables_apikey' );
33 43 register_setting( 'os-fusiontables-settings', 'os_fusiontables_tableid' );
34 44 register_setting( 'os-fusiontables-settings', 'os_fusiontables_wipe_querycache' );
  45 + register_setting( 'os-fusiontables-settings', 'os_data_source_url' );
35 46 } /* if */
36 47
  48 + public function api_url () {
  49 + return get_option('os_data_source_url', GOOGLE_FUSIONTABLE_API_URL);
  50 + }
  51 + public function data_source_class () {
  52 + $url = $this->api_url();
  53 + if (preg_match('|sahanafoundation\.org/.*\.geojsonp$|i', $url)) :
  54 + $ret = 'SahanaGeoJSONP';
  55 + elseif (preg_match('|/fusiontables/v1|i', $url)) :
  56 + $ret = 'FusionTable';
  57 + else :
  58 + // Default to FusionTable.
  59 + $ret = 'FusionTable';
  60 + endif;
  61 + return $ret;
  62 + }
  63 +
37 64 function options_page () {
  65 + $dataSourceURL = $this->api_url();
38 66 $apiKey = get_option('os_fusiontables_apikey', null);
39 67 $tableId = get_option('os_fusiontables_tableid', null);
40 68 $wipeCache = get_option('os_fusiontables_wipe_querycache', null);
@@ -43,25 +71,31 @@ function options_page () {
43 71 if (strlen($wipeCache) > 0) :
44 72 update_option('os_fusiontables_wipe_querycache', false);
45 73
46   - if ($this->ft->has_cache()) :
47   - $this->ft->cache()->wipe(null, $wipeCache);
  74 + if ($this->source->has_cache()) :
  75 + $this->source->cache()->wipe(null, $wipeCache);
48 76 endif;
49 77 endif;
50 78
51 79 ?>
52 80 <div class="wrap">
53 81 <div id="icon-options-general" class="icon32"><br/></div>
54   - <h2>FusionTables Backend Settings</h2>
  82 + <h2>Locations Data Backend Settings</h2>
55 83 <form method="post" action="options.php">
56 84 <?php settings_fields('os-fusiontables-settings'); ?>
57 85 <table class="form-table">
58 86 <tbody>
  87 + <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>
  88 + </tbody>
  89 + </table>
  90 + <h3>FusionTables Credentials. (If applicable.)</h3>
  91 + <table class="form-table">
  92 + <tbody>
59 93 <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" />
60 94 <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>
61 95 <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>
62 96
63 97 <?php
64   - if ($this->ft->has_cache()) :
  98 + if ($this->source->has_cache()) :
65 99 ?>
66 100 <tr><th>Wipe Cache:</th> <td><input type="checkbox" name="os_fusiontables_wipe_querycache" value="<?php echo esc_attr($apiKey); ?>" /> Wipe cache</td></tr>
67 101 <?php
@@ -72,9 +106,9 @@ function options_page () {
72 106 <p class="submit"><input class="button-primary" type="submit" name="Submit" value="<?php print __('Save Changes'); ?>" /></p>
73 107 </form>
74 108
75   - <?php if ($this->ft->has_cache()) : ?>
  109 + <?php if ($this->source->has_cache()) : ?>
76 110 <h3>Cache Status</h3>
77   - <?php $this->ft->cache()->dump(); ?>
  111 + <?php $this->source->cache()->dump(); ?>
78 112 <?php endif; ?>
79 113
80 114 <h3>Test Output</h3>
@@ -96,7 +130,8 @@ function options_page () {
96 130 foreach ($data->rows as $row) :
97 131 ?>
98 132 <tr style="vertical-align: top"><?php
99   - foreach ($row as $cell) :
  133 + foreach ($data->columns as $col => $cname) :
  134 + $cell = (isset($row[$col]) ? $row[$col] : NULL);
100 135 ?><td><?php ob_start(); var_dump($cell); $html = ob_get_clean(); print esc_html($html); ?></td><?php
101 136 endforeach;
102 137 endforeach; ?></tr>
@@ -123,13 +158,13 @@ function options_page () {
123 158 }
124 159
125 160 function has_data () {
126   - return ($this->ft InstanceOf FusionTable);
  161 + return ($this->source InstanceOf OccupySandyDataSource);
127 162 }
128 163
129 164 function get_data ($params = array()) {
130 165 $ret = NULL;
131 166 if ($this->has_data()) :
132   - $ret = $this->ft->data($params);
  167 + $ret = $this->source->data($params);
133 168 endif;
134 169 return $ret;
135 170 }
8 extensions/fusion/occupysandydatasource.class.php
@@ -9,6 +9,14 @@ public function data ($params) {
9 9 return NULL;
10 10 }
11 11
  12 + public function has_cache () {
  13 + return false;
  14 + }
  15 +
  16 + public function cache () {
  17 + return NULL;
  18 + }
  19 +
12 20 public function to_table_hash ($data) {
13 21 $ret = array();
14 22 foreach ($data->rows as $row) :
2  extensions/fusion/occupysandyfrontend.php
@@ -82,7 +82,7 @@ function the_occupy_sandy_cards ($params = array()) {
82 82 "matches" => null,
83 83 "template-class" => null,
84 84 ));
85   -
  85 +
86 86 $cards = get_occupy_sandy_cards($params);
87 87 if (is_wp_error($cards)) :
88 88 $cards = array($cards);

0 comments on commit f1d4001

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