<?php

class SubdomainManagerView extends Slot {

	var $SectionID = 43;
	var $ModuleID = 4;
	var $colors = array('red' => '#DC143C', 'yellow' => '#FF8C00', 'green' => '#006400', 'black' => '#000000', 'blue' => '#4169E1',);
	var $subdomains = array();
	var $subdomain = array();
	var $terminated = false;
	var $undo = 0;

	function __construct($pquery) {
		parent::__construct($pquery);
	}

	function init() {
		if(!isset($this->LocalTarget[0])) $this->LocalTarget[0] = '';
		$this->Scheme = 'default';
		if(!empty($this->LocalTarget[0]) && $this->LocalTarget[0] != 'View' && $this->server_type != 'dev'){
			$this->terminateAction();
			$this->setSchemeTemplate($this->Scheme);
			return;
		}
		$this->subdomains = $this->Action->Server->getGuide('subdomain');
		switch($this->LocalTarget[0]) {
			case 'Add': {
				$this->Scheme = 'action';
				$this->subdomain = $this->Action->Handler->_parseContent($this->Action->Handler->createSubdomain());
				break;
			}
			case 'Unlock': {
				if(empty($this->LocalTarget[1]) || !isset($this->subdomains[$this->LocalTarget[1]])) return $this->terminateAction();
				$this->Action->processEvent($this->SectionID, 'get_lock', array('id' => $this->LocalTarget[1]), $this);
				if ($this->PreparedTemplates['access_flag'] == $this->Action->Security->currentuser->id) $this->Action->processEvent($this->SectionID, 'unlock', array('id' => $this->LocalTarget[1]), $this);
				return $this->terminateAction();
				break;
			}
			case 'View': case 'Update': case 'Delete': {
				if(empty($this->LocalTarget[1]) || !isset($this->subdomains[$this->LocalTarget[1]])) return $this->terminateAction();
				if($this->LocalTarget[0] != 'View') {
					$this->Action->processEvent($this->SectionID, 'get_lock', array('id' => $this->LocalTarget[1]), $this);
					if(empty($this->PreparedTemplates['access_flag'])) $this->Action->processEvent($this->SectionID, 'lock', array('id' => $this->LocalTarget[1]), $this);
					else if ($this->PreparedTemplates['access_flag'] != $this->Action->Security->currentuser->id) return $this->terminateAction();
				}
				if($this->LocalTarget[0] == 'Update' && @$this->LocalTarget[2] == 'Undo' && @$this->LocalTarget[3] && is_numeric($this->LocalTarget[3])) $this->undo = $this->LocalTarget[3];
				$this->Action->processEvent($this->SectionID, 'open', array('id' => $this->LocalTarget[1], 'undo' => $this->undo), $this);
				$this->subdomain = $this->PreparedTemplates;
				$this->Scheme = 'action';
				break;
			}
			case 'FullHistory': {
				$request = array ('id' => $this->LocalTarget[1]);
				$request['pageRecordsQty'] = 20;
				$request['curPageNumber'] = isset($this->LocalTarget[2]) ? $this->LocalTarget[2] + 1 : 1;
				$this->Action->processEvent($this->SectionID, 'select_subdomaincomments', $request, $this);
				$this->getPageControl($this->Home.'/Registar/SubdomainManager/FullHistory/'.$this->LocalTarget[1]);
				$this->Scheme = 'fullhistory';
				break;
			}
			default: {
				$this->Action->processEvent($this->SectionID, 'select', array(), $this);
				break;
			}
		}
		if(!empty($this->Request)) {
			switch($this->LocalTarget[0]) {
				case 'Update': case 'Add': {
					foreach($this->Request['form'] as $key => $value) $this->subdomain[$key] = $value;
					$this->subdomain['_error'] = '';
					$this->subdomain['undo'] = $this->undo;
					if($this->validateSubdomainData()){
						if($this->Action->processEvent($this->SectionID, 'save', $this->subdomain, $this)){
							if(!empty($this->subdomain['id'])) $this->Action->processEvent($this->SectionID, 'unlock', $this->subdomain, $this);
							$this->terminateAction();
						}
						else $this->errors['validate'] = $this->PreparedTemplates['_error'];
					}
					break;
				}
				case 'Delete': {
					$this->Action->processEvent($this->SectionID, 'delete', $this->subdomain, $this)
					and $this->clearUndo($this->subdomain);
					$this->terminateAction();
					break;
				}
			}
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function fillElements() {
		if($this->terminated) return;
		switch($this->Scheme) {
			case 'action': $this->fillAction(); break;
			case 'fullhistory': $this->fillFullHistory(); break;
			case 'default': default: $this->fillDefault(); break;
		}
	}
	
	function fillDefault() {
		//printArray($this->PreparedTemplates);
		$edit_vis = $this->server_type == 'dev' ? 'visible' : 'hidden';
		$pub_vis = $this->server_type == 'pro' ? 'hidden' : 'visible';
		$this->TempFillEls['pub_vis'] = $pub_vis;
		$this->TempFillEls['btnEdit_vis'] = $edit_vis;
		$count = count($this->PreparedTemplates);
		for ($i = 0; $i < $count; $i++) {
			$this->TempFillEls['subdomain'][$i] = $this->PreparedTemplates[$i];
			$date_locked = explode('.', $this->PreparedTemplates[$i]['date_locked']);
			$this->TempFillEls['subdomain'][$i]['date_locked'] = $date_locked[0];
			if(empty($this->PreparedTemplates[$i]['_error'])) $this->TempFillEls['subdomain'][$i]['img'] = 'bullet';
			else {
				$this->TempFillEls['subdomain'][$i]['img'] = 'bullet2';
				$this->TempFillEls['subdomain'][$i]['chbAdds_disabled'] = 'disabled';
			}
			$this->TempFillEls['subdomain'][$i]['btnEdit_vis'] = $edit_vis;
			$this->TempFillEls['subdomain'][$i]['chbAdds_vis'] = $pub_vis;
			if(!empty($this->PreparedTemplates[$i]['access_flag']) && $this->PreparedTemplates[$i]['access_flag'] != $this->Action->Security->currentuser->id) {
				$this->TempFillEls['subdomain'][$i]['btnUnlock_disp'] = 'none';
				$this->TempFillEls['subdomain'][$i]['btnEdit_disp'] = 'none';
				$this->TempFillEls['subdomain'][$i]['btnDelete_disp'] = 'none';
			}
			else {
				$this->TempFillEls['subdomain'][$i]['locked_message_disp'] = 'none';
				if($this->PreparedTemplates[$i]['access_flag'] != $this->Action->Security->currentuser->id) $this->TempFillEls['subdomain'][$i]['btnUnlock_disp'] = 'none';
			}
			$presets_count = explode('/', $this->PreparedTemplates[$i]['published_count']);
			$c = count($this->PreparedTemplates[$i]['publish']);
			$this->TempFillEls['subdomain'][$i]['published_count_cursor'] = $c ? 'pointer' : 'auto';
			$this->TempFillEls['subdomain'][$i]['published_count_color'] = $this->colors[($presets_count[0] >= $presets_count[1] ? 'green' : (empty($presets_count[0]) ? 'red' : 'blue'))];
			$this->TempFillEls['subdomain'][$i]['publish'][]['variable'] = 'subdomains['.$this->PreparedTemplates[$i]['id'].']=[];';
			$this->TempFillEls['subdomain'][$i]['publish'][]['variable'] = 'subdomains['.$this->PreparedTemplates[$i]['id'].']["publish"]=[];';
			for($j = 0; $j < $c; $j++){
				$this->TempFillEls['subdomain'][$i]['publish'][]['variable'] = 'subdomains['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']=[];';
				$this->TempFillEls['subdomain'][$i]['publish'][]['variable'] = 'subdomains['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']["package_id"]='.$this->PreparedTemplates[$i]['publish'][$j]['package_id'].';';
				$this->TempFillEls['subdomain'][$i]['publish'][]['variable'] = 'subdomains['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']["package_name"]="'.$this->PreparedTemplates[$i]['publish'][$j]['package_name'].'";';
				$this->TempFillEls['subdomain'][$i]['publish'][]['variable'] = 'subdomains['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']["username"]="'.$this->PreparedTemplates[$i]['publish'][$j]['username'].'";';
				$this->TempFillEls['subdomain'][$i]['publish'][]['variable'] = 'subdomains['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']["hostpath"]="'.$this->PreparedTemplates[$i]['publish'][$j]['hostpath'].'";';
				$this->TempFillEls['subdomain'][$i]['publish'][]['variable'] = 'subdomains['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']["date_published"]="'.$this->PreparedTemplates[$i]['publish'][$j]['date_published'].'";';
			}
		}
		$this->clearAllTemplates();
	}
	
	function fillAction() {
		//printArray($this->subdomain);
		$this->TempFillEls['action'] = $this->LocalTarget[0];
		$this->TempFillEls['id'] = $this->subdomain['id'];
		$this->TempFillEls['name'] = $this->subdomain['name'];
		$this->TempFillEls['date_created'] = $this->subdomain['date_created'];
		$this->TempFillEls['uid'] = $this->subdomain['uid'];
		$this->TempFillEls['_error'] = $this->subdomain['_error'];
		$this->TempFillEls['form_name'] = inputText('form[name]', $this->subdomain['name'], 80, '', 128);
		$this->TempFillEls['form_domain'] = inputText('form[domain]', $this->subdomain['domain'], 80, 'id="txtDomain"', 256);
		$this->TempFillEls['form_description'] = inputTextArea('form[description]', $this->subdomain['description'], '', 5, 'style="width: 500px;"');
		if(empty($this->subdomain['_error'])) $this->TempFillEls['error_disp'] = 'none';
		if($this->LocalTarget[0] == 'Add'){
			$this->TempFillEls['header_info_disp'] = 'none';
			$this->TempFillEls['date_created_disp'] = 'none';
		}
		$this->TempFillEls['form_comment'] = inputTextarea('form[comment]', '', '', 5, 'id="form[comment]" style="width: 500px;"');
		if(!empty($this->subdomain['comments'])) $this->fillFullHistory(false);
		if(!empty($this->errors)) $this->errors['header'] = 'There were some errors.<br>Please see below for details';
		foreach ($this->errors as $k => $v) $this->TempFillEls[$k.'_error'] = $this->printError($v).($k == 'header' ? '<p>' : '');
		$this->clearAllTemplates();
	}
	
	function fillFullHistory($main = true) {
		if($this->terminated) return false;
		if($main) $history = &$this->PreparedTemplates['curPage'];
		else $history = &$this->subdomain['comments'];
		$count = count($history);
		for ($i = 0; $i < $count; $i++){
			$date = explode('.', $history[$i]['date']);
			$this->TempFillEls['comments'][$i] = array(
				'number' => $main ? ($this->PreparedTemplates['pageRecordsQty'] * ($this->PreparedTemplates['curPageNumber'] - 1) + $i + 1) : ($i + 1),
				'date' => $date[0],
				'member' => $history[$i]['member'],
				'version' => $history[$i]['version'],
				'value' => empty($history[$i]['value']) ? '&nbsp;' : wordwrap($history[$i]['value'], 30, '<br>', 1),
				'id' => $this->LocalTarget[1],
				'undo_vis' => $history[$i]['file'] ? 'visible' : 'hidden',
			);
		}
		if($main) {
			$this->TempFillEls['id'] = $this->PreparedTemplates['id'];
			$this->TempFillEls['name'] = $this->PreparedTemplates['name'];
			$this->clearAllTemplates();
		}
	}
	
	function validateSubdomainData() {
		if($this->LocalTarget[0] == 'Update' && (empty($this->subdomain['id']) || !is_numeric($this->subdomain['id']))) $this->errors['id'] = 'Subdomain ID is not specified';
		if(($id = array_search($this->subdomain['name'], $this->subdomains)) !== false && $id != $this->subdomain['id']) $this->errors['name'] = 'Subdomain with such name already exists';
		if(!is_NumText($this->subdomain['name'], 2, 128)) $this->errors['name'] = 'Subdomain name is not valid';
		if(!is_AlphaNumDefis($this->subdomain['domain'], 2, 256)) $this->errors['domain'] = 'Subdomain is not valid';
		if(!is_NumText($this->subdomain['description'], 6, 256)) $this->errors['description'] = 'Subdomain description is not valid';
		return empty($this->errors);
	}
	
	function clearUndo($subdomain) {
		GLOBAL $ROOT_PATH, $TOOLS_PATH;
		include_once($TOOLS_PATH.'/func.php');
		$path = $ROOT_PATH.'/undo/subdomain/'.$subdomain['id'];
		if(file_exists($path)) mysystem('rm -rf '.$path);
		return true;
	}
	
	function terminateAction () {
		$this->terminated = true;
		$this->clearAllTemplates();
		header('Location: '.$this->Home.'/Registar/SubdomainManager'.$this->SID);
	}
	
	function clearAllTemplates(){
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'select');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'get_lock');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'lock');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'unlock');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'open');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'save');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'delete');
	}

}
?>