<?php

class CSSManagerView extends Slot {

	var $SectionID = 6;
	var $ModuleID = 4;

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

	function init() {
		global $TOOLS_PATH;
		switch(@$this->LocalTarget[0]) {
			default: $this->Scheme = 'default'; break;
			case 'ViewAll':
				if($this->server_type=='dev') {
					$request = $this->LocalTarget;
					$request['pageRecordsQty'] = 20;
					$request['curPageNumber'] = isset($this->LocalTarget[2])? $this->LocalTarget[2]+1 : 1;
					$this->Action->processEvent($this->SectionID,'select_csscomments',$request,$this);
					if(!empty($this->PreparedTemplates['_error'])) $this->Scheme = 'message';
						else {
							$this->getPageControl($this->Home.'/Registar/CSSManager/ViewAll/');
							if(empty($this->PreparedTemplates['curPage'])) $this->Scheme = 'empty';
						}
					$this->Scheme = 'viewall';
				}
				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(!empty($this->LocalTarget[0]) && $this->LocalTarget[0]=='Undo' && !empty($this->LocalTarget[1]) && validate(intval($this->LocalTarget[1])))
					$this->Request['form']['_undo_'] = $this->LocalTarget[1];
				$this->validateForm()
				and $this->Action->processEvent($this->SectionID,'save_css',$this->getRequestField('form'),$this)
				and header('Location: '.$this->Home.'/Registar/CSSManager'.$this->SID);
			}
			else header('Location: '.$this->Home.'/Registar/CSSManager/'.$this->SID);
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function saveFile($array) {
		global $ROOT_PATH;
		$filename = $ROOT_PATH.'/css/default.style.css';
		$handle = fopen($filename,"w");
		flock($handle,LOCK_EX);
		fwrite($handle,$this->getRequestField('file'));
		flock($handle,LOCK_UN);
		fclose($handle);
		@chmod($filename,0664);
	}

	function validateForm() {
		if(!validate($this->getRequestField('file'))) $this->errors['file'] = 'CSS is not valid';
		if(!is_AdvCleanText($this->getRequestFormField('comment'),5,4096)) $this->errors['comment'] = 'Comment is empty or is not valid';
		if(count($this->errors) > 0) return FALSE;
		return TRUE;
	}

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

	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['file'] = inputTextArea('file',(!empty($this->Request)? $this->getRequestField('file') : $this->getFile(0)),200,50,'style="width:870px;height:600px;"'.($this->server_type!='dev'? ' readonly' : ''));
		$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/CSSManager/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 cc.*,m.username as member from csscomment cc, member m where m.id=cc.member order by cc.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/css/'.$val['version'].'.inc')? array(array('version' => $val['version'])) : array()) : array(),
				);
			}
		}
		!empty($this->PreparedTemplates['_error']) and $this->errors['model'] = $this->PreparedTemplates['_error'];
		if(count($this->errors) > 0) $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->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'open');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'save');
	}
	
	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/css/'.$val['version'].'.inc')? array(array('version' => $val['version'])) : array(),
				);
			}
		}
	
	function getFile($name = 0) {
		global $ROOT_PATH;
		if(!empty($this->LocalTarget[0]) && $this->LocalTarget[0]=='Undo') {
			if(!empty($this->LocalTarget[1]) && validate(intval($this->LocalTarget[1]))) {
				include($ROOT_PATH.'\undo\css\\'.$this->LocalTarget[1].'.inc');
				return $Undo_object['css_file'];
			}
			else header('Location: '.$this->Home.'/Registar/CSSManager'.$this->SID);
		}
		else {
			if(!empty($name)) {
				$template = $ROOT_PATH.'/css/'.$name.'.style.css';
				if(file_exists($template)) {
					return implode('',file($template));
				}
				else return '';
			}
			else {
				$template = $ROOT_PATH.'/css/default.style.css';
				return implode('',file($template));
			}
		}
	}
}
?>