<?php

	include_once( PLOG_CLASS_PATH."class/summary/action/registeraction.class.php" );
    include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
    include_once( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
    include_once( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" );	 		
	include_once( PLOG_CLASS_PATH."class/summary/view/doblogregistrationview.class.php" );
	include_once( PLOG_CLASS_PATH."class/summary/view/blogtemplatechooserview.class.php" );
	include_once( PLOG_CLASS_PATH."class/locale/locales.class.php" );
    include_once( PLOG_CLASS_PATH."class/data/validator/domainvalidator.class.php" );
	include_once( PLOG_CLASS_PATH."class/net/http/subdomains.class.php" );

	/**
	 * registers a blog
	 */
    class doBlogRegistration extends RegisterAction 
	{
        
        function doBlogRegistration( $actionInfo, $request )
        {
	    	$this->RegisterAction( $actionInfo, $request );
	    	
	    	// data validation
	    	//$this->registerFieldValidator( "userId", new IntegerValidator());
	    	$this->registerFieldValidator( "blogName", new StringValidator());
	    	$this->registerFieldValidator( "blogCategoryId", new IntegerValidator());
	    	$this->registerFieldValidator( "blogLocale", new StringValidator());
			$this->registerFieldValidator( "blogSubDomain", new DomainValidator(), true );
			$this->registerFieldValidator( "blogMainDomain", new DomainValidator(), true );
	    	$view = new doBlogRegistrationView();
	    	$view->setErrorMessage( $this->_locale->tr("register_error_creating_blog"));
	    	$this->setValidationErrorView( $view );   
        }

		function validate()
		{
			// if the other fields did not validate, no need to continue
			if( !parent::validate())
				return false;
				
            // check to see whether we are going to save subdomain information
			$valid = true;
            if( Subdomains::getSubdomainsEnabled()) {

				// Translate a few characters to valid names, and remove the rest
                $mainDomain = Textfilter::domainize($this->_request->getValue( "blogMainDomain" ));
                if(!$mainDomain)
                    $mainDomain = "?";
                $subDomain = Textfilter::domainize($this->_request->getValue( "blogSubDomain" ));

                // get list of allowed domains
				$available_domains = Subdomains::getAvailableDomains();

                // make sure the mainDomain parameter is one of the blogAvailableDomains and if not, 
				// force a validation error
                if( !Subdomains::isDomainAvailable( $mainDomain ) || !Subdomains::isValidDomainName( $subDomain )) {
					$valid = false;
					$this->_form->setFieldValidationStatus( "blogSubDomain", false );					
					$this->validationErrorProcessing();
                }

                if($mainDomain == "?")
                    $this->blogDomain = $subDomain;
                else
                    $this->blogDomain = $subDomain . "." . $mainDomain;
            }

			return( $valid );
		}

        function perform()
        {
            // create the new view and clean the cache
            $this->_view = new BlogTemplateChooserView();
            $this->setValues();
            $this->setCommonData();

			return( true );
        }
    }
?>