From fce3fa57638d2c2fabcfb64d742c733c00be4afb Mon Sep 17 00:00:00 2001
From: Boris Paing
Date: Sat, 12 Dec 2020 15:21:40 +0100
Subject: [PATCH] =?UTF-8?q?Ajout=20=C3=A9venements=20=C3=A0=20proximit?=
=?UTF-8?q?=C3=A9=20depuis=20le=20forum=20;=20thx=20@tuxmain?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
functions.php | 1 -
index.php | 52 ++++++------
lib/Location.class.php | 93 +++++++++++++++++++++
mytown.php | 186 +++++++++++++++++++++++++++++++++--------
tests/geodist.php | 8 ++
5 files changed, 277 insertions(+), 63 deletions(-)
create mode 100644 lib/Location.class.php
create mode 100644 tests/geodist.php
diff --git a/functions.php b/functions.php
index 471cc92..bcf2490 100644
--- a/functions.php
+++ b/functions.php
@@ -32,4 +32,3 @@ function moisFr ($n) {
}
-
diff --git a/index.php b/index.php
index 6c0e0ba..3e0a71c 100644
--- a/index.php
+++ b/index.php
@@ -89,37 +89,39 @@ echo '
';
$content = @file_get_contents(CALENDAR_1_FEED);
-$x = new SimpleXmlElement($content);
-echo '';
+if (!empty($content)) {
-$events = [];
+ $x = new SimpleXmlElement($content);
-foreach($x->item as $entry) {
+ echo '';
- $events[] = $entry;
+ $events = [];
+
+ foreach($x->item as $entry) {
+
+ $events[] = $entry;
+ }
+
+ $events = array_reverse($events);
+
+ foreach ($events as $entry) {
+
+ echo '
+
+ -
+
+
+ '. $entry->title . '
+
+
+
';
+ }
+
+ echo '
';
}
-$events = array_reverse($events);
-
-foreach ($events as $entry) {
-
- echo '
-
- -
-
-
- '. $entry->title . '
-
-
-
';
-}
-
-echo '
';
-
-
-
echo '
diff --git a/lib/Location.class.php b/lib/Location.class.php
new file mode 100644
index 0000000..7b11081
--- /dev/null
+++ b/lib/Location.class.php
@@ -0,0 +1,93 @@
+ array(
+ "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
+ )
+ )
+ );
+
+ $url = sprintf(Location::API_URL, $searchQuery);
+ $json = file_get_contents($url, false, $streamContext);
+ $results = json_decode($json);
+
+ if (isset($results[0])) {
+
+ $firstResult = $results[0];
+
+ $loc = new Location();
+
+ $loc->setPosition($firstResult->lat, $firstResult->lon);
+
+ return $loc;
+
+ } else {
+
+ return false;
+ }
+ }
+
+ public function setPosition ($lat, $lon) {
+
+ $this->lat = $lat;
+ $this->lon = $lon;
+ }
+
+ public function getPosition () {
+
+ return [$this->lat, $this->lon];
+ }
+
+
+ public function getLat () {
+
+ return $this->lat;
+ }
+
+
+ public function getLon () {
+
+ return $this->lon;
+ }
+
+ /*
+ * Retourne la distance (en km) entre les deux points.
+ * $pos1[x, y]
+ * $pos2[x, y]
+ */
+
+ static public function geoDist ($pos1, $pos2) {
+
+ // https://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates
+
+ $a = sin(deg2rad($pos2[0]-$pos1[0])/2)**2 + sin(deg2rad($pos2[1]-$pos1[1])/2)**2 * cos(deg2rad($pos1[0])) * cos(deg2rad($pos2[0]));
+
+ return 12742 * atan2(sqrt($a), sqrt(1-$a));
+ }
+
+}
diff --git a/mytown.php b/mytown.php
index c350101..49f66e8 100644
--- a/mytown.php
+++ b/mytown.php
@@ -3,6 +3,7 @@
require_once('config.php');
require_once('lib/Gchange.class.php');
require_once('lib/CesiumPlus.class.php');
+require_once('lib/Location.class.php');
$gchange = new Gchange();
$cesiumPlus = new CesiumPlus();
@@ -12,65 +13,176 @@ if (isset($_GET['location'])) {
$_SESSION['location'] = $_GET['location'];
- $streamContext = stream_context_create(
- array(
- "http" => array(
- "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
- )
- )
- );
+ if ($userPos = Location::createFromAddress($_GET['location'])) {
+
+ $_SESSION['lat'] = $userPos->getLat();
+ $_SESSION['lon'] = $userPos->getLon();
+ $_SESSION['location'] = htmlspecialchars($_GET['location']);
+ }
+
+} elseif (isset($_SESSION['lat'], $_SESSION['lon'])) {
-
- $url = 'http://nominatim.openstreetmap.org/search?q='. urlencode($_GET['location']) . '&format=json';
-
- $citiesJson = file_get_contents($url, false, $streamContext);
- $cities = json_decode($citiesJson);
-
- $_SESSION['lat'] = $cities[0]->lat;
- $_SESSION['lon'] = $cities[0]->lon;
- $_SESSION['location'] = htmlspecialchars($_GET['location']);
-
+ $userPos = new Location();
+ $userPos->setPosition($_SESSION['lat'], $_SESSION['lon']);
}
-
-
include('header.php');
echo '
Événements à '. EVENT_RADIUS .' km autour de '. $_SESSION['location'] . '
';
$feedUrl = sprintf(CALENDAR_1_LOCAL_FEED, EVENT_RADIUS, $_SESSION['lat'], $_SESSION['lon']);
+
$content = @file_get_contents($feedUrl);
-$x = new SimpleXmlElement($content);
-echo '';
+ echo '';
+}
echo '
- Ces événements sont importés automatiquement depuis
-
- l\'Agenda du libre
-
+ Événements importés automatiquement depuis
+
+ forum.monnaie-libre.fr
+ :
';
+$content = @file_get_contents(CALENDAR_2_FEED);
+$x = json_decode($content);
+echo '';
+$i = 0;
+$limit = 20;
+$todayIso = new DateTime();
+foreach($x->topic_list->topics as $entry) {
+
+ $eventDate = new DateTime($entry->event->start);
+
+ if ($eventDate >= $todayIso) {
+
+ if (isset($entry->location->geo_location->lat, $entry->location->geo_location->lon)) {
+
+ $loc = new Location();
+ $loc->setPosition($entry->location->geo_location->lat, $entry->location->geo_location->lon);
+
+ } else {
+
+ $searchTerms = [];
+
+ if (isset($entry->location->street)) {
+
+ $searchTerms[] = $entry->location->street;
+ }
+
+ if (isset($entry->location->name)) {
+
+ $searchTerms[] = $entry->location->name;
+ }
+
+ if (isset($entry->location->geo_location->address)) {
+
+ $searchTerms[] = $entry->location->geo_location->address;
+ }
+
+ if (isset($entry->location->geo_location->city)) {
+
+ $searchTerms[] = $entry->location->geo_location->city;
+ }
+
+ if (isset($entry->location->geo_location->postalcode)) {
+
+ $searchTerms[] = $entry->location->geo_location->postalcode;
+ }
+
+ if (isset($entry->location->geo_location->state)) {
+
+ $searchTerms[] = $entry->location->geo_location->state;
+ }
+
+ if (isset($entry->location->geo_location->country)) {
+
+ $searchTerms[] = $entry->location->geo_location->country;
+ }
+
+ if (!empty($searchTerms)) {
+
+ $q = implode(' ', $searchTerms);
+
+ $loc = Location::createFromAddress($q);
+ }
+ }
+
+
+ if (isset($loc) and $loc and (($dist = Location::geoDist($loc->getPosition(), $userPos->getPosition())) < EVENT_RADIUS)) {
+
+
+ echo '
+
+ - [à ' . round($dist) . ' km] ';
+
+ if (isset($eventDate)) {
+
+ echo '
+ , le '. $eventDate->format('j ') . moisFr($eventDate->format('n')) . $eventDate->format(' Y');
+ }
+
+ echo ' :
+
+
+
+ '. $entry->title . '
+
+ ';
+
+ if (isset($entry->location->geo_location->city)) {
+
+ echo '
+ , à '. $entry->location->geo_location->city;
+ }
+
+ echo '
+
';
+
+
+ ++$i;
+ if ($i == $limit) {
+
+ break;
+ }
+
+ }
+ }
+
+}
+
+echo '
';
echo 'Contacter d\'autres junistes près de '. $_SESSION['location'] . '
';
diff --git a/tests/geodist.php b/tests/geodist.php
new file mode 100644
index 0000000..1237f75
--- /dev/null
+++ b/tests/geodist.php
@@ -0,0 +1,8 @@
+