<?php
//global $Debug; $Debug=true;
class NavPanelView extends Slot {

	var $ModuleID = 3;
	var $SectionID = 1;
	var $entry = array();
	var $module = 0;
  	var $prodFlag = 0;

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

	function init() {
     	$this->getModule();
		$this->getRealUrl($this->module);
	}

	function getRealUrl($id) { // print $this->module.'<br>';
	    $this->getParents(&$parent);
	    $this->parent=array_reverse($parent);
	    if($this->prodFlag!=1) {
	      $selected = $this->Db->SelectArray('category',array('id','name','sys_name'),array('ownerid' => $parent[sizeof($parent)-1]),'sort_order DESC');
	  		foreach($selected as $value) {
	  			$this->entry[] = $value;
	  		}
	    }
	}

  function getParents(&$ownerid){  
    global $REGISTRY;
	$pos = strrpos(getenv('REQUEST_URI'), '?');
	if ($pos)
			$request_uri = substr(getenv('REQUEST_URI'), 0, $pos);
		else $request_uri = getenv('REQUEST_URI');
    $url = (substr($request_uri, -1) == '/') ? substr( $request_uri, 0, -1) : $request_uri;
    $urlparts = explode('/', $url);
    
    for($i=0;$i<sizeof($ownerid);$i++)
    	  array_pop($urlparts);
    $url = implode($urlparts, '/');
    foreach($REGISTRY['VirtualSlotTarget']['main'] as $k => $v) if(strcasecmp ($k,$url)) $value = $v;

    $query = explode('?',$value);
    $target = explode(':',array_shift($query));

    if($target[0]!='DescribeView'){
	      $properties = array_shift($query);
	      $pair = explode('=',$properties);
	      $name = array_shift($target);
	      $ownerid[]=$pair[1];
	      if(sizeof($urlparts)>0)
	        $this->getParents($ownerid);  
    }  else{
	      $query='select id from category where sys_name like \''.$urlparts[1].'\' and ownerid=1';
	      $selected=$this->Db->SelectAny($query);
	      if(!empty($selected)){
	        $ownerid[]=$selected[0]['id'];
	      }
       }
    return;
  }
  
	function fillElements() {
		$entry=array();
		krsort($this->entry);
		reset($this->entry);
		$parent=$this->parent;
	    foreach($parent as $parent1){
	      $query='select id,name,sys_name from category where id='.$parent1;
	      $selected=$this->Db->SelectAny($query);
	      $parent2[]=@$selected[0];
	    }
	    foreach($parent2 as $k=>$v) 
	      		$this->TempFillEls['owner'][] = array ( 'path_to_owner'=> $this->Home.$this->getCategoryPath($v['id']),  
      													'owner_name' => $v['name'],   
      										);
		if($this->prodFlag!==1){
			$this->entry = array_values($this->entry);
			foreach($this->entry as $id => $nav){
	        	  	  $path = $this->Home.$this->getCategoryPath($nav['id']);
      	       	      if($path == $this->Home.'/'.implode('/',$this->LocalTarget)){
			                if($id==0) $nname='<span class="item current first">'.$nav['name'].'</span>'; 
				          		else if($id== count($this->entry)-1 )
					            	$nname='<span class="item current last">'.$nav['name'].'</span>';
							        	  else $nname='<span class="item current">'.$nav['name'].'</span>';
							$entry[] = $nname;
				      } else {
					          if($id==0) $class='class="item first"';
						          else if($id== count($this->entry)-1)
						            	$class='class="item last"';
							          		else $class='class="item "';
							   $entry[] = makeLink($path,$nav['name'],$extra=array(),'',$class);							          		
				        }
		    }
   	 	} else {	// is product
   	 		$this->TempFillEls['style_adjust'] = ' style="padding-top: 55px;" ';
   	 	}
		if(count($entry))
		      foreach($entry as $path) $this->TempFillEls['nav'][]['entryLink'] = '<li>'.$path.'</li>';
	}

	function getModule($id = 0) {
		global $REGISTRY;
		$value = '';
		if(!empty($this->LocalTarget[$id])) {
			$path = implode('/',$this->LocalTarget);
			if(!empty($REGISTRY['Request']['Search'])) {
				foreach($REGISTRY['Request']['Search'] as $k => $v) {
					if(isset($REGISTRY['VirtualSlotTarget']['main']['/'.$path.'?'.$k.'='.$v])) {
						$value = $REGISTRY['VirtualSlotTarget']['main']['/'.$path.'?'.$k.'='.$v];
					}
				}
			}
			if(empty($value)) {
				foreach($REGISTRY['VirtualSlotTarget']['main'] as $k => $v) {
					if(equals($k,'/'.$path)) $value = $v;
				}
			}
		    if(strpos($value,'ProductView')!==FALSE)
		        	$this->prodFlag=1;
			$query = explode('?',$value);
			$target = explode(':',array_shift($query));
			$properties = array_shift($query);
			$pair = explode('=',$properties);
			$name = array_shift($target);
			$this->module = $pair[1];
		}
	}

	function getCategoryPath($id) {
		if(empty($Paths)) {
			$query = 'SELECT id, sys_name, ownerid FROM category';
			$selected = $this->Db->SelectAny($query);
			foreach($selected as $value)
				$Paths[$value['id']] = array(
					'path' => $value['sys_name'],
					'parent' => $value['ownerid'],
				);
		}
		$p = '';
		while(1){
			if(empty($Paths[$id])) return null;
			if(empty($Paths[$id]['parent']) || $Paths[$id]['parent'] == 1 ) return '/'.$Paths[$id]['path'].$p;
			$p = '/'.$Paths[$id]['path'].$p;
			$id = $Paths[$id]['parent'];
		}
	}
	
	
	function path_sticker($id){
		$p = '';
		while(10) {
			$selected = $this->Db->SelectArray('module',array('path','parent'),array('id' => $id));
      //printArray($selected);
			if(!empty($selected)) {
				$path = $selected[0]['path'];
				$parent = $selected[0]['parent'];
				if($parent == 0) return '/'.$path.$p;
				else {
					$p = '/'.$path.$p;
					$id = $parent;
				}
			}
			else return (null);
		}
	}
	
}
?>