<?php
/**
 * Ce script définit la classe 'travaux'.
 *
 * @package opencimetiere
 * @version SVN : $Id$
 */

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

/**
 * Définition de la classe 'travaux' (om_dbform).
 */
class travaux extends travaux_gen {

    /**
     * GETTER_FORMINC - champs.
     *
     * @return array
     */
    function get_var_sql_forminc__champs() {
        return array(
            "travaux",
            "emplacement",
            "entreprise",
            "datedebinter",
            "datefininter",
            "naturedemandeur",
            "naturetravaux",
            "travaux.demandeur",
            "travaux.observation",
        );
    }

    function get_var_sql_forminc__sql_demandeur_by_emplacement() {
        return sprintf(
            "SELECT 
                autorisation, 
                CONCAT_WS(' ',
                    '(' || 
                        CASE WHEN nature IS NOT NULL AND nature = 'ayantdroit'
                            THEN 
                                'ayant-droit' 
                            ELSE
                                nature 
                        END
                    || ')',
                    titre_de_civilite.libelle,                    
                    CASE WHEN autorisation.marital IS NOT NULL AND autorisation.marital != ''
                        THEN 
                            autorisation.marital 
                        ELSE
                            autorisation.nom 
                    END, 
                    prenom)
                FROM
                    %1\$sautorisation
                    LEFT JOIN %1\$stitre_de_civilite 
                        ON autorisation.titre = titre_de_civilite.titre_de_civilite
                WHERE
                    emplacement = <id_emplacement>",
            DB_PREFIXE
        );
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_demandeur_by_id() {
        return sprintf(
            "SELECT 
                autorisation, 
                CONCAT_WS(' ',
                    '(' || 
                        CASE WHEN nature IS NOT NULL AND nature = 'ayantdroit'
                            THEN 
                                'ayant-droit' 
                            ELSE
                                nature 
                        END
                    || ')',
                    titre_de_civilite.libelle,                    
                    CASE WHEN autorisation.marital IS NOT NULL AND autorisation.marital != ''
                        THEN 
                            autorisation.marital 
                        ELSE
                            autorisation.nom 
                    END, 
                    prenom)
                FROM
                    %1\$sautorisation
                    LEFT JOIN %1\$stitre_de_civilite 
                        ON autorisation.titre = titre_de_civilite.titre_de_civilite
                WHERE
                    autorisation = <idx>
            ",
            DB_PREFIXE
        );
    }

    /**
     * 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('travaux', "hiddenstatic");
        // On cache l'id de l'emplacement dans tous les cas
        $form->setType('emplacement', 'hidden');
        $form->setType('demandeur', 'selectstatic');
        if ($maj == 0 || $maj == 1) {
            //
            $form->setType('naturedemandeur', 'select');
            $form->setType("demandeur", "select");
        }
    }

    /**
     * SETTER_FORM - setValsousformulaire (setVal).
     *
     * @return void
     */
    function setValsousformulaire(&$form, $maj, $validation, $idxformulaire, $retourformulaire, $typeformulaire, &$dnu1 = null, $dnu2 = null) {
        parent::setValsousformulaire($form, $maj, $validation, $idxformulaire, $retourformulaire, $typeformulaire);
        //
        if ($validation == 0) {
            //
            if ($maj == 0) {
                // Affichage de l'id de la table avec la valeur [...]
                $form->setVal('travaux', "[...]");
                //
                $form->setVal('emplacement', $idxformulaire);
            }
        }
    }

    /**
     * SETTER_FORM - setSelect.
     *
     * @return void
     */
    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {
        parent::setSelect($form, $maj);
        // Filtre par emplacement pour demandeur
        $this->init_select(
            $form, 
            $this->f->db,
            $maj,
            null,
            "demandeur",
            str_replace(
                '<id_emplacement>',
                $this->getParameter('idxformulaire'),
                $this->get_var_sql_forminc__sql("demandeur_by_emplacement")
            ),
            $this->get_var_sql_forminc__sql("demandeur_by_id"),
            false
        );
        //
        if($maj<2){
            $contenu=array();
            $contenu[0]=array('Concessionnaire','Ayant droit','Autre');
            $contenu[1]=array(__('concessionnaire'),
                              __('ayantdroit'),
                              __('autre'));
            $form->setSelect("naturedemandeur",$contenu);
        }
    }

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

    /**
     * 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("travaux", "D", "", "emplacement-form travaux-form form-action-".$maj);

        //// LIGNE ID - START
        $this->form->setBloc("travaux", "D", "", "row-fluid");
            //// BLOC ID - START
            $this->form->setBloc("travaux", "D", "", $bloc_global_width_full." emplacement-bloc emplacement-bloc-identification ");
                //// BLOC ID - START
                $this->form->setBloc("travaux", "D", "", "span12");
                $this->form->setBloc("emplacement", "F");
                //// BLOC ID - END
            $this->form->setBloc("emplacement", "F");
            //// BLOC ID - END
        $this->form->setBloc("emplacement", "F");
        //// LIGNE ID - END

        //// LIGNE TRAVAUX - START
        $this->form->setBloc("entreprise", "D", "", "row-fluid");
            //// BLOC TRAVAUX - START
            $this->form->setBloc("entreprise", "D", "", $bloc_global_width_full." emplacement-bloc emplacement-bloc-infos ");
                //// LIGNE TRAVAUX - START
                $this->form->setBloc("entreprise", "D", "", "row-fluid");
                    //// BLOC TRAVAUX - START
                    $this->form->setBloc("entreprise", "D", "", $bloc_global_width_half);
                        //// LIGNE TRAVAUX - START
                        $this->form->setBloc("entreprise", "D", "", "row-fluid");
                            //// BLOC TRAVAUX - START
                            $this->form->setBloc("entreprise", "D", __("travaux"), " emplacement-bloc-personne span12");
                                $this->form->setBloc("entreprise", "D", "", "span12");
                                $this->form->setBloc("demandeur", "F");
                            $this->form->setBloc("demandeur", "F");
                            //// BLOC TRAVAUX - END
                        $this->form->setBloc("demandeur", "F");
                        //// LIGNE TRAVAUX - END
                    $this->form->setBloc("demandeur", "F");
                    //// BLOC TRAVAUX - END
                $this->form->setBloc("demandeur", "F");
                //// LIGNE TRAVAUX - END
            $this->form->setBloc("demandeur", "F");
            //// BLOC TRAVAUX - END
        $this->form->setBloc("demandeur", "F");
        //// LIGNE TRAVAUX- END

        //// LIGNE OBSERVATION - START
        $this->form->setBloc("observation", "D", "", "row-fluid");
            //// BLOC OBSERVATION - START
            $this->form->setBloc("observation", "D", "", $bloc_global_width_full." emplacement-bloc emplacement-bloc-observation ");
                $this->form->setBloc("observation", "DF", "", "group span12");
            $this->form->setBloc("observation", "F");
            // BLOC OBSERVATION - END
        $this->form->setBloc("observation", "F");
        //// LIGNE OBSERVATION - START

        //// CONTAINER GLOBAL - END
        $this->form->setBloc("observation", "F");
    }

    /**
     * MERGE_FIELDS - get_labels_merge_fields.
     *
     * @return array
     */
    function get_labels_merge_fields() {
        $labels = parent::get_labels_merge_fields();
        $labels[_("travaux")]["travaux.demandeur"] = __("identité du demandeur");
        $date_fields_to_format = array("datedebinter", "datefininter",);
        foreach ($date_fields_to_format as $date_field_to_format) {
            $labels[$this->table][$this->table.".".$date_field_to_format."_format_jj_mois_aaaa"] = $labels[$this->table][$this->table.".".$date_field_to_format]." (Format : 14 janvier 1978)";
        }
        return $labels;
    }

    /**
     * MERGE_FIELDS - get_values_merge_fields.
     *
     * @return array
     */
    function get_values_merge_fields() {
        $values = parent::get_values_merge_fields();
        // Demandeur
        $inst_autorisation = $this->f->get_inst__om_dbform(array(
            "obj" => "autorisation",
            "idx" => $this->getVal("demandeur"),
        ));
        $inst_titre_civilite = $this->f->get_inst__om_dbform(array(
            "obj" => "titre_de_civilite",
            "idx" => $inst_autorisation->getVal("titre"),
        ));
        $display = '';
        $display .= $inst_titre_civilite->getVal("libelle")." ";
        $display .= $inst_autorisation->getVal("prenom")." ";
        if ($inst_autorisation->getVal("marital") != '' ) {
            $display .= $inst_autorisation->getVal("marital")." né(e) ".$inst_autorisation->getVal("nom");
        }
        else {
            $display .= $inst_autorisation->getVal("nom");
        }
        $values["travaux.demandeur"] = $display;
        // Nature travaux
        $inst_travaux_nature = $this->f->get_inst__om_dbform(array(
            "obj" => "travaux_nature",
            "idx" => $this->getVal("naturetravaux"),
        ));
        $values["travaux.naturetravaux"] = $inst_travaux_nature->getVal("libelle");
        // Dates de début et de fin d'intervention
        $date_fields_to_format = array("datedebinter", "datefininter",);
        foreach ($date_fields_to_format as $date_field_to_format) {
            $values[$this->table.".".$date_field_to_format."_format_jj_mois_aaaa"] = '';
            $date_value_as_string = $this->getVal($date_field_to_format);
            if (!empty($date_value_as_string)
                && preg_match('|^[0-9]{4}-[0-9]{2}-[0-9]{2}$|', $date_value_as_string)) {
                $date_value_as_object = DateTime::createFromFormat('Y-m-d', $date_value_as_string);
                $values[$this->table.".".$date_field_to_format."_format_jj_mois_aaaa"] = strftime('%d %B %Y', $date_value_as_object->getTimestamp());
            }
        }
        return $values;
    }

}
