<?php

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

	var $SectionID = 4;
	var $ModuleID = 4;
	var $moduletype = '';
	var $ownerid = '';
	var $meta_type = '';
	var $meta_name = '';
	var $reqfield = '<img src="/i/must.gif">&nbsp;';
	var $colors = array(
		'red' => '#B00000',
		'yellow' => '#FF8C00',
		'green' => '#008000',
		'black' => '#000000',
	);
	var $default_js = array(
		'1' => 'Do not execute Default JS for this area',
		'2' => 'Execute Default JS before this area',
		'3' => 'Execute Default JS after this area',
	);

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

	function init() {             
//		$age = $this->Db->SelectAny("select * from modulejszone");printArray($age);exit;

		$this->getGuides();
		switch(@$this->LocalTarget[0]) {
			default:
				$this->pagetypes = $this->Action->Server->getGuide('pagetype');
				$this->pagetypes[0] = 'No Type';
				$this->Scheme = 'default';
			break;
			case 'Json':
				$this->pagetypes = $this->Action->Server->getGuide('pagetype');
				$this->pagetypes[0] = 'No Type';
				$this->Scheme = 'json';
			break;
			case 'Page':
				if($this->server_type=='dev') {
					if(!empty($this->LocalTarget[1]) && validate(intval($this->LocalTarget[1]))) {
						$this->Action->processEvent($this->SectionID,'getlock',array('module' => $this->LocalTarget[1],'member' => $this->Action->Security->currentuser->id),$this);
						if(!empty($this->PreparedTemplates['access_flag']) && $this->PreparedTemplates['access_flag']!=$this->Action->Security->currentuser->id) {
							header('Location: '.$this->Home.'/Registar/MetaManager'.$this->SID);
						}
						else {
							if(empty($this->PreparedTemplates['access_flag'])) $this->Action->processEvent($this->SectionID,'lock',array('module' => $this->LocalTarget[1],'member' => $this->Action->Security->currentuser->id),$this);
							$page = 0;
							foreach($this->pages as $key => $value) {
								foreach($value as $k => $v) {
									if($v['id']==$this->LocalTarget[1] && $v['is_describe']) $page = 1;;
								}
							}
							if(!$page) header('Location: '.$this->Home.'/Registar/MetaManager'.$this->SID);
							$this->Scheme = 'info';
							$this->moduletype = 1;
							$this->ownerid = $this->LocalTarget[1];
							$this->meta_type = 'Page';
							$this->Action->processEvent($this->SectionID,'open_meta',array('module' => 1,'item' => $this->LocalTarget[1]),$this);
							$this->Action->processEvent($this->SectionID,'open_js',array('module' => $this->LocalTarget[1]),$this);
						}
					}
					else header('Location: '.$this->Home.'/Registar/MetaManager'.$this->SID);
				}
				else header('Location: '.$this->Home.'/Registar/MetaManager'.$this->SID);
			break;
		}
		if(!empty($this->Request)) {
			if($this->server_type=='dev' || $this->getRequestField('step')=='filter') {
				switch($this->getRequestField('step')) {
					case 'unlock':
						$this->Request['form']['member'] = $this->Action->Security->currentuser->id;
						$this->Action->processEvent($this->SectionID,'unlock',$this->getRequestField('form'),$this);
						if(!isset($_POST['do_not_redirect']))
							header('Location: '.$this->Home.'/Registar/MetaManager'.$this->SID);
						exit;
					break;
					default:
						$this->validateForm()
						and $this->Action->processEvent($this->SectionID,'save_meta',$this->getRequestField('form'),$this)
						and header('Location: '.$this->Home.'/Registar/MetaManager'.$this->SID);
					break;
					case 'filter':
						if(isset($this->Request['form']['pagetype'])) {
							$cookie = implode(',',array_keys($this->Request['form']['pagetype']));
							setcookie("pagetypes",$cookie,time()+86400*365,"/");
						}
						else setcookie("pagetypes",'',time()+86400*365,"/");
						header('Location: '.$this->Home.'/Registar/MetaManager'.$this->SID);
					break;
				}
			}
			else header('Location: '.$this->Home.'/Registar/MetaManager/'.$this->SID);
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function validateForm() {
		if(!is_AdvCleanText($this->Request['form']['meta']['title'],3,512)) $this->errors['title'] = 'Page\'s Title is empty or is not valid';
		if(!is_AdvCleanText($this->Request['form']['meta']['keywords'],3,512)) $this->errors['keywords'] = 'Page\'s Keywords is empty or is not valid';
		if(!is_AdvCleanText($this->Request['form']['meta']['description'],3,512)) $this->errors['description'] = 'Page\'s Description is empty or is not valid';
		if(count($this->errors)>0) return FALSE;
		return TRUE;
	}

	function getGuides() {
		$this->Action->processEvent($this->SectionID,'select_pages',array(),$this);
		$this->pages = $this->PreparedTemplates;
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'select_pages');
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'info': $this->fillInfo(); break;
			case 'json': $this->fillJson(); break;
			default: $this->fillSelect(); break;
		}
	}

	function fillInfo($array = array()) {
		$this->TempFillEls['sec_name'] = $this->getName($this->ownerid);
		$this->TempFillEls['ownerid'] = $this->ownerid;
		$this->TempFillEls['module'] = $this->ownerid;
		$this->TempFillEls['moduletype'] = $this->moduletype;
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open_meta');
		foreach($this->PreparedTemplates as $value) {
			switch($value['metatype']['id']) {
				case 1: $this->TempFillEls['title'] = inputTextArea('form[meta][title]',(!empty($this->Request)? $this->Request['form']['meta']['title'] : @$value['value']),80,1,'style="width:870px;"'); break;
				case 2: $this->TempFillEls['description'] = inputTextArea('form[meta][description]',(!empty($this->Request)? $this->Request['form']['meta']['description'] : @$value['value']),80,3,'style="width:430px;"'); break;
				case 3: $this->TempFillEls['keywords'] = inputTextArea('form[meta][keywords]',(!empty($this->Request)? $this->Request['form']['meta']['keywords'] : @$value['value']),80,3,'style="width:430px;"'); break;
			}
		}
		empty($this->TempFillEls['title']) and $this->TempFillEls['title'] = inputTextArea('form[meta][title]',(!empty($this->Request)? $this->Request['form']['meta']['title'] : ''),80,1,'style="width: 870px;"');
		empty($this->TempFillEls['description']) and $this->TempFillEls['description'] = inputTextArea('form[meta][description]',(!empty($this->Request)? $this->Request['form']['meta']['description'] : ''),80,3,'style="width: 430px;"');
		empty($this->TempFillEls['keywords']) and $this->TempFillEls['keywords'] = inputTextArea('form[meta][keywords]',(!empty($this->Request)? $this->Request['form']['meta']['keywords'] : ''),80,3,'style="width: 430px;"');
		$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,10,'style="width:880px;"');
					$this->TempFillEls['head_use_default'] = inputSelect('form[js][head_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['head_use_default'] : (empty($value['use_default'])? 3 : $value['use_default'])));
				break;
				case 2:
					$this->TempFillEls['body_before'] = inputTextArea('form[js][body_before]',(!empty($this->Request)? $this->Request['js']['body_before'] : @$value['value']),80,10,'style="width:880px;"');
					$this->TempFillEls['before_use_default'] = inputSelect('form[js][before_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['before_use_default'] : (empty($value['use_default'])? 2 : $value['use_default'])));
				break;
				case 3:
					$this->TempFillEls['body_after'] = inputTextArea('form[js][body_after]',(!empty($this->Request)? $this->Request['form']['js']['body_after'] : @$value['value']),80,10,'style="width:880px;"');
					$this->TempFillEls['after_use_default'] = inputSelect('form[js][after_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['after_use_default'] : (empty($value['use_default'])? 3 : $value['use_default'])));
				break;
			}
		}
		empty($this->TempFillEls['head']) and $this->TempFillEls['head'] = inputTextArea('form[js][head]',(!empty($this->Request)? $this->Request['form']['js']['head'] : ''),80,10,'style="width:880px;"');
		empty($this->TempFillEls['head_use_default']) and $this->TempFillEls['head_use_default'] = inputSelect('form[js][head_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['head_use_default'] : 3));
		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,10,'style="width:880px;"');
		empty($this->TempFillEls['before_use_default']) and $this->TempFillEls['before_use_default'] = inputSelect('form[js][before_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['before_use_default'] : 2));
		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,10,'style="width:880px;"');
		empty($this->TempFillEls['after_use_default']) and $this->TempFillEls['after_use_default'] = inputSelect('form[js][after_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['after_use_default'] : 3));
		foreach($this->errors as $k => $v) $this->TempFillEls[$k.'_error'] = $k=='header'? $this->printError($v).'<p>' : $this->printError($v);
		//printArray($this->TempFillEls);
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'open_meta');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'open_js');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'select_pages');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'save');
	}

	function getParents($id,$arr){
		$count=count($this->allPage);
		for($i=0;$i<$count;$i++){
			if($this->allPage[$i]['parent']==$id) $arr[]=$this->allPage[$i];
			if($this->allPage[$i]['id']==$id) $arr = $this->getParents($this->allPage[$i]['parent'],$arr);
		}
		return $arr;
	}

	function fillJson(){
		global $REGISTRY,$subdomains,$signature;
		switch($this->LocalTarget[1]){
			case 'getSavePosition':
				if(isset($this->LocalTarget[2])&&is_numeric($this->LocalTarget[2])){
//					$this->allPage = $this->Db->SelectAny("select id, name, type, access_flag, parent, is_describe, date_locked, path,('now' - date_changed) as interval, sort_order, controlid from module where name!='' and (type=1 or type=3 or type=6 or type=8)  order by parent , sort_order ASC ");
					$this->allPage = $this->Db->SelectAny("select id, name, type, access_flag, parent, is_describe, date_locked, path,TIMESTAMPDIFF(day,date_changed,NOW()) as 'interval', sort_order, controlid from module where name!='' and (type=1 or type=3 or type=6 or type=8)  order by parent , sort_order ASC ");
					$pagetypes = array();
					if(isset($_COOKIE['pagetypes'])) {
						$pagetypes = explode(',',$_COOKIE['pagetypes']);
					}
					else $pagetypes = array_keys($this->pagetypes);
					foreach($this->allPage as $key => $value) {
						if($value['is_describe']==1 && !in_array($value['controlid'],$pagetypes)) unset($this->allPage[$key]);
					}
					$this->allPage = array_values($this->allPage);
					$aParents = $this->getParents($this->LocalTarget[2],Array());
					$countParents=count($aParents);
				}
			//break;
			case 'getChild':
				if(isset($this->LocalTarget[2])&&(is_numeric($this->LocalTarget[2])||$this->LocalTarget[2]=='All')){
					if(!isset($this->allPage))
//						$this->allPage = $this->Db->SelectAny("select id, name, type, access_flag, parent, is_describe, date_locked, path,('now' - date_changed) as interval, sort_order, controlid from module where name!='' and (type=1 or type=3 or type=6 or type=8)  order by parent , sort_order ASC ");
						$this->allPage = $this->Db->SelectAny("select id, name, type, access_flag, parent, is_describe, date_locked, path,TIMESTAMPDIFF(day,date_changed,NOW()) as 'interval', sort_order, controlid from module where name!='' and (type=1 or type=3 or type=6 or type=8)  order by parent , sort_order ASC ");
					$pagetypes = array();
					if(isset($_COOKIE['pagetypes'])) {
						$pagetypes = explode(',',$_COOKIE['pagetypes']);
					}
					else $pagetypes = array_keys($this->pagetypes);
					foreach($this->allPage as $key => $value) {
						if($value['is_describe']==1 && !in_array($value['controlid'],$pagetypes)) unset($this->allPage[$key]);
					}
					$this->allPage = array_values($this->allPage);
					$users = $this->Db->SelectGuide('member g', array('g.id','g.username as name'),array(1 => 1));
					$select_servers=$this->Db->SelectAny("select path from preset");
					$countServers=count($select_servers);
					$hasCurServer='false';
					foreach($select_servers as $value)
						if($value['path']=='http://'.$_SERVER['HTTP_HOST'].'/') $hasCurServer='true';
					$servers_num = $this->Db->SelectAny('select m.id,p.preset,pr.name ,p.date_published,p.type from publish p,module m, preset pr where p.ownerid=m.id and p.date_published>=m.date_changed and p.preset=pr.id and (p.type=3)');
					$pagesOnServers= Array();
					foreach($servers_num as $value){
						$times=explode('.',$value['date_published']);
						$pagesOnServers[$value['id']][$value['name']]=$times[0];
					}
					$cnt=0;
					$arrId = Array();
					$arrPath = Array();
					$count=count($this->allPage);
					$countParents=(isset($aParents))? count($aParents): 0;
					for($i=0;$i<$count;$i++){
						for($k=0;$k<$countParents;$k++)
							if($aParents[$k]['id']==$this->allPage[$i]['id'])
								$this->LocalTarget[2]=$aParents[$k]['parent'];
						if(($this->allPage[$i]['parent']==$this->LocalTarget[2]||$this->LocalTarget[2]=='All')&&$this->allPage[$i]['parent']!=0){
							$cnt++;
							$arrId[]=$this->allPage[$i]['id'];
						}
					}
					$arrIsChild=$this->is_child_arr($arrId);
					$this->TempFillEls['items'] = Array();
					$cntNow=0;
					if(empty($subdomains)) $subdomains = $this->Db->SelectList('domain','subdomain',array('domain'));
					for($i=0;$i<$count;$i++){
						for($k=0;$k<$countParents;$k++)
							if($aParents[$k]['id']==$this->allPage[$i]['id'])
								$this->LocalTarget[2]=$aParents[$k]['parent'];
						$arrPath[$this->allPage[$i]['id']]=(!isset($arrPath[$this->allPage[$i]['parent']]))? $this->getPath($this->allPage[$i]['id'],$this->allPage): $arrPath[$this->allPage[$i]['parent']].'/'.$this->allPage[$i]['path'];
						if(($this->allPage[$i]['parent']==$this->LocalTarget[2]||$this->LocalTarget[2]=='All')&&$this->allPage[$i]['parent']!=0){
							$cntNow++;
							$status=number_format(((count($this->TempFillEls['items'])+1)/$cnt)*100,0);
							$lock=!empty($this->allPage[$i]['access_flag']) &&$this->allPage[$i]['access_flag']!=$this->Action->Security->currentuser->id? true: false;
							$button='*';
							$lockLine= Array();
							if(!$lock){
								$button=($this->allPage[$i]['is_describe']==0)? 'V' : '';
								$button.=(!empty($this->allPage[$i]['access_flag']) && $this->allPage[$i]['access_flag']==$this->Action->Security->currentuser->id)? '':'U';

							}else{
								$lockLine['time']=$this->getTime($this->allPage[$i]['date_locked']);
								$lockLine['username']=$users[$this->allPage[$i]['access_flag']];
							}
							$publishServers=(isset($pagesOnServers[$this->allPage[$i]['id']]))? count($pagesOnServers[$this->allPage[$i]['id']]):'0';
							$level=(count(explode('/',$arrPath[$this->allPage[$i]['id']]))-2)*20;
							if($this->server_type!='dev') {
								$button='*';
								$lockLine = array();
							}
							if(preg_match("/^(\/subdomains)/i", $arrPath[$this->allPage[$i]['id']])) {
								$paths = explode('/',strtolower(preg_replace('/^\//i','',$arrPath[$this->allPage[$i]['id']])));
								array_shift($paths);
								if(in_array($paths[0],$subdomains)) {
									$this->allPage[$i]['sub'] = 1;
									$this->allPage[$i]['viewpath'] = 'http://'.(isset($REGISTRY['typedomain']) ? $REGISTRY['typedomain'].'.' : '').$paths[0].'.'.$signature['domain'][0].preg_replace('/^(\/subdomains\/'.$paths[0].')/i', '', $arrPath[$this->allPage[$i]['id']]);
								}
								else {
									$this->allPage[$i]['sub'] = 0;
									$this->allPage[$i]['viewpath'] = 'http://'.(isset($REGISTRY['typedomain']) ? $REGISTRY['typedomain'].'.' : '').$signature['domain'][0].$arrPath[$this->allPage[$i]['id']];
								}
							}
							else {
								$this->allPage[$i]['viewpath'] = 'http://'.(isset($REGISTRY['typedomain']) ? $REGISTRY['typedomain'].'.' : '').$signature['domain'][0].$arrPath[$this->allPage[$i]['id']];
								$this->allPage[$i]['sub'] = 0;
							}
							unset($paths);
							$this->TempFillEls['items'][$this->allPage[$i]['id']]=array(
								'pubBut' => $this->server_type=='pro' || $this->allPage[$i]['type'] == 8? 'hidden' : 'visible',
								'funcName' => 'pageControler.addItem',
								'id' => $this->allPage[$i]['id'],
								'name' => $this->allPage[$i]['name'],
								'parent' => $this->allPage[$i]['parent'],
								'bullet' => (isset($arrIsChild[$this->allPage[$i]['id']]))? '_plus'.($REGISTRY['HomeTarget']!=$this->allPage[$i]['id']? ($this->allPage[$i]['sub']? '_black' : '') : '_home_page') : ($REGISTRY['HomeTarget']!=$this->allPage[$i]['id']? ($this->allPage[$i]['is_describe']==0? ($this->allPage[$i]['sub']? '_black' : '_green') : ($this->allPage[$i]['sub']? '_1a' : '')) : '_home_page'),
								'radioChecked' => $REGISTRY['HomeTarget']!=$this->allPage[$i]['id']? '': 'checked',
								'radioDisabled' => ($this->allPage[$i]['is_describe']==0)?  'disabled':(!empty($lockLine)? 'disabled' : ''),
								'path' => $this->allPage[$i]['viewpath'],
								'level' => $level,
								'status' => $status,
								'all' => $cnt,
								'now' => $cntNow,
								'button' => $button,
								'hasServerInfo' => ($publishServers!=0)? 'true':'false',
								'lockLine' => (!empty($lockLine))? $lockLine['username'].'|'.$lockLine['time'] :'*',
								'is_child'=> isset($arrIsChild[$this->allPage[$i]['id']])? 'true':'false',
								'checkDisabled' => !empty($lockLine)? 'disabled' : '',
								'servers' => '[ '.$publishServers.' / '.$countServers.' ]',
								'colorServers' => !isset($pagesOnServers[$this->allPage[$i]['id']])? 'red':($hasCurServer? ($countServers-count($pagesOnServers[$this->allPage[$i]['id']])<=1? 'black':'blue'): ($countServers==count($pagesOnServers[$this->allPage[$i]['id']])? 'black':'blue')),
								'color' => $this->colors[$this->allPage[$i]['interval'] > 30? 'black' : ($this->allPage[$i]['interval'] > 14 && $this->allPage[$i]['interval'] <= 30? 'green' : ($this->allPage[$i]['interval'] > 7 && $this->allPage[$i]['interval'] <= 14? 'yellow' : 'red'))],
							);
							$this->TempFillEls['itemsfunc'][0]='';
						}
					}
				}
			break;
			case 'getServerInfo':
				if(isset($this->LocalTarget[2])&&is_numeric($this->LocalTarget[2])){
					$servers_num = $this->Db->SelectAny('select m.id,p.preset,pr.name ,p.date_published,p.type from publish p,module m, preset pr where p.ownerid=m.id and p.date_published>=m.date_changed and p.preset=pr.id and (p.type=3) and m.id='.$this->LocalTarget[2]);
					foreach($servers_num as $value)
						$this->TempFillEls['serverInfo'][]=array(
							'funcName' => 'pageControler.addServerInfo',
							'idPage' => $this->LocalTarget[2],
							'serverName' => $value['name'],
							'time' => $this->getTime($value['date_published']),
						);
					$this->TempFillEls['serverinfofunc'][0]['idPage']=$this->LocalTarget[2];
				}
			break;
			default:break;
		}
	}

	function fillSelect() {
		$this->TempFillEls['addBut'] = $this->server_type=='dev'? 'visible' : 'hidden';
		$this->TempFillEls['pubBut'] = $this->server_type=='pro'? 'hidden' : 'visible';
		$pagetypes = array();
		if(isset($_COOKIE['pagetypes'])) {
			$pagetypes = explode(',',$_COOKIE['pagetypes']);
		}
		else $pagetypes = array_keys($this->pagetypes);
		$this->TempFillEls['filter_count'] = count($pagetypes);
		$this->TempFillEls['filter_all'] = count($this->pagetypes);
		$this->TempFillEls['disabled'] = count($pagetypes) > 0? '' : 'disabled';
		foreach($this->pagetypes as $key => $value) {
			$this->TempFillEls['pagetypes'][] = array(
				'check' => inputCheckbox('form[pagetype]['.$key.']',(in_array($key,$pagetypes)? TRUE : FALSE),'onclick="setPT(this)"'),
				'name' => $value,
			);
		}
		$this->setArrayPage();
	}

	function setArrayPage(){
		global $REGISTRY;
//		$this->allPage = $this->Db->SelectAny("select id, name, type, access_flag, parent, is_describe, date_locked, path,('now' - date_changed) as interval, sort_order, controlid from module where name!='' and (type=1 or type=3 or type=6 or type=8) order by parent , sort_order ASC ");
		$this->allPage = $this->Db->SelectAny("select id, name, type, access_flag, parent, is_describe, date_locked, path,TIMESTAMPDIFF(day,date_changed,NOW()) as 'interval', sort_order, controlid from module where name!='' and (type=1 or type=3 or type=6 or type=8) and id not in (1,20)  order by parent , sort_order ASC ");
		$pagetypes = array();
		if(isset($_COOKIE['pagetypes'])) {
			$pagetypes = explode(',',$_COOKIE['pagetypes']);
		}
		else $pagetypes = array_keys($this->pagetypes);
		foreach($this->allPage as $key => $value) {
			if($value['is_describe']==1 && !in_array($value['controlid'],$pagetypes)) unset($this->allPage[$key]);
		}
		$this->allPage = array_values($this->allPage);
		$select_servers=$this->Db->SelectAny("select path from preset");
		$countServers=count($select_servers);
		$hasCurServer='false';
		foreach($select_servers as $value)
			if($value['path']=='http://'.$_SERVER['HTTP_HOST'].'/')
				$hasCurServer='true';
		$servers_num = $this->Db->SelectAny('select m.id,p.preset,pr.name ,p.date_published,p.type from publish p,module m, preset pr where p.ownerid=m.id and p.date_published>=m.date_changed and p.preset=pr.id and  (p.type=3)');
		$pagesOnServers= array();
		foreach($servers_num as $value){
			$times=explode('.',$value['date_published']);
			$pagesOnServers[$value['id']][$value['name']]=$times[0];
		}
		$users = $this->Db->SelectGuide('member g', array('g.id','g.username as name'),array(1 => 1));
		$count=count($this->allPage);
		$flagHP=false;
		for($i=0;$i < $count;$i++){
			$isHP = false;
			if($REGISTRY['HomeTarget'] == $this->allPage[$i]['id']){
				$flagHP = true;
				$isHP = true;
			}
			if($this->allPage[$i]['parent']==0){
				$is_child=$this->is_child($this->allPage[$i]['id']);
				$lock=!empty($this->allPage[$i]['access_flag']) &&$this->allPage[$i]['access_flag']!=$this->Action->Security->currentuser->id? true: false;
				$publishServers=(isset($pagesOnServers[$this->allPage[$i]['id']]))? count($pagesOnServers[$this->allPage[$i]['id']]):'0';
				$this->TempFillEls['idFirstLevel'][]=Array('id'=>$this->allPage[$i]['id']);
				if(preg_match("/^(subdomains)/i", $this->allPage[$i]['path']) && $this->allPage[$i]['type']==8) $this->allPage[$i]['sub'] = 1;
				else $this->allPage[$i]['sub'] = 0;
				$this->TempFillEls['pages'][$this->allPage[$i]['id']]=array(
					'funcName' => 'pageControler.addItem',
					'id' => $this->allPage[$i]['id'],
					'name' => $this->allPage[$i]['name'],
					'bullet' => $is_child? '_plus'.($isHP? '_home_page' : ($this->allPage[$i]['sub']? '_black' : '')) : ($isHP? '_home_page' : ($this->allPage[$i]['is_describe']==0? ($this->allPage[$i]['sub']? '_black' : '_green') : ($this->allPage[$i]['sub']? '_1a' : ''))),
					'radioChecked' => $REGISTRY['HomeTarget']!=$this->allPage[$i]['id']? '': 'checked',
					'radioDisabled' => ($this->allPage[$i]['is_describe']==0)?  'disabled':($lock? 'disabled' : ''),
					'path' => '/'.$this->allPage[$i]['path'],
					'bV' => ($this->allPage[$i]['is_describe']==0)? 'hidden' : 'vissible',
					'bU' => (!empty($this->allPage[$i]['access_flag']) && $this->allPage[$i]['access_flag']==$this->Action->Security->currentuser->id)? 'vissible':'hidden',
					'pubBut' => $this->server_type=='pro' || $this->allPage[$i]['type'] == 8? 'hidden' : 'visible',
					'is_child' => $is_child? 'true':'false',
					'cursor' => $is_child? 'pointer':'',
					'displayBut' =>  $this->server_type=='dev'? ($lock? 'none': 'block') : 'none',
					'displayLockLine' => $this->server_type=='dev'? ($lock? 'block': 'none') : 'none',
					'checkDisabled' => $this->server_type=='dev'? ($lock? 'disabled' : '') : '',
					'time' => $lock? $this->getTime($this->allPage[$i]['date_locked']) : '',
					'username' => $lock?  $users[$this->allPage[$i]['access_flag']]: '',
					'countServers' => $countServers,
					'colorServers' => !isset($pagesOnServers[$this->allPage[$i]['id']])? 'red':($hasCurServer? ($countServers-count($pagesOnServers[$this->allPage[$i]['id']])<=1? 'black':'blue'): ($countServers==count($pagesOnServers[$this->allPage[$i]['id']])? 'black':'blue')),
					'publishServers' => $publishServers,
					'hasServerInfo' => ($publishServers!=0)? 'true':'false',
					'action' => ($is_child)? 'getChild': '',
					'color' => $this->colors[$this->allPage[$i]['interval'] > 30? 'black' : ($this->allPage[$i]['interval'] > 14 && $this->allPage[$i]['interval'] <= 30? 'green' : ($this->allPage[$i]['interval'] > 7 && $this->allPage[$i]['interval'] <= 14? 'yellow' : 'red'))],
				);
			}
		}
		if($flagHP)
			$this->TempFillEls['idHomePage'][0]=Array('idHP'=>$REGISTRY['HomeTarget']);
	}

	function getPath($id,$arr,$path=Array()){
		foreach($arr as $value){
			if($value['id']==$id){
				$path[]=$value['path'];
				return $this->getPath($value['parent'],$arr,$path);
			}
		}
		return '/'.implode('/',array_reverse($path));
	}

	function getTime($str){
		$time=explode('.',$str);
		return $time[0];
	}

	function is_child($parent){
		$count = count($this->allPage);
		for($i=0;$i < $count;$i++)
			if($this->allPage[$i]['parent']==$parent) return true;
		return false;
	}

	function is_child_arr($arr){
		$count = count($this->allPage);
		$count_id=count($arr);
		$res = Array();
		for($i=0;$i < $count;$i++)
			for($k=0;$k<$count_id;$k++){
				if($this->allPage[$i]['parent']==$arr[$k]) $res[$arr[$k]]=true;
			}
		return $res;
	}

	function setTargetArray($array = array(),$level = 1,$levels = array(),$path = '',$subitems = array()) {
		$i = 0;
		foreach($array as $key => $value) {
			$levels[$level] = $i == sizeof($array)-1? 1 : 0;
			$this->TempFillEls['pages'][$value['id']] = array(
				'id' => $value['id'],
				'path' => $path.'/'.$value['path'],
				'name' => $value['name'],
				'width' => intval($level*19),
				'width2' => 622 - intval($level*19),
				'pub_disabled' => empty($value['is_describe']) ? 'disabled' : '',
				'publish_name' => $value['name'],
				'start_div' => '<div id=page'.$value['id'].(!empty($this->pages[$value['id']])? ' style="display:none;"' : '').'>',
				'minus' => !empty($this->pages[$value['id']])? '_minus' : '',
				'update' => empty($value['is_describe'])? array() : array(array('id' => $value['id'])),
				'noupdate' => empty($value['is_describe'])? array(array()) : array(),
			);
			if(!empty($this->pages[$value['id']])) {
				$this->TempFillEls['pages'][$value['id']]['header'] = array(array(
					'id' => $value['id'],
					'path' => $path.'/'.$value['path'],
					'name' => $value['name'],
					'width' => intval($level*19),
					'width2' => 622 - intval($level*19) - 19,
					'pub_disabled' => empty($value['is_describe']) ? 'disabled' : '',
					'publish_name' => $value['name'],
					'update' => empty($value['is_describe'])? array() : array(array('id' => $value['id'])),
					'noupdate' => empty($value['is_describe'])? array(array()) : array(),
				));
				$this->TempFillEls['pages'][$value['id']]['onclick'] = array(array('id' => $value['id']));
				$this->setTargetArray($this->pages[$value['id']],$level+1,$levels,$path.'/'.$value['path']);
			}
			else {
				if($level==1) $this->TempFillEls['pages'][$value['id']]['end_div'] = '</div>';
				else $this->TempFillEls['pages'][$value['id']]['end_div'] = !empty($levels[$level])? (!empty($levels[$level-1])? (!empty($levels[$level-2])? str_repeat('</div>',4) : str_repeat('</div>',3)) : str_repeat('</div>',2)) : '</div>';
			}
			$i++;
		}
	}

	function getName($id = 1) {
		foreach($this->pages as $key => $value) {
			foreach($value as $k => $v) {
				if($v['id']==$id) return $v['name'];
			}
		}
		return '';
	}
}
?>