<?php
//$Id$ 
//gen openMairie le 09/10/2023 11:34

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

class reprise_abandon extends reprise_abandon_gen {

    var $etat = array();
    var $statut = array();

    /**
     * Définition des actions disponibles sur la classe.
     * 
     * - Ajout de la view directlink.
     * - Ajout de la view advancedsearch.
     * 
     * @return void
     */
    function init_class_actions() {
        parent::init_class_actions();

        // ACTION - 701 - direct_link
        $this->class_actions[701] = array(
            "identifier" => "direct_link",
            "permission_suffix" => "tab",
            "view" => "view__direct_link",
        );

        // ACTION - 601 - listing_advs
        $this->class_actions[601] = array(
            "identifier" => "listing_advs",
            "permission_suffix" => "tab",
            "view" => "view__listing_advs",
        );
    }

    /**
     * VIEW - view__direct_link.
     *
     * Redirige vers la fiche de la DOT dans le contexte de l'établissement.
     * 
     * - 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();

        $inst_emplacement = $this->f->get_inst__om_dbform(array(
            "obj" => "emplacement",
            "idx" => $this->getVal('emplacement'),
        ));



        $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,
            $inst_emplacement->getVal('nature'),
            $this->getVal("emplacement"),
            $this->getVal($this->clePrimaire)
        );
        header('Location: '.$redirect_url);
        return;
    }

    /**
     * VIEW - view__listing_advs
     * 
     * Redirige l'utilisateur vers le listing avec la sélection souhaitée
     * sur la recherche avancée.
     * 
     * - Ajout de la view advancedsearch.
     *
     * Exemple d'appel : ../app/index.php?module=form&obj=reprise_abandon&action=601&filtre=date_limite_reponse&filtre=date_fin_validite&filtre=sans_avis.
     *
     * @return void
     */
    function view__listing_advs() {
        $this->checkAccessibility();
        $obj = $this->get_absolute_class_name();

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

        // Gestion des arguments <champ_date>._min ou <champ_date>.max
        $champs_date_possibles = array(
            'date_envoi_convo_1',
            'date_notification_pv1',
            'date_envoi_convo_2',
            'date_notification_pv2',
        );

        // Gestion des arguments <avis>
        $champs_possibles = array(
            'statut',
            'etat'
        );

        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)));
                }
            }
        }

        // On ajoute à l'url les avis et leurs valeurs si elle existe lors de la redirection vers la recherche avancée des DOT
        foreach ($champs_possibles as $champ) {
            if ($this->f->get_submitted_get_value($champ) != NULL) {
                $search[$champ] = $this->f->get_submitted_get_value($champ);
            }
        }
        
        $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;
    }


    /**
     *
     */
    function __construct($id, &$dnu1 = null, $dnu2 = null) {
        $this->constructeur($id);
        // Récupération des paramètres nécessaires aux calculs de taille et de
        // durée
        $this->init_parameters();
    }

    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"),
        );
    }

    /**
     *
     * @return array
     */
    function get_var_sql_forminc__champs() {
        return array(
            "reprise_abandon",
            "emplacement",
            "statut",
            "etat",
            "phase_preli",
            "date_envoi_convo_1",
            "date_constat_pv1",
            "heure_constat_pv1",
            "constat_pv1",
            "personnes_presentes_pv1",
            "date_notification_pv1",
            "date_pv1_affichage_1_debut",
            "date_pv1_affichage_1_fin",
            "date_pv1_affichage_2_debut",
            "date_pv1_affichage_2_fin",
            "date_pv1_affichage_3_debut",
            "date_pv1_affichage_3_fin",
            "date_envoi_convo_2",
            "date_constat_pv2",
            "heure_constat_pv2",
            "constat_pv2",
            "personnes_presentes_pv2",
            "date_notification_pv2",
            "date_pv2_affichage_debut",
            "date_pv2_affichage_fin",
            "arrete",
        );
    }

    /**
     * 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);

            //// LIGNE ID - START
            $this->form->setBloc("reprise_abandon", "D", "", "row-fluid");
                //// BLOC ID - START
                $this->form->setBloc("reprise_abandon", "D", "", $bloc_global_width_full." emplacement-bloc emplacement-bloc-identification ");
                    //// BLOC ID - START
                    $this->form->setBloc("reprise_abandon", "D", "", "span11");
                    $this->form->setBloc("emplacement", "F");
                    //// BLOC ID - END
                $this->form->setBloc("emplacement", "F");
                //// BLOC ID - END
            $this->form->setBloc("emplacement", "F");
            // Container État d'avancement
            $this->form->setBloc("statut", "D", "", "row-fluid");
                $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("etat", "F");
                $this->form->setBloc("etat", "F");
            $this->form->setBloc("etat", "F");

            //// BLOC Phase préli - START
            $this->form->setBloc("phase_preli", "D", "", "row-fluid");
                $this->form->setBloc("phase_preli", "D", __("1 - Phase préliminaire"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos");
                    $this->form->setBloc("phase_preli", "DF", "", "span6");
                $this->form->setBloc("phase_preli", "F");
            $this->form->setBloc("phase_preli", "F");
            //// BLOC Phase préli - END

            // Container étape 2 
            $this->form->setBloc("date_envoi_convo_1", "D", "", "row-fluid");
                $this->form->setBloc("date_envoi_convo_1", "D", __("2 - Convocation première visite"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos");
                   $this->form->setBloc("date_envoi_convo_1", "DF", "", "span6");
                $this->form->setBloc("date_envoi_convo_1", "F");
            $this->form->setBloc("date_envoi_convo_1", "F");

            // Container étape 3 
            $this->form->setBloc("date_constat_pv1", "D", "", "row-fluid");
                $this->form->setBloc("date_constat_pv1", "D", __("3 - Rédaction PV1"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos");
                   $this->form->setBloc("date_constat_pv1", "D", "", "span6");
                   $this->form->setBloc("personnes_presentes_pv1", "F", "", "");
                $this->form->setBloc("personnes_presentes_pv1", "F");
            $this->form->setBloc("personnes_presentes_pv1", "F");

            // Container étape 4
            $this->form->setBloc("date_notification_pv1", "D", "", "row-fluid");
                $this->form->setBloc("date_notification_pv1", "D", __("4 - Notification/affichage PV1"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos");
                   $this->form->setBloc("date_notification_pv1", "DF", "", "span11");               
                   $this->form->setBloc("date_pv1_affichage_1_debut", "D", __("Périodes d'affichage"), "span11");
                       $this->form->setBloc("date_pv1_affichage_1_debut", "D", "", "row-fluid group");
                       $this->form->setBloc("date_pv1_affichage_1_fin", "F", "", "");
                       $this->form->setBloc("date_pv1_affichage_2_debut", "D", "", "row-fluid group");
                       $this->form->setBloc("date_pv1_affichage_2_fin", "F", "", "");
                       $this->form->setBloc("date_pv1_affichage_3_debut", "D", "", "row-fluid group");
                       $this->form->setBloc("date_pv1_affichage_3_fin", "F", "", "");
                    $this->form->setBloc("date_pv1_affichage_3_fin", "F");
                $this->form->setBloc("date_pv1_affichage_3_fin", "F");
            $this->form->setBloc("date_pv1_affichage_3_fin", "F");


            // Container étape 5
            $this->form->setBloc("date_envoi_convo_2", "D", "", "row-fluid");
                $this->form->setBloc("date_envoi_convo_2", "D", __("5 - Convocation deuxième visite"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos");
                   $this->form->setBloc("date_envoi_convo_2", "DF", "", "row-fluid span11");
                $this->form->setBloc("date_envoi_convo_2", "F");
            $this->form->setBloc("date_envoi_convo_2", "F");

            // Container étape 6 
            $this->form->setBloc("date_constat_pv2", "D", "", "row-fluid");
                $this->form->setBloc("date_constat_pv2", "D", __("6 - Rédaction PV2"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos");
                   $this->form->setBloc("date_constat_pv2", "D", "", "row-fluid span11");
                   $this->form->setBloc("personnes_presentes_pv2", "F", "", "");
                $this->form->setBloc("personnes_presentes_pv2", "F");
            $this->form->setBloc("personnes_presentes_pv2", "F");

            $this->form->setBloc("date_notification_pv2", "D", "", "row-fluid");
                $this->form->setBloc("date_notification_pv2", "D", __("7 - Notification/affichage PV2"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos");
                   $this->form->setBloc("date_notification_pv2", "DF", "", "span11");               
                   $this->form->setBloc("date_pv2_affichage_debut", "D", __("Période"), "span11");
                       $this->form->setBloc("date_pv2_affichage_debut", "D", "", "row-fluid group");
                       $this->form->setBloc("date_pv2_affichage_fin", "F", "", "");
                    $this->form->setBloc("date_pv2_affichage_fin", "F");
                $this->form->setBloc("date_pv2_affichage_fin", "F");
            $this->form->setBloc("date_pv2_affichage_fin", "F");

            $this->form->setBloc("arrete", "D", "", "row-fluid");
                $this->form->setBloc("arrete", "D", __("8 - Délibération"), $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos");
                   $this->form->setBloc("arrete", "DF", "", "row-fluid span11");
                $this->form->setBloc("arrete", "F");
            $this->form->setBloc("arrete", "F");

        $this->form->setBloc("arrete", "F");


    }

    /**
     * SETTER_FORM - setLib.
     *
     * @return void
     */
    function setLib(&$form, $maj) {
        parent::setLib($form, $maj);
        //
        $form->setLib($this->clePrimaire, __("id"));
        $form->setLib('phase_preli', __("Effectuée"));

        $form->setLib('date_envoi_convo_1', __("Envoi/Affichage Convocation"));
        $form->setLib('date_constat_pv1', __('Date constat'));
        $form->setLib('heure_constat_pv1', __('Heure constat'));
        $form->setLib('constat_pv1', __('Description'));
        $form->setLib('personnes_presentes_pv1', __('Personnes présentes'));
        $form->setLib('date_notification_pv1', __('Date de notification'));
        $form->setLib('date_pv1_affichage_1_debut', __('Début Affichage 1'));
        $form->setLib('date_pv1_affichage_1_fin', __('Fin Affichage 1 '));
        $form->setLib('date_pv1_affichage_2_debut', __('Début Affichage 2'));
        $form->setLib('date_pv1_affichage_2_fin', __('Fin Affichage 2'));
        $form->setLib('date_pv1_affichage_3_debut', __('Début Affichage 3'));
        $form->setLib('date_pv1_affichage_3_fin', __('Fin Affichage 3'));
        $form->setLib('date_envoi_convo_2', __("Envoi/Affichage convocation"));
        $form->setLib('date_constat_pv2', __('Date constat'));
        $form->setLib('heure_constat_pv2', __('Heure constat'));
        $form->setLib('constat_pv2', __('Description'));
        $form->setLib('personnes_presentes_pv2', __('Personnes présentes'));
        $form->setLib('date_notification_pv2', __('Date de notification'));
        $form->setLib('date_pv2_affichage_debut', __('Début Affichage'));
        $form->setLib('date_pv2_affichage_fin', __('Fin Affichage'));
        $form->setLib('arrete', __('Arrêté'));
    }

    /**
     * SETTER_FORM - setType.
     *
     * @return void
     */
    function setType(&$form, $maj) {
        parent::setType($form, $maj);
        // Affichage de l'id de la table dans tous les cas
        $form->setType('contrat', 'hiddenstatic');
        // $form->setType("dateterme", "hiddenstaticdate");
        //
        if ($this->is_in_context_of_foreign_key("emplacement", $this->getParameter("retourformulaire"))) {
            $form->setType('emplacement', 'hidden');
        } else {
            if ($maj == 0 || $maj == 1) {
                $form->setType('emplacement', 'autocomplete');
            }
            if ($maj == 3){
                $form->setType('emplacement', 'link');
            }
        }

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

        if ($maj == 3) {
            $form->setType('statut', 'selecthiddenstatic');
            $form->setType('etat', 'selecthiddenstatic');
        }
    }

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

        $form->setSelect("statut", $contenu_statut);

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

    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.")";
    }

    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 - set_form_default_values (setVal).
     *
     * @return void
     */
    function set_form_default_values(&$form, $maj, $validation) {
        parent::set_form_default_values($form, $maj, $validation);
        //
        if ($validation == 0) {
            if ($maj == 0) {
                $form->setVal("reprise_abandon", "[...]");
                $form->setVal('etat', 1);
                $form->setVal('statut', 'en_cours');
                $form->setVal('heure_constat_pv1', '00:00:00');
                $form->setVal('heure_constat_pv2', '00:00:00');
            }
        }
    }

}
