<?php

//global $Debug;$Debug = true;
class AdminView extends Slot {

	var $ModuleID = 3;
	var $SectionID = 3;

	function __construct($pquery) {
		parent::__construct($pquery);
		$this->Cache = 0;
		$this->Passing = TRUE;
	}

	function init() { //print "test";exit;
		global $PHP_SELF, $WEB_ROOT, $REGISTRY;
//		if(!validate(getenv('HTTPS'))) header("Location: ".$this->sHome.getenv('REQUEST_URI').$this->SID);
//		$REGISTRY['Globals']['static']['homePath'] = 'https://'.getenv('HTTP_HOST').$WEB_ROOT;
		if(!empty($this->LocalTarget[0]) && $this->LocalTarget[0]=='LogOut') {
			$this->Action->processEvent($this->SectionID,'logout',array(),$this)
			and header("Location: ".$this->Home.'/Registar'.$this->SID);
		}
		if(!$this->Action->isAuthorizeAdmin()) {
			if(!$this->Action->isAuthorizeMember()) $this->Scheme = 'auth';
			else $this->Scheme = 'message';
		}
		else {
			$this->Scheme ='fast';
			if(!empty($this->LocalTarget[0]) && in_array($this->LocalTarget[0],$this->Properties['Tasks'])) {
				if(!empty($this->LocalTarget[1]) && in_array($this->LocalTarget[1],$this->Properties['Clear']))
					$this->Scheme ='clear';
				foreach($this->Properties['Full'] as $value)
					if(preg_match("/^".$value."/",implode('/',$this->LocalTarget))) $this->Scheme ='default';
				foreach($this->Properties['Full2'] as $value)
					if(preg_match("/^".$value."/",implode('/',$this->LocalTarget))) $this->Scheme ='fast2';
				$localTarget = $this->LocalTarget;
				array_shift($localTarget);
				$this->render = array (
					'Name' => $this->LocalTarget[0].'View',
					'Type' => 'object',
					'LocalTarget' => $localTarget,
					'Properties' => array(),
					'Template' => $this->LocalTarget[0].'View',
					'Cache' => 0,
					'Passing' => TRUE,
				);
			}
		}
		if(!empty($this->Request)) {
			switch($this->getRequestField('step')) {
				case 'auth':
					$this->Scheme = 'auth';
					$this->validateForm()
					and $this->Action->authorize($this->SectionID,$this->getRequestField('form'),$this)
					and header('Location: '.getenv('REQUEST_URI'));
				break;
			}
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function validateForm() {
		if(!is_AlphaNumDefis($this->getRequestFormField('username'),2,32)) $this->errors['username'] = 'The username you entered is not a valid string';
		if(!is_AlphaNumDefis($this->getRequestFormField('password'),3,32)) $this->errors['passw'] = 'The password you entered is not a valid string';
		if(count($this->errors) > 0) return FALSE;
		return TRUE;
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'auth': $this->fillAuth(); break;
			default: $this->fillBody(); break;
		}
	}

	function fillBody() {
		global $CORE_PATH;
		if(!empty($this->render)) {
			$render = renderSlot($this->render,'',FALSE,TRUE);
			//printArray($render['Content']);
			$this->TempFillEls['body'] = $render['Content'];
		}
		else {
			$this->render = array (
		//		'Name' => 'ProductsManagerView',
				'Name' => 'ContentManagerView',
				'Type' => 'object',
				'LocalTarget' => array(),
				'Properties' => array(),
//				'Template' => 'ProductsManagerView',
				'Template' => 'ContentManagerView',
				'Cache' => 0,
				'Passing' => TRUE,
			);
			$render = renderSlot($this->render,'',FALSE,TRUE);
			$this->TempFillEls['body'] = $render['Content'];
		}
		$this->TempFillEls['member'] = $this->Action->Security->currentuser->username;
	}
}
?>