<?php

   	lt_include( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
    lt_include( PLOG_CLASS_PATH."plugins/trash/class/view/adminviewtrashview.class.php" );
	
	/**
     * empty trash
     */
    class AdminEmptyTrashAction extends AdminAction 
	{

	    var $_postIds;

	    
        function AdminEmptyTrashAction( $actionInfo, $request )
        {
            lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
        	$this->AdminAction( $actionInfo, $request );

                // check various permissions.
        	$this->requirePermission( 'add_post' );
        	$this->requirePermission( 'view_posts' );
        	$this->requirePermission( 'update_post' );
        	
			// data validation stuff...
			if( $request['postId'] ) {
				$this->registerFieldValidator( "postId", new IntegerValidator());
				$this->_postIds[] = $this->_request->getValue( "postId" );
			} else {
                lt_include( PLOG_CLASS_PATH."class/data/validator/arrayvalidator.class.php" );
				$this->registerFieldValidator( "postIds", new ArrayValidator(new IntegerValidator()));
				$this->_postIds = $this->_request->getValue( "postIds" );
			}

			$view = new AdminViewTrashView ( $this->_blogInfo );
			$view->setErrorMessage( $this->_locale->tr("error_incorrect_article_id"));
			$this->setValidationErrorView( $view );	
			$this->requirePermission( "update_post" );	
        	
        }

        
        
        function perform()
        {
            lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );

            $articles = new Articles();
            $errorMessage = "";
			$successMessage = "";
			$totalOk = 0;
			
            foreach( $this->_postIds as $postId ) {
            	
            	$this->_view = new AdminViewTrashView( $this->_blogInfo );
            	
            	// get the post
                $post = $articles->getBlogArticle( $postId, $this->_blogInfo->getId());
				
				if( $post ) {
				    
                    // we won't permanently delete undeleted posts
				    if( POST_STATUS_DELETED != $post->getStatus() ) {
				        $errorMessage .= $this->_locale->tr("error_remove_deleted_only")."<br />";
                        continue;
                    }
                    
				    if( ($post->getUserId() == $this->_userInfo->getId()) ||
                        $this->userHasPermission( "update_all_user_articles" ))
                    {
				        $result = $articles->deleteArticle( $postId, $post->getUser(),
                                                            $this->_blogInfo->getId(), true);
				    } else {
				        $errorMessage .= $this->_locale->tr("error_can_only_update_own_articles")." ";
				        $result = false;
				    }
				        
				    if( !$result ) {
				        $errorMessage .= $this->_locale->pr("error_deleting_article",
                                                            $post->getTopic())."<br/>";
				    } else {
				        $totalOk++;
				        if( $totalOk < 2 ) 
				        	$successMessage = $this->_locale->pr("article_deleted_ok",
                                                                 $post->getTopic())."<br/>";
				        else
				        	$successMessage = $this->_locale->pr("articles_deleted_ok", $totalOk );
				    }
                }
                else{
                    $errorMessage .= $this->_locale->pr("error_deleting_article2", $postId)."<br />";
                }                    
            }
			
			if( $errorMessage != "" ) 
				$this->_view->setErrorMessage( $errorMessage );
				
			if( $successMessage != "" )
				$this->_view->setSuccessMessage( $successMessage );
				
			$this->setCommonData();
            return true;
        }
    }
?>