<?php
//$Id$ 
//gen openMairie le 10/05/2013 09:23

require_once ("../gen/obj/scrutin.class.php");

class scrutin extends scrutin_gen {

    function scrutin($id,&$db,$debug) {
        $this->constructeur($id,$db,$debug);
    }// fin constructeur
    
    function verifier (){

        $this -> correct = True;
        
        if ($this -> valF ['scrutin'] == "")
        {
            $this -> correct = false;
            $this -> msg = $this -> msg ._("code")."&nbsp;"._("scrutin")."&nbsp"._("obligatoire");
        }else{
            $pos = strpos($this -> valF ['scrutin'], ' ');
            if ($pos === false) {
             //Le caractere blanc  n'a pas été trouvée dans la chaîne scrutin
            }else{
               $this -> correct = false;
               $this -> msg = $this -> msg ._("code")."&nbsp;"._("scrutin")."&nbsp:&nbsp;"._("NE_DOIT_PAS_COMPORTER_DE_CARACTERE(S)_BLANC");
            }
        }
        if ($this -> valF ['canton'] == "")
        {
            $this -> correct = false;
            $this -> msg = $this -> msg ._("canton")."&nbsp"._("obligatoire");
        }
        if ($this -> valF ['libelle'] == "")
        {
            $this -> correct = false;
            $this -> msg = $this -> msg ._("libelle")."&nbsp;"._("scrutin")."&nbsp"._("obligatoire");
        }
        if ($this->valF['date_scrutin']==""){
            $this -> correct = false;
            $this -> msg = $this -> msg ._("date")."&nbsp;"._("scrutin")."&nbsp"._("obligatoire");
        }
    } // verifier ()



    function setType (&$form, $maj) {
        parent::setType ($form, $maj);
        if ($maj < 2)
        {
            // Ajout
            $form -> setType ('canton', 'select');
            $form -> setType ('tour', 'select');
            $form -> setType ('date_scrutin', 'date');
            $form -> setType ('solde', 'checkbox');
            if ($maj == 1)
            {
                // Modification
                 $form -> setType ('scrutin', 'hiddenstatic');
            }
        }
        else
        {
             $form -> setType ('scrutin', 'hiddenstatic');
            // Suppression
        }
    } // setType (&$form, $maj) 


    function setSelect(&$form,$maj,$db,$DEBUG){
        parent::setType ($form,$maj,$db,$DEBUG);
        include ("../sql/".OM_DB_PHPTYPE."/".$this -> table.".form.inc.php");
        
        // Liste de choix : canton
        $res = $db->query($sql_canton);
        if (DB :: isError($res))
            die($res->getMessage().$sql_canton);
        else
        {
            if ($DEBUG == 1)
                echo " la requete ".$sql_canton." est exécutée<br>";
            $contenu [0][0] = "";
            $contenu [1][0] = _("choisir")."&nbsp;"._("un")."&nbsp;"._("canton");
            $k=1;
            while ($row=& $res->fetchRow())
            {
                    $contenu[0][$k]=$row[0];
                    $contenu[1][$k]=$row[1];
                    $k++;
            }
            $form->setSelect('canton',$contenu);
        }           
     // tour
        $contenu=array();
        $contenu[0]=array('1','2');
        $contenu[1]=array(_("1er")."&nbsp;"._("tour"),_("2eme")."&nbsp;"._("tour"));
        $form->setSelect("tour",$contenu);
    } // 



    function setTaille(&$form,$maj){
         parent::setTaille ($form, $maj);
        $form->setTaille('date_scrutin', 12);
        $form->setTaille('convocation_agent', 80);
        $form->setTaille('convocation_president', 80);
    }
    function cleSecondaire($id,$db,$val,$debug) {
        $this->rechercheAffectation($id,$db,$debug) ;
        $this->rechercheCandidature($id,$db,$debug) ;
    }
    
    function rechercheAffectation($id,$db,$debug){
        $this -> correct = True;
        
        $sql = "select * from  ".DB_PREFIXE."affectation where scrutin ='".$id."'";
        $res = $db->query($sql);
        if($debug==1) echo $sql;
        if (DB :: isError($res))
           die($res->getMessage(). " => Echec  ".$sql);
        else{
            $nbligne=$res->numrows();
            $this->msg = $this->msg."&nbsp;<font>&nbsp;".$nbligne."&nbsp;</font>&nbsp;".
            _("enregistrement(s)")."&nbsp;"._("en lien")."&nbsp;"._("avec")."&nbsp;"._("table")." \"affectation\" (&nbsp;"._("scrutin")."&nbsp; [".
            $id."]".")<br>";
            if ($nbligne>0)
               $this->correct=false;
        }
    }
    function rechercheCandidature($id,$db,$debug){
        $this -> correct = True;
       
        $sql = "select * from  ".DB_PREFIXE."candidature where scrutin ='".$id."'";
        $res = $db->query($sql);
        if($debug==1) echo $sql;
        if (DB :: isError($res))
           die($res->getMessage(). " => Echec  ".$sql);
        else{
            $nbligne=$res->numrows();
            $this->msg = $this->msg."&nbsp;<font>&nbsp;".$nbligne."&nbsp;</font>&nbsp;".
            _("enregistrement")."&nbsp;"._("en lien")."&nbsp;"._("avec")."&nbsp;"._("table")." \"candidature\"(&nbsp;"._("scrutin")."&nbsp; [".
            $id."]".")<br>";
            if ($nbligne>0)
               $this->correct=false;
        }
    }  
    function setOnchange(&$form,$maj){
         parent::setTaille ($form, $maj);
        $form->setOnchange("scrutin","suppesp(this)");
    }
}// fin classe
?>