<?php

    include_once( MOBILE_PLOG_CLASS_PATH."class/view/mobileview.class.php" );
    include_once( PLOG_CLASS_PATH."class/dao/articlecomments.class.php" );
    include_once( PLOG_CLASS_PATH."class/data/pager/pager.class.php" );
    
    class MobileViewArticleView extends MobileView
    {
    
        var $_page;
        var $_goToLastPage;
    
        function MobileViewArticleView( $blogInfo, $tInfo )
        {
            $this->MobileView( $blogInfo, $tInfo, "postandcomments" );
            
            $this->_page = $this->getCurrentPageFromRequest();
            $this->_goToLastPage = false;            
        }
        
        /**
         * instead of relying on whatever getCurrentPageFromRequest() says, this method
         * tells this view to set the article page as the last one.
         *
         * It is used from MobileAddCommentAction so that users get to see the comment that they
         * just typed.
         */
        function setLastPage()
        {
            $this->_goToLastPage = true;
        }
        
        function render()
        {
            // load a paginated view of the article comments
            $comments = new ArticleComments();
            $article = $this->getValue( "post" );
            
            // get the number of comments
            $numComments = $comments->getNumPostComments( $article->getId(),
                                                          COMMENT_STATUS_NONSPAM );
                                                          
            // generate the pager
            $murl = new MobileRequestGenerator( $this->_blogInfo );
            $pager = new Pager( $murl->getMobileUrl()."?op=ViewArticle&amp;blogId=".
                                $this->_blogInfo->getId()."&amp;articleId=".$article->getId()."&amp;page=",
                                $this->_page,
                                $numComments,
                                $this->_tInfo->getNumItemsPerPage());
            
            // check whether we have to go to the last page
            if( $this->_goToLastPage ) {
                // this is a bit of a hack, but or else the pager will not display that the current
                // page is the last one (we should *not* access private attributes!!, but since we
                // forgot to add a setter for this attribute, there is no other way around...
                $this->_page = $pager->getEndPage();
                $pager->_curPage = $this->_page;
            }
            
            $articleComments = $comments->getPostComments( $article->getId(),
                                                           COMMENT_ORDER_OLDEST_FIRST,
                                                           COMMENT_STATUS_NONSPAM,
                                                           $this->_page,
                                                           $this->_tInfo->getNumItemsPerPage());
                                
            // pass everything to the templates
            $this->setValue( "comments", $articleComments );
            $this->setValue( "pager", $pager );
            
            parent::render();
        }
    }
?>