<?php
namespace App\Controller\Fonctions;
class Fonctions
{
// renvoie le nom de domaine du site pour le partage des lien
function FonctionUrl()
{
$url = 'localhost:8000';
return $url;
}
function FonctionLogo()
{
$logo = '1';
return $logo;
}
// fonction comparer les id
function FonctionSecurite($userOpe, $iduser)
{
$idteste = 0;
$villeteste = '';
$logement = '';
$superficie = '';
$usersByRole = $userOpe->findByuser($iduser);
foreach ($usersByRole as $user) {
$idteste = $user->getUser();
$logement = $user->getLogement();
$superficie = $user->getSuperficie();
$villeteste = $user->getVille();
$somme = $user->getSomme();
$transaction = $user->getTransaction();
}
return array('idteste' => $idteste, 'villeteste' => $villeteste, 'logement' => $logement, 'superficie' => $superficie, 'somme' => $somme, 'transaction' => $transaction);
}
// fonction pour crer la date
function FonctionDate()
{
$today = date("Y-m-d");
$format = 'Y-m-d';
// Création de la date de l'operation
$date = \DateTime::createFromFormat($format, $today);
$annee = $date->format('Y');
$mois = $date->format('m');
$jour = $date->format('d');
$moisenr = '' . $annee . '' . $mois . '' . $jour . '';
return $moisenr;
}
// fonction pour crer un token
function random($var)
{
$string = "";
$chaine = "a0b1c2d3e4f5g6h7i8j9klmnpqrstuvwxy123456789";
srand((float)microtime() * 1000000);
for ($i = 0; $i < $var; $i++) {
$string .= $chaine[rand() % strlen($chaine)];
}
return $string;
}
function CalculVPM($mensualite, $pourcent_annuel, $prix)
{
$t_mensuel = $pourcent_annuel / 12;
$t_mensuel = $t_mensuel / 100;
$R = (1 - pow((1 + $t_mensuel), -$mensualite)) / $t_mensuel;
$VPM = (($prix) / $R);
return $VPM;
}
function NomLocalite($nom)
{
$nom = ucwords($nom);
$message = '( prise de vue: ' . $nom . ') ';
return $message;
}
// fonction pour recupoere l'adresse IP.
function NomIp()
{
$ip = $_SERVER['REMOTE_ADDR'];
if ($ip == '127.0.0.1') {
$ip = '84.100.177.102';
}
return $ip;
}
// fonction pour trouver la ville de l'utulisateur
function FonctionOujesuis($ip)
{
// recherche ip et adresse
// https://ipinfo.io/
// https://qastack.fr/programming/409999/getting-the-location-from-an-ip-address
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}?token=8e662a92ab261a"));
$ville = $details->city; // -> "Mountain View"
$postal = $details->postal;
$country = $details->country;
return array('ville' => $ville, 'postal' => $postal, 'country' => $country);
}
// fonction pour importer une image
function FonctionImage($lieux, $rechimage)
{
$nbr = 0;
$re = $lieux;
$tagliste = array();
$largeImageURL = '0';
$webformatURL = '0';
$tag = '';
$people_json = file_get_contents("https://pixabay.com/api/?key=26307686-649627882781f3a30d2540fa1&q={$re}+france&lang=fr&image_type=photo");
$decoded_json = json_decode($people_json, true);
$customers = $decoded_json['hits'];
$premiereimage = '';
$premieretag = '';
foreach ($customers as $customer) {
$nbr++;
// recuperation de la premiere image
$premiereimage = $customer['webformatURL'];
// recuperation du premier tag.
$premieretag = $customer['tags'];
$pieces = explode(",", $premieretag);
$tagliste = $pieces;
$premieretag = $pieces[0];
// condition pour prendre la primiere image
if ($nbr == $rechimage) {
$chaine = $customer['tags'];
$chaine = str_replace(", ", ",", $chaine);
$pieces = explode(",", $chaine);
$tagliste = $pieces;
$tag = $pieces[0];
$largeImageURL = $customer['largeImageURL'];
$webformatURL = $customer['webformatURL'];
}
}
return array('largeImageURL' => $largeImageURL, 'webformatURL' => $webformatURL, 'tag' => $tag, 'tagliste' => $tagliste, 'premiereimage' => $premiereimage, 'premieretag' => $premieretag);
}
// fonction pour importer une image
function FonctionImageVille($lieux, $rechimage, $departement)
{
if ($departement != 0) {
$image = '+ville';
} else {
$image = '';
}
$image = '+ville';
$rest0 = substr($lieux, 0, 1); // retourne "le premier caractaire"
$rest1 = strtolower($rest0);
$re = str_replace($rest0, $rest1, $lieux);
$imageliste1 = 0;
$imageliste2 = 0;
$tagliste = array();
$largeImageURL = '0';
$webformatURL = '0';
$tag = '';
$nbr = 0;
$people_json = file_get_contents("https://pixabay.com/api/?key=26307686-649627882781f3a30d2540fa1&q={$re}{$image}&lang=fr&image_type=photo");
$decoded_json = json_decode($people_json, true);
$customers = $decoded_json['hits'];
$premiereimage = 0;
$premieretag = '';
foreach ($customers as $customer) {
$chaine = $customer['tags'];
$chaine = str_replace(", ", ",", $chaine);
$pieces = explode(",", $chaine);
$tagliste = $pieces;
$tag = $pieces[0];
$imageliste1 = $customer['largeImageURL'];
$imageliste2 = $customer['webformatURL'];
// condition pour prendre la primiere image
if ($tag == $re) {
$nbr++;
$largeImageURL = $customer['largeImageURL'];
$webformatURL = $customer['webformatURL'];
if ($nbr == 1) {
$premiereimage = $webformatURL;
}
}
}
return array('largeImageURL' => $largeImageURL, 'webformatURL' => $webformatURL, 'tag' => $tag, 'tagliste' => $tagliste, 'premiereimage' => $premiereimage, 'premieretag' => $premieretag, 'imageliste1' => $imageliste1, 'imageliste2' => $imageliste2);
}
// fonction pour importer une image
function FonctionDepartement($departement)
{
$array = array("1" => " Ain", "2" => " Aisne", "3" => " Allier", "4" => " Alpes-de-Haute-Provence", "6" => " Alpes-Maritimes", "7" => " Ardèche", "8" => " Ardennes", "9" => " Ariège", "10" => " Aube", "11" => " Aude", "12" => " Aveyron", "67" => " Bas-Rhin", "13" => " Bouches-du-Rhône", "14" => " Calvados", "15" => " Cantal", "16" => " Charente", "17" => " Charente-Maritime", "18" => " Cher", "19" => " Corrèze", "2A" => " Corse-du-Sud", "21" => " Côte-d'Or", "22" => " Côtes-d'Armor", "23" => " Creuse", "79" => " Deux-Sèvres", "24" => " Dordogne", "25" => " Doubs", "26" => " Drôme", "91" => " Essonne", "27" => " Eure", "28" => " Eure-et-Loir", "29" => " Finistère", "30" => " Gard", "32" => " Gers", "33" => " Gironde", "971" => " Guadeloupe", "973" => " Guyane", "68" => " Haut-Rhin", "2B" => " Haute-Corse", "31" => " Haute-Garonne", "43" => " Haute-Loire", "52" => " Haute-Marne", "70" => " Haute-Saône", "74" => " Haute-Savoie", "87" => " Haute-Vienne", "05" => " Hautes-Alpes", "65" => " Hautes-Pyrénées", "92" => " Hauts-de-Seine", "34" => " Hérault", "35" => " Ille-et-Vilaine", "36" => " Indre", "37" => " Indre-et-Loire", "38" => " Isère", "39" => " Jura", "974" => " La Réunion", "40" => " Landes", "41" => " Loir-et-Cher", "42" => " Loire", "44" => " Loire-Atlantique", "45" => " Loiret", "46" => " Lot", "47" => " Lot-et-Garonne", "48" => " Lozère", "49" => " Maine-et-Loire", "50" => " Manche", "51" => " Marne", "972" => " Martinique", "53" => " Mayenne", "976" => " Mayotte", "54" => " Meurthe-et-Moselle", "55" => " Meuse", "56" => " Morbihan", "57" => " Moselle", "58" => " Nièvre", "59" => " Nord", "60" => " Oise", "61" => " Orne", "75" => " Paris", "62" => " Pas-de-Calais", "63" => " Puy-de-Dôme", "64" => " Pyrénées-Atlantiques", "66" => " Pyrénées-Orientales", "69" => " Rhône", "71" => " Saône-et-Loire", "72" => " Sarthe", "73" => " Savoie", "77" => " Seine-et-Marne", "76" => " Seine-Maritime", "93" => " Seine-Saint-Denis", "80" => " Somme", "81" => " Tarn", "82" => " Tarn-et-Garonne", "Belfort" => " Territoire", "95" => " Val-d'Oise", "94" => " Val-de-Marne", "83" => " Var", "84" => " Vaucluse", "85" => " Vendée", "86" => " Vienne", "88" => " Vosges", "89" => " Yonne", "78 " => " Yvelines",);
$departement = $departement * 1;
$departement = $array[$departement];
return $departement;
}
// fonction pour importer une image
function FonctionRegion($numdepartement)
{
$array = array("1" => "RhôneAlpes", "2" => "Picardie", "3" => "Auvergne", "4" => "Provence-Alpes-Côted'Azur", "5" => "Provence-Alpes-Côted'Azur", "6" => "Provence-Alpes-Côted'Azur", "7" => "RhôneAlpes", "8" => "Ardenne", "9" => "Midi-Pyrénées", "10" => "Champagne-Ardenne", "11" => "LanguedocRoussillon", "12" => "Midi-Pyrénées", "13" => "Provence-Alpes-Côted'Azur", "14" => "Normandie", "15" => "Auvergne", "16" => "Poitou-Charentes", "17" => "Poitou-Charentes", " 18" => "Centre-ValdeLoire", "19" => "Limousin", " 2A" => "Corse", " 2B" => "Corse", "21" => "Bourgogne", "22" => "Bretagne", "23" => "Limousin", "24" => "Aquitaine", "25" => "Franche-Comté", "26" => "Rhône-Alpes", "27" => "Normandie", "28" => "Centre-ValdeLoire", "29" => "Bretagne", "30" => "LanguedocRoussillon", "31" => "Midi-Pyrénées", "32" => "Midi-Pyrénées", "33" => "Aquitaine", "34" => "LanguedocRoussillon", "35" => "Bretagne", "36" => "Centre-ValdeLoire", "37" => "Centre-ValdeLoire", "38" => "Rhône-Alpes", "39" => "Franche-Comté", "40" => "Aquitaine", "41" => "Centre-ValdeLoire", "42" => "Rhône-Alpes", "43" => "Auvergne", "44" => "PaysdelaLoire", "45" => "Centre-ValdeLoire", "46" => "Midi-Pyrénées", "47" => "Aquitaine", "48" => "LanguedocRoussillon", "49" => "PaysdelaLoire", "50" => "Manche", "51" => "Champagne-Ardenne", "52" => "Champagne-Ardenne", "53" => "PaysdelaLoire", "54" => "Lorraine", "55" => "Lorraine", "56" => "Bretagne", "57" => "Lorraine", "58" => "Bourgogne", "59" => "Nord-Pas-de-Calais", "60" => "Picardie", "61" => "Normandie", "62" => "Nord-Pas-de-Calais", "63" => "Auvergne", "64" => "Midi-Pyrénées", "65" => "Occitanie", "66" => "LanguedocRoussillon", "67" => "Alsace", "68" => "Alsace", "69" => "Rhône-Alpes", "70" => "Franche-Comté", "71" => "Bourgogne", "72" => "PaysdelaLoire", "73" => "Rhône-Alpes", "74" => "Rhône-Alpes", "75" => "Île-de-France", "76" => "Normandie", "77" => "Île-de-France", "78" => "Île-de-France", "79" => "Poitou-Charentes", "80" => "Picardie", "81" => "Midi-Pyrénées", "82" => "Midi-Pyrénées", "83" => "Provence-Alpes-Côted'Azur", "84" => "Provence-Alpes-Côted'Azur", "85" => "PaysdelaLoire", "86" => "Poitou-Charentes", "87" => "Limousin", "88" => "Vosges", "89" => "Bourgogne", "90" => "Franche-Comté", "91" => "Île-de-France", "92" => "Île-de-France", "93" => "Île-de-France", "94" => "Île-de-France", "95" => "Île-de-France", "971" => "Guadeloupe", "972" => "Martinique", "973" => "Guyane", "974" => "LaRéunion", "976" => "Mayotte ",);
$numdepartement = $numdepartement * 1;
$region = $array[$numdepartement];
return $region;
}
// fonction pour calculer la distance gps
function Distance($lat1, $lng1, $lat2, $lng2, $miles = false)
{
$pi80 = M_PI / 180;
$lat1 *= $pi80;
$lng1 *= $pi80;
$lat2 *= $pi80;
$lng2 *= $pi80;
$r = 6372.797; // rayon moyen de la Terre en km
$dlat = $lat2 - $lat1;
$dlng = $lng2 - $lng1;
$a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin(
$dlng / 2
) * sin($dlng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$km = $r * $c;
return ($miles ? ($km * 0.621371192) : $km);
}
// fonction générer le tableau des adresses
function Overpass($lat, $lon, $around, $typen)
{
$tabligne = array();
$highway = '';
if ($typen == 1) {
$typereq = '"amenity"="school"';
$nomfr = 'Education';
$type = '1';
$typenbr = '1';
}
if ($typen == 2) {
$typereq = '"amenity"="doctors"';
$nomfr = 'Santé';
$type = '2';
$typenbr = '2';
}
if ($typen == 3) {
$typereq = 'shop=supermarket';
$nomfr = 'Supermarché';
$type = '3';
$typenbr = '3';
}
if ($typen == 4) {
$typereq = 'bus';
$highway = 'bus';
$nomfr = 'Transports';
$type = 'bus';
$typenbr = '4';
}
if ($typen == 5) {
$typereq = 'tram';
$highway = 'tram';
$nomfr = 'Transports';
$type = '5';
$typenbr = '5';
}
if ($typen == 6) {
$typereq = 'subway';
$highway = 'métro';
$nomfr = 'Transports';
$type = 'métro';
$typenbr = '6';
}
$lien = "https://www.overpass-api.de/api/interpreter?data=[out:json];node[{$typereq}](around:{$around},{$lat},{$lon});out%20center;";
$fields_string = file_get_contents($lien);
$data = json_decode($fields_string, true); // return array not object
// var_dump($data);
$trolley = 0;
$nbr = 0;
foreach ($data['elements'] as $customer) {
$nbr++;
if (isset($customer['tags']['name']) == 1) {
$name = $customer['tags']['name'];
} else {
$name = $nomfr;
}
if (isset($customer['tags']['brand']) == 1) {
$brand = $customer['tags']['brand'];
} else {
$brand = 0;
}
if (isset($customer['tags']['school:FR']) == 1) {
$typesenseing = $customer['tags']['school:FR'];
} else {
$typesenseing = 0;
}
if (isset($customer['tags']['highway']) == 'bus_stop') {
$highway = 'bus';
}
if (isset($customer['tags']['railway']) == 'tram_stop') {
$highway = 'tram';
}
if (isset($customer['tags']['trolleybus']) == 'yes') {
$trolley = 'trolleybus';
}
$tabligne[$nbr] = array('lat' => $customer['lat'], 'lon' => $customer['lon'], 'name' => $name, 'nomfr' => $nomfr, 'type' => $type, 'typenbr' => $typenbr, 'nbr' => $nbr, 'brand' => $brand, 'typesensei' => $typesenseing, 'highway' => $highway, 'amenity' => '', 'trolley' => $trolley);
}
$array = $tabligne;
return $array;
}
// fonction générer le tableau des adresses
function Openstreetmap($ville, $type, $typen)
{
$limit = 100;
$locSearch = "https://nominatim.openstreetmap.org/?addressdetails=1&q={$type}+in+{$ville}&format=json&limit={$limit}";
$options = array(
"http" => array(
"header" => "User-Agent: StevesCleverAddressScript 3.7.6\r\n"
)
);
$context = stream_context_create($options);
$fields_string = file_get_contents($locSearch, false, $context);
$data = json_decode($fields_string, true); // return array not object
$nbr = 0;
foreach ($data as $customer) {
$nbr++;
$highway = '';
$amenity = '';
$name = '';
if (isset($customer['address']['highway']) == 1) {
$highway = $customer['address']['highway'];
}
if (isset($customer['address']['display_name']) == 1) {
$name = $customer['address']['display_name'];
}
if (isset($customer['address']['shop']) == 1) {
$name = $customer['address']['shop'];
}
if ($type == 'school') {
if (isset($customer['address']['amenity']) == 1) {
$amenity = $customer['address']['amenity'];
} else {
$amenity = 0;
}
}
$tabligne[$nbr] = array('lat' => $customer["lat"], 'lon' => $customer["lon"], 'name' => $name, 'highway' => $highway, 'nomfr' => $typen, 'type' => $type, 'typenbr' => $typen, 'nbr' => $nbr, 'brand' => $typen, 'highway' => $highway, 'typesensei' => $typen, 'highway' => $highway, 'amenity' => $amenity, 'trolley' => '');
}
$array = $tabligne;
return $array;
}
// fonction générer le tableau des adresses
function Transport($lat, $lon, $type, $around)
{
$type1 = $type;
if ($type1 == 'subway') {
$type1 = 'métro';
}
$tabligne = array();
$lien = "https://www.overpass-api.de/api/interpreter?data=[out:json];node[route={$type}];way[route={$type}];relation[route={$type}](around:{$around},{$lat},{$lon});out%20center;";
$fields_string = file_get_contents($lien);
$data = json_decode($fields_string, true); // return array not object
// var_dump($data);
$nbr = 0;
foreach ($data['elements'] as $customer) {
// var_dump($customer);
$colour = '';
$networkshort = '';
$network = '';
$operatorshort = '';
$from = '';
$name = '';
$nbr++;
$name = $customer['tags']['name'];
$from = '';
if (isset($customer['tags']['from']) == 1) {
$from = $customer['tags']['from'];
}
$lat = $customer['center']['lat'];
$lon = $customer['center']['lon'];
$ligne = $customer['tags']['ref'];
if (isset($customer['tags']['operator:short']) == 1) {
$operatorshort = $customer['tags']['operator:short'];
}
if (isset($customer['tags']['network']) == 1) {
$network = $customer['tags']['network'];
}
if (isset($customer['tags']['network:short']) == 1) {
$networkshort = $customer['tags']['network:short'];
}
if (isset($customer['tags']['colour']) == 1) {
$colour = $customer['tags']['colour'];
}
$tabligne[$nbr] = array('colour' => $colour, 'network:short' => $networkshort, 'network' => $network, 'operator:short' => $operatorshort, 'from' => $from, 'name' => $name, 'ligne' => $ligne, 'lat' => $lat, 'lon' => $lon, 'type' => $type1);
}
$array = $tabligne;
return $array;
}
// fonction pour importer une image
function FonctionRayon($lat, $long, $meters)
{
$equator_circumference = 6371000; //meters
$polar_circumference = 6356800; //meters
$m_per_deg_long = 360 / $polar_circumference;
$rad_lat = ($lat * M_PI / 180); //convert to radians, cosine takes a radian argument and not a degree argument
$m_per_deg_lat = 360 / (cos($rad_lat) * $equator_circumference);
$deg_diff_long = $meters * $m_per_deg_long; //Number of degrees latitude as you move north/south along the line of longitude
$deg_diff_lat = $meters * $m_per_deg_lat; //Number of degrees longitude as you move east/west along the line of latitude
//changing north/south moves along longitude and alters latitudinal coordinates by $meters * meters per degree longitude, moving east/west moves along latitude and changes longitudinal coordinates in much the same way.
$coordinates['north']['lat'] = $lat + $deg_diff_long;
$coordinates['north']['long'] = $long;
$coordinates['south']['lat'] = $lat - $deg_diff_long;
$coordinates['south']['long'] = $long;
$coordinates['east']['lat'] = $lat;
$coordinates['east']['long'] = $long + $deg_diff_lat; //Might need to swith the long equations for these two depending on whether coordinates are east or west of the prime meridian
$coordinates['west']['lat'] = $lat;
$coordinates['west']['long'] = $long - $deg_diff_lat;
$array = array('SouthLat' => $coordinates['south']['lat'], 'WestLong' => $coordinates['west']['long'], 'NorthLat' => $coordinates['north']['lat'], 'EastLong' => $coordinates['east']['long']);
return $array;
}
// comparaison de dates
function Compardates($date)
{
if (strlen($date) == 10) {
$date = str_replace("-", "", $date);
if (strlen($date) == 8) {
$jourj = '' . date(" Y") . '' . date('m') . '' . date('d') . '';
$annemax = date(" Y") + 2;
$datemax = '' . $annemax . '' . date('m') . '' . date('d') . '';
if ($jourj <= $date) {
if ($datemax >= $date) {
$jour = 1;
} else {
$jour = 0;
}
} else {
$jour = 0;
}
} else {
$jour = 0;
}
} else {
$jour = 0;
}
return $jour;
}
function Convertiondate($date)
{
$pieces = explode("-", $date);
$date = '' . $pieces[0] . '' . $pieces[1] . '' . $pieces[2] . '';
return $date;
}
function Remplacedate($date)
{
$date = str_replace("-", "", $date);
$date = str_replace(" 00:00:00", "", $date);
return $date;
}
function demain($annee ,$mois, $jour)
{
$nbrjour = $jour;
if ($mois == 1) {
$nbrjour = 31;
}
if ($mois == 2) {
if ((is_int($annee / 4) && !is_int($annee / 100)) || is_int($annee / 400)) {
// Année bissextile
// vous remplacez le retour par ce que vou voulez
$nbrjour = 29;
} else {
// Année NON bissextile
// vous remplacez le retour par ce que vou voulez
$nbrjour = 28;
}
}
if ($mois == 3) {
$nbrjour = 31;
}
if ($mois == 4) {
$nbrjour = 30;
}
if ($mois == 5) {
$nbrjour = 31;
}
if ($mois == 6) {
$nbrjour = 30;
}
if ($mois == 7) {
$nbrjour = 31;
}
if ($mois == 8) {
$nbrjour = 31;
}
if ($mois == 9) {
$nbrjour = 30;
}
if ($mois == 10) {
$nbrjour = 31;
}
if ($mois == 11) {
$nbrjour = 30;
}
if ($mois == 12) {
$nbrjour = 31;
}
if ($nbrjour <= $jour) {
$jour = 01;
$mois = $mois + 1;
if ($mois == 12) {
$jour = 01;
$mois == 1;
$annee = $annee + 1;
}
} else {
$jour = $jour + 1;
}
if ($mois < 10) {
$mois = '0' . $mois . '';
}
if ($jour < 10) {
$jour = '0' . $jour . '';
}
$date = '' . $annee . '-' . $mois . '-' . $jour . '';
return $date;
}
function dateDiff($date1, $date2){
$diff = abs($date1 - $date2); // abs pour avoir la valeur absolute, ainsi éviter d'avoir une différence négative
$retour = array();
$tmp = $diff;
$retour['second'] = $tmp % 60;
$tmp = floor( ($tmp - $retour['second']) /60 );
$retour['minute'] = $tmp % 60;
$tmp = floor( ($tmp - $retour['minute'])/60 );
$retour['hour'] = $tmp % 24;
$tmp = floor( ($tmp - $retour['hour']) /24 );
$retour = $tmp;
return $retour;
}
}