From 710d27281c467810fd17dc3637e0ee98396f971a Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Tue, 7 Jun 2022 22:45:00 +0000 Subject: [PATCH] query for last checkin and offer as an option in place of geolocation --- controllers/controllers.php | 25 ++++++++++++---- lib/helpers.php | 27 +++++++++++++++++ schema/migrations/0003.sql | 2 ++ schema/schema.sql | 1 + views/new-post.php | 58 ++++++++++++++++++++++++++++++++----- 5 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 schema/migrations/0003.sql diff --git a/controllers/controllers.php b/controllers/controllers.php index cb0852f..828754b 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -188,9 +188,14 @@ $app->post('/post', function() use($app) { $entry->user_id = $user->id; $location = false; - if(k($params, 'location') && $location=parse_geo_uri($params['location'])) { - $entry->latitude = $location['latitude']; - $entry->longitude = $location['longitude']; + + if(k($params, 'venue_url')) { + $entry->checkin_url = $params['venue_url']; + } else { + if(k($params, 'location') && $location=parse_geo_uri($params['location'])) { + $entry->latitude = $location['latitude']; + $entry->longitude = $location['longitude']; + } } if(k($params,'note_date')) { @@ -253,8 +258,10 @@ $app->post('/post', function() use($app) { 'created' => [$published], 'summary' => [$text_content] ); - $location = k($params, 'location'); - if($location) { + + if($venue = k($params, 'venue_url')) { + $mp_properties['location'] = [$venue]; + } elseif($location = k($params, 'location')) { $mp_properties['location'] = [$location]; } if($entry->photo_url) { @@ -340,6 +347,14 @@ $app->get('/micropub/config', function() use($app) { } }); +$app->get('/micropub/last-checkin', function() use($app){ + if($user=require_login($app)) { + $checkin = get_micropub_checkin($user); + $app->response()['Content-type'] = 'application/json'; + $app->response()->body(json_encode($checkin)); + } +}); + $app->get('/options.json', function() use($app) { if($user=require_login($app)) { $params = $app->request()->params(); diff --git a/lib/helpers.php b/lib/helpers.php index c1a79f9..758e3bd 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -195,6 +195,33 @@ function get_micropub_config(&$user) { ); } +function get_micropub_checkin(&$user) { + $r = micropub_get($user->micropub_endpoint, ['q' => 'source', 'limit' => 1, 'post-type' => 'checkin'], $user->access_token); + + $url = null; + $name = null; + $lat = null; + $lng = null; + + if($r['data'] && is_array($r['data'])) { + if(isset($r['data']['items'][0]['properties']['checkin'][0]) && + isset($r['data']['items'][0]['properties']['checkin'][0]['properties']['name'][0])) { + $checkin = $r['data']['items'][0]['properties']['checkin'][0]; + $url = $r['data']['items'][0]['properties']['url'][0]; + $name = $checkin['properties']['name'][0]; + $lat = $checkin['properties']['latitude'][0]; + $lng = $checkin['properties']['longitude'][0]; + } + } + + return [ + 'url' => $url, + 'name' => $name, + 'lat' => $lat, + 'lng' => $lng, + ]; +} + function build_static_map_url($latitude, $longitude, $height, $width, $zoom) { return '/map.png?marker[]=lat:' . $latitude . ';lng:' . $longitude . ';icon:small-green-cutout&width=' . $width . '&height=' . $height . '&zoom=' . $zoom; } diff --git a/schema/migrations/0003.sql b/schema/migrations/0003.sql new file mode 100644 index 0000000..b1d4eaf --- /dev/null +++ b/schema/migrations/0003.sql @@ -0,0 +1,2 @@ +ALTER TABLE entries +ADD COLUMN `checkin_url` VARCHAR(512) NOT NULL DEFAULT '' AFTER `longitude`; diff --git a/schema/schema.sql b/schema/schema.sql index 622bf55..b79b5c0 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -29,6 +29,7 @@ CREATE TABLE `entries` ( `tz_offset` int(11) DEFAULT NULL, `latitude` double DEFAULT NULL, `longitude` double DEFAULT NULL, + `checkin_url` varchar(512) NOT NULL DEFAULT '', `type` enum('eat','drink') DEFAULT NULL, `content` text, `canonical_url` varchar(255) DEFAULT NULL, diff --git a/views/new-post.php b/views/new-post.php index 8eb0aa3..5a2468b 100644 --- a/views/new-post.php +++ b/views/new-post.php @@ -33,17 +33,26 @@
- +

Location

- - - + - @@ -296,6 +305,41 @@ $(function(){ } }); + var last_checkin = false; + + function fetch_last_checkin() { + $.get("/micropub/last-checkin", function(data){ + if(data.url) { + $("#note_venue").removeClass("hidden"); + $("#note_venue .name").val(data.name); + last_checkin = data; + bind_venue_buttons(); + } + }); + } + + function bind_venue_buttons() { + $("#note_venue .entry-buttons li input").click(function(){ + if($(this).hasClass("btn-primary")) { + // Disable venue tag + $(this).addClass("btn-default").removeClass("btn-primary"); + $("#note_venue_url").val(""); + fetch_location(); + } else { + // Enable venue tag + $(this).addClass("btn-primary").removeClass("btn-default"); + $("#note_venue_url").val(last_checkin.url); + $("#note_location_msg").val(last_checkin.url); + $("#note_location_img_small").attr("src", ""); + $("#note_location_img_wide").attr("src", ""); + $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', last_checkin.lat).replace('{lng}', last_checkin.lng)); + $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', last_checkin.lat).replace('{lng}', last_checkin.lng)); + } + }); + } + + fetch_last_checkin(); + });