351 lines
8.9 KiB
PHP
351 lines
8.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* This file is part of Zwii.
|
|
*
|
|
* For full copyright and license information, please see the LICENSE
|
|
* file that was distributed with this source code.
|
|
*
|
|
* @author Rémi Jean <remi.jean@outlook.com>
|
|
* @copyright Copyright (C) 2008-2018, Rémi Jean
|
|
* @author Frédéric Tempez <frederic.tempez@outlook.com>
|
|
* @copyright Copyright (C) 2018-2024, Frédéric Tempez
|
|
* @license CC Attribution-NonCommercial-NoDerivatives 4.0 International
|
|
* @link http://zwiicms.fr/
|
|
*/
|
|
|
|
class geolocation extends common
|
|
{
|
|
|
|
|
|
const VERSION = '0.1';
|
|
const REALNAME = 'Géo Evénements';
|
|
const DATADIRECTORY = self::DATA_DIR . 'geolocation/';
|
|
|
|
const SORT_ASC = 'SORT_ASC';
|
|
const SORT_DSC = 'SORT_DSC';
|
|
const SORT_HAND = 'SORT_HAND';
|
|
|
|
|
|
public static $locations = [];
|
|
public static $locationsId = [];
|
|
public static $locationsCenter = [];
|
|
|
|
public static $actions = [
|
|
'config' => self::GROUP_EDITOR,
|
|
'delete' => self::GROUP_EDITOR,
|
|
'dirs' => self::GROUP_EDITOR,
|
|
'add' => self::GROUP_EDITOR,
|
|
'edit' => self::GROUP_EDITOR,
|
|
'index' => self::GROUP_VISITOR
|
|
];
|
|
|
|
|
|
/**
|
|
* Mise à jour du module
|
|
* Appelée par les fonctions index et config
|
|
*/
|
|
private function update()
|
|
{
|
|
|
|
//$versionData = $this->getData(['module', $this->getUrl(0), 'config', 'versionData']);
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Configuration
|
|
*/
|
|
public function config()
|
|
{
|
|
|
|
// Soumission du formulaire
|
|
if (
|
|
$this->getUser('permission', __CLASS__, __FUNCTION__) === true
|
|
) {
|
|
foreach ($this->getData(['module', $this->getUrl(0), 'content']) as $locationId => $locationData) {
|
|
self::$locations[] = [
|
|
$locationData['name'],
|
|
$locationData['lat'],
|
|
$locationData['long'],
|
|
template::button('locationConfigEdit' . $locationId, [
|
|
'href' => helper::baseUrl() . $this->getUrl(0) . '/edit/' . $locationId,
|
|
'value' => template::ico('pencil'),
|
|
'help' => 'Configuration'
|
|
]),
|
|
template::button('galleryConfigDelete' . $locationId, [
|
|
'class' => 'galleryConfigDelete buttonRed',
|
|
'href' => helper::baseUrl() . $this->getUrl(0) . '/delete/' . $locationId,
|
|
'value' => template::ico('trash'),
|
|
'help' => 'Supprimer'
|
|
])
|
|
|
|
];
|
|
}
|
|
}
|
|
|
|
// Valeurs en sortie
|
|
$this->addOutput([
|
|
'showBarEditButton' => true,
|
|
'title' => helper::translate('Configuration'),
|
|
'view' => 'config'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Ajout d'une localisation
|
|
*/
|
|
public function add()
|
|
{
|
|
// Soumission du formulaire d'ajout d'une galerie
|
|
if (
|
|
$this->getUser('permission', __CLASS__, __FUNCTION__) === true &&
|
|
$this->isPost()
|
|
) {
|
|
if ($this->getInput('locationAddName', null, true)) {
|
|
$locationId = helper::increment($this->getInput('locationAddName', helper::FILTER_ID, true), (array) $this->getData(['module', $this->getUrl(0), 'content']));
|
|
// Le dossier de la galerie est vide
|
|
|
|
$this->setData([
|
|
'module',
|
|
$this->getUrl(0),
|
|
'content',
|
|
$locationId,
|
|
[
|
|
'name' => $this->getInput('locationAddName'),
|
|
'description' => $this->getInput('locationAddDescription', helper::FILTER_STRING_SHORT, true),
|
|
'lat' => $this->getInput('locationAddLat', helper::FILTER_FLOAT, true),
|
|
'long' => $this->getInput('locationAddLong', helper::FILTER_FLOAT, true)
|
|
]
|
|
]);
|
|
}
|
|
|
|
// Valeurs en sortie
|
|
$this->addOutput([
|
|
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
|
|
'notification' => helper::translate('Localisation créée'),
|
|
'state' => true
|
|
]);
|
|
|
|
|
|
} else {
|
|
// Valeurs en sortie
|
|
$this->addOutput([
|
|
'title' => helper::translate('Nouvelle localisaton'),
|
|
'view' => 'add',
|
|
'vendor' => [
|
|
'tinymce'
|
|
],
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ajout d'une localisation
|
|
*/
|
|
public function edit()
|
|
{
|
|
// Soumission du formulaire d'ajout d'une galerie
|
|
if (
|
|
$this->getUser('permission', __CLASS__, __FUNCTION__) === true &&
|
|
$this->isPost()
|
|
) {
|
|
$this->setData([
|
|
'module',
|
|
$this->getUrl(0),
|
|
'content',
|
|
$this->getUrl(2),
|
|
[
|
|
'name' => $this->getInput('locationEditName'),
|
|
'description' => $this->getInput('locationEditDescription', helper::FILTER_STRING_LONG, true),
|
|
'lat' => $this->getInput('locationEditLat', helper::FILTER_FLOAT, true),
|
|
'long' => $this->getInput('locationEditLong', helper::FILTER_FLOAT, true)
|
|
]
|
|
]);
|
|
|
|
// Valeurs en sortie
|
|
$this->addOutput([
|
|
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
|
|
'notification' => helper::translate('Modifications enregistrées'),
|
|
'state' => true
|
|
]);
|
|
|
|
|
|
} else {
|
|
// Valeurs en sortie
|
|
$this->addOutput([
|
|
'title' => helper::translate('Edition'),
|
|
'view' => 'edit',
|
|
'vendor' => [
|
|
'tinymce'
|
|
],
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Suppression
|
|
*/
|
|
public function delete()
|
|
{
|
|
// La galerie n'existe pas
|
|
if (
|
|
$this->getUser('permission', __CLASS__, __FUNCTION__) !== true ||
|
|
$this->getData(['module', $this->getUrl(0), 'content', $this->getUrl(2)]) === null
|
|
) {
|
|
// Valeurs en sortie
|
|
$this->addOutput([
|
|
'access' => false
|
|
]);
|
|
}
|
|
// Suppression
|
|
else {
|
|
$this->deleteData(['module', $this->getUrl(0), 'content', $this->getUrl(2)]);
|
|
// Valeurs en sortie
|
|
$this->addOutput([
|
|
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
|
|
'notification' => helper::translate('Evenement effacé'),
|
|
'state' => true
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Accueil (deux affichages en un pour éviter une url à rallonge)
|
|
*/
|
|
public function index()
|
|
{
|
|
|
|
// Mise à jour des données de module
|
|
$this->update();
|
|
|
|
$locations = $this->getData(['module', $this->getUrl(0), 'content']);
|
|
|
|
// Affichage du template si les données sont disponibles sinon redirection vers la configuration
|
|
if (is_null($locations)) {
|
|
// Valeurs en sortie
|
|
$this->addOutput([
|
|
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/add',
|
|
'notification' => helper::translate('Paramétres'),
|
|
'state' => true
|
|
]);
|
|
} else {
|
|
// Lecture des données
|
|
foreach ($locations as $locationsId => $datas) {
|
|
self::$locations[] = $datas;
|
|
}
|
|
|
|
// Calcul du point central
|
|
// Calculer le centre géographique
|
|
$totalLat = 0;
|
|
$totalLong = 0;
|
|
$count = count(self::$locations);
|
|
|
|
foreach (self::$locations as $coordinate) {
|
|
$totalLat += $coordinate["lat"];
|
|
$totalLong += $coordinate["long"];
|
|
}
|
|
|
|
$centerLat = $totalLat / $count;
|
|
$centerLong = $totalLong / $count;
|
|
|
|
// Calculer la distance maximale au centre pour déterminer le niveau de zoom
|
|
$maxDistance = 0;
|
|
foreach (self::$locations as $coordinate) {
|
|
if (
|
|
is_numeric($centerLat)
|
|
&& is_numeric($centerLong)
|
|
&& $coordinate["lat"]
|
|
&& $coordinate["long"]
|
|
) {
|
|
$distance = $this->haversineGreatCircleDistance($centerLat, $centerLong, $coordinate["lat"], $coordinate["long"]);
|
|
if ($distance > $maxDistance) {
|
|
$maxDistance = $distance;
|
|
}
|
|
}
|
|
|
|
$zoomLevel = $this->getZoomLevel($maxDistance);
|
|
|
|
self::$locationsCenter = array(
|
|
'lat' => $centerLat,
|
|
'long' => $centerLong,
|
|
'zoom' => $zoomLevel
|
|
);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
// Valeurs en sortie
|
|
$this->addOutput([
|
|
'showBarEditButton' => true,
|
|
'view' => 'index',
|
|
'vendor' => [
|
|
'leaflet'
|
|
],
|
|
]);
|
|
}
|
|
|
|
|
|
// Fonction pour convertir les coordonnées GPS au format décimal
|
|
private function gps_decimal($coordinate, $hemisphere)
|
|
{
|
|
// Extrait les degrés, minutes et secondes
|
|
$degrees = count($coordinate) > 0 ? $this->gps2Num($coordinate[0]) : 0;
|
|
$minutes = count($coordinate) > 1 ? $this->gps2Num($coordinate[1]) : 0;
|
|
$seconds = count($coordinate) > 2 ? $this->gps2Num($coordinate[2]) : 0;
|
|
|
|
// Convertit les degrés, minutes et secondes en décimal
|
|
$decimal = $degrees + ($minutes / 60) + ($seconds / 3600);
|
|
|
|
// Si l'hémisphère est au Sud ou à l'Ouest, les coordonnées sont négatives
|
|
$decimal *= ($hemisphere == 'S' || $hemisphere == 'W') ? -1 : 1;
|
|
|
|
return $decimal;
|
|
}
|
|
|
|
// Fonction pour convertir les coordonnées GPS en nombre
|
|
private function gps2Num($coordPart)
|
|
{
|
|
$parts = explode('/', $coordPart);
|
|
if (count($parts) <= 0)
|
|
return 0;
|
|
if (count($parts) == 1)
|
|
return $parts[0];
|
|
return floatval($parts[0]) / floatval($parts[1]);
|
|
}
|
|
|
|
// Fonction pour calculer la distance entre deux points géographiques
|
|
private function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371)
|
|
{
|
|
$latFrom = deg2rad($latitudeFrom);
|
|
$lonFrom = deg2rad($longitudeFrom);
|
|
$latTo = deg2rad($latitudeTo);
|
|
$lonTo = deg2rad($longitudeTo);
|
|
|
|
$latDelta = $latTo - $latFrom;
|
|
$lonDelta = $lonTo - $lonFrom;
|
|
|
|
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
|
|
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
|
|
return $angle * $earthRadius;
|
|
}
|
|
|
|
// Déterminer le niveau de zoom
|
|
// Cette fonction est une approximation pour le calcul du zoom
|
|
private function getZoomLevel($maxDistance)
|
|
{
|
|
$maxZoom = 21; // Le zoom maximal pour Leaflet
|
|
$earthCircumference = 40075; // La circonférence de la Terre en km
|
|
|
|
for ($zoom = $maxZoom; $zoom >= 0; $zoom--) {
|
|
if ($maxDistance < ($earthCircumference / pow(2, $zoom))) {
|
|
return $zoom;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
}
|