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

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

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

    /**
     *
     */
    function __construct($id, &$dnu1 = null, $dnu2 = null) {
        $this->constructeur($id);
        // XXX Modifier la base de données en conséquence ?
        $this->required_field[] = "lettretype";
    }
    /**
     * Définition des actions disponibles sur la classe.
     *
     * @return void
     */
    function init_class_actions() {
        parent::init_class_actions();
        // ACTION - 031 - edition pdf
        // Retourne le pdf généré
        $this->class_actions[31] = array(
            "identifier" => "pdf-edition",
            "portlet" => array(
                "type" => "action-blank",
                "libelle" => __("édition pdf"),
                "order" => 120,
                "class" => "pdf-16",
            ),
            "view" => "view_pdf_edition",
            "permission_suffix" => "pdf_edition",
        );
    }

    /**
     *
     * @return array
     */
    function get_var_sql_forminc__champs() {
        return array(
            "courrier",
            "emplacement",
            "destinataire",
            "datecourrier",
            "lettretype",
            "complement",
        );
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_destinataire() {
        // Filtre des destinataires par emplacement
        $filter = "";
        if ($this->getParameter("idxformulaire") != "") {
            $filter = " WHERE emplacement=".intval($this->getParameter("idxformulaire"))." ";
        }
        return "SELECT autorisation.autorisation, coalesce(nom, '-')||' '||coalesce(prenom, '-') as nom FROM ".DB_PREFIXE."autorisation ".$filter." ORDER BY nom";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_om_lettretype() {
        return "SELECT om_lettretype.id, (om_lettretype.id||' '||om_lettretype.libelle) as libelle FROM ".DB_PREFIXE."om_lettretype WHERE om_lettretype.actif is TRUE ORDER BY id";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_om_lettretype_by_id() {
        return "SELECT om_lettretype.id, (om_lettretype.id||' '||om_lettretype.libelle) as libelle FROM ".DB_PREFIXE."om_lettretype WHERE om_lettretype.id='<idx>'";
    }

    function setType(&$form, $maj) {
        //
        parent::setType($form, $maj);
        // Affichage de l'id de la table dans tous les cas
        $form->setType('courrier', 'hiddenstatic');
        // On cache l'id de l'emplacement dans tous les cas
        $form->setType('emplacement', 'hidden');
        //
        if ($maj == 0 || $maj == 1) {
            $form->setType('lettretype', 'select');
            $form->setType('complement', 'html');
            
        }
        //
        if ($maj == 3) {
            $form->setType('lettretype', 'selectstatic');
        }
    }

    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('courrier', "[...]");
                // 
                $form->setVal('emplacement', $idxformulaire);
                //
                $form->setVal('datecourrier', date('Y-m-d'));
            }
        }
    }

    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {
        parent::setSelect($form, $maj);
        //
        $this->init_select(
            $form,
            $this->f->db,
            $maj,
            null,
            "lettretype",
            $this->get_var_sql_forminc__sql("om_lettretype"),
            $this->get_var_sql_forminc__sql("om_lettretype_by_id"),
            false
        );
    }

    function setLib(&$form, $maj) {
        //
        parent::setLib($form, $maj);
        //
        $form->setLib('courrier', __("id"));
    }

    function setLayout(&$form, $maj) {
        //
        $bloc_global_width_full = "span12";
        $bloc_global_width_half = "span6";

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

        //// LIGNE ID - START
        $this->form->setBloc("courrier", "D", "", "row-fluid");
            //// BLOC ID - START
            $this->form->setBloc("courrier", "D", "", $bloc_global_width_full." emplacement-bloc emplacement-bloc-identification ");
                //// BLOC ID - START
                $this->form->setBloc("courrier", "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

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

    /**
     * VIEW - view_pdf_edition.
     *
     * @return void
     */
    function view_pdf_edition() {
        $this->checkAccessibility();
        // Génération du PDF
        $pdfedition = $this->compute_pdf_output(
            "lettretype",
            $this->getVal("lettretype"),
            null,
            $this->getVal($this->clePrimaire)
        );
        // Affichage du PDF
        $this->expose_pdf_output(
            $pdfedition['pdf_output'],
            $pdfedition['filename']
        );
    }
}
