<?php

class Mention
{
    /**
     * @var Modalite $modalite
     */
    private $modalite;
    /**
     * @var string $code
     */
    private $code;

    /**
     * @var string TypeMention
     */
    private $typeMention;

    /**
     * @var array $familleActes
     */
    private $famillesActe;

    private $cdcIsValid;

    private $nbActesTotal;


    public function __construct($modalite, $typeMention)
    {
        $this->code = $modalite->code . $typeMention;
        $this->modalite = $modalite;
        $this->typeMention = $typeMention;
        $this->famillesActe = array();
        $this->cdcIsValid = false;
        $this->nbActesTotal = 0;
    }

    /**
     * @return string
     */
    public function getCode()
    {
        return $this->code;
    }


    /**
     * @return Modalite
     */
    public function getModalite()
    {
        return $this->modalite;
    }


    /**
     * @return string
     */
    public function getTypeMention()
    {
        return $this->typeMention;
    }


    /**
     * @param $iCTI_connexion
     *
     * @return array
     */
    public function getFamillesActes($iCTI_connexion)
    {
        if (!$this->famillesActe) {$famillesActe = array();

            $modaliteCode = $this->modalite->getCode();
            $sql = <<<SQL
            SELECT
                t_etude_cardio_familles_actes.oid,
                t_etude_cardio_familles_actes.code
             FROM pmsi.t_etude_cardio_familles_actes
             JOIN pmsi.t_etude_cardio_mentions ON t_etude_cardio_mentions.oid = t_etude_cardio_familles_actes.cardio_mention_id
             JOIN pmsi.t_etude_cardio_modalites ON t_etude_cardio_modalites.oid = t_etude_cardio_mentions.cardio_modalite_id
            WHERE  t_etude_cardio_modalites.code = '$modaliteCode' AND t_etude_cardio_mentions.type like '$this->typeMention'
            ORDER BY 1
            ;
SQL;
            $result = $iCTI_connexion->query($sql);
            while ($row = $iCTI_connexion->fetchAssoc($result)) {
                $famillesActe[$row['code']] = $row['oid'];
            }
            $this->famillesActe = $famillesActe;
        }

        return $this->famillesActe;
    }

    public function getNbActes($iCTI_connexion, $year)
    {

        $count = 0;
        $modaliteCode = $this->modalite->getCode();

        $sql = <<<SQL
            SELECT
                count(acte_id)
            FROM pmsi.p_rss_cardio
            JOIN pmsi.t_etude_cardio_mentions ON p_rss_cardio.cardio_mention_id = t_etude_cardio_mentions.oid          
	        JOIN pmsi.t_etude_cardio_modalites ON t_etude_cardio_modalites.oid = t_etude_cardio_mentions.cardio_modalite_id
	        WHERE  t_etude_cardio_modalites.code = '$modaliteCode' AND t_etude_cardio_mentions.type like '$this->typeMention' 
	        AND EXTRACT('Year' From date_acte) like '$year'
	        ORDER BY 1;
	
SQL;
        $result = $iCTI_connexion->query($sql);
        $record = $iCTI_connexion->fetchAssoc($result);

        if ($record) {
            $count = $record['count'];
        }
        return $count;
    }

    /**
     * @return int
     */
    public function getNbActesTotal()
    {
        return $this->nbActesTotal;
    }

    /**
     * @param int $nbActesTotal
     */
    public function setNbActesTotal($nbActesTotal)
    {
        $this->nbActesTotal = $nbActesTotal;
    }

    /**
     * @return false
     */
    public function getCdcIsValid()
    {
        return $this->cdcIsValid;
    }

    /**
     * @param boolean $cdcIsValid
     */
    public function setCdcIsValid($cdcIsValid)
    {
        $this->cdcIsValid = $cdcIsValid;
    }


}