From: Steve Sutton Date: Wed, 20 Feb 2019 21:24:36 +0000 (-0500) Subject: Adding state and enable_weather to cities. X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=8b9d016d8c59a08d13619510530daace009c6b07;p=WP-Plugins%2Fglm-member-db.git Adding state and enable_weather to cities. Also adding the lat lon into the data class and fetching lat lon with nomnatim database. --- diff --git a/classes/data/dataCities.php b/classes/data/dataCities.php index 6ab8057a..53e74d2e 100644 --- a/classes/data/dataCities.php +++ b/classes/data/dataCities.php @@ -137,6 +137,38 @@ class GlmDataCities extends GlmDataAbstract 'use' => 'a', ), + // State + 'state' => array ( + 'field' => 'state', + 'type' => 'list', + 'list' => $this->config['states'], + 'default' => $this->config['settings']['default_state'], + 'use' => 'a' + ), + + // Enable City Weather + 'enable_weather' => array( + 'field' => 'enable_weather', + 'type' => 'checkbox', + 'use' => 'a', + ), + + // Latitude + 'lat' => array( + 'field' => 'lat', + 'type' => 'float', + 'default' => $this->config['settings']['maps_default_lat'], + 'use' => 'a' + ), + + // Longitude + 'lon' => array( + 'field' => 'lon', + 'type' => 'float', + 'default' => $this->config['settings']['maps_default_lon'], + 'use' => 'a' + ), + ); diff --git a/models/admin/settings/cities.php b/models/admin/settings/cities.php index 2ee87c1b..6eb26e8a 100644 --- a/models/admin/settings/cities.php +++ b/models/admin/settings/cities.php @@ -14,8 +14,8 @@ */ // Load Member Types data abstract -require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataCities.php'; -require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataCounties.php'; +require_once GLM_MEMBERS_PLUGIN_CLASS_PATH . '/data/dataCities.php'; +require_once GLM_MEMBERS_PLUGIN_CLASS_PATH . '/data/dataCounties.php'; /* * This class performs the work for the default action of the "Members" menu @@ -49,7 +49,7 @@ class GlmMembersAdmin_settings_cities extends GlmDataCities * @return object Class object * */ - public function __construct ($wpdb, $config) + public function __construct ( $wpdb, $config ) { // Save WordPress Database object @@ -59,7 +59,7 @@ class GlmMembersAdmin_settings_cities extends GlmDataCities $this->config = $config; // Run constructor for members data class - parent::__construct(false, false); + parent::__construct( false, false ); } @@ -97,39 +97,42 @@ class GlmMembersAdmin_settings_cities extends GlmDataCities * produce output. * */ - public function modelAction ($actionData = false) + public function modelAction ( $actionData = false ) { - $success = true; - $haveCities = false; - $cities = false; - $counties = false; - $error = false; + $success = true; + $haveCities = false; + $cities = false; + $counties = false; + $states = false; + $error = false; // Check if a city ID is supplied $id = 0; - if (isset($_REQUEST['id'])) { + if ( isset( $_REQUEST['id'] ) ) { $id = $_REQUEST['id']-0; } // If there's an action option - if (isset($_REQUEST['option'])) { + if ( isset( $_REQUEST['option'] ) ) { - switch($_REQUEST['option']) { + switch( $_REQUEST['option'] ) { case 'addNew': + $this->getLatLonForCity(); $this->insertEntry(); break; case 'update': if ($id > 0) { - $this->updateEntry($id); + $this->getLatLonForCity(); + $this->updateEntry( $id ); } break; case 'delete': if ($id > 0) { - $this->deleteEntry($id, true); + $this->deleteEntry( $id, true ); } break; @@ -146,42 +149,43 @@ class GlmMembersAdmin_settings_cities extends GlmDataCities // If we have list entries - even if it's an empty list $success = true; $haveCities = false; - if ($cities !== false) { + if ( $cities !== false ) { $success = true; // If we have any entries - if (count($cities) > 0) { + if ( count( $cities ) > 0 ) { $haveCities = true; } } // Get a list of all members by city, used in the Settings > Cities list to limit city deletion - require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataMemberInfo.php'; - $MemberInfo = new GlmDataMemberInfo($this->wpdb, $this->config); - foreach ($cities as $city) { - $sql = "SELECT T.member_name, T.id FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."member_info T WHERE T.city = " . $city["id"] . " "; - $membersForCity = $this->wpdb->get_results($sql, ARRAY_A); - foreach ($membersForCity as $memberForCity) { - //echo var_dump(addSlashes($memberForCity["member_name"])); - $citiesWithMembers[$city["id"]][$memberForCity["id"]] = $memberForCity["member_name"]; - } + require_once GLM_MEMBERS_PLUGIN_CLASS_PATH . '/data/dataMemberInfo.php'; + $MemberInfo = new GlmDataMemberInfo( $this->wpdb, $this->config ); + if ( isset( $cities ) && is_array( $cities ) && !empty( $cities ) ) { + foreach ( $cities as $city ) { + $sql = "SELECT T.member_name, T.id FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."member_info T WHERE T.city = " . $city["id"] . " "; + $membersForCity = $this->wpdb->get_results( $sql, ARRAY_A ); + foreach ( $membersForCity as $memberForCity ) { + $citiesWithMembers[$city["id"]][$memberForCity["id"]] = $memberForCity["member_name"]; + } + } } // If we had a fatal error, redirect to the error page if ($error) { return array( - 'status' => $success, - 'menuItemRedirect' => 'error', - 'modelRedirect' => 'index', - 'view' => 'admin/error/index.html', - 'data' => false + 'status' => $success, + 'menuItemRedirect' => 'error', + 'modelRedirect' => 'index', + 'view' => 'admin/error/index.html', + 'data' => false ); } // $citiesWithMembersJSON = htmlspecialchars(json_encode($citiesWithMembers), ENT_QUOTES, 'UTF-8'); - $citiesWithMembersJSON = json_encode($citiesWithMembers); - $citiesWithMembersJSON = str_replace("'", "\'", $citiesWithMembersJSON); + $citiesWithMembersJSON = json_encode( $citiesWithMembers ); + $citiesWithMembersJSON = str_replace( "'", "\'", $citiesWithMembersJSON ); // Compile template data @@ -189,6 +193,7 @@ class GlmMembersAdmin_settings_cities extends GlmDataCities 'haveCities' => $haveCities, 'cities' => $cities, 'counties' => $counties, + 'states' => $this->config['states'], 'citiesWithMembers' => $citiesWithMembers, 'citiesWithMembersJSON' => $citiesWithMembersJSON @@ -196,7 +201,7 @@ class GlmMembersAdmin_settings_cities extends GlmDataCities // Return status, suggested view, and data to controller return array( - 'status' => $success, + 'status' => $success, 'menuItemRedirect' => false, 'modelRedirect' => false, 'view' => 'admin/settings/cities.html', @@ -205,7 +210,23 @@ class GlmMembersAdmin_settings_cities extends GlmDataCities } + public function getLatLonForCity() + { + if ( isset( $_REQUEST['name'] ) ) { + $city = filter_var( $_REQUEST['name'] ); + $state = filter_var( $_REQUEST['state'] ); + + if ( $city && $state ) { + $addressString = $city . '+' . $state; + + $url = "https://nominatim.openstreetmap.org/search/?format=json&addressdetails=1&q={$addressString}&limit=1"; + $return = json_decode( wp_remote_retrieve_body( wp_remote_get( $url ) ) ); + if ( $return[0]->lat && $return[0]->lon ) { + $_REQUEST['lat'] = $return[0]->lat; + $_REQUEST['lon'] = $return[0]->lon; + } + } + } + } } - -?> diff --git a/models/front/members/cityWeather.php b/models/front/members/cityWeather.php index b67cd160..f7634e58 100644 --- a/models/front/members/cityWeather.php +++ b/models/front/members/cityWeather.php @@ -136,15 +136,18 @@ class GlmMembersFront_members_cityWeather extends GlmDataCities $alpha = filter_var( $_REQUEST['alpha'], FILTER_SANITIZE_STRING ); } - $alphaList = $this->getAlphaList(' AND T.lat IS NOT NULL AND lon IS NOT NULL ', $alpha); + $whereCityWeatherSql = ' AND enable_weather AND T.lat IS NOT NULL AND lon IS NOT NULL '; + + $alphaList = $this->getAlphaList( $whereCityWeatherSql, $alpha ); $alphaSelected = strtoupper( $alpha ); - $where = " T.name LIKE '$alphaSelected%'"; + $where = " T.name LIKE '$alphaSelected%'"; + $where .= $whereCityWeatherSql; $cities = $this->getList( $where, 'name' ); - if ( $cities ) { + if ( isset( $cities) && is_array( $cities ) && !empty( $cities ) ) { foreach ( $cities as &$city ) { $cWeather = $this->checkWeather( $city['id'] ); if ( $cWeather ) { diff --git a/views/admin/settings/cities.html b/views/admin/settings/cities.html index adf10802..a5faaaf1 100644 --- a/views/admin/settings/cities.html +++ b/views/admin/settings/cities.html @@ -1,253 +1,322 @@ {include file='admin/settings/header.html'} - -
Add a City
-
-
- - - {if !$settings.enable_counties} - - {/if} - + +
Add a City
+
+ + + + {if !$settings.enable_counties} + + {/if} +
+ + + + + + + + + {if $settings.enable_counties} - + - {if $settings.enable_counties} - - - - - {/if} -
City Name: + +
State: + +
City Name:{$terms.term_county_cap}: - +
{$terms.term_county_cap}: - -
-

* Required

- Cancel - -
-
- - -
-
-

Are you sure you want to delete this city?

-

Yes, delete this city

-

Cancel

-
-
- - -
-
-

This city is in use by several members and may not be deleted to make sure these are not left without a city.

-

Please set a different city for the following members, then refresh this page, and try again.

-
- Cancel -
-
- - -
-
- - - - {if !$settings.enable_counties} - {/if} - + + + + +
City Weather: + +
+

* Required

+ Cancel + +
+
+ + +
+
+

Are you sure you want to delete this city?

+

Yes, delete this city

+

Cancel

+
+
+ + +
+
+

This city is in use by several members and may not be deleted to make sure these are not left without a city.

+

Please set a different city for the following members, then refresh this page, and try again.

+
+ Cancel +
+
+ + +
+
+ + + + {if !$settings.enable_counties} + + {/if} + + + + + + + + + + {if $settings.enable_counties} - + - {if $settings.enable_counties} - - - - - {/if} -
City Name: + +
State: + +
City Name:{$terms.term_county_cap}: - +
{$terms.term_county_cap}: - -
-

* Required

- Cancel - -
-
- - -
-
-
- Cancel -
-
- -

Cities

- - - + {/if} - - + + - - -{if $haveCities} - - - {assign var="i" value="0"} - {foreach $cities as $t} - {if $i++ is odd by 1} - {else} - - {/if} + + +
City City Weather: + +
City Location: - {$t.name} + Lat:
+ Lon:
+

* Required

+ Cancel + + + - - {$citiesWithMembers = $citiesWithMembers} - {if $t.id|array_key_exists:$citiesWithMembers} - Assigned to {$citiesWithMembers[$t.id]|count} members -
view
-
Delete
- {else} - Not assigned to any members -
Delete
- {/if} + +
+
+
+ Cancel +
+
- - - {/foreach} -{else} - (no cities listed) -{/if} - - +

Cities

- + $('#showMembersWithCityCancel').click( function() { + $("#showMembersWithCityDialog").dialog("close"); + }); + + }); + {include file='admin/footer.html'}