<?php
/*
$Id $
*/
require_once ("formulaireparc.class.php");
require_once ("dbformparc.class.php");

class requete extends dbFormparc {

    var $table="requete";
    var $clePrimaire= "idrequete";
    var $typeCle= "N" ;
    
function requete($id,$db,$debug) {
$this->constructeur($id,$db,$debug);
} // fin constructeur

// cle automatique *************************************************************
function setId($db){
// id automatique nextid
$this->valF['idrequete'] = $db->nextId($this->table);
}
function verifierAjout(){
}
function setValFAjout($val){
}
// *****************************************************************************
function setvalF($val){
  $this->valF['librequete'] = $val['librequete'];
  $this->valF['naturemateriel'] = $val['naturemateriel'];
  $this->valF['organisme'] = $val['organisme'];
  $this->valF['trigeneral'] = $val['trigeneral'];
  $this->valF['reseaumairie'] = $val['reseaumairie'];
  $this->valF['etataffectation'] = $val['etataffectation'];
  $this->valF['etatintervention'] = $val['etatintervention'];
  if($val['datedebut']!="")
    $this->valF['datedebut'] = $this->dateDB($val['datedebut']); //==
  else
   $this->valF['datedebut'] = '0000-00-00';
  if($val['datefin']!="")
    $this->valF['datefin'] = $this->dateDB($val['datefin']); //==
  else
   $this->valF['datefin'] = '0000-00-00';
  $this->valF['tablerecherche'] = $val['listethemes'];
  $this->valF['idsrecherche'] = $val['idsrecherche'];
  $this->valF['tablerecherche2'] = $val['listethemes2'];
  $this->valF['idsrecherche2'] = $val['idsrecherche2'];
  $this->valF['souschaprecherche'] = $val['souschaprecherche'];
  $this->valF['souschaprecherche2'] = $val['souschaprecherche2'];
}

function verifier(){
  $this->correct=True;
  // obligatoire
  if ($this->valF['librequete']==""){
    $this->correct=false;
    $this->msg= $this->msg."Description obligatoire!<br>";
  } 
  /*if ($this->valF['tablerecherche'] && $this->valF['idsrecherche']=="" && 
  $this->valF['souschaprecherche']=="Non"){
    $this->correct=false;
    $this->msg= $this->msg."Ni liste sélection, ni sous-chapitre!<br>";
  } 
  if ($this->valF['tablerecherche2'] && $this->valF['idsrecherche2']=="" && 
  $this->valF['souschaprecherche2']=="Non"){
    $this->correct=false;
    $this->msg= $this->msg."Ni liste sélection2, ni sous-chapitre2!<br>";
  }*/ 
  if (!$this->valF['tablerecherche'] && $this->valF['souschaprecherche']=="Oui"){
    $this->correct=false;
    $this->msg= $this->msg."Sous-chapitre impossible!<br>";
  } 
  if (($this->valF['souschaprecherche']=="Non" || !$this->valF['tablerecherche2']) && $this->valF['souschaprecherche2']=="Oui"){
    $this->correct=false;
    $this->msg= $this->msg."Sous-chapitre2 impossible!<br>";
  } 
  if ($this->valF['tablerecherche']==$this->valF['tablerecherche2'] &&
  $this->valF['tablerecherche']){
    $this->correct=false;
    $this->msg= $this->msg."Thèmes identiques!<br>";
  } 
}//verifier

function setType(&$form,$maj) {
  if ($maj < 2) { //ajouter et modifier
    if ($maj==0){ //insert
      $form->setType('idrequete', 'hidden');
    } else { //modifier
      $form->setType('idrequete', 'hiddenstatic');
    }
//    $form->setType('librequete', 'text');
    $form->setType('naturemateriel', 'select');
    $form->setType('organisme', 'select');
    $form->setType('trigeneral', 'select');
    $form->setType('reseaumairie', 'select');
    $form->setType('etataffectation', 'select');
    $form->setType('etatintervention', 'select');
    $form->setType('datedebut', 'date');
    $form->setType('datefin', 'date');
    $form->setType('listethemes', 'select');
    $form->setType('souschaprecherche', 'select');
    $form->setType('listeinstances', 'selectliste');
    $form->setType('champrecherche', 'selectlistemulti');
    $form->setType('listethemes2', 'select');
    $form->setType('souschaprecherche2', 'select');
    $form->setType('listeinstances2', 'selectliste');
    $form->setType('champrecherche2', 'selectlistemulti');
  } else {
    $form->setType('idrequete', 'hiddenstatic');
    $form->setType('librequete', 'hiddenstatic');
    $form->setType('naturemateriel', 'hidden');
    $form->setType('organisme', 'hidden');
    $form->setType('trigeneral', 'hidden');
    $form->setType('reseaumairie', 'hidden');
    $form->setType('etataffectation', 'hidden');
    $form->setType('etatintervention', 'hidden');
    $form->setType('datedebut', 'hidden');
    $form->setType('datefin', 'hidden');
    $form->setType('listethemes', 'hidden');
    $form->setType('souschaprecherche', 'hidden');
    $form->setType('listeinstances', 'hidden');
    $form->setType('champrecherche', 'hidden');
    $form->setType('listethemes2', 'hidden');
    $form->setType('souschaprecherche2', 'hidden');
    $form->setType('listeinstances2', 'hidden');
    $form->setType('champrecherche2', 'hidden');
  }
  $form->setType('idsrecherche', 'hidden');   //pour deboggage
  $form->setType('idsrecherche2', 'hidden');   //pour deboggage
}

function setOnchange(&$form,$maj){
$form->setOnchange("listethemes","peuplerliste(this,'')");
$form->setOnchange("listethemes2","peuplerliste(this,'2')");
?>
<script language="javascript">
function peuplerliste(l1,s) {
  var l2 = document.getElementsByName("listeinstances"+s);   
  var l3 = document.getElementsByName("champrecherche"+s);   
  var t1 = document.getElementsByName("idsrecherche"+s);   
  l3[0].options.length = 0;   
  t1[0].value = "";   
  var index = l1.selectedIndex;   
  if(index < 1)   
    l2[0].options.length = 0;   
  else {   
    var xhr_object = null;   
            
    if(window.XMLHttpRequest) // Firefox   
      xhr_object = new XMLHttpRequest();   
    else if(window.ActiveXObject) // Internet Explorer   
      xhr_object = new ActiveXObject("Microsoft.XMLHTTP");   
    else { // XMLHttpRequest non supporté par le navigateur   
      alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");   
      return;   
    }   
   
    var data = "theme="+escape(l1.options[index].value)+"&form="+f1.name+"&select=listeinstances"+s;   

    xhr_object.open("POST", "../spg/dynselectinstances.php", true);   
       
    xhr_object.onreadystatechange = function() {   
      if(xhr_object.readyState == 4) {   
        eval(xhr_object.responseText);
       }   
    }   
    xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");   
    xhr_object.send(data);   
  }   
}
</script>
<?php
}

function setSelect(&$form, $maj,&$db,$DEBUG) {
  include ("../dyn/connexion.php");
  include ("../sql/".$dsn['phptype']."/".$this->table.".form.inc");
  if($maj<2){
    // 
    $contenu=array();
    $contenu[0]=array("","direction","service","batiment","fournisseur","modeleprocesseur","modelememoire","modeledisquedur","modelelecteuroptique","modelecartevideo","modelecarteson","modelecartereseau","modeleecran","modeleimprimante","modelecopieur","modeleappareilphoto","modelelogiciel","modelesysteme");
    $contenu[1]=array("Sélectionner","direction","service","batiment","fournisseur","modeleprocesseur","modelememoire","modeledisquedur","modelelecteuroptique","modelecartevideo","modelecarteson","modelecartereseau","modeleecran","modeleimprimante","modelecopieur","modeleappareilphoto","modelelogiciel","modelesysteme");
    $form->setSelect("listethemes",$contenu);
    $form->setSelect("listethemes2",$contenu);
    $contenu=array();
    $contenu[0]=array("Non","Oui");
    $contenu[1]=array("Non","Oui"); 
    $form->setSelect("souschaprecherche",$contenu);
    $form->setSelect("souschaprecherche2",$contenu);
    $contenu=array();
    $contenu[0]=array("Tout","Bureautique","Station","Station+Prn+Copieur","PeriphsLibres","Prn+Copieur","Reseau+Telecom","Reseau","Telecom");
    $contenu[1]=array("Tout","Bureautique","Station","Station+Prn+Copieur","PeriphsLibres","Prn+Copieur","Reseau+Telecom","Reseau","Telecom"); 
    $form->setSelect("naturemateriel",$contenu);
    //multi-organisme
    $form->setSelect("organisme",$this->selectArray($db,$sql_organisme,0," Tous "));
    //tri par num/par lib
    $contenu=array();
    $contenu[0]=array("Alpha","Num");
    $contenu[1]=array("Alpha","Num");
    $form->setSelect("trigeneral",$contenu);
    $contenu=array();
    $contenu[0]=array("NS","Non","Oui");
    $contenu[1]=array("NS","Non","Oui"); 
    $form->setSelect("reseaumairie",$contenu);
    $contenu=array();
    $contenu[0]=array("Tous","Affecté","Nonaffecté","En panne","En réparation","En stock");
    $contenu[1]=array("Tous","Affecté","Nonaffecté","En panne","En réparation","En stock"); 
    $form->setSelect("etataffectation",$contenu);
    $contenu=array();
    $contenu[0]=array("Tous","Cloturée","En cours");
    $contenu[1]=array("Tous","Cloturée","En cours"); 
    $form->setSelect("etatintervention",$contenu);
    // parametre 
    for ($i=1;$i<=2;$i++) {
      if ($i==1) $st=""; else $st="2";
      $listethemes=$this->val[array_search("listethemes$st", $this->champs)]; 
      $contenu="";
      if ($maj==1 && $listethemes) {  //si maj
        if (strstr($listethemes,"modele")) {
          $id="idmodele";
          $lib="libmodele";
          $table="modeleelement";
          $condition="nature='".substr($listethemes,6)."'";
          if (strstr($listethemes,"logiciel")) 
            $condition.=" and integre = 'v'";   
        } else {
          $id="id$listethemes";
          $lib="lib$listethemes";
          $table=$listethemes;
          $condition="";
        }
        //setselect listeinstances
        $sql="select $id,$lib from $table";
        if ($condition)
          $sql.=" where $condition";
        $sql.=" order by $lib";
        $res = $db->query($sql);
        if (DB :: isError($res))
          die($res->getMessage().$sql);
        else{
          if ($DEBUG == 1)
            echo " la requete ".$sql." est exécutée<br>";
          $k=0;
          while ($row=& $res->fetchRow()){
            $contenu[0][$k]=$row[0];
            $contenu[1][$k]=$row[1];
            $k++;
          }
          $res->free();
          $form->setSelect("listeinstances$st",$contenu);
        } 
        //setselect champrecherche
        $idsrecherche=$this->val[array_search("idsrecherche$st", $this->champs)]; 
        $contenu="";
        //peupler champrecherche en fonction des ids
        if ($idsrecherche=='') {
          $sql="";
        } else {
          $sql="select $id,$lib from $table where $id in ($idsrecherche) order by $lib";
//echo ($sql);
        }
        if ($sql) {
          $res = $db->query($sql);
          if (DB :: isError($res))
            die($res->getMessage().$sql);
          else{
            if ($DEBUG == 1)
              echo " la requete ".$sql." est exécutée<br>";
            $k=0;
            while ($row=& $res->fetchRow()){
              $contenu[0][$k]=$row[0];
              $contenu[1][$k]=$row[1];
              $k++;
            }
            $res->free();
          }
        } 
      }
      $contenu[2] ="listeinstances$st"; //liste utilisee pour remplissage
      $contenu[3] ="idsrecherche$st";  //chp cache pour liste id ajoutes
      $form->setSelect("champrecherche$st",$contenu);
    }
  }
}

function setTaille(&$form,$maj){
  $form->setTaille('champrecherche',10);
  $form->setTaille('listeinstances',10);
  $form->setTaille('champrecherche2',10);
  $form->setTaille('listeinstances2',10);
}

function setMax(&$form,$maj){
    $form->setMax('champrecherche',10);
    $form->setMax('listeinstances',10);
    $form->setMax('champrecherche2',10);
    $form->setMax('listeinstances2',10);
}

function setGroupe(&$form, $maj) {
  if ($maj<2) {
    $form->setGroupe('idrequete','D');
    $form->setGroupe('librequete','F');
    $form->setGroupe('naturemateriel', 'D');
    $form->setGroupe('organisme', 'G');
    $form->setGroupe('trigeneral', 'F');
    $form->setGroupe('reseaumairie', 'D');
    $form->setGroupe('etataffectation', 'G');
    $form->setGroupe('etatintervention', 'F');
    $form->setGroupe('datedebut','D');
    $form->setGroupe('datefin','F');
    $form->setGroupe('listethemes','D');
    $form->setGroupe('souschaprecherche','F');
    $form->setGroupe('listeinstances','D');
    $form->setGroupe('champrecherche','F');
    $form->setGroupe('listethemes2','D');
    $form->setGroupe('souschaprecherche2','F');
    $form->setGroupe('listeinstances2','D');
    $form->setGroupe('champrecherche2','F');
  }
}

function setRegroupe(&$form,$maj){
  if ($maj<2) {
    $form->setRegroupe('naturemateriel', 'D','Options');
    $form->setRegroupe('organisme', 'G','');
    $form->setRegroupe('trigeneral', 'G','');
    $form->setRegroupe('reseaumairie', 'G','');
    $form->setRegroupe('etataffectation', 'G','');
    $form->setRegroupe('etatintervention', 'G','');
    $form->setRegroupe('datedebut','G','');
    $form->setRegroupe('datefin','F','');
    $form->setRegroupe('listethemes','D','Sélection');
    $form->setRegroupe('souschaprecherche','G','');
    $form->setRegroupe('listeinstances','G','');
    $form->setRegroupe('champrecherche','F','');
    $form->setRegroupe('listethemes2','D','Sélection2');
    $form->setRegroupe('souschaprecherche2','G','');
    $form->setRegroupe('listeinstances2','G','');
    $form->setRegroupe('champrecherche2','F','');
  }
}

function setVal(&$form,$maj,$validation,&$db) {
   if ($validation==0 and $maj==0){
   }
}

function setLib(&$form,$maj) {
  $form->setLib('idrequete','ID');
  $form->setLib('librequete',' ------------   Requête');
  $form->setLib('datedebut','Période achat: de ');
  $form->setLib('datefin','A ');
  $form->setLib('listethemes','Thème');
  $form->setLib('listeinstances','');
  $form->setLib('champrecherche','');
  $form->setLib('listethemes2','Thème');
  $form->setLib('listeinstances2','');
  $form->setLib('champrecherche2','');
  $form->setLib('souschaprecherche','Sous-chapitre');
  $form->setLib('souschaprecherche2','Sous-chapitre');
}

}// fin de classe
?>