Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Maps and OpenStreetMaps geocoding #300

Merged
merged 2 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions oc-admin/themes/modern/items/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ function customPageTitle($string)
</div>
</div>
</div>
<div class="form-row">
<div class="form-label"> <?php _e('Maps'); ?></div>
<div class="separate-top-medium">
<div class="form-controls">
<?php $map_type = osc_item_map_type(); ?>
<select class="input-small" name="map_type">
<option value="0" <?php if(!$map_type) echo 'selected'; ?>><?php _e('None'); ?></option>
<option value="google" <?php if($map_type == 'google') echo 'selected'; ?>><?php _e('Google Maps'); ?></option>
<option value="openstreet" <?php if($map_type == 'openstreet') echo 'selected'; ?>><?php _e('OpenStreetMaps'); ?></option>
</select>
<div class="help-box"><?php _e('Set the API key in Settings -> General.'); ?></div>
</div>
</div>
</div>
<div class="form-actions">
<input type="submit" id="save_changes" value="<?php echo osc_esc_html(__('Save changes')); ?>"
class="btn btn-submit"/>
Expand Down
32 changes: 26 additions & 6 deletions oc-admin/themes/modern/settings/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ function render_offset()
function addHelp()
{
echo '<p>'
. __("Change the basic configuration of your Osclass. From here, you can modify variables such as the site’s name,
the default currency or how lists of listings are displayed. <strong>Be careful</strong> when modifying default values
. __("Change the basic configuration of your Osclass. From here, you can modify variables such as the site’s name,
the default currency or how lists of listings are displayed. <strong>Be careful</strong> when modifying default values
if you're not sure what you're doing!")
. '</p>';
}
Expand Down Expand Up @@ -372,7 +372,7 @@ class="input-medium" <?php echo($custom_checked ? 'value="'
<label>
<input type="checkbox" <?php echo(osc_selectable_parent_categories()
? 'checked="checked"' : ''); ?> name="selectable_parent_categories" value="1"/>
<?php _e('Allow users to select a parent category as a category
<?php _e('Allow users to select a parent category as a category
when inserting or editing a listing '); ?>
</label>
</div>
Expand Down Expand Up @@ -413,16 +413,36 @@ class="input-medium" <?php echo($custom_checked ? 'value="'
</div>
<h2 class="render-title"><?php _e('Google Analytics'); ?></h2>
<div class="form-row">
<div class="form-label"><?php _e('Google Tracking ID'); ?></div>
<div class="form-label"><?php _e('Tracking ID'); ?></div>
<div class="form-controls">
<input type="text" class="xlarge" name="ga_tracking_id"
value="<?php echo osc_esc_html(osc_google_analytics_id()); ?>"/>
<div class="help-box">
<?php _e('Add your google analytics tracking id. Example: UA-12345678-0'); ?>
<?php _e('Add your Google Analytics tracking ID. Example: UA-12345678-0'); ?>
</div>
</div>
</div>
<h2 class="render-title"><?php _e('Maps'); ?></h2>
<div class="form-row">
<div class="form-label"><?php _e('Google Maps key'); ?></div>
<div class="form-controls">
<input type="text" class="xlarge" name="googlemaps_api_key"
value="<?php echo osc_esc_html(osc_google_maps_api_key()); ?>"/>
<div class="help-box">
<?php _e('Add your Google Maps JavaScript API key.'); ?>
</div>
</div>
</div>
<div class="form-row">
<div class="form-label"><?php _e('OpenStreetMaps key'); ?></div>
<div class="form-controls">
<input type="text" class="xlarge" name="openstreet_api_key"
value="<?php echo osc_esc_html(osc_openstreet_api_key()); ?>"/>
<div class="help-box">
<?php _e('Add your Mapquest Consumer key.'); ?>
</div>
</div>
</div>


<?php
/**
Expand Down
35 changes: 35 additions & 0 deletions oc-includes/osclass/classes/actions/ItemActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ public function add()
'd_coord_long' => $aItem['d_coord_long'],
's_zip' => $aItem['s_zip']
);
$location = array_merge($location, $this->getItemCoordinates($location));

$locationManager = ItemLocation::newInstance();
$locationManager->insert($location);
Expand Down Expand Up @@ -754,6 +755,7 @@ public function edit()
'd_coord_long' => $aItem['d_coord_long'],
's_zip' => $aItem['s_zip']
);
$location = array_merge($location, $this->getItemCoordinates($location));

$locationManager = ItemLocation::newInstance();
$old_item_location = $locationManager->findByPrimaryKey($aItem['idItem']);
Expand Down Expand Up @@ -1754,6 +1756,39 @@ public function prepareData($is_add)
$aItem = osc_apply_filter('item_prepare_data', $aItem);
$this->data = $aItem;
}

/**
* Return item location array with geocoded coords if maps are enabled and coords data isn't already filled.
*
* @param array $location
*
* @return array
*/
private function getItemCoordinates($location) {
if($location['d_coord_lat'] != '' && $location['d_coord_long'] != '') return array();
$mapType = osc_item_map_type();
if(!$mapType) return array();

$address = sprintf('%s, %s, %s, %s', $location['s_address'], $location['s_city'], $location['s_region'], $location['s_country']);

if($mapType == 'google') {
$res = json_decode(osc_file_get_contents(osc_google_maps_geocode_url($address)));
if(isset($res->results[0]->geometry->location) && count($res->results[0]->geometry->location)) {
$coords = $res->results[0]->geometry->location;
$location['d_coord_lat'] = $coords->lat;
$location['d_coord_long'] = $coords->lng;
}
} else if($mapType == 'openstreet') {
$res = json_decode(osc_file_get_contents(osc_openstreet_geocode_url($address)));
if(isset($res->results[0]->locations[0]->latLng) && count($res->results[0]->locations[0]->latLng)) {
$coords = $res->results[0]->locations[0]->latLng;
$location['d_coord_lat'] = $coords->lat;
$location['d_coord_long'] = $coords->lng;
}
}

return $location;
}
}

if (osc_force_jpeg()) {
Expand Down
5 changes: 3 additions & 2 deletions oc-includes/osclass/classes/controller/admin/CAdminItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ public function doModel()
$moderatePost = Params::getParam('moderate_admin_post');
$moderateEdit = Params::getParam('moderate_admin_edit');
$tinymce = Params::getParam('tinymce');

$mapType = Params::getParam('map_type');

$msg = '';
if (!osc_validate_int(Params::getParam('items_wait_time'))) {
Expand Down Expand Up @@ -808,7 +808,8 @@ public function doModel()
$iUpdated += osc_set_preference('moderate_admin_post', $moderatePost);
$iUpdated += osc_set_preference('moderate_admin_edit', $moderateEdit);
$iUpdated += osc_set_preference('tinymce_frontend', $tinymce);

$iUpdated += osc_set_preference('map_type', $mapType);

if ($iUpdated > 0) {
osc_add_flash_ok_message(_m("Listings' settings have been updated"), 'admin');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function doModel()
$selectableParent = Params::getParam('selectable_parent_categories');
$bAutoCron = Params::getParam('auto_cron');
//$sAutoUpdate = implode('|', Params::getParam('auto_update'));
$gaTrackingId = Params::getParam('ga_tracking_id');
$gMapsKey = Params::getParam('googlemaps_api_key');
$osMapsKey = Params::getParam('openstreet_api_key');
// preparing parameters
$sPageTitle = trim(strip_tags($sPageTitle));
$sPageDesc = trim(strip_tags($sPageDesc));
Expand Down Expand Up @@ -134,7 +137,9 @@ public function doModel()
$iUpdated += osc_set_preference('contact_attachment', $contactAttachment);
$iUpdated += osc_set_preference('auto_cron', $bAutoCron);
$iUpdated += osc_set_preference('selectable_parent_categories', $selectableParent);
$iUpdated += osc_set_preference('ga_tracking_id', Params::getParam('ga_tracking_id'));
$iUpdated += osc_set_preference('ga_tracking_id', $gaTrackingId);
$iUpdated += osc_set_preference('googlemaps_api_key', $gMapsKey);
$iUpdated += osc_set_preference('openstreet_api_key', $osMapsKey);

if ($iUpdated > 0) {
if ($error) {
Expand Down
10 changes: 10 additions & 0 deletions oc-includes/osclass/helpers/hItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -1784,3 +1784,13 @@ function osc_query_item($params = null)
}
View::newInstance()->_exportVariableToView('customItems', $mSearch->doSearch());
}

/**
* Get selected map type.
*
* @return string
*/
function osc_item_map_type()
{
return osc_get_preference('map_type');
}
43 changes: 43 additions & 0 deletions oc-includes/osclass/helpers/hUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,49 @@ function osc_google_analytics_id()
return osc_get_preference('ga_tracking_id');
}

/**
* Get Google Maps API key.
*
* @return string
*/
function osc_google_maps_api_key()
{
return osc_get_preference('googlemaps_api_key');
}

/**
* Get Open Street Maps API key.
*
* @return string
*/
function osc_openstreet_api_key()
{
return osc_get_preference('openstreet_api_key');
}

/**
* Get Google Maps geocode URL.
*
* @return string
*/
function osc_google_maps_geocode_url($address)
{
return 'https://maps.googleapis.com/maps/api/geocode/json?address='
. urlencode($address) . '&sensor=false&key='.osc_google_maps_api_key();
}

/**
* Get OpenStreetMaps geocode URL.
*
* @return string
*/
function osc_openstreet_geocode_url($address)
{
return 'https://www.mapquestapi.com/geocoding/v1/address?location='
. urlencode($address) . '&key='.osc_openstreet_api_key();
}


/**
* Get URL of location files JSON.
*
Expand Down