<?php
/**
 * Ce script définit la classe 'autorisation'.
 *
 * @package opencimetiere
 * @version SVN : $Id: autorisation.class.php 1517 2024-04-05 15:16:44Z gmalvolti $
 */

require_once "../gen/obj/reprise_abandon.class.php";

class reprise_abandon extends reprise_abandon_gen {
    /**
     * @return void
     */
    function init_class_actions() {
        parent::init_class_actions();
        //
        // ACTION - 601 - listing_advs
        $this->class_actions[601] = array(
            "identifier" => "listing_advs",
            "permission_suffix" => "tab",
            "view" => "view__listing_advs",
        );
        // ACTION - 701 - direct_link
        $this->class_actions[701] = array(
            "identifier" => "direct_link",
            "permission_suffix" => "tab",
            "view" => "view__direct_link",
        );
    }
    /**
     * SETTER_FORM - setType.
     *
     * @return void
     */

         /**
     *
     */
    function __construct($id, &$dnu1 = null, $dnu2 = null) {
        $this->constructeur($id);
        // Récupération des paramètres nécessaires a l'affichage des statuts et des etats
        $this->init_parameters();
    }
    
    function get_default_libelle() {
        return 'id '.$this->getVal($this->clePrimaire)."&nbsp; - du ".date('d/m/Y', strtotime($this->getVal("datecreation")));
    }

    function init_parameters() {
        $this->statut = array(
            '' => __("choisir ").__("statut"),
            'en_cours' => __("En cours"),
            'finalisee' => __("Finalisée"),
            'annulee' => __("Annulée"),
        );
        
        $this->etat = array(
            '' => __("choisir ").__("etat"),
            1 =>__("1 - Phase préliminaire"),
            2 => __("2 - Convocation première visite"),
            3 => __("3 - Rédaction PV1"),
            4 => __("4 - Notification/affichage PV1"),
            5 => __("5 - Convocation deuxième visite"),
            6 => __("6 - Rédaction PV2"),
            7 => __("7 - Notification/affichage PV2"),
            8 => __("8 - Délibération"),
        );
    }

    /**
     * SETTER_FORM - setSelect.
     *
     * @return void
     */
    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {
        parent::setSelect($form, $maj);
            $contenu[0]=array_keys($this->statut);
            $contenu[1]=array_values($this->statut);
            $form->setSelect("statut", $contenu);

            $contenu[0]=array_keys($this->etat);
            $contenu[1]=array_values($this->etat);
            $form->setSelect("etat", $contenu);
    }

    function set_form_default_values(&$form, $maj, $validation) {
        parent::set_form_default_values($form, $maj, $validation);
        if ($validation == 0) {
            if ($maj == 0) {
                $form->setVal('statut', 'en_cours');
                $form->setVal('etat', 1);
                $form->setVal('heureconstatpv1', '00:00:00');
                $form->setVal('heureconstatpv2', '00:00:00');
            }
        }
    }


    function setType(&$form, $maj) {
        parent::setType($form, $maj);
        
        $form->setType('emplacement', 'hidden');
        $form->setType('reprise_abandon', 'hidden');
        $form->setType('arrete', 'filestatic');
        $form->setType('statut', 'selectstatic');
        $form->setType('etat', 'selectstatic');
        
        if ($maj == 3) {
            $form->setType('arrete', 'file');
        }

        if ($maj < 2) {
            $form->setType('statut', 'select');
            $form->setType('etat', 'select');
            $form->setType('arrete', 'upload2');
        }

    }

    function setOnchange(&$form, $maj) {
        parent::setOnchange($form, $maj);
        $form->setOnchange('heureconstatpv1','ftime(this)');
        $form->setOnchange('heureconstatpv2','ftime(this)');
    }

    /**
     * VIEW - view__direct_link.
     *
     * Redirige vers la fiche de la reprise sur abandon dans le contexte de la concession.
     * 
     * - Ajout de la view directlink.
     *
     * Exemple d'appel : ../app/index.php?module=form&obj=derogation_ouverture_tardive&idx=ID&action=701.
     *
     * @return void
     */
    function view__direct_link() {
        $this->checkAccessibility();
        $redirect_url = sprintf(
            '%s&direct_link=true&obj=%s&action=3&idx=%s&direct_form=reprise_abandon&direct_action=3&direct_idx=%s',
            OM_ROUTE_FORM,
            "concession",
            $this->getVal("emplacement"),
            $this->getVal($this->clePrimaire)
        );
        header('Location: '.$redirect_url);
        return;
    }

    /**
     * Redirige l'utilisateur vers le listing avec la sélection souhaitée
     * sur la recherche avancée.
     *
     * Un critère est géré par cette méthode :
     * - sanction_fin_min : valeurs "true" ou "false",
     *
     * Exemple d'appel : ../app/index.php?module=form&obj=declaration&action=601&filtre=declaration_statut&filtre=date_declaration&filtre=declaration_type.
     *
     * @return void
     */
    function view__listing_advs() {
        $this->checkAccessibility();
        $obj = $this->get_absolute_class_name();
        
        // Formatage des arguments reçus en paramètres
        $inst_om_widget = $this->f->get_inst__om_dbform(array(
            "obj" => "om_widget",
            "idx" => 0,
        ));

        $search = array(
            "advanced-search-submit" => "",
        );

        // Permet de lister tous les paramètres qu'il est possible d'avoir concernant le widget
        $parametres_possibles = array(
            "statut",
            "etat",
        );

        foreach ($parametres_possibles as $champ) {        
            if ($this->f->get_submitted_get_value($champ) != NULL) {
                $search[$champ] = $this->f->get_submitted_get_value($champ);
            }
        }
        
        // Gestion des arguments <champ_date>._min ou <champ_date>.max
        $champs_date_possibles = array(
            'datecreation',
            'dateconvocationvisite1',
            'datenotificationpv1',
            'datenotificationpv2',
            'dateconvocationvisite2'
        );

        foreach ($champs_date_possibles as $champ) {
            foreach (array('_min', '_max') as $suffixe) {
                if ($this->f->get_submitted_get_value($champ.$suffixe) != NULL) {
                    $search[$champ.$suffixe] = date("d/m/Y", strtotime($this->f->get_submitted_get_value($champ.$suffixe)));
                }
            }
        }

        $advs_id = str_replace(array('.',','), '', microtime(true));
        $_SESSION["advs_ids"][$advs_id] = serialize($search);
        header('Location: '.OM_ROUTE_TAB.'&obj='.$obj.'&advs_id='.$advs_id);

        return;
    }

    /**
     * Permet de composer le CASE WHEN qui remplace les valeurs
     * du champ statut par sa traduction.
     * Cette méthode est principalement utilisée pour la construction du listing.
     */
    function compose_statut_reprise_trad() {
        $select_composed = "(";
        foreach ($this->statut as $statut_key => $statut_trad) {
            $select_composed .= sprintf(
                '%1$s WHEN 
                    reprise_abandon.statut = \'%2$s\'
                THEN
                    \'%3$s\'
                %4$s
                ',
                $statut_key == array_keys($this->statut)[0] ? 'CASE' : '',
                $statut_key,
                __($this->statut[$statut_key]),
                $statut_key == array_keys($this->statut)[count($this->statut)-1] ? 'END' : ''
            );
        }
        return $select_composed.")";
    }

    /**
     * Permet de composer le CASE WHEN qui remplace les valeurs
     * du champ etat par sa traduction.
     * Cette méthode est principalement utilisée pour la construction du listing.
     */
    function compose_etat_reprise_trad() {
        $select_composed = "(";
        foreach ($this->etat as $etat_key => $etat_trad) {
            $select_composed .= sprintf(
                '%1$s WHEN 
                    reprise_abandon.etat = \'%2$s\'
                THEN
                    \'%3$s\'
                %4$s
                ',
                $etat_key == array_keys($this->etat)[0] ? 'CASE' : '',
                $etat_key,
                __($this->etat[$etat_key]),
                $etat_key == array_keys($this->etat)[count($this->etat)-1] ? 'END' : ''
            );
        }
        return $select_composed.")";
    }

    /**
     * SETTER_FORM - setLayout.
     *
     * @return void
     */
    function setLayout(&$form, $maj) {
        parent::setLayout($form, $maj);

        $bloc_global_width_full = "span12";
        $bloc_global_width_half = "span6";

        //// CONTAINER GLOBAL - START
        $this->form->setBloc("reprise_abandon", "D", "", "emplacement-form reprise_abandon-form form-action-".$maj);

            $this->form->setBloc("statut", "D", __("État D'avancement"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos ");
                $this->form->setBloc("statut", "D","", "group span6");
                $this->form->setBloc("datecreation", "F");
            $this->form->setBloc("datecreation", "F");

            $this->form->setBloc("effectuee", "DF", __("1 - Phase Préliminaire"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos ");

            $this->form->setBloc("dateconvocationvisite1", "DF", __("2 - Convocation Première Visite"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos ");

            $this->form->setBloc("dateconstatpv1", "D", __("3 - Rédaction PV1"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos ");
            $this->form->setBloc("personnepresentepv1", "F");

            $this->form->setBloc("datenotificationpv1", "D", __("4 - Notification/Affichage PV1"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos ");
                $this->form->setBloc("debutaffichage1pv1", "D", __("Période D'affichage"), 'span12 reprise-abandon-periode-affichage');
                    $this->form->setBloc("debutaffichage1pv1", "D","", 'group span6');
                    $this->form->setBloc("finaffichage1pv1", "F");
                    $this->form->setBloc("debutaffichage2pv1", "D","", 'group span6');
                    $this->form->setBloc("finaffichage2pv1", "F");
                    $this->form->setBloc("debutaffichage3pv1", "D","", 'group span6');
                    $this->form->setBloc("finaffichage3pv1", "F");
                $this->form->setBloc("finaffichage3pv1", "F");
            $this->form->setBloc("finaffichage3pv1", "F");

            $this->form->setBloc("dateconvocationvisite2", "DF", __("5 - Convocation Deuxième Visite"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos ");

            $this->form->setBloc("dateconstatpv2", "D", __("6 - Redaction PV2"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos ");
            $this->form->setBloc("personnepresentepv2", "F");

            $this->form->setBloc("datenotificationpv2", "D", __("7 - Notification/Affichage PV2"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos ");
                $this->form->setBloc("debutaffichagepv2", "D", __("Période D'affichage"), 'span12 reprise-abandon-periode-affichage');
                    $this->form->setBloc("debutaffichagepv2", "D","", 'group span6');
                    $this->form->setBloc("finaffichagepv2", "F");
                $this->form->setBloc("finaffichagepv2", "F");
            $this->form->setBloc("finaffichagepv2", "F");

            $this->form->setBloc("arrete", "DF", __("8 - Délibération"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos ");
        //// CONTAINER GLOBAL - END
        $this->form->setBloc("arrete", "F");
    }


    /**
     * MERGE_FIELDS - get_values_merge_fields.
     *
     * @return array
     */
    function get_values_merge_fields() {
        $values = parent::get_values_merge_fields();
        //
        $values["reprise_abandon.etat"] = $this->etat[$this->getVal("etat")];
        $values["reprise_abandon.statut"] = $this->statut[$this->getVal("statut")];

        return $values;
    }
}