<?php

include('adminBroker.class.php');
include 'includes/functions.php';
define('INI_SERVER', '../serveur/config/clients.ini');
define('INI_SEARCH', 'conf/config.ini');

function getAlias($code) {
    static $mysqli;
    static $connect = false;
    
    if (!$connect) {
        $connect = true;
        $ini = parse_ini_file(INI_SEARCH,true);  
        
        if (isset($ini['Database']['database_institutions'])) {
            $mysqli = connect($ini['Database']['database_institutions']);  
        }        
    }
    if (!$mysqli) return $code;
    
    $alias = '';
    
    $query = "SELECT alias from institution i where i.code='".$code."' AND i.level='I'";
    $resultset = $mysqli->query($query);
    if ($resultset && ($row = $resultset->fetch_object())) {
        $alias = $row->alias;
    }
    if ($alias!='') {
        $bits = explode(".", $alias);
        $cc  = array_pop($bits);
        $nom = array_pop($bits);
        $domain = $nom.'.'.$cc;
        
        // Try to find the real name
        
        // Remove uninteresting bits
        if (in_array($bits[0],array('bibliotheque', 'mediatheque', 'www', 'biblio', 'bm'))) {
            array_shift($bits);
        }
                
        switch ($domain) {
            case 'e-sezhame.fr':
            case 'opac3d.fr':
            case 'paprika.net':
                $alias = implode('.',$bits);
                break;
        }
        
        return ($alias!='') ? $code.' ('.$alias.')' : $code;
    } else {
        return $code;
    }
    
}

function printConfig($aConfig) {   
    $select = '<select id="pass_id_client">';
    foreach ($aConfig as $partConfig => $configTab) {
        // This line added 15/05/2013
        $title = getAlias($partConfig);
        $data = $configTab[0];
        $select .= '<option '.(strtolower($_GET['idclient']) == $partConfig ? ' selected ' : '').' value="'.$partConfig.$data.'">'.$title.'</option>';
        $select .= '<!-- '.strtolower($_GET['idclient']).' == '.$partConfig.'-->';
		
    }
    $select .= '</select>';
    return $select;
}

$adminBroker = new adminBroker();
$tabConfigServeur = $adminBroker->readConfig(INI_SERVER);
ksort($tabConfigServeur);
$select = printConfig($tabConfigServeur);
echo $select;

