<?php

    lt_include( PLOG_CLASS_PATH."class/action/blogaction.class.php" );
    lt_include( PLOG_CLASS_PATH."class/view/plugintemplatedview.class.php" );
    lt_include( PLOG_CLASS_PATH."class/view/errorview.class.php" );
    
    class ImportRssFeedAction extends BlogAction
    {
	var $_enteredPassword;
	var $_html;
		
        function ImportRssFeedAction( $actionInfo, $request )
        {

            $this->BlogAction( $actionInfo, $request );
        }
        
        function perform()
        {
	   // Get and confirm the password
	    $this->_enteredPassword =  $this->_request->getValue( "accessKey"  ); 

	    $this->_html = "";

	    // Check the ip address to make sure that it is the list of
	    // allowd ip address

	    $blogSettings = $this->_blogInfo->getSettings();
	    if ($blogSettings->getValue( "plugin_rssimport_enabled" ) == FALSE)
	    {
		$this->_view = new ErrorView( $this->_blogInfo );
		$this->_view->setValue( "message", "Plugin not enabled" );
		
		$this->setCommonData();			
		return true;
	    }
	    
	    $feedurl = $blogSettings->getValue( "plugin_rssimport_feedurl" );
	    $accessKey = $blogSettings->getValue( "plugin_rssimport_accessKey" );
//	    $accessHosts = $blogSettings->getValue( "plugin_rssimport_accessHosts" );
	    $titleFormat = $blogSettings->getValue( "plugin_rssimport_titleFormat" );

	    // Compare the access key to that which is in the accessKey parameter
	    if ($accessKey != $this->_enteredPassword)
	    {
		// Passwords don't match
		$this->_view = new ErrorView( $this->_blogInfo );
		$this->_view->setValue( "message", "Incorrect Access Key" );
		
		$this->setCommonData();			
		return true;

	    }

//	    if ( $accessHosts != null && $accessHosts != "" )
//	    {
//		
//	    }


    	    lt_include( PLOG_CLASS_PATH."class/xml/rssparser/rssparser.class.php" );	    
	    
	    $parser = new RSSParser();

	    if ( $parser->parse($feedurl) == FALSE )
	    {
		// failed to parse the feed
		$this->_view = new ErrorView( $this->_blogInfo );
		$this->_view->setValue( "message", "Failed to parse feed" );
		
		$this->setCommonData();			
		return true;
	    }

	    $items = $parser->getItems();
	    
	    // Now add each of the items to our html documen that we are building
	    foreach ($items as $item) {
	    	    $this->addItem($item);
	    }
	    
	    if ( $this->_html == "" )
	    {
		// There were no items
		$this->_view = new ErrorView( $this->_blogInfo );
		$this->_view->setValue( "message", "No items in feed" );
		
		$this->setCommonData();			
		return true;
	    }

	    // Now create an article with the html as the body,

    	    lt_include( PLOG_CLASS_PATH.'class/dao/articles.class.php' );	    
   	    $articles = new Articles();

            $postText = Textfilter::xhtmlize("<ul>" . $this->_html . "</ul>");

	    $postTitle = date($titleFormat);


	    // XXX_PW This should pull the category from the settings
	    // Get the default category
    	    lt_include( PLOG_CLASS_PATH.'class/dao/articlecategories.class.php' );
	    $category = new ArticleCategories();
            $cats = $category->getBlogCategories($this->_blogInfo->getId());
            
	    // some protection again blogs without categories
            if( !$cats ) {				
		$this->_view = new ErrorView( $this->_blogInfo );
		$this->_view->setValue( "message", "Error getting categories" );
		
		$this->setCommonData();			
                return true;
            }


	    $idCategory = $blogSettings->getValue( "plugin_rssimport_article_category_id" );            

            $article  = new Article( $postTitle, 
			             $postText,
				     Array( $idCategory ), // catid
                                     $this->_blogInfo->getOwner(), 
				     $this->_blogInfo->getId(), 
				     POST_STATUS_PUBLISHED,
									 0 );
            $article->setDateObject( new Timestamp());
            $article->setCommentsEnabled( true );
			
			
	    // add the article to the db, finally
	    if( $articles->addArticle( $article )) {
    	    	lt_include( PLOG_CLASS_PATH."class/view/defaultview.class.php" );
		$this->_view = new DefaultView( $this->_blogInfo );
	    }
	    else {
		$this->_view = new ErrorView( $this->_blogInfo );
		$this->_view->setValue( "message", "Error adding article to the database" );
  	   }
			
	   $this->setCommonData();			
	    
            
            return true;
        }

	function addItem( $item )
	{
		// Add this item to the html text
	    	$this->_html .= "<li>";
	    	$this->_html .= "<a href=\"" . $item->getLink() . "\">" . $item->getTitle() . " </a>";
	    	$this->_html .= "</li>";				
	}


    }
?>
