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

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

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

    var $foreign_keys_extended = array(
        "nature_defunt" => array("nature_defunt", ),
        "titre_de_civilite" => array("titre_de_civilite", ),
    );

    /**
     * Définition des actions disponibles sur la classe.
     *
     * @return void
     */
    function init_class_actions() {
        parent::init_class_actions();
        // ACTION - 001 - modifier
        // -> modification impossible
        unset($this->class_actions[1]);
        // ACTION - 002 - supprimer
        // -> suppression impossible
        unset($this->class_actions[2]);
        // ACTION - 003 - consulter
        // Ajout de la vérification de collectivité
        $this->class_actions[3]['condition'][] = "is_user_from_allowed_collectivite";
    }

    /**
     * Retourne la collectivité du défunt archivé.
     * 
     * Surcharge de la méthode parent pour récupérer la collectivité
     * via une jointure SQL avec l'emplacement archivé.
     *
     * @return int|null L'identifiant de la collectivité ou null
     */
    function get_collectivite() {
        // Si le défunt archivé n'existe pas, on retourne null
        if ($this->exists() !== true) {
            return null;
        }
        
        // Récupération de la collectivité via jointure avec emplacement_archive
        $sql = sprintf(
            "SELECT emplacement_archive.om_collectivite
            FROM %sdefunt_archive
            INNER JOIN %semplacement_archive ON defunt_archive.emplacement = emplacement_archive.emplacement
            WHERE defunt_archive.defunt = %s",
            DB_PREFIXE,
            DB_PREFIXE,
            intval($this->getVal($this->clePrimaire))
        );
        
        $res = $this->f->db->getOne($sql);
        $this->addToLog(__METHOD__."(): db->getOne(\"".$sql."\");", VERBOSE_MODE);
        
        if ($this->f->isDatabaseError($res, true)) {
            return null;
        }
        
        return $res;
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_nature() {
        return "SELECT nature_defunt.nature_defunt, nature_defunt.libelle FROM ".DB_PREFIXE."nature_defunt WHERE ((nature_defunt.om_validite_debut IS NULL AND (nature_defunt.om_validite_fin IS NULL OR nature_defunt.om_validite_fin > CURRENT_DATE)) OR (nature_defunt.om_validite_debut <= CURRENT_DATE AND (nature_defunt.om_validite_fin IS NULL OR nature_defunt.om_validite_fin > CURRENT_DATE))) ORDER BY nature_defunt.libelle ASC";
    }

    /**
     *
     * @return string
     */
    function get_var_sql_forminc__sql_nature_by_id() {
        return "SELECT nature_defunt.nature_defunt, nature_defunt.libelle FROM ".DB_PREFIXE."nature_defunt WHERE nature_defunt = <idx>";
    }

    /**
     * SETTER_FORM - setType.
     *
     * @return void
     */
    function setType(&$form, $maj) {
        parent::setType($form, $maj);
        //
        // MODE AJOUTER
        if ($maj == 0) {
            $form->setType("nature", "select");
        }

        // MDOE MODIFIER
        if ($maj == 1) {
            $form->setType("nature", "select");
        }

        // MODE SUPPRIMER
        if ($maj == 2) {
            $form->setType("nature", "selecthiddenstatic");
        }

        // MODE CONSULTER
        if ($maj == 3) {
            $form->setType("nature", "selectstatic");
            $form->setType('historique', 'jsontotab');
        }
    }
    /**
     *
     */
    function setSelect(&$form, $maj, &$dnu1 = null, $dnu2 = null) {
        parent::setSelect($form, $maj);
        // nature
        $this->init_select(
            $form, 
            $this->f->get_db_reference(),
            $maj,
            null,
            "nature",
            $this->get_var_sql_forminc__sql("nature"),
            $this->get_var_sql_forminc__sql("nature_by_id"),
            true
        );
    }
}
