<?php

class EngineManagerView extends Slot {

	var $SectionID = 39;
	var $ModuleID = 4;
	var $snapshot = array();
	var $db_rules = array();
	var $db_rule = array();

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

	function init() {
		GLOBAL $ROOT_PATH, $TOOLS_PATH;
		switch(@$this->LocalTarget[0]){
			case 'Update': {
				if(empty($this->LocalTarget[1]) || (empty($this->LocalTarget[2]))){
					header('Location: '.$this->Home.'/Registar/EngineManager'.$this->SID);
					return;
				}
				if(empty($this->Request)){
					$this->Action->processEvent($this->SectionID, 'select_engine_db_rules', array($this->LocalTarget[2]), $this);
					if(empty($this->PreparedTemplates)){
						$this->clearAllTemplates();
						header('Location: '.$this->Home.'/Registar/EngineManager'.$this->SID);
						return;
					}
					$this->db_rule = reset($this->PreparedTemplates);
				}
				$this->Scheme = 'dbaction';
				break;
			}
			default: {
				if(empty($this->Request)){
					$this->Action->processEvent($this->SectionID, 'select_engine_snapshot', array(), $this);
					$this->snapshot = $this->PreparedTemplates;
					$this->Action->processEvent($this->SectionID, 'select_engine_db_rules', array(), $this);
					$this->db_rules = $this->PreparedTemplates;
				}
				$this->Scheme = 'default';
			}
		}
		if(!empty($this->Request)){
			switch(@$this->LocalTarget[0]){
				case 'IncludeFileDir':
					if(preg_match("/^\/?[[:alnum:]-_\.\/]+[\*]?$/i", $this->Request['form']['filedir'][0], $matches)) $this->Action->processEvent($this->SectionID, 'include_filedir_to_snapshot', $this->Request['form']['filedir'], $this);
					$this->clearAllTemplates();
					header('Location: '.$this->Home.'/Registar/EngineManager'.$this->SID);
					break;
				case 'ExcludeFileDir':
					if(!empty($this->Request['form']['filedir'])) $this->Action->processEvent($this->SectionID, 'exclude_filedir_from_snapshot', $this->Request['form']['filedir'], $this);
					$this->clearAllTemplates();
					header('Location: '.$this->Home.'/Registar/EngineManager'.$this->SID);
					break;
				case 'Update':
					$this->Scheme = 'dbaction';
					if(!empty($this->Request['form']['data'])){
						$request = array('name' => $this->LocalTarget[2], 'data' => array(),);
						$count = count($this->Request['form']['data']);
						for ($i = 0; $i < $count; $i++){
							$c = count($this->Request['form']['data'][$i]);
							for($j = 0; $j < $c; $j++)
								if(!empty($this->Request['form']['data'][$i][$j]['checked']))
									$request['data'][$i][$this->Request['form']['data'][$i][$j]['field']] = $this->Request['form']['data'][$i][$j]['value'];
						}
						$this->Action->processEvent($this->SectionID, 'add_engine_db_rules', $request, $this);
					}
					$this->clearAllTemplates();
					header('Location: '.$this->Home.'/Registar/EngineManager'.$this->SID);
					break;
				case 'DeleteDBRules':
					if(!empty($this->Request['form']['dbrules'])){
						if(!isset($this->Request['form']['dbrules']['table'])) $this->Request['form']['dbrules']['table'] = array();
						if(!isset($this->Request['form']['dbrules']['data'])) $this->Request['form']['dbrules']['data'] = array();
						$this->Action->processEvent($this->SectionID, 'delete_engine_db_rules', $this->Request['form']['dbrules'], $this);
					}
					$this->clearAllTemplates();
					header('Location: '.$this->Home.'/Registar/EngineManager'.$this->SID);
					break;
			}
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'default': $this->fillDefault(); break;
			case 'dbaction': $this->fillDBAction(); break;
		}
	}

	function fillDefault() {
		GLOBAL $COMMON_VARS;
		$this->TempFillEls['default_rights'] = '';
		foreach($COMMON_VARS['default_rights_engine'] as $key => $value){
			$this->TempFillEls['default_rights'] .= '<b>'.strtoupper($key).'</b>: '.$value[0].' ('.$value[1].$value[2].$value[3].'); ';
		}
		$this->TempFillEls['form_inc_filedir'] = inputText('form[filedir][]', '', 80);
		$error_count = 0;
		$count = count($this->snapshot);
		for ($i = 0; $i < $count; $i++){
			$this->TempFillEls['filedir'][$i]['index'] = $i;
			$this->TempFillEls['filedir'][$i]['path'] = implode('/ ', explode('/', $this->snapshot[$i]['path']));
			$this->TempFillEls['filedir'][$i]['_error'] = $this->snapshot[$i]['_error'];
			$this->TempFillEls['filedir'][$i]['hash'] = $this->snapshot[$i]['hash'];
			$this->TempFillEls['filedir'][$i]['modified'] = $this->snapshot[$i]['modified'];
			$this->TempFillEls['filedir'][$i]['size'] = $this->snapshot[$i]['size'];
			$this->TempFillEls['filedir'][$i]['owner'] = $this->snapshot[$i]['perm']['u'][0];
			$this->TempFillEls['filedir'][$i]['owner_perm'] = $this->snapshot[$i]['perm']['u'][1].$this->snapshot[$i]['perm']['u'][2].$this->snapshot[$i]['perm']['u'][3];
			$this->TempFillEls['filedir'][$i]['group'] = $this->snapshot[$i]['perm']['g'][0];
			$this->TempFillEls['filedir'][$i]['group_perm'] = $this->snapshot[$i]['perm']['g'][1].$this->snapshot[$i]['perm']['g'][2].$this->snapshot[$i]['perm']['g'][3];
			$this->TempFillEls['filedir'][$i]['other_perm'] = $this->snapshot[$i]['perm']['o'][1].$this->snapshot[$i]['perm']['o'][2].$this->snapshot[$i]['perm']['o'][3];
			if(empty($this->snapshot[$i]['special_rights'])) $this->TempFillEls['filedir'][$i]['special_rights_img'] = '1';
			else{
				$this->TempFillEls['filedir'][$i]['special_rights_img'] = 'attention';
				$rights = array();
				foreach($this->snapshot[$i]['special_rights'] as $key => $value) $rights[] = '<nobr>'.strtoupper($key).': '.$value[0].' ('.$value[1].$value[2].$value[3].')</nobr>';
				$this->TempFillEls['filedir'][$i]['special_rights'] = implode('<br>', $rights);
			}
			$this->TempFillEls['filedir'][$i]['form_filedir_checkbox'] = inputCheckbox('form[filedir][]', false, '', $this->snapshot[$i]['path']);
			if(empty($this->snapshot[$i]['_error'])){
				switch($this->snapshot[$i]['pathtype']){
					case 1: $this->TempFillEls['filedir'][$i]['img'] = 'pack1'; break;
					case 2: $this->TempFillEls['filedir'][$i]['img'] = 'pack2'; break;
					case 3: $this->TempFillEls['filedir'][$i]['img'] = 'pack3'; break;
					default: $this->TempFillEls['filedir'][$i]['img'] = 'pack1'; break;
				}
			}
			else {
				switch($this->snapshot[$i]['pathtype']){
					case 1: $this->TempFillEls['filedir'][$i]['img'] = 'pack4'; break;
					case 2: $this->TempFillEls['filedir'][$i]['img'] = 'pack5'; break;
					case 3: $this->TempFillEls['filedir'][$i]['img'] = 'pack6'; break;
					default: $this->TempFillEls['filedir'][$i]['img'] = 'pack1'; break;
				}
				$error_count++;
			}
		}
		$this->TempFillEls['filedir_problem_count'] = $error_count;
		$this->TempFillEls['filedir_problem_color'] = $error_count ? 'red' : 'steelblue';
		$error_count = 0;
		foreach($this->db_rules as $key => $value){
			$this->TempFillEls['tables'][$key]['name'] = $value['name'];
			$this->TempFillEls['tables'][$key]['checkbox'] = inputCheckbox('form[dbrules][table][]', false, '', $value['name']);
			foreach($value['structure'] as $ind => $val){
				if(!empty($value['_error'])) $this->TempFillEls['tables'][$key]['structure'][$ind]['img'] = '1';
				else if(empty($val['_error'])) $this->TempFillEls['tables'][$key]['structure'][$ind]['img'] = 'tick1';
				else{
					$error_count++;
					$this->TempFillEls['tables'][$key]['structure'][$ind]['img'] = 'cross_red';
					$this->TempFillEls['tables'][$key]['structure'][$ind]['error'] = $val['_error'];
				}
				$this->TempFillEls['tables'][$key]['structure'][$ind]['name'] = $val['name'];
				$this->TempFillEls['tables'][$key]['structure'][$ind]['type'] = $COMMON_VARS['dbfield_types'][$val['type']];
				$this->TempFillEls['tables'][$key]['structure'][$ind]['length'] = !empty($val['length']) ? $val['length'] : '-';
				$this->TempFillEls['tables'][$key]['structure'][$ind]['notnull'] = $val['notnull'] == 't' ? 'not null' : '-';
				$this->TempFillEls['tables'][$key]['structure'][$ind]['default'] = $val['default'] == 't' ? $val['defaultvalue'] : '-';
				$this->TempFillEls['tables'][$key]['structure'][$ind]['constraint'] = $val['constraint'] == NULL ? '-' : ($val['constraint'] == 'p' ? 'primary key' : 'unique');
			}
			foreach($value['index'] as $ind => $val){
				if(!empty($value['_error'])) $this->TempFillEls['tables'][$key]['indexes'][$ind]['img'] = '1';
				else if(empty($val['_error'])) $this->TempFillEls['tables'][$key]['indexes'][$ind]['img'] = 'tick1';
				else{
					$error_count++;
					$this->TempFillEls['tables'][$key]['indexes'][$ind]['img'] = 'cross_red';
					$this->TempFillEls['tables'][$key]['indexes'][$ind]['error'] = $val['_error'];
				}
				$this->TempFillEls['tables'][$key]['indexes'][$ind]['name'] = $val['name'];
				$this->TempFillEls['tables'][$key]['indexes'][$ind]['unique'] = empty($val['unique']) ? '' : 'UNIQUE';
				$this->TempFillEls['tables'][$key]['indexes'][$ind]['fields'] = implode(', ', $val['fields']);
			}
			foreach($value['data'] as $ind => $val){
				if(!empty($value['_error'])) $this->TempFillEls['tables'][$key]['data'][$ind]['img'] = '1';
				else if(empty($val['_error'])) $this->TempFillEls['tables'][$key]['data'][$ind]['img'] = 'tick1';
				else{
					$error_count++;
					$this->TempFillEls['tables'][$key]['data'][$ind]['img'] = 'cross_red';
					$this->TempFillEls['tables'][$key]['data'][$ind]['error'] = $val['_error'];
				}
				$text = '';
				unset($val['_error']);
				foreach($val as $k => $v){
					if(strlen($v) != strlen(strip_tags($v))){
						$text .= $k.' = <span onClick="div = document.getElementById(\''.$key.'_'.$ind.'_'.$k.'\'); if(div.style.display == \'none\') div.style.display = \'\'; else div.style.display = \'none\';" style="color: navy; cursor: pointer;">{HTML or PHP Code}</span>, ';
						$this->TempFillEls['tables'][$key]['data'][$ind]['code'] = '<div id="'.$key.'_'.$ind.'_'.$k.'" style="display: none; position: absolute;">'.inputTextArea('txrCode', str_replace("{", "&#123;", $v), 90, 15, 'readonly').'</div>';
					}
					else $text .= $k.' = '.$v.', ';
				}
				$text = substr($text, 0, strlen($text) - 2);
				$this->TempFillEls['tables'][$key]['data'][$ind]['text'] = $text;
				$this->TempFillEls['tables'][$key]['data'][$ind]['checkbox'] = inputCheckbox('form[dbrules][data][]', false, 'style="padding: 0; margin: 0;"', $value['name'].'/'.$ind);
			}
			if(empty($value['_error'])) $this->TempFillEls['tables'][$key]['img'] = empty($value['data']) && empty($value['index']) ? 'pack1' : (empty($value['data']) || empty($value['index']) ? 'pack2' : 'pack3');
			else {
				$this->TempFillEls['tables'][$key]['img'] = empty($value['data']) && empty($value['index']) ? 'pack4' : (empty($value['data']) || empty($value['index']) ? 'pack5' : 'pack6');
				$this->TempFillEls['tables'][$key]['error'] = $value['_error'];
				$error_count++;
			}
		}
		$this->TempFillEls['dbrules_problem_count'] = $error_count;
		$this->TempFillEls['dbrules_problem_color'] = $error_count ? 'red' : 'steelblue';
		$this->clearAllTemplates();
	}
	
	function fillDBAction(){
		GLOBAL $COMMON_VARS;
		$this->TempFillEls['name'] = $this->db_rule['name'];
		$count = count($this->db_rule['structure']);
		for($i = 0; $i < $count; $i++){
			$this->TempFillEls['structure'][$i]['variable'] = 'structure["'.$this->db_rule['structure'][$i]['name'].'"] = "'.$this->db_rule['structure'][$i]['type'].'";';
		}
		$this->clearAllTemplates();
	}
	
	function clearAllTemplates(){
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'select_engine_snapshot');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'include_filedir_to_snapshot');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'exclude_filedir_from_snapshot');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'select_engine_db_rules');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'add_engine_db_rules');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'delete_engine_db_rules');
	}
}
?>