<?php

class MenuManagerView extends Slot {

	var $ModuleID = 4;
	var $SectionID = 5;
	var $sort_order = 1;
	var $colors = array(
		'red' => '#DC143C',
		'yellow' => '#FF8C00',
		'green' => '#006400',
		'black' => '#000000',
		'blue' => '#4169E1',
	);

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

	function init() {
		global $TOOLS_PATH;
		$undo = 0;
		$this->Scheme = 'default';
		switch(@$this->LocalTarget[0]) {
			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_menucomments',$request,$this);
					if(!empty($this->PreparedTemplates['_error'])) $this->Scheme = 'message';
					else {
						$this->getPageControl($this->Home.'/Registar/MenuManager/ViewAll/'.$this->LocalTarget[1]);
						if(empty($this->PreparedTemplates['curPage'])) $this->Scheme = 'empty';
					}
					$this->Scheme = 'viewall';
				}
				else $this->Scheme = 'default';
			break;
			case 'Preview':
				if($this->server_type=='dev') $this->Scheme = 'preview';
				else $this->Scheme = 'default';
			break;
			case 'XHR':
				if(empty($this->LocalTarget[1])) {
					$this->Action->processEvent($this->SectionID,'getxhr',array('id' => 0),$this);
					$this->Scheme = 'actionxhr';
				}
				else {
					$array = array('id' => $this->LocalTarget[1]);
					$this->Action->processEvent($this->SectionID,'getxhr',$array,$this);
					$this->Scheme = 'actionxhr';
				}
				break;
			case 'Preview':
				if($this->server_type=='dev') $this->Scheme = 'preview';
				else $this->Scheme = 'default';
			break;
			case 'MenuList':
				if($this->server_type=='dev') {
					$this->Action->processEvent($this->SectionID,'select_list',array('server_type'=>$this->server_type,'currentuser_id'=>$this->Action->Security->currentuser->id),$this);
 					$this->Scheme = 'list';
				}
				else $this->Scheme = 'default';
			break;
			case 'Json':
				$this->pagetypes = $this->Action->Server->getGuide('pagetype');
				$this->pagetypes[0] = 'No Type';
				switch(@$this->LocalTarget[1]){
					case 'getSavePosition':
					case 'getChild':
						if(isset($this->LocalTarget[2])&&(is_numeric($this->LocalTarget[2])||$this->LocalTarget[2]=='All')){
							$this->Action->processEvent($this->SectionID,'getChild',array('LocalTarget2'=>$this->LocalTarget[2],'server_type'=>$this->server_type,'currentuser_id'=>$this->Action->Security->currentuser->id),$this);
						}
					break;
					case 'getServerInfo':
						if(isset($this->LocalTarget[2]) && is_numeric($this->LocalTarget[2])){
							$this->Action->processEvent($this->SectionID,'getServerInfo',array('LocalTarget1'=>$this->LocalTarget[1],'LocalTarget2'=>$this->LocalTarget[2]),$this);
						}
					break;
					default:break;
				}
				$this->Scheme = 'json';
			break;
			case 'Item':
				if($this->server_type=='dev') {
					switch(@$this->LocalTarget[1]) {
						case 'Add': $this->Scheme = 'item'; break;
					}
				}
				else $this->Scheme = 'default';
			break;
			case 'LeftMenu':
				if($this->server_type=='dev') {
					$this->type = 'left';
					switch(@$this->LocalTarget[1]) {
						case 'Add':
							$this->Action->processEvent($this->SectionID,'get',array('id' => 0),$this);
							$this->menu = $this->PreparedTemplates;
							$this->Scheme = 'action';
						break;
						case 'Update': case 'Duplicate': case 'Delete':
							if(empty($this->LocalTarget[2])) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
							else {
								if(!empty($this->LocalTarget[4]) && validate(intval($this->LocalTarget[4])) && $this->LocalTarget[3]=='Undo')
									$undo = 1;
								else $undo = 0;
								$array = array('id' => $this->LocalTarget[2]);
								if($undo == 1) $array['undo'] = $this->LocalTarget[4];
								$this->Action->processEvent($this->SectionID,'getlock',array('menu' => $this->LocalTarget[2],'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/MenuManager'.$this->SID);
								}else{
									if(empty($this->PreparedTemplates['access_flag']))
										$this->Action->processEvent($this->SectionID,'lock',array('menu' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
									$this->Action->processEvent($this->SectionID,'get',$array,$this);
									$this->menu = $this->PreparedTemplates;
									if($this->menu['type']!=4) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
									else if(empty($this->menu['name'])) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
									else $this->Scheme = $this->LocalTarget[1]=='Delete'? 'delete' : 'action';
								}
							}
						break;
						case 'Unlock':
							if(isset($this->LocalTarget[2])){
								$idMenu=$this->LocalTarget[2];
								$this->Action->processEvent($this->SectionID,'getlock',array('menu' => $idMenu,'member' => $this->Action->Security->currentuser->id),$this);
								if(!empty($this->PreparedTemplates))
									$this->Action->processEvent($this->SectionID,'unLock',array('menu' => $idMenu,'member' => $this->Action->Security->currentuser->id),$this);
							}
							header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
						break;
						default: header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID); break;
					}
				}
				else  header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
			break;
			case 'RightMenu':
				if($this->server_type=='dev') {
					$this->type = 'right';
					switch(@$this->LocalTarget[1]) {
						case 'Add':
							$this->Action->processEvent($this->SectionID,'get',array('id' => 0),$this);
							$this->menu = $this->PreparedTemplates;
							$this->Scheme = 'action';
						break;
						case 'Update': case 'Duplicate': case 'Delete':
							if(empty($this->LocalTarget[2])) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
							else {
								if(!empty($this->LocalTarget[4]) && validate(intval($this->LocalTarget[4])) && $this->LocalTarget[3]=='Undo')
									$undo = 1;
								else $undo = 0;
								$array = array('id' => $this->LocalTarget[2]);
								if($undo == 1) $array['undo'] = $this->LocalTarget[4];
								$this->Action->processEvent($this->SectionID,'getlock',array('menu' => $this->LocalTarget[2],'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/MenuManager'.$this->SID);
								}else{
									if(empty($this->PreparedTemplates['access_flag']))
										$this->Action->processEvent($this->SectionID,'lock',array('menu' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
									$this->Action->processEvent($this->SectionID,'get',$array,$this);
									$this->menu = $this->PreparedTemplates;
									if($this->menu['type']!=6) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
									else if(empty($this->menu['name'])) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
									else $this->Scheme = $this->LocalTarget[1]=='Delete'? 'delete' : 'action';
								}
							}
						break;
						case 'Unlock':
							$idMenu=$this->LocalTarget[2];
							$this->Action->processEvent($this->SectionID,'getlock',array('menu' => $idMenu,'member' => $this->Action->Security->currentuser->id),$this);
							if(!empty($this->PreparedTemplates)){
								$this->Action->processEvent($this->SectionID,'unLock',array('menu' => $idMenu,'member' => $this->Action->Security->currentuser->id),$this);
							}
							header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
						break;
						default: header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID); break;
					}
				}
				else  header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
			break;
			case 'TabsMenu':
				if($this->server_type=='dev') {
					$this->type = 'tabs';
					switch(@$this->LocalTarget[1]) {
						case 'Add':
							$this->Action->processEvent($this->SectionID,'get',array('id' => 0),$this);
							$this->menu = $this->PreparedTemplates;
							$this->Scheme = 'action';
						break;
						case 'Update': case 'Duplicate': case 'Delete':
							if(empty($this->LocalTarget[2])) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
							else {
								if(!empty($this->LocalTarget[4]) && validate(intval($this->LocalTarget[4])) && $this->LocalTarget[3]=='Undo')
									$undo = 1;
								else $undo = 0;
								$array = array('id' => $this->LocalTarget[2]);
								if($undo == 1) $array['undo'] = $this->LocalTarget[4];
								$this->Action->processEvent($this->SectionID,'getlock',array('menu' => $this->LocalTarget[2],'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/MenuManager'.$this->SID);
								}else{
									if(empty($this->PreparedTemplates['access_flag']))
										$this->Action->processEvent($this->SectionID,'lock',array('menu' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
									$this->Action->processEvent($this->SectionID,'get',$array,$this);
									$this->menu = $this->PreparedTemplates;
									if($this->menu['type']!=7) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
									else if(empty($this->menu['name'])) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
									else $this->Scheme = $this->LocalTarget[1]=='Delete'? 'delete' : 'action';
								}
							}
						break;
						case 'Unlock':
							$idMenu=$this->LocalTarget[2];
							$this->Action->processEvent($this->SectionID,'getlock',array('menu' => $idMenu,'member' => $this->Action->Security->currentuser->id),$this);
							if(!empty($this->PreparedTemplates)){
								$this->Action->processEvent($this->SectionID,'unLock',array('menu' => $idMenu,'member' => $this->Action->Security->currentuser->id),$this);
							}
							header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
						break;
						default: header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID); break;
					}
				}
				else header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
			break;
			case 'BottomMenu':
				if($this->server_type=='dev') {
					$this->type = 'bottom';
					switch(@$this->LocalTarget[1]) {
						case 'Add':
							$this->Action->processEvent($this->SectionID,'get',array('id' => 0),$this);
							$this->menu = $this->PreparedTemplates;
							$this->Scheme = 'action';
						break;
						case 'Update': case 'Duplicate': case 'Delete':
							if(empty($this->LocalTarget[2])) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
							else {
								if(!empty($this->LocalTarget[4]) && validate(intval($this->LocalTarget[4])) && $this->LocalTarget[3]=='Undo')
									$undo = 1;
								else $undo = 0;
								$array = array('id' => $this->LocalTarget[2]);
								if($undo == 1) $array['undo'] = $this->LocalTarget[4];
								$this->Action->processEvent($this->SectionID,'getlock',array('menu' => $this->LocalTarget[2],'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/MenuManager'.$this->SID);
								}else{
									if(empty($this->PreparedTemplates['access_flag']))
										$this->Action->processEvent($this->SectionID,'lock',array('menu' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
									$this->Action->processEvent($this->SectionID,'get',$array,$this);
									$this->menu = $this->PreparedTemplates;
									if($this->menu['type']!=3) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
									else if(empty($this->menu['name'])) header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
									else $this->Scheme = $this->LocalTarget[1]=='Delete'? 'delete' : 'action';
								}
							}
						break;
						case 'Unlock':
							$idMenu=$this->LocalTarget[2];
							$this->Action->processEvent($this->SectionID,'getlock',array('menu' => $idMenu,'member' => $this->Action->Security->currentuser->id),$this);
							if(!empty($this->PreparedTemplates)){
								$this->Action->processEvent($this->SectionID,'unLock',array('menu' => $idMenu,'member' => $this->Action->Security->currentuser->id),$this);
							}
							header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
						break;
						default: header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID); break;
					}
				}
				else header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
			break;
			case 'SiteMap':
				if($this->server_type=='dev') {
					$this->type = 'sitemap';
					if (isset($this->LocalTarget[1]) && $this->LocalTarget[1] == 'Unlock'){
						$this->Action->processEvent($this->SectionID,'getlock',array('menu' => 1,'member' => $this->Action->Security->currentuser->id),$this);
						if(!empty($this->PreparedTemplates)){
							$this->Action->processEvent($this->SectionID,'unLock',array('menu' => 1,'member' => $this->Action->Security->currentuser->id),$this);
						}
						header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
					}else{
						if(!empty($this->LocalTarget[2]) && validate(intval($this->LocalTarget[2])) && $this->LocalTarget[1]=='Undo')
							$undo = 1;
						else $undo = 0;
						$array = array('id' => 1);
						if($undo == 1) $array['undo'] = $this->LocalTarget[2];
						$this->Action->processEvent($this->SectionID,'getlock',array('menu' => 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/MenuManager'.$this->SID);
						}else{
							if(empty($this->PreparedTemplates['access_flag']))
								$this->Action->processEvent($this->SectionID,'lock',array('menu' => 1,'member' => $this->Action->Security->currentuser->id),$this);
								$this->Action->processEvent($this->SectionID,'get',$array,$this);
								$this->menu = $this->PreparedTemplates;
								$this->Scheme = 'action';
						}
					}
				}
				else header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
			break;
			default: $this->Action->processEvent($this->SectionID, 'select', array(), $this); break;
		}
		if(!empty($this->Request)) {
			switch($this->getRequestField('step')) {
				case 'submit':
					if($this->server_type=='dev') {
						$this->Request['form']['id_user']=$this->Action->Security->currentuser->id;
						if($undo ==1) $this->Request['form']['_undo_'] = $this->type == 'sitemap'? $this->LocalTarget[2] : $this->LocalTarget[4];
						$this->validateForm()
						and $this->Action->processEvent($this->SectionID,'save',$this->getRequestField('form'),$this)
						and header('Location: '.$this->Home.'/Registar/MenuManager/'.$this->SID);
					}
					else header('Location: '.$this->Home.'/Registar/MenuManager/'.$this->SID);
				break;
				case 'remove':
					if($this->server_type=='dev') {
						include($TOOLS_PATH.'\checker.php');
						$this->check = check_to_delete($this->getRequestFormField('id'),'Menu');
						if(empty($this->check)) {
							$this->Action->processEvent($this->SectionID,'delete',$this->getRequestField('form'),$this)
							and $this->clearUndo($this->getRequestFormField('id'))
							and header('Location: '.$this->Home.'/Registar/MenuManager'.$this->SID);
						}
						else $this->Scheme = 'nodelete';
					}
					else header('Location: '.$this->Home.'/Registar/MenuManager/'.$this->SID);
				break;
			}
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function clearUndo($id) {
		global $ROOT_PATH;
		if(file_exists($ROOT_PATH.'/undo/menu/'.$id)) {
			chdir($ROOT_PATH.'/undo/menu/'.$id);
			$dir = dir('.');
			$dir->rewind();
			while($file = $dir->read()) if(is_file($file)) unlink($ROOT_PATH.'/undo/menu/'.$id.'/'.$file);
			rmdir($ROOT_PATH.'/undo/menu/'.$id);
		}
		return true;
	}

	function validateForm() {
		if (@$this->LocalTarget[0] == 'SiteMap'){if(!is_AdvCleanText($this->getRequestFormField('comment'),5,4096)) $this->errors['comment'] = 'Comment is empty or is not valid';}
		else if (@$this->LocalTarget[1] == 'Update') if(!is_AdvCleanText($this->getRequestFormField('comment'),5,4096)) $this->errors['comment'] = 'Comment is empty or is not valid';
		if($this->getRequestField('step')=='submit') {
			if(!is_AlphaNumDefis($this->getRequestFormField('name'),1,64)) $this->errors['name'] = 'Menu\'s Name is empty or is not valid';
			if(!is_AdvCleanText($this->getRequestFormField('title'),0,128)) $this->errors['title'] = 'Menu\'s Title is empty or is not valid';
			else if(validate($this->getRequestFormField('title')) && !validate($this->getRequestFormField('title'))) $this->errors['link'] = 'Menu\'s URL for Title is empty or is not valid';
			if(empty($this->Request['form']['menulineitems'])) $this->errors['li'] = 'Menu\'s Items is empty';
			else {
				foreach($this->Request['form']['menulineitems'] as $k => $v)
					$this->Request['form']['menulineitems'][$k]['sort_order'] = !empty($v['sort_order']) && is_numeric($v['sort_order'])? (int)$v['sort_order'] : 1;
			}
		}
		if(count($this->errors) > 0) return FALSE;
		return TRUE;
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'list': $this->fillList(); break;
			case 'item': $this->fillItem(); break;
			case 'action': $this->fillAction(); break;
			case 'delete': $this->fillDelete(); break;
			case 'nodelete': $this->fillNoDelete(); break;
			case 'preview': $this->fillPreview(); break;
			case 'viewall': $this->fillViewAll(); break;
			case 'json': $this->fillJson(); break;
			case 'actionxhr':$this->fillActionXHR();break;
			default: $this->fillSelect(); break;
		}
	}

	function fillViewAll() {
		global $REGISTRY, $ROOT_PATH;
		if(!empty($this->PreparedTemplates['curPage'][0])) {
			switch($this->PreparedTemplates['curPage'][0]['type']) {
				case 7: $undotype = 'TabsMenu'; break;
				case 6: $undotype = 'RightMenu'; break;
				case 4: $undotype = 'LeftMenu'; break;
				case 3: $undotype = 'BottomMenu'; break;
				case 1: $undotype = 'SiteMap'; break;
			}
		}
		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' => ($ind != 0 || $this->PreparedTemplates['curPageNumber'] > 1) && file_exists($ROOT_PATH.'/undo/menu/'.$this->PreparedTemplates[1].'/'.$val['version'].'.inc')? array(array('undotype' => $undotype == 'SiteMap'? $undotype : $undotype.'/Update/'.$this->PreparedTemplates[1],'version' => $val['version'])) : array(),
			);
		}
	}

	function fillNoDelete() {
		$this->TempFillEls['sec_name'] = '" '.$this->PreparedTemplates['name'].' "';
		empty($this->check) and $this->check = array();
		foreach($this->check as $key => $value) {
			$this->TempFillEls['Pages'][0]['pitems'][] = array(
				'item' => makeLink($this->Home.$value['path'],$value['name']),
				'id' => $value['id'],
			);
		}
	}

	function fillPreview() {
		if(!empty($this->Request)) {
			printArray($this->Request);
		}
	}

	function fillList() {
		global $REGISTRY;
		$count = count($this->PreparedTemplates['allPage']);
		for($i = 0; $i < $count; $i++) {
			if(!empty($this->PreparedTemplates['allPage'][$i]['parent'])) continue;
			$this->TempFillEls['idFirstLevel'][] = Array('id' => $this->PreparedTemplates['allPage'][$i]['id']);
			$this->TempFillEls['pages'][$this->PreparedTemplates['allPage'][$i]['id']] = $this->PreparedTemplates['pages'][$this->PreparedTemplates['allPage'][$i]['id']];
		}
		$this->TempFillEls['idHomePage'][0]['idHP'] = $this->PreparedTemplates['idHomePage'][0]['idHP'];
	}

	function fillJson(){
		global $REGISTRY;//$REGISTRY['HomeTarget']
		switch(@$this->LocalTarget[1]){
			case 'getSavePosition':
			case 'getChild':
				if(isset($this->LocalTarget[2])&&(is_numeric($this->LocalTarget[2])||$this->LocalTarget[2]=='All')){
					$this->TempFillEls['items'] = Array();
					$count=count($this->PreparedTemplates['getChild']);
					for($i=0;$i<$count;$i++){
						if(($this->PreparedTemplates['getChild'][$i]['parent']==$this->PreparedTemplates['LocalTarget2']||$this->PreparedTemplates['LocalTarget2']=='All')&&$this->PreparedTemplates['getChild'][$i]['parent']!=0){
							$this->TempFillEls['items'][$this->PreparedTemplates['getChild'][$i]['id']]=$this->PreparedTemplates['items'][$this->PreparedTemplates['getChild'][$i]['id']];
							$this->TempFillEls['items'][$this->PreparedTemplates['getChild'][$i]['id']]['pubBut'] =  $this->server_type=='pro'? 'hidden' : 'visible';
							$this->TempFillEls['items'][$this->PreparedTemplates['getChild'][$i]['id']]['funcName'] = 'pageControler.addItem';
							$this->TempFillEls['itemsfunc'][0]='';
						}
					}

				}
			break;
			case 'getServerInfo':
				if(isset($this->LocalTarget[2]) && is_numeric($this->LocalTarget[2])){
					$count = $this->PreparedTemplates['count'];
					for ($i = 0; $i < $count; $i++){
						$this->TempFillEls['serverInfo'][$i]= $this->PreparedTemplates['serverInfo'][$i];
						$this->TempFillEls['serverInfo'][$i]['funcName'] = 'pageControler.addServerInfo';
					}
					$this->TempFillEls['serverinfofunc'][0]['idPage'] = $this->LocalTarget[2];
				}
			break;
			default:break;
		}
	}

	function fillItem() {
		$this->TempFillEls['label'] = inputTextArea('form[label]',(!empty($this->Request)? $this->getRequestFormField('label') : ''),40,1,'style="width:370px;"');
		$this->TempFillEls['path'] = inputTextArea('form[path]',(!empty($this->Request)? $this->getRequestFormField('path') : ''),40,1,'style="width:370px;"');
		$this->TempFillEls['anchor'] = inputText('form[anchor]',(!empty($this->Request)? $this->getRequestFormField('anchor') : ''),20,'style="width:185px;"');
		$this->TempFillEls['sort_order'] = inputText('form[sort_order]',(!empty($this->Request)? $this->getRequestFormField('sort_order') : ''),5,'style="width:30px;"');
	}

	function fillDelete() {
		global $CORE_PATH, $ROOT_PATH;
		$this->TempFillEls['action'] = $this->LocalTarget[1];
		switch($this->type) {
			case 'tabs': $undotype = 'TabsMenu'; $this->TempFillEls['menutype'] = 'Tabs Menu'; $this->TempFillEls['type'] = 7; break;
			case 'right': $undotype = 'RightMenu'; $this->TempFillEls['menutype'] = 'Right Menu'; $this->TempFillEls['type'] = 6; break;
			case 'left': $undotype = 'LeftMenu'; $this->TempFillEls['menutype'] = 'Left Menu'; $this->TempFillEls['type'] = 4; break;
			case 'bottom': $undotype = 'BottomMenu'; $this->TempFillEls['menutype'] = 'Bottom Menu'; $this->TempFillEls['type'] = 3; break;
		}
		$this->TempFillEls['name'] = $this->menu['name'];
		$this->TempFillEls['id'] = $this->menu['id'];
		$this->TempFillEls['sec_name'] = '" '.$this->menu['name'].' "';
		$menuTree = $this->makeMenuTree($this->menu['menulineitems'],0);
		$this->setMenuArray($menuTree,1);
		$this->TempFillEls['sort_order'] = !empty($this->sort_order)? $this->sort_order : 1;
		if($this->LocalTarget[1] == 'Delete'){
			$selected = $this->Db->SelectAny("select mmc.*,m.username as member from menucomment mmc, member m where m.id=mmc.member and mmc.ownerid = ".$this->PreparedTemplates['id']." order by mmc.version DESC LIMIT 10 OFFSET 0");
			if(!empty($selected)) {
				$this->TempFillEls['ViewAllButton']='<input type="button" name="View_All" class=adminsubmit value="View All" onclick="open(\'/Registar/MenuManager/ViewAll/'.$this->PreparedTemplates['id'].'\',\'displayWindow\',\'width=950,height=600,scrollbars=yes\');">';
				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' => $ind!=0 && file_exists($ROOT_PATH.'/undo/menu/'.$this->PreparedTemplates['id'].'/'.$val['version'].'.inc')? array(array('undotype' => $undotype.'/Update/'.$this->PreparedTemplates['id'],'version' => $val['version'])) : array(),
					);
				}
			}
		}
	}

	function fillAction() {
		global $CORE_PATH, $ROOT_PATH;
		$this->TempFillEls['action'] = $this->type == 'sitemap'? 'Update' : $this->LocalTarget[1];
		if($this->type == 'sitemap') {
			if(!empty($this->LocalTarget[2]) && validate(intval($this->LocalTarget[2])) && $this->LocalTarget[1]=='Undo')
				$this->TempFillEls['undo'] = '(Undo v.'.$this->LocalTarget[2].')';
		}
		else {
			if(!empty($this->LocalTarget[4]) && validate(intval($this->LocalTarget[4])) && $this->LocalTarget[3]=='Undo')
				$this->TempFillEls['undo'] = '(Undo v.'.$this->LocalTarget[4].')';
		}
		switch($this->type) {
			case 'tabs': $undotype = 'TabsMenu'; $this->TempFillEls['menutype'] = 'Tabs Menu'; $this->TempFillEls['type'] = 7; break;
			case 'right': $undotype = 'RightMenu'; $this->TempFillEls['menutype'] = 'Right Menu'; $this->TempFillEls['type'] = 6; break;
			case 'left': $undotype = 'LeftMenu'; $this->TempFillEls['menutype'] = 'Left Menu'; $this->TempFillEls['type'] = 4; break;
			case 'bottom': $undotype = 'BottomMenu'; $this->TempFillEls['menutype'] = 'Bottom Menu'; $this->TempFillEls['type'] = 3; break;
			case 'sitemap': $undotype = 'SiteMap'; $this->TempFillEls['menutype'] = 'Site Map'; $this->TempFillEls['type'] = 1; break;
		}
		$this->TempFillEls['name'] = inputText('form[name]',(!empty($this->Request)? $this->getRequestFormField('name') : $this->menu['name']),80,1,'style="width:600px;height:17px;"');
		$this->TempFillEls['title'] = inputText('form[title]',(!empty($this->Request)? $this->getRequestFormField('title') : $this->menu['title']),80,1,'style="width:600px;height:17px;"');
		$this->TempFillEls['link'] = inputText('form[link]',(!empty($this->Request)? $this->getRequestFormField('link') : $this->menu['link']),80,1,'style="width:600px;height:17px;"');
		$this->TempFillEls['id'] = !empty($this->LocalTarget[1]) && $this->LocalTarget[1]=='Duplicate'? '' : (!empty($this->Request)? (validate($this->getRequestFormField('id'))? $this->getRequestFormField('id') : ''): (!empty($this->menu['id'])? $this->menu['id'] : ''));
		$this->TempFillEls['duplicate_id'] = !empty($this->LocalTarget[1]) && $this->LocalTarget[1]=='Duplicate'? (!empty($this->Request)? (validate($this->getRequestFormField('id'))? $this->getRequestFormField('id') : ''): (!empty($this->menu['id'])? $this->menu['id'] : '')) : '';
		$this->TempFillEls['sec_name'] = !empty($this->menu['name'])? '" '.$this->menu['name'].' "' : '';
		$this->TempFillEls['hidden_name_form'] =inputHiddenWithAttributes('form[name]',(!empty($this->Request)? $this->getRequestFormField('name') : $this->menu['name']),'id','form[name][req]');
		$this->TempFillEls['hidden_title_form'] =inputHiddenWithAttributes('form[title]',(!empty($this->Request)? $this->getRequestFormField('title') : $this->menu['title']),'id','form[title][req]');
		$this->TempFillEls['hidden_link_form'] =inputHiddenWithAttributes('form[link]',(!empty($this->Request)? $this->getRequestFormField('link') : $this->menu['link']),'id','form[link][req]');
		if($this->TempFillEls['action'] == 'Update'){
			$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']='<input type="button" name="View_All" class=adminsubmit value="View All" onclick="open(\'/Registar/MenuManager/ViewAll/'.$this->PreparedTemplates['id'].'\',\'displayWindow\',\'width=950,height=600,scrollbars=yes\');">';
			$this->TempFillEls['textareaComment']='<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 mmc.*,m.username as member from menucomment mmc, member m where m.id=mmc.member and mmc.ownerid = ".$this->PreparedTemplates['id']." order by mmc.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' => $ind!=0 && file_exists($ROOT_PATH.'/undo/menu/'.$this->PreparedTemplates['id'].'/'.$val['version'].'.inc')? array(array('undotype' => $this->type=='sitemap'? $undotype : $undotype.'/Update/'.$this->PreparedTemplates['id'],'version' => $val['version'])) : array(),
					);
				}
			}
		}
		if(!empty($this->Request)) {
			if(empty($this->Request['form']['menulineitems'])) $this->Request['form']['menulineitems'] = array();
			if(validate($this->getRequestField('label')) && $this->getRequestField('step')!='delete') {
				$mli = array(
					'id' => $this->getRequestField('id')? $this->getRequestField('id') : count($this->Request['form']['menulineitems'])+1,
					'parent' => $this->getRequestField('parent'),
					'path' => $this->getRequestField('path'),
					'label' => $this->getRequestField('label'),
					'anchor' => $this->getRequestField('anchor'),
					'target' => $this->getRequestField('target'),
					'is_external' => $this->getRequestField('is_external'),
					'sort_order' => (int)$this->getRequestField('sort_order'),
				);
				if(validate($this->getRequestField('id'))) {
					foreach($this->Request['form']['menulineitems'] as $key => $value)
						if($value['id']==$mli['id']) $this->Request['form']['menulineitems'][$key] = $mli;
				}
				else $this->Request['form']['menulineitems'][] = $mli;
			}
			if($this->getRequestField('step')=='delete') {
				if(validate($this->getRequestField('id'))) {
					foreach($this->Request['form']['menulineitems'] as $key => $value)
						if($value['id']==$this->getRequestField('id')) unset($this->Request['form']['menulineitems'][$key]);
				}
			}
			$menulineitems = array();
			uasort($this->Request['form']['menulineitems'],'by_parent');
			$this->Request['form']['menulineitems'] = array_values($this->Request['form']['menulineitems']);
			uasort($this->Request['form']['menulineitems'],'by_order');
			$this->Request['form']['menulineitems'] = array_values($this->Request['form']['menulineitems']);
			$menuTree = $this->makeMenuTree($this->Request['form']['menulineitems'],0);
			$this->setMenuArray2($menuTree,1);
			$this->TempFillEls['sort_order'] = !empty($this->sort_order)? (int)$this->sort_order : 1;
		}
		else {
			$menuTree = $this->makeMenuTree($this->menu['menulineitems'],0);
			$this->setMenuArray2($menuTree,1);
			$this->TempFillEls['sort_order'] = !empty($this->sort_order)? (int)$this->sort_order : 1;
		}
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'save');
		!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,'get');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'save');
	}

	function fillSelect() {
		$menutypes = $this->Db->SelectGuide(array('menutype g'), '*');
		unset($menutypes['2']);// No Top Menus
		unset($menutypes['5']);// No General Menus
		$count = count($menutypes);
		$managertypes = array(1 => 'SiteMap', 3 => 'BottomMenu', 4 => 'LeftMenu', 6 => 'RightMenu', 7 => 'TabsMenu');
		foreach ($menutypes as $type => $name){
			$this->TempFillEls['menutype'][$type]['menutypename'] = ucwords($name);
			$this->TempFillEls['menutype'][$type]['managertype'] = $managertypes[$type];
			if($type == 1 || $this->server_type != 'dev') $this->TempFillEls['menutype'][$type]['btnAdd_disp'] = 'none';
		}
		$this->TempFillEls['addBut'] = $btnEdit_vis = $this->server_type == 'dev' ? 'visible' : 'hidden';
		$this->TempFillEls['pubBut'] = $this->server_type == 'pro' ? 'hidden' : 'visible';
		$count = count($this->PreparedTemplates);
		for ($i = 0; $i < $count; $i++){
			if(!array_key_exists($this->PreparedTemplates[$i]['type'], $menutypes)) continue;
			$presets_count = explode('/', $this->PreparedTemplates[$i]['published_count']);
			$c = count($this->PreparedTemplates[$i]['publish']);
			$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i] = array(
				'id' => $this->PreparedTemplates[$i]['id'],
				'name' => $this->PreparedTemplates[$i]['name'],
				'managertype' => $managertypes[$this->PreparedTemplates[$i]['type']],
				'name_color' => $this->colors[($this->PreparedTemplates[$i]['unmodified'] > 30 ? 'black' : ($this->PreparedTemplates[$i]['unmodified'] > 14 && $this->PreparedTemplates[$i]['unmodified'] <= 30 ? 'green' : ($this->PreparedTemplates[$i]['unmodified'] > 7 && $this->PreparedTemplates[$i]['unmodified'] <= 14 ? 'yellow' : 'red')))],
				'published_count' => $this->PreparedTemplates[$i]['published_count'],
				'published_count_cursor' => $c ? 'pointer' : 'auto',
				'published_count_color' => $this->colors[($presets_count[0] >= $presets_count[1] ? 'green' : (empty($presets_count[0]) ? 'red' : 'blue'))],
				'date_changed' => $this->PreparedTemplates[$i]['date_changed'],
				'btnUnlock_vis' => $this->PreparedTemplates[$i]['access_flag'] == $this->Action->Security->currentuser->id ? 'visible' : 'hidden',
				'btnEdit_vis' => $btnEdit_vis,
				'chbAdds_vis' => $this->TempFillEls['pubBut'],
				'user_name' => $this->PreparedTemplates[$i]['username'],
				'date_locked' => $this->PreparedTemplates[$i]['date_locked'],
			);
			if($this->PreparedTemplates[$i]['type'] == 1){
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['btnDuplicate_vis'] = 'hidden';
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['btnDelete_vis'] = 'hidden';
			}
			else{
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['btnDuplicate_vis'] = $btnEdit_vis;
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['btnDelete_vis'] = $btnEdit_vis;
			}
			if(!empty($this->PreparedTemplates[$i]['access_flag']) && $this->PreparedTemplates[$i]['access_flag'] != $this->Action->Security->currentuser->id){
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['chbAdds_disabled'] = 'disabled';
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['btnUnlock_disp'] = 'none';
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['btnEdit_disp'] = 'none';
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['btnDelete_disp'] = 'none';
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['btnDuplicate_disp'] = 'none';
			}
			else $this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['locked_message_disp'] = 'none';
			// Publish Info
			$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['publish'][]['variable'] = 'menus['.$this->PreparedTemplates[$i]['id'].']=[];';
			$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['publish'][]['variable'] = 'menus['.$this->PreparedTemplates[$i]['id'].']["publish"]=[];';
			for($j = 0; $j < $c; $j++){
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['publish'][]['variable'] = 'menus['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']=[];';
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['publish'][]['variable'] = 'menus['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']["package_id"]='.$this->PreparedTemplates[$i]['publish'][$j]['package_id'].';';
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['publish'][]['variable'] = 'menus['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']["package_name"]="'.$this->PreparedTemplates[$i]['publish'][$j]['package_name'].'";';
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['publish'][]['variable'] = 'menus['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']["username"]="'.$this->PreparedTemplates[$i]['publish'][$j]['username'].'";';
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['publish'][]['variable'] = 'menus['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']["hostpath"]="'.$this->PreparedTemplates[$i]['publish'][$j]['hostpath'].'";';
				$this->TempFillEls['menutype'][$this->PreparedTemplates[$i]['type']]['menu'][$i]['publish'][]['variable'] = 'menus['.$this->PreparedTemplates[$i]['id'].']["publish"]['.$j.']["date_published"]="'.$this->PreparedTemplates[$i]['publish'][$j]['date_published'].'";';
			}
		}
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'select');
	}

	function setMenuArray($array = array(),$level = 1,$levels = array(),$parent = 0,$subitems = array()) {
		$i = 0;
		foreach($array as $key => $value) {
			$levels[$level] = $i == sizeof($array)-1? 1 : 0;
			$this->TempFillEls['lineitems'][$key] = array(
				'hidden_id' => inputHidden('form[menulineitems]['.$key.'][id]',$value['id']),
				'hidden_parent' => inputHidden('form[menulineitems]['.$key.'][parent]',$value['parent']),
				'hidden_path' => inputHidden('form[menulineitems]['.$key.'][path]',$value['path']),
				'hidden_anchor' => inputHidden('form[menulineitems]['.$key.'][anchor]',$value['anchor']),
				'hidden_label' => inputHidden('form[menulineitems]['.$key.'][label]',$value['label']),
				'hidden_name' => inputHidden('form[menulineitems]['.$key.'][name]',$value['label']),
				'hidden_target' => inputHidden('form[menulineitems]['.$key.'][target]',$value['target']),
				'hidden_is_external' => inputHidden('form[menulineitems]['.$key.'][is_external]',$value['is_external']),
				'hidden_sort_order' => inputText('form[menulineitems]['.$key.'][sort_order]',$value['sort_order'],5,'style="width:20px;"'),
				'hidden_object' => inputHidden('form[menulineitems]['.$key.'][object]',0),
				'id' => $key,
				'parent' => $parent,
				'anchor' => $value['anchor'],
				'target' => $value['target'],
				'is_external' => $value['is_external'],
				'real_sort_order' => $value['sort_order'],
				'sort_order' => !empty($value['subcat'])? count($value['subcat'])+1 : 1,
				'u_path' => $value['path'].(!empty($value['anchor'])? '#'.$value['anchor'] : ''),
				'path' => $value['path'],
				'name' => $value['label'],
				'width' => intval($level*20),
				'width2' => 732 - intval($level*20),
				'width3' => 820 - intval($level*20),
				'delete' => !empty($value['subcat'])? array() : array(array('id' => $value['id'])),
				'nodelete' => empty($value['subcat'])? array() : array(array()),
				'add' => $this->type!='bottom' && $this->type!='tabs'? array(array('id' => $key,'sort_order' => !empty($value['subcat'])? sizeof($value['subcat'])+1 : 1)) : array(),
				'noadd' => $this->type!='bottom' && $this->type!='tabs'? array() : array(array('id' => $key,'sort_order' => !empty($value['subcat'])? sizeof($value['subcat'])+1 : 1)),
			);
			if(!empty($value['subcat'])) {
				$this->TempFillEls['lineitems'][$key]['header'] = array(array(
					'hidden_id' => inputHidden('form[menulineitems]['.$key.'][id]',$value['id']),
					'hidden_parent' => inputHidden('form[menulineitems]['.$key.'][parent]',$value['parent']),
					'hidden_path' => inputHidden('form[menulineitems]['.$key.'][path]',$value['path']),
					'hidden_anchor' => inputHidden('form[menulineitems]['.$key.'][anchor]',$value['anchor']),
					'hidden_label' => inputHidden('form[menulineitems]['.$key.'][label]',$value['label']),
					'hidden_name' => inputHidden('form[menulineitems]['.$key.'][name]',$value['label']),
					'hidden_target' => inputHidden('form[menulineitems]['.$key.'][target]',$value['target']),
					'hidden_is_external' => inputHidden('form[menulineitems]['.$key.'][is_external]',$value['is_external']),
					'hidden_sort_order' => inputText('form[menulineitems]['.$key.'][sort_order]',$value['sort_order'],5,'style="width:20px;"'),
					'hidden_object' => inputHidden('form[menulineitems]['.$key.'][object]',0),
					'id' => $key,
					'parent' => $parent,
					'anchor' => $value['anchor'],
					'target' => $value['target'],
					'is_external' => $value['is_external'],
					'sort_order' => !empty($value['subcat'])? count($value['subcat'])+1 : 1,
					'u_path' => $value['path'].(!empty($value['anchor'])? '#'.$value['anchor'] : ''),
					'path' => $value['path'],
					'name' => $value['label'],
					'width' => intval($level*20),
					'width2' => 732 - intval($level*20),
					'width3' => 820 - intval($level*20),
					'delete' => !empty($value['subcat'])? array() : array(array('id' => $value['id'])),
					'nodelete' => empty($value['subcat'])? array() : array(array()),
					'add' => $this->type!='bottom' && $this->type!='tabs'? array(array('id' => $key,'sort_order' => !empty($value['subcat'])? sizeof($value['subcat'])+1 : 1)) : array(),
					'noadd' => $this->type!='bottom' && $this->type!='tabs'? array() : array(array('id' => $key,'sort_order' => !empty($value['subcat'])? sizeof($value['subcat'])+1 : 1)),
				));
				$this->TempFillEls['lineitems'][$key]['onclick'] = array(array('id' => $key));
				$this->setMenuArray($value['subcat'],$level+1,$levels,$key);
			}
			$i++;
			if($level==1) $this->sort_order = $i+1;
		}
	}

	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['lineitems'][$value['id']] = array(
				'id' => $value['id'],
				'path' => $path.'/'.$value['path'],
				'name' => $value['name'],
				'label' => $value['name'],
				'width' => intval($level*20),
				'width2' => 432 - intval($level*20),
				'width3' => 520 - intval($level*20),
				'start_div' => '<div id=page'.$value['id'].(!empty($this->general[$value['id']])? ' style="display: none;"' : '').'>',
				'minus' => !empty($this->general[$value['id']])? '_minus' : '',
			);
			if(!empty($this->general[$value['id']])) {
				$this->TempFillEls['lineitems'][$value['id']]['header'] = array(
					array(
						'id' => $value['id'],
						'path' => $path.'/'.$value['path'],
						'name' => $value['name'],
						'label' => $value['name'],
						'width' => intval($level*20),
						'width2' => 432 - intval($level*20),
						'width3' => 520 - intval($level*20),
					)
				);
				$this->TempFillEls['lineitems'][$value['id']]['onclick'] = array(array('id' => $value['id']));
				$this->setTargetArray($this->general[$value['id']],$level+1,$levels,$path.'/'.$value['path']);
			}
			else {
				if($level==1) $this->TempFillEls['lineitems'][$value['id']]['end_div'] = '</div>';
				else $this->TempFillEls['lineitems'][$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 makeMenuTree($array,$parent = 0,$tree = array()) {
		foreach($array as $value) {
			if($value['parent'] == $parent) {
				$tree[$value['id']] = array(
					'id' => $value['id'],
					'parent' => $value['parent'],
					'label' =>$value['label'],
					//'path' => htmlspecialchars($value['path'],ENT_QUOTES),
					//'path' => addslashes($value['path']),
					'path' => str_replace("'","&quot;",$value['path']),
					'sort_order' => $value['sort_order'],
					'target' => $value['target'],
					'is_external' => $value['is_external'],
					'anchor' => $value['anchor'],
					'subcat' => $this->makeMenuTree($array,$value['id'])
				);
			}
		}
		return $tree;
	}


	function setMenuArray2($array = array(),$level = 1,$levels = array(),$parent = 0,$subitems = array()) {
		$i = 0;
		foreach($array as $key => $value) {
			$levels[$level] = $i == sizeof($array)-1? 1 : 0;
			$this->TempFillEls['lineitems'][$key] = array(
				'hidden_id' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][id]',$value['id'],'id_a','form[menulineitems][id]'),
				'hidden_parent' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][parent]',$value['parent'],'id_a','form[menulineitems][parent]'),
				'hidden_path' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][path]',htmlspecialchars($value['path'],ENT_QUOTES),'id_a','form[menulineitems][path]'),
				'hidden_anchor' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][anchor]',$value['anchor'],'id_a','form[menulineitems][anchor]'),
				'hidden_label' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][label]',htmlspecialchars($value['label'],ENT_QUOTES),'id_a','form[menulineitems][label]'),
				'hidden_name' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][name]',htmlspecialchars($value['label'],ENT_QUOTES),'id_a','form[menulineitems][name]'),
				'hidden_target' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][target]',$value['target'],'id_a','form[menulineitems][target]'),
				'hidden_is_external' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][is_external]',$value['is_external'],'id_a','form[menulineitems][is_external]'),
				'hidden_sort_order' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][sort_order]',$value['sort_order'],'id_a','form[menulineitems][sort_order]'),
				'hidden_object' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][object]',0,'id_a','form[menulineitems][object]'),
				'visible_sort_order' => inputText('form[menulineitems]['.$key.'][sort_order_vis]',$value['sort_order'],5,'style="width:20px;"'),
				'id' => $key,
				'parent' => $parent,
				'anchor' => $value['anchor'],
				'target' => $value['target'],
				'is_external' => $value['is_external'],
				'real_sort_order' => $value['sort_order'],
				'sort_order' => !empty($value['subcat'])? count($value['subcat'])+1 : 1,
				'u_path' => htmlspecialchars($value['path'].(!empty($value['anchor'])? '#'.$value['anchor'] : ''),ENT_QUOTES),
				'path' => $value['path'],
				'name' => $value['label'],
				'name_' => htmlspecialchars($value['label'],ENT_QUOTES),
				'level' => $level,
				'width' => intval($level*20),
				'width2' => 732 - intval($level*20),
				'width3' => 820 - intval($level*20),
				'delete' => !empty($value['subcat'])? array() : array(array('id' => $value['id'])),
				'nodelete' => empty($value['subcat'])? array() : array(array()),
				//'add' => $this->type!='bottom' && $this->type!='tabs'? array(array('id' => $key,'sort_order' => !empty($value['subcat'])? sizeof($value['subcat'])+1 : 1)) : array(),
				//'noadd' => $this->type!='bottom' && $this->type!='tabs'? array() : array(array('id' => $key,'sort_order' => !empty($value['subcat'])? sizeof($value['subcat'])+1 : 1)),
			);
			if(!empty($value['subcat'])) {
				$this->TempFillEls['lineitems'][$key]['header'] = array(array(
					'hidden_id' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][id]',$value['id'],'id_a','form[menulineitems][id]'),
					'hidden_parent' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][parent]',$value['parent'],'id_a','form[menulineitems][parent]'),
					'hidden_path' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][path]',htmlspecialchars($value['path'],ENT_QUOTES),'id_a','form[menulineitems][path]'),
					'hidden_anchor' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][anchor]',$value['anchor'],'id_a','form[menulineitems][anchor]'),
					'hidden_label' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][label]',htmlspecialchars($value['label'],ENT_QUOTES),'id_a','form[menulineitems][label]'),
					'hidden_name' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][name]',htmlspecialchars($value['label'],ENT_QUOTES),'id_a','form[menulineitems][name]'),
					'hidden_target' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][target]',$value['target'],'id_a','form[menulineitems][target]'),
					'hidden_is_external' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][is_external]',$value['is_external'],'id_a','form[menulineitems][is_external]'),
					'hidden_sort_order' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][sort_order]',$value['sort_order'],'id_a','form[menulineitems][sort_order]'),
					'hidden_object' => inputHiddenWithAttributes('form[menulineitems]['.$key.'][object]',0,'id_a','form[menulineitems][object]'),
					'visible_sort_order' => inputText('form[menulineitems]['.$key.'][sort_order_vis]',$value['sort_order'],5,'style="width:20px;"'),
					'id' => $key,
					'parent' => $parent,
					'anchor' => $value['anchor'],
					'target' => $value['target'],
					'is_external' => $value['is_external'],
					'real_sort_order' => $value['sort_order'],
					'sort_order' => !empty($value['subcat'])? count($value['subcat'])+1 : 1,
					'u_path' => htmlspecialchars($value['path'].(!empty($value['anchor'])? '#'.$value['anchor'] : ''),ENT_QUOTES),
					'path' => htmlspecialchars($value['path'],ENT_QUOTES),
					'name' => $value['label'],
					'name_' => htmlspecialchars($value['label'],ENT_QUOTES),
					'level' => $level,
					'width' => intval($level*20),
					'width2' => 732 - intval($level*20),
					'width3' => 820 - intval($level*20),
					'delete' => !empty($value['subcat'])? array() : array(array('id' => $value['id'])),
					'nodelete' => empty($value['subcat'])? array() : array(array()),
					//'add' => $this->type!='bottom' && $this->type!='tabs'? array(array('id' => $key,'sort_order' => !empty($value['subcat'])? sizeof($value['subcat'])+1 : 1)) : array(),
					//'noadd' => $this->type!='bottom' && $this->type!='tabs'? array() : array(array('id' => $key,'sort_order' => !empty($value['subcat'])? sizeof($value['subcat'])+1 : 1)),
				));
				//$this->TempFillEls['lineitems'][$key]['onclick'] = array(array('id' => $key));
				$this->setMenuArray2($value['subcat'],$level+1,$levels,$key);
			}
			$i++;
			if($level==1) $this->sort_order = $i+1;
		}
	}

	function fillActionXHR(){
		global $CORE_PATH, $ROOT_PATH;
		$this->TempFillEls['name'] = inputText('form[name]',(!empty($this->Request)? $this->getRequestFormField('name') : $this->menu['name']),80,1,'style="width:600px;height:17px;"');
		$this->TempFillEls['title'] = inputText('form[title]',(!empty($this->Request)? $this->getRequestFormField('title') : $this->menu['title']),80,1,'style="width:600px;height:17px;"');
		$this->TempFillEls['link'] = inputText('form[link]',(!empty($this->Request)? $this->getRequestFormField('link') : $this->menu['link']),80,1,'style="width:600px;height:17px;"');
		$this->TempFillEls['id'] = !empty($this->LocalTarget[1]) && $this->LocalTarget[1]=='Duplicate'? '' : (!empty($this->Request)? (validate($this->getRequestFormField('id'))? $this->getRequestFormField('id') : ''): (!empty($this->menu['id'])? $this->menu['id'] : ''));
		$this->TempFillEls['sec_name'] = !empty($this->menu['name'])? '" '.$this->menu['name'].' "' : '';
		$this->TempFillEls['hidden_name_form'] =inputHiddenWithAttributes('form[name]',(!empty($this->Request)? $this->getRequestFormField('name') : $this->menu['name']),'id','form[name][req]');
		$this->TempFillEls['hidden_title_form'] =inputHiddenWithAttributes('form[title]',(!empty($this->Request)? $this->getRequestFormField('title') : $this->menu['title']),'id','form[title][req]');
		$this->TempFillEls['hidden_link_form'] =inputHiddenWithAttributes('form[link]',(!empty($this->Request)? $this->getRequestFormField('link') : $this->menu['link']),'id','form[link][req]');
		if(!empty($this->Request)) {
			if(empty($this->Request['form']['menulineitems'])) $this->Request['form']['menulineitems'] = array();
			if(validate($this->getRequestField('label')) && $this->getRequestField('step')!='delete') {
				$mli = array(
					'id' => $this->getRequestField('id')? $this->getRequestField('id') : count($this->Request['form']['menulineitems'])+ time(),
					'parent' => $this->getRequestField('parent'),
					'path' => $this->getRequestField('path'),
					'label' => $this->getRequestField('label'),
					'anchor' => $this->getRequestField('anchor'),
					'target' => $this->getRequestField('target'),
					'is_external' => $this->getRequestField('is_external'),
					'sort_order' => (int)$this->getRequestField('sort_order'),
				);
				if(validate($this->getRequestField('id'))) {
					foreach($this->Request['form']['menulineitems'] as $key => $value)
						if($value['id']==$mli['id']) $this->Request['form']['menulineitems'][$key] = $mli;
				}
				else $this->Request['form']['menulineitems'][] = $mli;
			}
			if($this->getRequestField('step')=='delete') {
				if(validate($this->getRequestField('id'))) {
					foreach($this->Request['form']['menulineitems'] as $key => $value)
						if($value['id']==$this->getRequestField('id')) unset($this->Request['form']['menulineitems'][$key]);
				}
			}
			$menulineitems = array();
			uasort($this->Request['form']['menulineitems'],'by_parent');
			$this->Request['form']['menulineitems'] = array_values($this->Request['form']['menulineitems']);
			uasort($this->Request['form']['menulineitems'],'by_order');
			$this->Request['form']['menulineitems'] = array_values($this->Request['form']['menulineitems']);
			$menuTree = $this->makeMenuTree($this->Request['form']['menulineitems'],0);
			$this->setMenuArray2($menuTree,1);
			$this->TempFillEls['sort_order'] = !empty($this->sort_order)? (int)$this->sort_order : 1;
		}
		else {
			$menuTree = $this->makeMenuTree($this->menu['menulineitems'],0);
			$this->setMenuArray2($menuTree,1);
			$this->TempFillEls['sort_order'] = !empty($this->sort_order)? (int)$this->sort_order : 1;
		}
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'save');
		!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,'get');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'save');
	}
}
?>