<?php

	
    
	
	
	

    /**
     * \ingroup Action
     * @private
     *
     * Action that shows a form to change the settings of the current blog.
     */
    class AdminChangeLinksCategoryAction extends AdminAction 
	{

        var $_linkIds;
		var $_linkCategoryId;

    	/**
         * Constructor. If nothing else, it also has to call the constructor of the parent
         * class, BlogAction with the same parameters
         */
        function AdminChangeLinksCategoryAction( $actionInfo, $request )
        {
        	$this->AdminAction( $actionInfo, $request );
			
			$this->registerFieldValidator( "linkIds", new ArrayValidator( new IntegerValidator()));
			$this->registerFieldValidator( "linkCategoryId", new IntegerValidator());
			$view = new AdminLinksListView( $this->_blogInfo );	
			$view->setErrorMessage( $this->_locale->tr("error_no_links_selected"));
			$this->setValidationErrorView( $view );
			
			// permission checks
			$this->requirePermission( "update_link" );			
        }
		
		function perform()
		{
			$results = $this->_changeLinks();
			
            $this->_view = new AdminLinksListView( $this->_blogInfo );
            if( $results["errorMessage"] != "" ) $this->_view->setErrorMessage( $results["errorMessage"] );
			if( $results["successMessage"] != "" ) $this->_view->setSuccessMessage( $results["successMessage"] );
            $this->setCommonData();

			return( true );
		}
		
		function performAjax()
		{
			$results = $this->_changeLinks();
			
			
            $this->_view = new AdminAjaxView( $this->_blogInfo );
			$this->_view->setMessage( $results );
			$this->_view->setSuccess( true );

            // better to return true if everything fine
            return true;						
		}

        /**
         * Carries out the specified action
		 * @private
         */
        function _changeLinks()
        {
			$this->_linkIds = $this->_request->getValue( "linkIds" );
			$this->_linkCategoryId = $this->_request->getValue( "linkCategoryId" );			
	
            $errorMessage = "";
			$successMessage = "";
			$numOk = 0;

        	// update the link
            $links = new MyLinks();

            foreach( $this->_linkIds as $linkId ) {
            	// load the link
                $link = $links->getMyLink( $linkId, $this->_blogInfo->getId());
				
				if( $link ) {
					// fire the event
					$this->notifyEvent( EVENT_PRE_LINK_UPDATE, Array( "link" => &$link ));

					// update the link category
					$link->setCategoryId( $this->_linkCategoryId );
					$result = $links->updateMyLink( $link );
					
					if( !$result )
						$errorMessage .= $this->_locale->pr("error_updating_link", $link->getName())."<br/>";
					else {
						$numOk++;
						if( $numOk > 1 )
							$successMessage = $this->_locale->pr("links_updated_ok", $numOk );
						else
							$successMessage = $this->_locale->pr("link_updated_ok", $link->getName());
					}
				}
				else {
					$errorMessage .= $this->_locale->pr("error_updating_link2", $linkId)."<br/>";
				}
            }

			// clear the cache
			if( $numOk > 0 ) 
				CacheControl::resetBlogCache( $this->_blogInfo->getId(), false );

			return( Array( "errorMessage" => $errorMessage, "successMessage" => $successMessage ));
        }
    }
?>