<?php
	
	include_once( PLOG_CLASS_PATH."class/view/admin/adminplugintemplatedview.class.php" );
	include_once( PLOG_CLASS_PATH."class/dao/articlecomments.class.php" );
    include_once( PLOG_CLASS_PATH."class/dao/articlecommentstatus.class.php" );	
    include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );	
	include_once( PLOG_CLASS_PATH."class/data/pager/pager.class.php" );

	class AdminEditCommentsView extends AdminPluginTemplatedView
	{
        var $_article;
        var $_commentStatus;
        var $_page;
        
		function AdminEditCommentsView( $blogInfo, $params = Array() ){
			$this->AdminPluginTemplatedView( $blogInfo, "editcomments", "editcomments" );
			$this->_setParameters( $params );
            $this->_page = $this->getCurrentPageFromRequest();
		}
		
		/**
		 * @private
		 */
		function _setParameters( $params )
		{
			// fetch the article id
			$this->_article = null;
			if( isset( $params["article"] ))
				$this->_article = $params["article"];
						
			// load the status
			if( isset( $params["showStatus"] ))
				$this->_commentStatus = $params["showStatus"];
				
			if( !ArticleCommentStatus::isValidStatus( $this->_commentStatus )) 
				$this->_commentStatus = COMMENT_STATUS_ALL;	
				
			// laod the search terms
			$this->_searchTerms = "";
			if( isset( $params["searchTerms"] ))			
				$this->_searchTerms = $params["searchTerms"];
		}
		
		/**
		 * show the contents of the view
		 */
		function render()
		{
			// load the comments and throw the correct event
			$comments = new CommentsCommon();
			if( $this->_article ) {
				// id of the article, for the pager...
				$articleId = $this->_article->getId();

                    // load only the comments of the given post
				$postComments = $comments->getPostComments( $articleId,
															COMMENT_ORDER_NEWEST_FIRST,
															$this->_commentStatus, 
															COMMENT_TYPE_COMMENT,
															$this->_page, 
															DEFAULT_ITEMS_PER_PAGE );
				// number of comments
				$numPostComments = $comments->getNumPostComments( $articleId, 
				                                                  $this->_commentStatus,
																  COMMENT_TYPE_COMMENT );
			}
			else {
                    // load all comments given the current status
				$postComments = $comments->getBlogComments( $this->_blogInfo->getId(),
				                                            COMMENT_ORDER_NEWEST_FIRST,
				                                            $this->_commentStatus,
															COMMENT_TYPE_COMMENT,
															$this->_searchTerms,
														    $this->_page,
														    DEFAULT_ITEMS_PER_PAGE );
				// number of comments
				$numPostComments = $comments->getNumBlogComments( $this->_blogInfo->getId(),
				                                                  $this->_commentStatus,
																  COMMENT_TYPE_COMMENT,
																  $this->_searchTerms );
				// no article id...
				$articleId = 0;
			}
			$this->notifyEvent( EVENT_COMMENTS_LOADED, Array( "comments", &$postComments ));
			
            $pagerUrl = "?op=editComments";

			if( $this->_commentStatus > -1 ){
				$pagerUrl .= "&amp;articleId={$articleId}&amp;showStatus=".
                    $this->_commentStatus."&amp;searchTerms=".
                    $this->_searchTerms."&amp;page=";
            }
			else{
				$pagerUrl .= "&amp;articleId={$articleId}&amp;searchTerms=".
                    $this->_searchTerms."&amp;page=";
            }
			// calculate the pager url
			$pager = new Pager( $pagerUrl,
					    $this->_page,
					    $numPostComments,
					    DEFAULT_ITEMS_PER_PAGE );					
														
			// get a list with all the different comment status
			$statusList = ArticleCommentStatus::getStatusList( true );
			$statusListWithoutAll = ArticleCommentStatus::getStatusList( false );
			
			// and pass all the information to the templates
			$this->setValue( "comments", $postComments);
			$this->setValue( "commentstatus", $statusList );
			$this->setValue( "commentstatusWithoutAll", $statusListWithoutAll );
			$this->setValue( "currentstatus", $this->_commentStatus );
			$this->setValue( "searchTerms", $this->_searchTerms );

			// pass the pager to the view
			$this->setValue( "pager", $pager );+
		
			// pass the common data to the templates
			$this->setValue( "post", $this->_article );
						
			parent::render();
		}

	}
?>