<?php
/* 
 * Interface d'administration de configuration de l'agent SIGB
 */
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 class="instsel" name="id_client" id="id_client" onchange="admin.chooseClient(this.value);">';
    foreach ($aConfig as $partConfig => $configTab) {
        $data = '';
        // This line added 15/05/2013
        $title = getAlias($partConfig);
        foreach ($configTab as $value) {
            $data .= '|'.$value;
        }
        $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 headerPage('Configuration de l\'agent SIGB : 3');
?>

<?php if (isset($_GET['ipclient'])) echo '<h2>'.$_GET['ipclient'].'</h2>';else echo '<h2>Clients hébergés</h2>'; ?>
<div class="main">
        <form>
            <table style="width:70%;margin:0 auto;">
                <tr>
                    <td>Liste des clients : </td>
                    <td><?php echo $select; ?></td>
                    <td><input type="text" name="key_client" id="key_client" disabled="disabled" /> 
					    <span class="help" <?php if(isset($_REQUEST['idclient']))echo strtolower($_REQUEST['idclient']); else echo readHelp('keyclient'); ?>>&nbsp;</span><br />
					    <input type="text" name="ip_client" id="ip_client" disabled="disabled" /> 
						<span class="help" <?php if(isset($_REQUEST['ipclient']))echo $_REQUEST['ipclient']; else echo readHelp('ipclient'); ?>>&nbsp;</span></td>
                </tr>
                <tr>
                    <td colspan="3"><input type="button" value="Lire la configuration générale" onclick="admin.printConfig('general', 'read', document.getElementById('ip_client').value, document.getElementById('id_client').value);"/> <input type="button" value="Lire la configuration monitoring" onclick="admin.printConfig('monitoring', 'read', document.getElementById('ip_client').value, document.getElementById('id_client').value);"/></td>
                </tr>
                <tr>
                    <td colspan="3">&nbsp;</td>
                </tr>
                <tr id="addBlocTr" style="display:none;">
                    <td>Ajouter un bloc : </td>
                    <td><select id="add_bloc">
                            <option value="ftp">FTP</option>
                            <option value="sftp">SFTP</option>
                            <option value="gabi">Gabi</option>
                            <option value="ktm">KTM</option>
                            <option value="karvi">Karvi</option>
                            <option value="agate">Agate</option>
                            <option value="aloes">Aloes</option>
                            <option value="koha">Koha</option>
                        </select>
                    </td>
                    <td>
                        <input type="hidden" id="lastSIGB" value="ktm" />
                        <?php if (isset($_GET['passftp'])) echo '<input id="addClientPassFTP" type="hidden" value="'.$_GET['passftp'].'" />' ?>
                        <input type="button" value="Ajouter" onclick="admin.addBloc(document.getElementById('add_bloc').value);"/>
                    </td>
                </tr>
            </table>
        </form>
		
		<?php echo @$_REQUEST['debug'];?>
		
    <p id="contenu_config"></p>
    <p id="contenu_config2"></p>
    <p id="resultSend"></p>
	<?php echo $_REQUEST['error'];?>
    <div id="infosNext"></div>
    <script type="text/javascript">admin.chooseClient(document.getElementById('id_client').value);</script>
</div>

<?php
echo footerPage();
?>