<?php
class Blog_Model_ArticleMonthArchive extends Blog_Model_Model
{
    private $_month;
    private $_year;
    private $_articleCount;
    private $_dayStats;

    public function setMonth($month)
    {
        $this->_month=$month;
    }

    public function getMonth()
    {
        return $this->_month;
    }

    public function setYear($year)
    {
        $this->_year=$year;
    }

    public function getYear()
    {
        return $this->_year;
    }

    public function getArticleCount()
    {
        if (!isset($this->_articleCount)) {
            $this->_articleCount = $this->getDao()->getArchiveItemsCount($this->_year,$this->_month);
        }
        return $this->_articleCount;
    }

    protected function _setDefaultDao()
    {
        $this->setDao(new Blog_Model_Dao_Article);
    }

    /**
     * Returns the array with ArticleMonthArchive objects for all months,
     * which have at least one article.
     * @return array
     */
    public function getMonthStats()
    {
        $allMonthStat=$this->getDao()->getMonthStat();

        return $this->_getSet($allMonthStat);
    }

    private function _makeDayStats()
    {
        $monthArticles=$this->getDao()->getArchiveItems($this->_year,$this->_month,
            null,null,null,
            array("id","title","day"=>"DAY(posted)")
        );

        //number of days in month
        $days=date('t',mktime(0, 0, 0, $this->_month, 1, $this->_year));

        for($i=1;$i<($days+1);$i++) {
            if (!count($dayRows=self::_getDayRows($monthArticles,$i))) {
                $this->_dayStats[$i]=null;
            } else {
                $dayArchive = new Blog_Model_ArticleDayArchive($this->_year,$this->_month,$i);
                foreach($dayRows as $dRow) {
                    $article = new Blog_Model_Article();
                    $article->setId($dRow["id"]);
                    $article->setTitle($dRow["title"]);
                    $dayArchive->addArticle($article);
                }
                $this->_dayStats[$i]=$dayArchive;
            }
        }
    }

    public function getDayArchive($day)
    {
        if (!isset($this->_dayStats)) $this->_makeDayStats();
        return $this->_dayStats[$day];
    }

    /**
     * Returns next month with one article at least
     */
    public function getNextMonthStat()
    {
        $daoRow = $this->getDao()->getNextMonthStat($this->_year,$this->_month);
        if (!$daoRow) return null;
        $stat = new Blog_Model_ArticleMonthArchive();
        $stat->_setFromDaoRow($daoRow);
        return $stat;
    }

    /**
     * Returns previous month with one article at least
     */
    public function getPrevMonthStat()
    {
        $daoRow = $this->getDao()->getPrevMonthStat($this->_year,$this->_month);
        if (!$daoRow) return null;
        $stat = new Blog_Model_ArticleMonthArchive();
        $stat->_setFromDaoRow($daoRow);
        return $stat;
    }

    protected static function _getDayRows(&$monthResult,$day)
    {
        $rows = array();
        foreach($monthResult as $row) {
            if ($row["day"]==$day) $rows[]=$row;
        }
        return $rows;
    }

    protected function _setPropFromArray(&$arr)
    {
        $ignoreKeys = array("yearMonth");
        foreach($arr as $key=>$val) {
            if (in_array($key,$ignoreKeys)) continue;
            $this->{"_".$key}=$val;
        }
    }

    protected function _inflectDaoRowIndexes(&$arr)
    {
        $keys=array_keys($arr);
        foreach($keys as $key) {
            switch ($key) {
            case "artcnt":
                MKLib_Utils::renameKey($arr,$key,"articleCount");
                break;
            default:
                break;
            }
        }
    }
}
