<?php

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

	var $ModuleID = 1;
	var $SectionID = 2;
	var $req_types = array(
		'phrase' => 'this exact phrase',
		'any' => 'any of these words',
		'all' => 'all of these words',
	);
	var $sort_types = array(
		'price' => 'price',
		'generation' => 'generation',
		'name' => 'name',
	);
	var $sort_direct = array(
		'up' => 'up',
		'down' => 'down',
	);
	var $cont_types = array(
		'content' => 'content',
		'product' => 'product',
	);
	var $qty = array(
		'10' => '10',
		'20' => '20',
		'50' => '50',
		'100' => '100',
		'200' => '200',
	);
	var $selected_qty = 10;
	var $search = '';
	var $type = 'phrase';
	var $area = 'product';
	var $generation_sel = 0;
	var $type_sel = 'all';
	var $export_sel = 0;
	var $sort_sel = 'price';
	var $sort_direct_sel = 'up';
	var $city = 0;
	var $error_text = '<div style="font-family: Verdana;padding: 10px;text-align:left"><h2 class="header2" style="font-size: 17px;">Have a local presence in any number of major U.S. cities</h2><br /><div style="color:#798496"><b><a href="/features/local-numbers/area-codes.html" style="color:#798496">by Area Code</a> | by City</b></div></div>';
	var $page = 0;
	var $pagesItems = array();
	
	function __construct($pquery) {
		parent::__construct($pquery);
	}

	function init() {    
		global $TOOLS_PATH, $REGISTRY;
//print base64_encode("ShoppingCartView").'-----------------';print base64_decode("U2hvcHBpbmdDYXJ0Vmlldw=="); exit; 		
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'ajax-form-efq' ){
			if  ($this->validateForm()) {
	   			$this->Action->processEvent($this->SectionID,'sendefq',array($_POST),$this);
				print '{"status":"success"}'; 
				exit;
			} else {
				print '{"status":"failed"}'; 
				exit;
			}
		}
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'ajax-form-rfq' ){
			if  ($this->validateForm()) {
	   			$this->Action->processEvent($this->SectionID,'sendrfq',array($_POST),$this);
				print '{"status":"success"}'; 
				exit;
			} else {
				print '{"status":"failed"}'; 
				exit;
			}
		}
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'ajax-form-customer_feedback' ){   //Customer Feedback
			if  ($this->validateForm()) {
	   			$this->Action->processEvent($this->SectionID,'sendfeedback',array($_POST),$this);
				print '{"status":"success"}'; 
				exit;
			} else {
				print '{"status":"failed"}'; 
				exit;
			}
		}
		
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'ajax-warranty-reg' ){
			if  ($this->validateForm()) {
	   			$this->Action->processEvent($this->SectionID,'sendreg',array($_POST),$this);
				print '{"status":"success"}'; 
				exit;
			} else {
				print '{"status":"failed"}'; 
				exit;
			}
		}
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'ajax-form-expadv' ){     //  Ask Night Vision Expert
			if  ($this->validateForm()) { 
	   			$this->Action->processEvent($this->SectionID,'sendexpadv',array($_POST),$this);
				print '{"status":"success"}'; 
				exit;
			} else {
				print '{"status":"failed"}'; 
				exit;
			}
		}
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'ajax-rmarf' ){     //  Return Merchandize Authorization (RMA) Request Form
			if  ($this->validateForm()) { 
	   			$this->Action->processEvent($this->SectionID,'sendrma',array($_POST),$this);
				print '{"status":"success"}'; 
				exit;
			} else {
				print '{"status":"failed"}'; 
				exit;
			}
		}
		if (isset($_POST["ajax"]) && $_POST["ajax"] == 'ajax-rma' ){     // Service and Upgrade Payment Portal
			if  ($this->validateForm()) {
				$_POST["id"]  		= 906 and
				$_POST["amount"]  	= $_POST["rma-amount"] and
				$_POST["firstName"] = $_POST["rma-firstname"] and
				$_POST["lastName"]  = $_POST["rma-lastname"] and
				$_POST["phone"]		= $_POST["rma-phonenumber"] and
				$_POST["email"]		= $_POST["rma-email"] and
				$_POST["notes"] = $_POST["rma-notes"] and
				$request = $_POST and  
				$this->Action->processEvent($this->SectionID,'add',array($request),$this)	and
				print '{"status":"success", "link":"'.$this->sHome.'/cart'.'"          }';
				exit;
			} else {
				print '{"status":"failed"}'; 
				exit;
			}
		}

		
		
		$this->Action->processEvent($this->SectionID,'open',array('id' => $this->ID),$this);
		if(!empty($this->PreparedTemplates['_error'])) header('Location: '.$this->Home.$this->SID);
		if(!$this->PreparedTemplates['is_describe']) header('Location: '.$this->Home.$this->SID);
		if($this->PreparedTemplates['type']['id']==3) {
				$this->Cache = 0;
				$this->Passing = TRUE;
				
				$this->Action->processEvent($this->SectionID,'select_pages',array(),$this);
				$this->pagesItems = $this->makeCatTree(1,$this->PreparedTemplates, 1, 0);
				$res_array2 = $this->makeCatTree(1, $this->PreparedTemplates, 0, 1);
				foreach($res_array2 as $k => $v) $res_array2[$k]['ownerid'] = 1;	
				unset($res_array2[1]);
				unset($res_array2[$REGISTRY['HomeTarget']]);
				$this->pagesItems = mergeArrays($this->pagesItems, $res_array2);
				$this->Action->processEvent($this->SectionID,'open_full_chart',array('parent' => $this->ID),$this);
				$this->Scheme = 'sitemap';
		} elseif($this->PreparedTemplates['type']['id']==6) {
			$this->Cache = 0;
			$this->Passing = TRUE;

			$this->Scheme = 'search';
			parse_str($_SERVER['QUERY_STRING'], $output);
			if(!empty($output["query"])) { 
				$this->search = urldecode($output["query"]);
				$this->type = !empty($output["req_type"]) && in_array($output["req_type"],array_keys($this->req_types))? $output["req_type"] : 'phrase';
				$this->area = 'content';
				$this->generation_sel = !empty($generation)? $generation : '0';
				$this->type_sel = !empty($product_class)? $product_class : 'all';
				$this->export_sel = !empty($export)? $export : '0';
				$this->selected_qty = !empty($qty) && in_array($qty,array_keys($this->qty))? $qty : '10';
				$this->sort_sel = !empty($sort) && in_array($sort,array_keys($this->sort_types))? $sort : ($this->area=='product'? 'price' : 'name');
				$this->sort_direct_sel = !empty($direct) && in_array($direct,array_keys($this->sort_direct))? $direct : ($this->area=='product'? 'down' : 'up');
				$this->page = !empty($output["page"]) && validate(intval($output["page"]))? $output["page"] : 0;
				include($TOOLS_PATH.'\site_search.php');
//				$curPageNumber = !empty($output["page"])? (int)$output["page"]+1 : 1;
				$curPageNumber = !empty($output["page"])? (int)$output["page"] : 1;
				$this->log = site_search(htmlspecialchars($this->search), array(), $this,$this->Home.'/'.$this->Target[1].'?query='.urlencode($this->search).'&req_type='.$this->type.'&cont_type='.$this->area.'&qty='.$this->selected_qty.'&uni='.md5(uniqid('unique',1)).'&sort='.$this->sort_sel.'&direct='.$this->sort_direct_sel,$this->type,$this->area,$curPageNumber,$this->selected_qty);
			}
		} elseif($this->PreparedTemplates['type']['id']==9) {
			$this->city = 1;
			if(!empty($this->Target[2])) 
				$this->Action->processEvent($this->SectionID,'city',array('name' => $this->Target[2]),$this);
		}
		$this->setSchemeTemplate($this->Scheme);
	}
	
	function fillElements() {
		switch($this->Scheme) {
			case 'sitemap':
				$this->TempFillEls['category_part'] = array();
				$this->TempFillEls['category_part'] += $this->makeMainItems($this->PreparedTemplates);
				
				$this->TempFillEls['pages_part'] = array();
				$this->TempFillEls['pages_part'] += $this->makePagesItems($this->pagesItems);
				
			break;
			default: $this->fillSelect(); break;
		}
	}
	
	function validateForm() { 			
		switch($_POST["ajax"]) {
			case 'ajax-form-efq': {
				if( !validate($_POST['efq-name']) ||
					!is_Email($_POST['efq-email']) ||
					!is_Email($_POST['efq-confeamail']) ) return FALSE;
			break;
			}
			case 'ajax-form-rfq': {
				if( !validate($_POST['rfq-name']) ||
					!is_Email($_POST['rfq-email']) ||
					!is_Email($_POST['rfq-confemail']) ) return FALSE;
			break;
			}
			case 'ajax-form-customer_feedback': {
				if( !validate($_POST['cf-name']) ||
					!is_Email($_POST['cf-email']) ||
					!is_Email($_POST['cf-confemail']) || 
					!validate($_POST['cf-message']) ) return FALSE;
			break;
			}
			case 'ajax-warranty-reg': {
				if( !validate($_POST['warranty-reg-firstname']) ||
					!validate($_POST['warranty-reg-lastname']) ||
					!validate($_POST['warranty-reg-addressline1']) ||
					!validate($_POST['warranty-reg-city']) ||
					!validate($_POST['warranty-reg-zip']) ||
					!validate($_POST['warranty-reg-phonenumber']) ||
					!validate($_POST['warranty-reg-unitpurchased']) ||
					!validate($_POST['warranty-reg-dateofpurchase']) ||
					!validate($_POST['warranty-reg-purchasedfrom']) ||
					 (!empty($_POST['warranty-reg-email']) && !is_Email($_POST['warranty-reg-email']) )    ) return FALSE;
			break;
			}
			case 'ajax-rmarf': {
				if( !validate($_POST['rmarf-firstname']) ||
					!validate($_POST['rmarf-lastname']) ||
					!validate($_POST['rmarf-addressline1']) ||
					!validate($_POST['rmarf-city']) ||
					!validate($_POST['rmarf-zip']) ||
					!validate($_POST['rmarf-country']) ||
					!validate($_POST['rmarf-phonenumber']) ||
					!validate($_POST['rmarf-unitpurchased']) ||
					!validate($_POST['rmarf-dateofpurchase']) ||
					!validate($_POST['rmarf-purchasedfrom']) ) return FALSE;
			break;
			}
			case 'ajax-rma': {          // Service and Upgrade Payment Portal
				if( !validate($_POST['rma-firstname']) ||
					!validate($_POST['rma-lastname']) ||
					!validate($_POST['rma-rmanumber']) ||
//					!validate($_POST['rma-customernumber']) ||
					!is_NumText($_POST['rma-amount'], 1, 20) ||
					!validate($_POST['rma-phonenumber']) ||
					!is_Email($_POST['rma-email'])       ) return FALSE;
			break;
			}
			
			case 'ajax-form-expadv': {
				if( !validate($_POST['expadv-name']) ||
					!validate($_POST['expadv-message']) ||
					!is_Email($_POST['expadv-email']) ||
					!is_Email($_POST['expadv-confemail']) ) return FALSE;
			break;
			}
			default: {
				return FALSE;
			break;
			}
		}
		if(sizeof($this->errors) > 0) return FALSE;
		return TRUE;
	}
	
	function fillSelect() { 
		$this->TempFillEls['describe'] = array();
		if($this->city) {
			$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'city');
			if(empty($this->PreparedTemplates)) {
				$city = '';
				$cities = array();
				$letters = array();
				$result = '<p>';
				$set = $this->Db->SelectAny('SELECT c.name_in_url, c.name_in_text, s.name as state FROM city c, state s WHERE s.id = c.state ORDER BY s.name');
				$count = sizeof($set);
				for($i = 0; $i < $count; $i++) $cities[$set[$i]['state']][] = makeLink($this->Home.'/cities/'.$set[$i]['name_in_url'],$set[$i]['name_in_text']);
				foreach($cities as $key => $value) {
					$letters[$key{0}] = '<a href="#'.$key{0}.'"><b>'.$key{0}.'</b></a>';
					$city.= '<div style="text-align: left"><ul><b style="color:#798496"><a name="'.$key{0}.'"> </a>'.$key.'</b><li>'.implode(', ',$value).'</ul></div>';
				}
				$this->TempFillEls['describe'][] = array('value' => $this->error_text.$result.implode(' ',$letters).$city);
			}
			else {
				$city = $this->PreparedTemplates[0];
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
				foreach($this->PreparedTemplates['contents'] as $v) if($v['type']==1) $template = $v['value'];
				foreach($city as $key => $value) {
					switch($key) {
						case 'state': $template = preg_replace('({city.'.$key.'})',$value['name'],$template); break;
						case 'area_codes': $template = preg_replace('({city.'.$key.'})',eregi_replace('{|}','',$value),$template); break;
						default: $template = preg_replace('({city.'.$key.'})',$value,$template); break;
					}
					unset($city[$key]);
				}
				$this->TempFillEls['describe'][] = array('value' => $template);
			}
		}
		else {
			$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
			foreach($this->PreparedTemplates['contents'] as $value)
				if($value['type']==1) $this->TempFillEls['describe'][] = array('value' => $value['value']);
				if($this->PreparedTemplates['type']['id']==6) { 	
				$index = 1;
				if(!empty($this->log) && empty($this->log['error'])) {
					foreach($this->log['result'] as $key => $value) {
						if($this->area=='content') {
							$this->TempFillEls['pages'][$key] = array(
								'name' => $value['name'],
								'content' => implode(' ',$value['content']),
								'url' => $value['url'],
								'path' => $value['path'],
								'num' => $index,
							);
							$index++;	
							$this->TempFillEls['display'] = 'none';
							$this->TempFillEls['name_display'] = 'block';
							$this->TempFillEls['price_display'] = 'none';
							$this->TempFillEls['generation_display'] = 'none';
						}
						else {
							$price_addtocart = array();
							$price_efq = array();
							if ($value['callforquote'] == 1 || $value["ownerid"] == 21 )	{	
												$price_efq 		 = array(array('name' => $value['name'],'pid' => $value['pid'],	));
										} else	$price_addtocart = array(array( 'id' => $value['id'] ));
							$this->TempFillEls['products'][$key] = array(
								'id' => $value['id'],
								'pid' => $value['pid'],
								'name' => $value['name'],
								'content' => strlen(strip_tags($value['description'])) > 1000? substr(strip_tags($value['description']),0,1000).' ... ' : $value['description'],
								'path' => $value['path'],
								'price' => ($value['callforquote'] == 1 || $value["ownerid"] == 21 )? '': $value['price'],
								'image' => $value['image'],
								'type' => $value['type'],
								'price_addtocart' => $price_addtocart, 
								'price_efq' => $price_efq,
							);
							$index++;
							$i = 1;
							if(is_array($value['shipcosts'])) {
								foreach($value['shipcosts'] as $sh_key => $sh_val) {
									if($sh_key=='UPS Ground') $this->TempFillEls['products'][$key]['sales'][0]['shipp_price'] = number_format($sh_val,2,'.','');
									$this->TempFillEls['products'][$key]['sales'][0]['google_ship'][] = array(
										'number' => $i,
										'name' => $sh_key,
										'price' => number_format($sh_val,2,'.',''),
									);
									$i++;
								}
							}
							$this->TempFillEls['display'] = 'block';
							$this->TempFillEls['name_display'] = 'block';
							$this->TempFillEls['price_display'] = 'block';
							$this->TempFillEls['generation_display'] = 'block';
						}
					}
				} else if(!empty($this->log['error'])) {
					$this->TempFillEls['errors'][]['error'] = $this->log['result'][0];
				}
				
				$this->TempFillEls['query'] = htmlspecialchars($this->search);				
				$this->TempFillEls['checked'] = ($this->type == "any")?"checked":"";							
				foreach(radioButtons($this->req_types,'req_type',$this->type) as $value)
					$this->TempFillEls['req_type'][]['value'] = $value;
				foreach(radioButtons($this->cont_types,'cont_type',$this->area) as $value)
					$this->TempFillEls['cont_type'][]['value'] = $value;
				$this->generation[0] = 'All Generations';
				$generation = $this->Db->SelectAny('select * from generation order by sort_order');
				foreach($generation as $k => $v)
					if($v['id']==10) $this->generation[$v['id']] = 'None Generation';
						else if($v['gen_type']==2) $this->generation[$v['id']] = 'Gen '.$v['name'].' International';
							else $this->generation[$v['id']] = 'Gen '.$v['name'];
				foreach($generation as $k => $v)
					$this->TempFillEls['generation'][] = array('id' =>  $v['id'], 'name' => $v['mask'] );
				
				$this->product_class['all'] = 'All Types';
				$this->product_class['none'] = 'None Type';
				$product_class = $this->Db->SelectAny('select distinct product_class from product WHERE product_class IS NOT NULL order by product_class');
				foreach($product_class as $v)
					$this->product_class[$v['product_class']] = $v['product_class'];
				foreach($product_class as $k => $v)
					$this->TempFillEls['product_class'][] = array('name' => $v['product_class'] );
				$this->export[0] = 'All Export Restrictions';
				$this->export[1000] = 'This product is Available for export';
				$this->export = mergeArrays($this->export,$this->Db->SelectGuide(array('export g'),array('g.id','g.name'),array('1' => '1')));
				$this->TempFillEls['export'] = inputSelect('export',$this->export,$this->export_sel,'style="width:160px;"');
				$this->TempFillEls['qty'] = inputSelect('qty',$this->qty,$this->selected_qty,'onchange="document.getElementById(\'search_form\').submit();"');
				$this->TempFillEls['sort_path'] = $this->Home.'/'.$this->Target[1].'?query='.urlencode(htmlspecialchars($this->search)).'&req_type='.$this->type.'&cont_type='.$this->area.'&qty='.$this->selected_qty.'&generation='.$this->generation_sel.'&product_class='.urlencode($this->type_sel).'&export='.$this->export_sel.'&uni='.md5(uniqid('unique',1)).'&page='.$this->page;
			}
		}
	}

	function setTargetArray($array = array(),$level = 1,$levels = array()) {
		//printArray($array);exit;
		$i = 0;
		foreach($array as $key => $value) {
			$levels[$level] = $i==sizeof($array)-1? 1 : 0;
			$this->TempFillEls['sitemap'][0]['items'][$key] = array(
				'id' => $key,
				'path' => $value['path'],
				'target' => $value['target']? '_blank' : '_top',
				'anchor' => !empty($value['anchor'])? '#'.$value['anchor'] : '',
				'label' => trim($value['label']),
				'start_ul' => !empty($value['subcat'])? '<ul>' : '',
			);
			if(!empty($value['subcat'])) $this->setTargetArray($value['subcat'],$level+1,$levels);
			else {
				if($level==1) $this->TempFillEls['sitemap'][0]['items'][$key]['end_ul'] = '';
				else $this->TempFillEls['sitemap'][0]['items'][$key]['end_ul'] = !empty($levels[$level])? (!empty($levels[$level-1])? (!empty($levels[$level-2])? str_repeat('</ul>',3) : str_repeat('</ul>',2)) : str_repeat('</ul>',1)) : '';
			}
			$i++;
		}
	}
	
	function makeMainItems($prepared,$flag = true) {
		global $REGISTRY;
		$ret_items = array();
		$make_sys_name = true;
		foreach($prepared as $key => $val) {
			if ($val['ownerid'] == 27 ) $val['cat_type'] = 1;
			switch($key) {
				case 'parent': {
					// set description, name, color
					$this->TempFillEls['name'] = $val['name'];
					$this->TempFillEls['color'] = $val['color']['name'];
					// check on category, where categorylineitems in parent
					if(count($prepared) != 1) break;
					$make_sys_name = false;
				}
				default: {
					if($val['cat_type']==0 && count($prepared) > 2 && $flag){ 
						$ret_items[sizeof($ret_items)+1]['subcategories'][0]['subcategory'][$key]['subcat_name']= makeLink($this->Home.$this->getCategoryPath($val['id']),$val['name']);
						$ret_items[sizeof($ret_items)+1]['subcategories_end'][0]['subcategory_end'] = array(array());
					}
					if(array_key_exists('subcat', $val)) {
						if(!empty($ret_items) && $val['cat_type']==3) continue;
						else {
							$ret_items_temp = $this->makeMainItems($val['subcat'],false);
							foreach($ret_items_temp as $k_temp => $v_temp)
								$ret_items[sizeof($ret_items)+1] = $ret_items_temp[$k_temp];
							continue;
						}
					}
					switch($val['cat_type']) {
						case 0: {				//----------Category-------------------------
							$items = array();
							$index = 0;
							$itteration = 0;
							foreach($val['categorylineitems'] as $item_val) {
								$items[$index]['lineitems'][$item_val['item']['id']] = array(
									'name' => makeLink($item_val['item']['url'],$item_val['item']['name'],array(),'','',(!empty($item_val['item']['nav_name'])? $item_val['item']['nav_name'] : $item_val['item']['name'])),
									'path' => $item_val['item']['path'],
								);
								if ( $itteration == 5) {$index++; $itteration =0;}
									else $itteration++;
							}
							if(!empty($items)) {
								$ret_items[$val['id']]['Header_name'] = ($make_sys_name)? array(array('header_name' =>        makeLink($this->Home.$this->getCategoryPath($val['id']), $val['name'], array(),'',' class="level1_head" ' ))       ) : array();
								$ret_items[$val['id']]['Header_name_end'] = ($make_sys_name)? array(array()) : array();
								$ret_items[$val['id']]['color'] = $val['color']['name'];
								$ret_items[$val['id']]['items'] = $items;
							}
							$this->Scheme = 'default';
							break;
													}
						case 1: {//----------ACCESSORIES-------------------------
							$items = array();
							foreach($val['categorylineitems'] as $item_key => $item_val) {
								$items[$item_key] = array(
									'name' => $item_val['item']['name'],
									'id' => $item_val['item']['id'],
									'color' => $val['color']['value'],
									'pid' => $item_val['item']['pid'],
								);
								// get price
								if(empty($items[$item_key]['price'])) {
									unset($items[$item_key]);
									continue;
								}
							}
							if(!empty($items)) {
								$ret_items[$val['id']]['Header_name'] = ($make_sys_name)? array(array('header_name' => $val['name'])) : array();
								$ret_items[$val['id']]['Header_name_end'] = ($make_sys_name)? array(array()) : array();
								$ret_items[$val['id']]['color'] = $val['color']['name'];
								$ret_items[$val['id']]['items'] = $items;
							}
							$this->Scheme = 'accessories';
							break;
						}
					}
				}
			}
		}
		return $ret_items;
	}

	function makePagesItems($prepared,$flag = true) { //printArray($prepared);exit;
		global $REGISTRY;
		$ret_items = array();
		$make_sys_name = true;
		foreach($prepared as $key => $val) {
			switch($key) {
				case 'parent': {
					if(count($prepared) != 1) break;
					$make_sys_name = false;
				}
				default: {
					if( $flag){
						$this->getRealUrl($val['id']);
						$ret_items[sizeof($ret_items)+1]['subpages'][$key]['subcat_name']= makeLink($this->Home.$this->url, $val['name']);
						$ret_items[sizeof($ret_items)+1]['subpages_end'] = array(array());
					}
					if(array_key_exists('subcat', $val)  ) {
							$ret_items_temp = $this->makePagesItems($val['subcat'], false);
							foreach($ret_items_temp as $k_temp => $v_temp) { 
								$ret_items[sizeof($ret_items)+1] = $ret_items_temp[$k_temp];
							}
					} 

						$items = array();
						$index = 0;
						$itteration = 0;
						foreach($val['subcat'] as  $item_key => $item_val) {   if (!empty($val['subcat'][$item_key]['subcat']) ) continue;   
								$this->getRealUrl($item_val['id']);
								$items[$index]['page'][$item_val['id']] = array(
									'name' => makeLink($this->Home.$this->url, $item_val['name'], array(),'','',  $item_val['name']  ),
									'path' => $item_val['path'],
								);
								if ( $itteration == 5) {$index++; $itteration =0;}
									else $itteration++;
						}
						if(!empty($items)   ) { 
							if(count($items) == 1) { $make_sys_name = false;
								$ret_items[$val['id']]['pageHeader_name'] = array(array('header_name' =>  '')  );
								$ret_items[$val['id']]['pageHeader_name_end'] = array(array());
							} else {
								$this->getRealUrl($val['id']);
								if ($make_sys_name) {
									$ret_items[$val['id']]['pageHeader_name'] = array(array('header_name' =>  makeLink($this->Home.$this->url, $val['name'], array(),'',' class="level1_head" ' ))  );
									$ret_items[$val['id']]['pageHeader_name_end'] = array(array());
								} else {
									$ret_items[$val['id']]['pageHeader_name'] = array(array('header_name' =>  '')  );//array();
									$ret_items[$val['id']]['pageHeader_name_end'] = array(array());//array();
								}
							}
							$ret_items[$val['id']]['pages'] = $items;
						}
					
					

				}
			}
		}
		return $ret_items;
	}
	
	
	function getRealUrl($id) {
		$selected = $this->Db->SelectArray('module',array('id','name','path','parent'),array('id' => $id));
		foreach($selected as $value) {
			$this->url = '';
			if( $value['parent']==0) {
					$sel = $this->Db->SelectArray('module',array('path','parent'),array('id' => $value['id']));
					$this->url .= '/'.$sel[0]['path'];
			} else {
				$this->getRealUrl($value['parent']);
				$this->url .= '/'.$value['path'];
			}
		}
	}
	
	
	
	
	
	function getCategoryPath($id) {
		global $Paths;
		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 makeCatTree($admin = 0,$array = array(),$parent = 1,$root = 0,$tree = array()) {
		if(!$root)
			foreach($array as $value){
				if($value['id'] == $parent) {
					if($admin ) {
						$tree['parent'] = $value;
					}
				}
			}
		foreach($array as $value) {
			if($value['ownerid'] == $parent) {
				if($admin) {
					$tree[$value['id']] = $value;
					$tree[$value['id']]['subcat'] = $this->makeCatTree($admin,$array,$value['id'],1);
				}
			}
		}
		return $tree;
	}
	
	
	
	
	
	
}
?>