<?php
require_once 'application/class/class_ManageSession.php';

global $connecteur;

$action = getValue('action');
$no_ao = getValue('no_ao');
$val = getValue('val');
$index = getValue('index');
$id = getValue('id');
$mode = getValue('mode');
$ancrage = getValue('ancrage');

switch ($action) {
    case 'AffichePopup':
        $popup = new AO_Quest_Biblio($no_ao);
        $xslt = new GenXslt(true);
        $array = $popup->buildXml($action);
        echo $xslt->XslHtml(AO_Quest_Biblio::fichierXsl, $array);
        echo '<script type="text/javascript">CallUrl("ao_quest_biblio.php?action=initTreeview&no_ao=', $no_ao, '&mode=' . $mode . '&ancrage=' . $ancrage . '&id=' . urlencode($id) . '");</script>';
        break;
    //
    // Gestion du treeview
    //
    case 'initTreeview':
        $genXslt = new GenXslt(true);
        $array = AO_Quest_Biblio::BuildXmlArbo($no_ao, $mode, $ancrage, $id);

        Affichage::innerHTMLPointeur(AO_Quest_Biblio::id_TreeView_Biblio, $genXslt->XslHtml(AO_Quest_Biblio::fichierXslTreeview, $array));
        break;
    
    case 'selectTheme':
        // if ('true' == $val) {
        $genXslt = new GenXslt(true);
        $array = AO_Quest_Biblio::buildXmlQuestions($action, $index);
        Affichage::innerHTMLPointeur(AO_Quest_Biblio::id_Panel_Questions, $genXslt->XslHtml(AO_Quest_Biblio::fichierXsl, $array));
        // } else {
        // $vide = '';
        // Affichage::innerHTMLPointeur(AO_Quest_Biblio::id_Panel_Questions, $vide);
        // }
        break;
    
    case 'checkall':
        $checkValue = ('true' == $val) ? 'true' : 'false';
        echo '
			var form = document.forms["', AO_Quest_Biblio::id_Form, '"];
			if (form) {
				var form_len = form.length;
				var elt;
				for (var idx = 0 ; idx < form_len ; idx++) {
					elt = form.elements[idx];
					if (elt.id.startsWith("chck_")) {
						elt.checked = ', $checkValue, ';
						//alert(elt.id + " => " + elt.checked);
					}
				}
			}
		';
        break;
    
    case 'importTheme':
        $resultat = AOQuestionnaireXml::ImporterTheme($no_ao, $index, $id);
        if ('O' != $resultat['Statut']) {
            Affichage::messageJS($resultat['MsgErreur']);
        } else {
            echo 'setTimeout(function() {window.closePopup();}, 0);';
            echo 'setTimeout(function() {parent.CallUrl("', AO_Questionnaire::fichierPhp, '?action=rafraichirAffichage&no_ao=', $no_ao, '&mode=', $mode, '&ancrage=', $ancrage, '");}, 0);';
        }
        break;
    case 'importQuestions':
        $arrayQuestions = array();
        foreach (getValueAll($_REQUEST) as $key => & $value) {
            if (false !== strpos($key, 'chck_')) {
                if ('on' == $value) {
                    $arrayQuestions[] = substr($key, 5);
                }
            }
        }
        if (0 == count($arrayQuestions)) {
            $messageErreur = Trad('Vous devez sélectionner au moins une question.');
            Affichage::messageJS($messageErreur);
        } else {
            $resultat = AOQuestionnaireXml::ImporterQuestions($no_ao, $arrayQuestions, $id);
            if ('O' != $resultat['Statut']) {
                Affichage::messageJS($resultat['MsgErreur']);
            } else {
                echo 'setTimeout(function() {window.closePopup();}, 0);';
                echo 'setTimeout(function() {parent.CallUrl("', AO_Questionnaire::fichierPhp, '?action=rafraichirAffichage&no_ao=', $no_ao, '&mode=', $mode, '&ancrage=', $ancrage, '");}, 0);';
            }
        }
        
        break;
    default:
        $messageErreur = Trad('action non définie : ') . $action;
        Affichage::messageJS($messageErreur);
        break;
}