<?php
//$Id$ 
//gen openMairie le 30/05/2019 16:10

require_once "election.class.php";

class election_cloturee extends election {

	protected $_absolute_class_name = "election_cloturee";

    function init_class_actions() {
		parent::init_class_actions();
		$this->class_actions[1] = array();
		$this->class_actions[2] = array();
		$this->class_actions[7] = array();
		$this->class_actions[20] = array();
		$this->class_actions[21] = array();
		$this->class_actions[16] = array();
		$this->class_actions[10] = array();
		$this->class_actions[11] = array();
		$this->class_actions[12] = array();
		$this->class_actions[13] = array();
		$this->class_actions[15] = array();
		$this->class_actions[19] = array();
		
		
		// decloturer
		$this->class_actions[4] = array(
            "identifier" => "decloture",
            "portlet" => array(
                "type" => "action-direct-with-confirmation",
                "libelle" => _("decloturer election"),
                "order" => 50,
                "class" => "edit-16",
            ),
            "view" => "formulaire",
            "method" => "decloturer",
            "button" => "diffuser",
           // "permission_suffix" => "election_cloture",
        );

		
         $this->class_actions[5] = array(
            "identifier" => "supprimer",
            "portlet" => array(
                "type" => "action-direct-with-confirmation",
                "libelle" => _("suppression totale"),
                "order" => 52,
                "class" => "delete-16",
            ),
            "view" => "formulaire",
			"method" => "supprimer_all",
            "button" => "diffuser",
            //"permission_suffix" => "tab",
            "condition" => array(
                "is_clotured")
        );


 		$this->class_actions[8] = array(
			"identifier" => "edition",
			"portlet" => array(
				"type"=>"action-blank",
				"libelle" => _("edition resultat"),
				"order" => 55,
				"class" => "pdf-16",
				),
			"view" => "edition_resultat",
			 //"permission_suffix" => "edition",
			);

 
         $this->class_actions[9] = array(
            "identifier" => "extraction",
            "portlet" => array(
                "type" => "action-direct-with-confirmation",
                "libelle" => _("extraction en csv"),
                "order" => 100,
                "class" => "arrow-right-16",
            ),
            "view" => "formulaire",
			"method" => "extraction",
            "button" => "diffuser",
            //"permission_suffix" => "tab",
        ); 

		

	}


// ================================
// gestion des actions suivant etat
// ================================

    function is_clotured() {
        //
        $etat = $this->getVal("cloture");
        if ($etat== 't' ) {
            return true;
        }
        return false;
    }



// =========================
// action suppression totale
// =========================

	// cette action supprime l'election de la base et dans les
	// repertoires d'affichage aff et repertoire web
    function  supprimer_all() {
		$election=$this->getval('election');
		$this->f->db->autoCommit(false);
		$this->msg="";
		// base
		// election resultat
		$sql = " delete from ".DB_PREFIXE."election_resultat ";
		$sql .= " using ".DB_PREFIXE."election_bureau ";
		$sql .= " where election_resultat.election_bureau=election_bureau.election_bureau ";
		$sql .= " and election_bureau.election = ".$election;
		$res = $this->db->query($sql);
		$this->addToLog(__METHOD__."(): db->query(\"".$sql."\");", VERBOSE_MODE);		
		$this->f->isDatabaseError($res);
		$this->msg .= $this->db->affectedRows()." election_resultat "._("supprime(s)")."</br>";	
		// election candidat
		$sql = " delete from ".DB_PREFIXE."election_candidat ";
		$sql .= " where election = ".$election;
		$res = $this->db->query($sql);
		$this->addToLog(__METHOD__."(): db->query(\"".$sql."\");", VERBOSE_MODE);		
		$this->f->isDatabaseError($res);
		$this->msg .= $this->db->affectedRows()." election_candidat "._("supprime(s)")."</br>";
		// participation bureau
		$sql = " delete from ".DB_PREFIXE."participation_bureau ";
		$sql .= " using ".DB_PREFIXE."participation_election ";
		$sql .= " where participation_election.participation_election=participation_bureau.participation_election";
		$sql .= " and participation_election.election = ".$election;
		$res = $this->db->query($sql);
		$this->addToLog(__METHOD__."(): db->query(\"".$sql."\");", VERBOSE_MODE);		
		$this->f->isDatabaseError($res);
		$this->msg .= $this->db->affectedRows()." participation_bureau "._("supprime(s)")."</br>";
		// election bureau
		$sql = " delete from ".DB_PREFIXE."election_bureau ";
		$sql .= " where election = ".$election;
		$res = $this->db->query($sql);
		$this->addToLog(__METHOD__."(): db->query(\"".$sql."\");", VERBOSE_MODE);		
		$this->f->isDatabaseError($res);
		$this->msg .= $this->db->affectedRows()." election_burau "._("supprime(s)")."</br>";	
		// participation election			
		$sql = " delete from ".DB_PREFIXE."participation_election ";
		$sql .= " where election = ".$election;
		$res = $this->db->query($sql);
		$this->addToLog(__METHOD__."(): db->query(\"".$sql."\");", VERBOSE_MODE);		
		$this->f->isDatabaseError($res);	
		$this->msg .= $this->db->affectedRows()." participation_bureau "._("supprime(s)")."</br>";	
		// election
		$sql = " delete from ".DB_PREFIXE."election ";
		$sql .= " where election.election ='".$election."'";
		//$sql .= " and election.cloture is true ";
		$res = $this->db->query($sql);
		$this->addToLog(__METHOD__."(): db->query(\"".$sql."\");", VERBOSE_MODE);
		$this->f->isDatabaseError($res);
		$this->msg .= $this->db->affectedRows()." election "._("supprime")."</br>";
		$this->f->db->commit();	
		// destruction repertoire
		$this->rrmdir("../aff/res/".$election."/candidat");
		$this->msg .= _("repertoire")." "."../aff/res/".$election."/candidat "._("detruit")."<br>";		
		$this->rrmdir("../aff/res/".$election."/bres");
		$this->msg .= _("repertoire")." "."../aff/res/".$election."/bres "._("detruit")."<br>";		
		$this->rrmdir("../aff/res/".$election."/bpar");
		$this->msg .= _("repertoire")." "."../aff/res/".$election."/bpar "._("detruit")."<br>";		
		$this->rrmdir("../aff/res/".$election);
		$this->msg .= _("repertoire")." "."../aff/res/".$election." "._("detruit")."<br>";
		$this->rrmdir("../web/res/".$election);
		$this->msg .= _("repertoire")." "."../web/res/".$election." "._("detruit")."<br>";		 
		$this->f->layout->display_message('valid', $this->msg);		 		
		
    }

	function rrmdir($dir) {
		if (is_dir($dir)) { // si le paramètre est un dossier
		$objects = scandir($dir); // on scan le dossier pour récupérer ses objets
		foreach ($objects as $object) { // pour chaque objet
			 if ($object != "." && $object != "..") { // si l'objet n'est pas . ou ..
				if (filetype($dir."/".$object) == "dir") 
					rmdir($dir."/".$object);
				else 
					unlink($dir."/".$object); // on supprime l'objet
				}
			}
			reset($objects); // on remet à 0 les objets
			rmdir($dir); // on supprime le dossier
		}
	}

// =================
// action decloturer
// =================

    function decloturer() {
		$election=$this->getVal("election");
		$this->f->db->autoCommit(false);
		$sql="update  ".DB_PREFIXE.
			"election set cloture='f' where election=".$election;	
		$res = $this->f->db->query($sql);
		$this->addToLog(__METHOD__."(): db->query(\"".$sql."\");", VERBOSE_MODE);
		$this->f->isDatabaseError($res);
		$this->f->db->commit();	
		$this->addToMessage(_("election")." ".$election." "._('non cloturee'));		
	}


}
