*
* This file is part of CHD Gestion.
*
* CHD Gestion is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CHD Gestion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CHD Gestion. If not, see .
**/
// topological sorting from https://gist.github.com/gowon/1744369137826450b185
function topoNodeLevel($k, $ar,$n,$ref=array())
{
if(!isset($ar[$n][$k])){return 0;}
if(in_array($n,$ref)){return -1;}
$ref[]=$n;
$r=topoNodeLevel($k, $ar,$ar[$n][$k],$ref);
return ($r==-1?-1:$r+1);
}
function toposort($k, &$array) {
$ar=array();
foreach($array as $i=>$tmp)
{
$ar[]=topoNodeLevel($k, $array,$i);
}
if(!in_array(-1,$ar))
{
array_multisort($ar,SORT_ASC,$array);
}else{
error_log("Circular reference detected : " . print_r($array, true));
}
}
include_once('inc/config.php');
$mysqli = new mysqli($db_config['host'], $db_config['username'], $db_config['password'], $db_config['database']);
if (mysqli_connect_errno()) {
die(mysqli_connect_error());
}
unset($db_config);
$mysqli->set_charset("utf8") or die($mysqli->error);
// Détail des équipements (nom, ip, uplink)
$res = $mysqli->query("SELECT e.id,
IFNULL(NULLIF(e.hostname,''), CONCAT('e', e.id)) as 'name', e.uplink_id, e.ipmgmt_id, r.title as relais
FROM equipements e
LEFT JOIN relais r ON ( r.id = e.relais_id )
WHERE (e.relais_id IS NOT NULL OR e.hostname='STG1')
AND e.date_hs IS NULL
AND e.equipement_mode_id != 'power'");
$equipements = array();
while ( $e_row = $res->fetch_assoc() ) {
$id = $e_row['id'];
$equipements[$id] = $e_row;
}
// Liste des relais par ville, avec les équipements associés
$res = $mysqli->query("SELECT v.title as ville, r.title as relais, GROUP_CONCAT(e.id) as equipement_ids
FROM villes v
JOIN relais r ON (v.id = r.ville_id)
JOIN equipements e ON (r.id = e.relais_id)
WHERE e.date_hs IS NULL
GROUP BY v.title, r.title
");
// Pour chaque relais
$world = array();
while ( $r_row = $res->fetch_assoc() ) {
// La ville sera en clé du tableau associatif
$ville = $r_row['ville'];
unset($r_row['ville']);
// On veut un tableau d'id d'équipements à partir de la chaine séparée par des virgules récupérée
$eids = explode(',', $r_row['equipement_ids']);
unset($r_row['equipement_ids']);
// Construction d'un tableau d'équipements impliqué dans l'accès à internet du relais considéré
$r_row['equipements'] = array();
// Pour chaque id d'équipement (initialement : les équipements sur le relai, après : les équipements sur le chemin)
while ( $eid = array_pop($eids) ) {
if ( array_key_exists($eid, $equipements) ) {
// On référence l'équipement depuis le tableau en mémoire de tous les équipements
$r_row['equipements'][$eid] = &$equipements[$eid];
// On ajoute les équipements en amont s'il ne sont pas déjà là
$uplink_id = $equipements[$eid]['uplink_id'];
if ( ! array_key_exists($uplink_id, $r_row['equipements'])) {
array_push($eids, $uplink_id);
}
}
}
// On trie topologiquement les équipements
toposort('uplink_id', $r_row['equipements']);
// On ajoute la ligne relais enrichie au tableau des relais, indexé par ville
$world[$ville][] = $r_row;
}
?>