Compare commits

...

3 commits

Author SHA1 Message Date
n1rwana
5a7c99be74 Better geo points 2023-08-04 02:10:28 +03:00
n1rwana
92690767a1 Update WallPresenter.php 2023-08-04 01:07:36 +03:00
veselcraft
22ab724ab4
VKAPI: Geo support (not tested idk) 2023-08-04 00:59:06 +03:00
5 changed files with 88 additions and 11 deletions

View file

@ -111,6 +111,18 @@ final class Wall extends VKAPIRequestHandler
];
}
$geo = [];
if($post->getGeo()) {
$geoarray = $post->getGeo();
$geo = [
"coordinates" => $geoarray->lat . " " . $geoarray->lng,
"showmap" => 1,
"type" => "point"
];
}
$items[] = (object)[
"id" => $post->getVirtualId(),
"from_id" => $from_id,
@ -127,6 +139,7 @@ final class Wall extends VKAPIRequestHandler
"is_pinned" => $post->isPinned(),
"is_explicit" => $post->isExplicit(),
"attachments" => $attachments,
"geo" => $geo,
"post_source" => $post_source,
"comments" => (object)[
"count" => $post->getCommentsCount(),
@ -379,7 +392,7 @@ final class Wall extends VKAPIRequestHandler
];
}
function post(string $owner_id, string $message = "", int $from_group = 0, int $signed = 0, string $attachments = ""): object
function post(string $owner_id, string $message = "", int $from_group = 0, int $signed = 0, string $attachments = "", ?float $latitude = NULL, ?float $longitude = NULL, ?string $geo_name = NULL): object
{
$this->requireUser();
$this->willExecuteWriteAction();
@ -420,6 +433,41 @@ final class Wall extends VKAPIRequestHandler
if(empty($message) && empty($attachments))
$this->fail(100, "Required parameter 'message' missing.");
$geo = NULL;
if ($latitude && $longitude) {
$geo = array(
"name" => null,
"lat" => $latitude,
"lng" => $longitude,
);
if ($latitude > 90 || $latitude < -90 || $longitude > 180 || $longitude < -180) {
$this->fail(100, "Invalid latitude or longitude");
}
if (strlen(trim($geo_name))) {
$geo["name"] = $geo_name;
} else {
$info = file_get_contents("https://nominatim.openstreetmap.org/reverse?lat=${latitude}&lon=${longitude}&format=jsonv2", false, stream_context_create([
'http' => [
'method' => 'GET',
'header' => implode("\r\n", [
'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2',
"Referer: https://$_SERVER[SERVER_NAME]/"
])
]
]));
if ($info) {
$info = json_decode($info, true, JSON_UNESCAPED_UNICODE);
if (key_exists("place_id", $info)) {
$geo["name"] = $info["name"] ?? $info["display_name"];
}
}
}
}
try {
$post = new Post;
$post->setOwner($this->getUser()->getId());
@ -428,6 +476,11 @@ final class Wall extends VKAPIRequestHandler
$post->setContent($message);
$post->setFlags($flags);
$post->setApi_Source_Name($this->getPlatform());
if ($geo) {
$post->setGeo(json_encode($geo));
$post->setGeo_Lat($geo["lat"]);
$post->setGeo_Lon($geo["lng"]);
}
$post->save();
} catch(\LogicException $ex) {
$this->fail(100, "One of the parameters specified was missing or invalid");

View file

@ -300,6 +300,12 @@ final class WallPresenter extends OpenVKPresenter
if (!$geo["lat"] || !$geo["lng"] || !$geo["name"]) {
$this->flashFail("err", tr("error"), tr("error_geolocation"));
}
$latitude = (float) $geo["lat"];
$longitude = (float) $geo["lng"];
if ($latitude > 90 || $latitude < -90 || $longitude > 180 || $longitude < -180) {
$this->flashFail("err", tr("error"), "Invalid latitude or longitude");
}
}
if(empty($this->postParam("text")) && !$photo && !$video && !$poll && !$note && !$geo)
@ -316,8 +322,8 @@ final class WallPresenter extends OpenVKPresenter
$post->setNsfw($this->postParam("nsfw") === "on");
if ($geo) {
$post->setGeo(json_encode($geo));
$post->setGeo_Lat($geo["lat"]);
$post->setGeo_Lon($geo["lng"]);
$post->setGeo_Lat($latitude);
$post->setGeo_Lon($longitude);
}
$post->save();
} catch (\LengthException $ex) {

View file

@ -76,7 +76,7 @@
<div n:if="$post->getGeo()" class="post-signature">
<a onclick="javascript:openGeo({$post->getGeo()}, {$post->getOwner()->getId()}, {$post->getVirtualId()})">
<img src="/assets/packages/static/openvk/img/geo.svg">
<b>{_geotag}</b>: {$post->getGeo()->name}
<b>{_geotag}</b>: {$post->getGeo()->name ?? tr("admin_open")}
</a>
</div>
<div n:if="$post->isAd()" style="color:grey;">

View file

@ -69,7 +69,9 @@
</div>
<div n:if="$post->getGeo()" style="padding: 4px;">
<div style="border-bottom: #ECECEC solid 1px;" />
<div style="cursor: pointer; padding: 4px;" onclick="javascript:openGeo({$post->getGeo()}, {$post->getOwner()->getId()}, {$post->getVirtualId()})"><b>{_geotag}</b>: {$post->getGeo()->name}</div>
<div style="cursor: pointer; padding: 4px;" onclick="javascript:openGeo({$post->getGeo()}, {$post->getOwner()->getId()}, {$post->getVirtualId()})">
<b>{_geotag}</b>: <a>{$post->getGeo()->name ?? tr("admin_open")}</a>
</div>
</div>
<div n:if="$post->isAd()" style="color:grey;">
<br/>

View file

@ -353,8 +353,9 @@ function openGeo(data, owner_id, virtual_id) {
let map = L.map(element, {attributionControl: false});
let target = L.latLng(data.lat, data.lng);
map.setView(target, 15);
let marker = L.marker(target).addTo(map);
marker.bindPopup(data.name).openPopup();
marker.bindPopup(data.name ?? tr("geotag")).openPopup();
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
@ -404,7 +405,7 @@ function getPostPopup(post) {
</div>
<div style="padding: 4px;">
<div style="border-bottom: #ECECEC solid 1px;"></div>
<div style="cursor: pointer; padding: 4px;"><b>${tr("geotag")}</b>: ${post.geo.name}</div>
<div style="cursor: pointer; padding: 4px;"><b>${tr("geotag")}</b>: ${post.geo.name ?? tr("admin_open")}</div>
</div>
</div>
</td>
@ -430,12 +431,27 @@ function openNearPosts(posts) {
markerLayers.addTo(map);
let markersBounds = [];
let coords = [];
posts.posts.forEach((post) => {
let marker = L.marker(L.latLng(post.geo.lat, post.geo.lng)).addTo(map);
marker.bindPopup(getPostPopup(post));
markerLayers.addLayer(marker);
markersBounds.push(marker.getLatLng());
if (coords.includes(`${post.geo.lat} ${post.geo.lng}`)) {
markerLayers.getLayers().forEach((marker) => {
if (marker.getLatLng().lat === post.geo.lat && marker.getLatLng().lng === post.geo.lng) {
let content = marker.getPopup()._content += getPostPopup(post);
if (!content.startsWith(`<div style="max-height: 300px; overflow-y: auto;">`))
content = `<div style="max-height: 300px; overflow-y: auto;">${content}`;
marker.getPopup().setContent(content);
}
});
} else {
let marker = L.marker(L.latLng(post.geo.lat, post.geo.lng)).addTo(map);
marker.bindPopup(getPostPopup(post));
markerLayers.addLayer(marker);
markersBounds.push(marker.getLatLng());
}
coords.push(`${post.geo.lat} ${post.geo.lng}`);
})
let bounds = L.latLngBounds(markersBounds);