<?php
/**
 * Implementation for the helper for month calendar archive
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */

/**
 * The helper for one month calendar.
 *
 * This helper renders inner part of the html table without <table></table>
 * tags.
 *
 * @package koubel homepage
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class HomeWebApp_View_Helper_MonthCalendarArchive extends Zend_View_Helper_Abstract
{
    /**
     * @var Calendar calendar object from the library
     */
    private $_calendar;

    /**
     * @var Zend_Locale
     */
    private $_locale;

    /**
     * localized day names
     * @var array 
     */
    private $_dayNames;

    /**
     * Initilizes the calendar, locale and names for a days
     */
    public function __construct()
    {
        $this->_calendar = new Calendar();
        $this->_locale = Zend_Controller_Front::getInstance()
            ->getParam('bootstrap')
            ->getPluginResource('locale')
            ->getLocale();
        $this->_dayNames=$this->_locale->getTranslationList("Day",null,
            array('gregorian', 'format', 'abbreviated'));

        //adjust daynames
        foreach($this->_dayNames as $key=>$val) {
            //use iconv, because strlen/substr doens't support utf-8
            if (iconv_strlen($val,'utf-8')>2) $val=iconv_substr($val,0,2,'utf-8');
            $this->_dayNames[$key]=$val;
        }

        switch ($this->_locale->toString()) {
            case 'cs_CZ':
                $this->_calendar->setStartDay(1);
            default:
                break;
        }
    }

    /**
     * Main helper function
     * @return string xhtml for the one month table without <table></table>
     * tags
     */
    public function monthCalendarArchive(ArticleMonthArchive $archive)
    {
        $this->_calendar->setMonth( $archive->getMonth() );
        $this->_calendar->setYear( $archive->getYear() );

        Calendar::shiftDayNames($this->_calendar->getStartDay(),$this->_dayNames);

        //renders head
        $this->view->archiveItem = $archive;
        $xHtml = $this->view->render("calendarArchiveHead.phtml");
        unset($this->view->archiveItem);

        $xHtml.="<tr class=\"dayhead\">\n";
        foreach($this->_dayNames as $dayName) {
            $xHtml.="<td>$dayName</td>";
        }
        $xHtml.="</tr>\n";

        $daysInMonth=$this->_calendar->getMonthArray();

        if ($this->_calendar->getMonth()==date("n")
            && $this->_calendar->getYear()==date("Y")
           ) $isCurrentMonth=true;
        else $isCurrentMonth=false;

        $currentDay=date("j");

        foreach($daysInMonth as $week) {
            $xHtml .= "<tr>\n";
            foreach($week as $day) {
                $xHtml .= "<td>";

                if (is_null($day)) {
                    $xHtml .= "&nbsp;";
                } else {
                    $dayLink = $this->_getDayLink($day,$archive->getDayArchive($day));
                    if ($isCurrentMonth && $day == $currentDay) {
                        $xHtml .= "<div style=\"margin:1px; padding:1px; border:1px solid black\">"
                            . $dayLink
                            . "</div>";
                    } else {
                        $xHtml .= $dayLink;
                    }
                }

                $xHtml .= "</td>\n";
            }
            $xHtml .= "</tr>\n";
        }

        //add calendar navigator
        $xHtml .= $this->_getNavigator($archive);

        return $xHtml;
    }

    /**
     * Returns links for a one day
     * @param int $day (1-31)
     * @param ArticleDayArchive $dayArchive articles archive for $day
     * @return string <a href> link for $day
     */
    private function _getDayLink($day,ArticleDayArchive $dayArchive=null)
    {
        if (is_null($dayArchive)) return $day;

        $title = "";
        $articles = $dayArchive->getArticles();
        $printArticleOrder = (count($articles)>1) ? true : false;
        for($i=0;$i<count($articles);$i++) {
            if ($printArticleOrder) {
                if ($i>0) $title .= "  ";
                $title .= ($i+1).": ";
            }
            $title .= $articles[$i]->getTitle();
        }
        $title = htmlspecialchars($title,ENT_QUOTES);

        $link = "<a href=\"" . $this->view->url(
            array(
                "year" => $this->_calendar->getYear(),
                "month" => $this->_calendar->getMonth(),
                "day" => $day
            ),"blogArchiveDay")
            . "\" title=\"$title\">$day</a>";
        return $link;
    }

    /**
     * Returns the navigator markup for the next and previous months.
     * Only month with some articles are considered.
     *
     * @param ArticleMonthArchive $archive article archive for current viewed month.
     * @return string xhmth markup - see calendarNavRow.phtml template
     */
    private function _getNavigator(ArticleMonthArchive $archive)
    {
        //previous month - only month with at least one article are returned
        //months without articles are skipped
        $prevArchive = $archive->getPrevMonthStat();
        if (!is_null($prevArchive)) {
            $this->_calendar->setMonth($prevArchive->getMonth());
            $this->_calendar->setYear($prevArchive->getYear());
            $this->view->prevMonth = $prevArchive->getMonth();
            $this->view->prevYear = $prevArchive->getYear();
            $this->view->prevMonthName = Utils::getMonthFirstCaseName(
                $this->_calendar->getMonth(),
                $this->_locale);
            if ($this->_calendar->getYear() != $archive->getYear())
                $this->view->prevMonthName.= " ".substr($this->_calendar->getYear(),-2);
        }

        //next month - only month with at least one article are returned
        //months without articles are skipped
        $nextArchive=$archive->getNextMonthStat();
        if (!is_null($nextArchive)) {
            $this->_calendar->setMonth($nextArchive->getMonth());
            $this->_calendar->setYear($nextArchive->getYear());
            $this->view->nextMonth = $nextArchive->getMonth();
            $this->view->nextYear = $nextArchive->getYear();
            $this->view->nextMonthName = Utils::getMonthFirstCaseName(
                $this->_calendar->getMonth(),
                $this->_locale);
            if ($this->_calendar->getYear() != $archive->getYear())
                $this->view->nextMonthName.= " ".substr($this->_calendar->getYear(),-2);
        }

        return $this->view->render("calendarNavRow.phtml");
    }
}
