Ajout évenements à proximité depuis le forum ; thx @tuxmain

This commit is contained in:
Boris Paing 2020-12-12 15:21:40 +01:00
parent 76e2ec9d40
commit fce3fa5763
5 changed files with 277 additions and 63 deletions

View File

@ -32,4 +32,3 @@ function moisFr ($n) {
}

View File

@ -89,37 +89,39 @@ echo '
</p>';
$content = @file_get_contents(CALENDAR_1_FEED);
$x = new SimpleXmlElement($content);
echo '<ul>';
if (!empty($content)) {
$events = [];
$x = new SimpleXmlElement($content);
foreach($x->item as $entry) {
echo '<ul>';
$events[] = $entry;
$events = [];
foreach($x->item as $entry) {
$events[] = $entry;
}
$events = array_reverse($events);
foreach ($events as $entry) {
echo '
<li>
<a href="'. $entry->link .'"
title="'. substr(strip_tags($entry->description), 0, 60) . '">
'. $entry->title . '
</a>
</li>';
}
echo '</ul>';
}
$events = array_reverse($events);
foreach ($events as $entry) {
echo '
<li>
<a href="'. $entry->link .'"
title="'. substr(strip_tags($entry->description), 0, 60) . '">
'. $entry->title . '
</a>
</li>';
}
echo '</ul>';
echo '
<p>

93
lib/Location.class.php Normal file
View File

@ -0,0 +1,93 @@
<?php
class Location {
const API_URL = 'http://nominatim.openstreetmap.org/search?q=%s&format=json';
private $lat;
private $lon;
private $name;
private $city;
private $postCode;
public function __construct () {
}
public function createFromAddress ($searchQuery) {
$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"
)
)
);
$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));
}
}

View File

@ -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 '<h2>Événements à '. EVENT_RADIUS .' km autour de '. $_SESSION['location'] . '</h2>';
$feedUrl = sprintf(CALENDAR_1_LOCAL_FEED, EVENT_RADIUS, $_SESSION['lat'], $_SESSION['lon']);
$content = @file_get_contents($feedUrl);
$x = new SimpleXmlElement($content);
echo '<ul>';
if (!empty($content)) {
foreach($x->item as $entry) {
$x = new SimpleXmlElement($content);
echo '
<li>
<a href="'. $entry->link .'"
title="'. substr(strip_tags($entry->description), 0, 60) . '">
'. $entry->title . '
</a>
</li>';
}
<p>
Événements importés automatiquement depuis
<a href="'. htmlspecialchars(CALENDAR_1_URL) .'">
l\'Agenda du libre
</a>&nbsp;:
</p>';
echo '</ul>';
echo '<ul>';
foreach($x->item as $entry) {
echo '
<li>
<a href="'. $entry->link .'"
title="'. substr(strip_tags($entry->description), 0, 60) . '">
'. $entry->title . '
</a>
</li>';
}
echo '</ul>';
}
echo '
<p>
Ces événements sont importés automatiquement depuis
<a href="'. CALENDAR_1_URL .'">
l\'Agenda du libre
</a>
Événements importés automatiquement depuis
<a href="'. CALENDAR_2_URL .'">
forum.monnaie-libre.fr
</a>&nbsp;:
</p>';
$content = @file_get_contents(CALENDAR_2_FEED);
$x = json_decode($content);
echo '<ul>';
$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 '
<li>[à ' . round($dist) . '&nbsp;km] ';
if (isset($eventDate)) {
echo '
, le '. $eventDate->format('j ') . moisFr($eventDate->format('n')) . $eventDate->format(' Y');
}
echo '&nbsp;:
<a href="'. sprintf(CALENDAR_2_EVENT_URL, $entry->id) .'"
title="'. substr(strip_tags($entry->excerpt), 0, 60) . '">
'. $entry->title . '
</a>';
if (isset($entry->location->geo_location->city)) {
echo '
, à '. $entry->location->geo_location->city;
}
echo '
</li>';
++$i;
if ($i == $limit) {
break;
}
}
}
}
echo '</ul>';
echo '<h2>Contacter d\'autres junistes près de '. $_SESSION['location'] . '</h2>';

8
tests/geodist.php Normal file
View File

@ -0,0 +1,8 @@
<?php
require_once('../lib/Location.class.php');
$point1 = [47.62862971426153, -2.6431060558159647];
$point2 = [48.305325, -0.6185254];
echo Location::geoDist($point1, $point2);