<?php

class ScriptEditorView extends Slot {

	var $SectionID = 4;
	var $ModuleID = 4;
	var $moduletype = '';
	var $ownerid = '';
	var $meta_type = '';

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

	function init() {
		$this->moduletype = 1;
		$this->ownerid = 30;
		$this->meta_type = 'Page';
		switch(@$this->LocalTarget[0]) {
			default:
				if(!empty($this->LocalTarget[1]) && validate(intval($this->LocalTarget[1])) && $this->LocalTarget[0]=='Undo')
					$undo = 1;
				else $undo = 0;
				$array = array('module' => 30);
				if($undo == 1) $array['undo'] = $this->LocalTarget[1];
				$this->Action->processEvent($this->SectionID,'open_js',$array,$this);
				$this->Scheme = 'default';
			break;
			case 'ViewAll':
				if($this->server_type=='dev') {
					$request[1] = 30;
					$request['pageRecordsQty'] = 20;
					$request['curPageNumber'] = isset($this->LocalTarget[2])? $this->LocalTarget[2]+1 : 1;
					$this->Action->processEvent($this->SectionID,'select_modulecomments',$request,$this);
					if(!empty($this->PreparedTemplates['_error'])) $this->Scheme = 'message';
						else {
							$this->getPageControl($this->Home.'/Registar/ScriptEditor/ViewAll/');
							if(empty($this->PreparedTemplates['curPage'])) $this->Scheme = 'empty';
						}
					$this->Scheme = 'viewall';
					//printArray($this->PreparedTemplates);
				}
				else $this->Scheme = 'default';
			break;
		}
		if(!empty($this->Request)) {
			if($this->server_type=='dev') {
				$this->Request['form']['id_user']=$this->Action->Security->currentuser->id;
				if($undo ==1) $this->Request['form']['_undo_'] = $this->LocalTarget[1];
				$this->Action->processEvent($this->SectionID,'save_meta',$this->getRequestField('form'),$this)
				and header('Location: '.$this->Home.'/Registar/ScriptEditor'.$this->SID);
			}
			else header('Location: '.$this->Home.'/Registar/ScriptEditor/'.$this->SID);
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'viewall': $this->fillViewAll(); break;
			default: case 'action': $this->fillAction(); break;
		}
	}

	function fillViewAll(){
		global $REGISTRY, $ROOT_PATH;
		foreach($this->PreparedTemplates['curPage'] as $ind => $val) {
			$tok = explode(".",$val['date']);
			$this->TempFillEls['comments'][] = array(
				'number' => ($this->PreparedTemplates['pageRecordsQty']*($this->PreparedTemplates['curPageNumber']-1))+($ind+1),
				'date' => $tok[0],
				'author' => $val['member'],
				'changed' => $val['value'],
				'version' => $val['version'],
				'button' =>  file_exists($ROOT_PATH.'/undo/module/'.$this->PreparedTemplates[1].'/'.$val['version'].'.inc')? array(array('id' => $this->PreparedTemplates[1],'version' => $val['version'])) : array(),
			);
		}
	}

	function fillAction() {
		global $CORE_PATH, $ROOT_PATH;
		$this->TempFillEls['addBut'] = $this->server_type=='dev'? 'visible' : 'hidden';
		$this->TempFillEls['pubBut'] = $this->server_type=='pro'? 'hidden' : 'visible';
		if(!empty($this->LocalTarget[1]) && validate(intval($this->LocalTarget[1])) && $this->LocalTarget[0]=='Undo')
			$this->TempFillEls['undo'] = '(Undo v.'.$this->LocalTarget[1].')';
		$this->TempFillEls['textareaValidJS']='if(parseInt(document.getElementById("comment").value.length+"-")<5){
								//document.getElementById("comment").focus();
								alert("Comment is empty or is not valid");
								return false;
								}';
		$this->TempFillEls['ViewAllButton'] = $this->server_type=='dev'? '<input type="button" name="View_All" class=adminsubmit value="View All" onclick="open(\'/Registar/ScriptEditor/ViewAll/\',\'displayWindow\',\'width=950,height=600,scrollbars=yes\');">' : '';
		$this->TempFillEls['textareaComment'] = $this->server_type=='dev'? '<textarea name=form[comment] id=comment cols=80 rows=5>'.(!empty($this->Request['form']['comment'])? $this->Request['form']['comment'] : '').'</textarea>' : '';
		$selected = $this->Db->SelectAny("select mc.*,m.username as member from modulecomment mc, member m where m.id=mc.member and mc.ownerid = 30 order by mc.version DESC LIMIT 10 OFFSET 0");
		if(!empty($selected)) {
			foreach($selected as $ind => $val) {
				$tok = explode(".",$val['date']);
				$this->TempFillEls['comment'][] = array(
					'number' => ($ind+1),
					'date' => $tok[0],
					'author' => $val['member'],
					'changed' => $val['value'],
					'version' => $val['version'],
					'button' => $this->server_type=='dev'? ($ind!=0 && file_exists($ROOT_PATH.'/undo/module/30/'.$val['version'].'.inc')? array(array('version' => $val['version'])) : array()) : array(),
				);
			}
		}
		$this->TempFillEls['ownerid'] = $this->ownerid;
		$this->TempFillEls['module'] = $this->ownerid;
		$this->TempFillEls['moduletype'] = $this->moduletype;
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open_js');
		foreach($this->PreparedTemplates as $value) {
			switch($value['type']['id']) {
				case 1: $this->TempFillEls['head'] = inputTextArea('form[js][head]',(!empty($this->Request)? $this->Request['form']['js']['head'] : @$value['value']),80,5,'style="width:870px;"'); break;
				case 2: $this->TempFillEls['body_before'] = inputTextArea('form[js][body_before]',(!empty($this->Request)? $this->Request['js']['body_before'] : @$value['value']),80,5,'style="width:870px;"'); break;
				case 3: $this->TempFillEls['body_after'] = inputTextArea('form[js][body_after]',(!empty($this->Request)? $this->Request['form']['js']['body_after'] : @$value['value']),80,5,'style="width:870px;"'); break;
			}
		}
		empty($this->TempFillEls['head']) and $this->TempFillEls['head'] = inputTextArea('form[js][head]',(!empty($this->Request)? $this->Request['form']['js']['head'] : ''),80,5,'style="width:870px;"');
		empty($this->TempFillEls['body_before']) and $this->TempFillEls['body_before'] = inputTextArea('form[js][body_before]',(!empty($this->Request)? $this->Request['form']['js']['body_before'] : ''),80,5,'style="width:870px;"');
		empty($this->TempFillEls['body_after']) and $this->TempFillEls['body_after'] = inputTextArea('form[js][body_after]',(!empty($this->Request)? $this->Request['form']['js']['body_after'] : ''),80,5,'style="width:870px;"');
		foreach($this->errors as $k => $v) $this->TempFillEls[$k.'_error'] = $k=='header'? $this->printError($v).'<p>' : $this->printError($v);
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'open_js');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'save');
	}
}
?>