<?

include_once( FULL_PATH . 'classes/class.DBWork.php' );	



class Menu extends DBWork {

	# menu initialization
	var $menu_name;
	var $menu_parent;
	var $path;	
	var $page_type; // 0 = text; 1 = product; 2 = both;  
	
	# menu elements
	var $menu_begin;
	var $menu_end;	
	var $menu_element_begin;
	var $menu_element_end;			
	var $menu_element;	
	var $element_selected;
	var $all_path_elements_selected;
	var $no_active_element;
	var $advanced_element; 
	var $featured;	
	var $default_icon;	
	
	# levels depth
	var $all_opened;	
	var $level_count;

	# pagination
	var $pagination_element;
	var $per_page;
	var $per_page_number;
	
	# columns
	var $column_count;
	var $row_start;
	var $row_end;
	var $row_item_empty;	
	
	# system
	var $query_advanced;
	var $query_advanced_custom;
	var $db_fields;
	var $final;
	
	
	    function get_path() {
			
			$table = "page";
			$db_fields = Array(
				'id'												=>	Array( $this->menu_parent, 0, 1),
				'active_'. $this->variables['language'][0]['id']	=>	Array( '', 0, 0),
				'title_'. $this->variables['language'][0]['id']		=>	Array( '', 1, 0),								
				'parent'											=>	Array( '', 0, 0),
				'page_type'											=>	Array( '', 0, 0),
			);			
			
			$Res = Array();
			do {
				$Mas = $this->read_record( $db_fields, $table);
				if ( count($Mas) > 0 )
					$Res[] = Array( 'id' => $db_fields['id'][0], 'active_'. $this->variables['language'][0]['id'] => $Mas[0]['active_'. $this->variables['language'][0]['id'] ], 'title' => $Mas[0]['title_' . $this->variables['language'][0]['id'] ], 'page_type' => $Mas[0]['page_type'] );
				$db_fields['id'][0] = $Mas[0]['parent'];
			} while ( count($Mas) > 0 );
			$Res = array_reverse($Res);
			return $Res;
	    }	
		
		
		
		function print_root_link() {
			if ( $this->variables['admin'] )
				echo '
					<a href="'.$_SERVER["PHP_SELF"]. '?id=' . $this->menu_parent . '">
						<img src="'. FULL_URL .'admin_templates/img/system_menu_root.gif" style="position:absolute" border="0" alt=""  />
					</a>
					';
		}
		
		
		
	    function pagination_get() {
		
			$result = "";
			$table = "page";

			$db_fields = Array(
					'id' =>	Array( '', 0, 0),	
					'parent' =>	Array( $this->menu_parent, 0, 1),											
				);	
			
				
			if ( $this->variables['admin'] == 1 ) {
				$db_fields['active_'. $this->variables['language'][0]['id']] = Array( '', 0, 0);
				$db_fields['created_'. $this->variables['language'][0]['id']] = Array( '', 0, 0);
			} else {
				$db_fields['active_'. $this->variables['language'][0]['id']] = Array( 1, 0, 1);
				$db_fields['created_'. $this->variables['language'][0]['id']] = Array( 1, 0, 1);			
			}
			
			if ( isset( $this->page_type ) ) {
				$db_fields['page_type'] = Array( $this->page_type, 0, 1 );
			}	

			
			$res = $this->read_record($this->db_fields, $table);
			
			if (isset($this->per_page)) {
			
				$pages_count = (int)( count($res) / $this->per_page );
				if ( count($res) % $this->per_page != 0 ) $pages_count++;
				
				if ( $pages_count > 1 )
					for ( $i = 1; $i <= $pages_count; $i++ ) {
						$tmp = $this -> pagination_element;
						$tmp = str_replace('<blo:page>', $i, $tmp);
						
						if ( ($this->per_page_number == '') && ($i==1) ) $tmp = str_replace('<blo:selected>', $this->pagination_selected, $tmp);
						
						if ( $i == $this->per_page_number ) 
							$tmp = str_replace('<blo:selected>', $this->pagination_selected, $tmp);
						else $tmp = str_replace('<blo:selected>', '', $tmp);
						
						$result .= $tmp;
					}
				
				return $result; 			
			
			}
			
	    }	
		
		
		function pagination_print() {
			echo $this->pagination_get();
		}
		
		
		
		function menu_list_get_array() {
		
			$table = "page";		
			$Mas = $this->read_record( $this -> db_fields, $table, $this -> query_advanced);
			
			for ($i=0; $i<count($Mas); $i++) {
				if ( $Mas[$i]['title_'.$this->variables['language'][0]['id']] == '' )
					$Mas[$i]['title_'.$this->variables['language'][0]['id']] = $this -> get_title_from_other_language($Mas[$i]['id']);
			}
			
			return $Mas;

		} 			
		
		
		
		function get_title_from_other_language($id) {
			
			$table = "page";
			
			for ($i=1; $i<count($this->variables['language']); $i++) {

				$db_fields_advanced = Array(
					'title_' .$this->variables['language'][$i]['id']	=>	Array( '', 1, 0),	
					'created_'. $this->variables['language'][$i]['id']	=>	Array( '', 0, 0),											
					'id'												=>	Array( $id, 0, 1),
				);		
				$res = $this->read_record($db_fields_advanced, $table);

				if ( $res[0][ 'created_'.$this->variables['language'][$i]['id'] ] == 1 ) {
					return $res[0]['title_'.$this->variables['language'][$i]['id']] . ' [' . $this->variables['language'][$i]['short'] . ']';
					break;
				}
			}	
		}
		
		
			
	
		function menu_print_item($CurrentItem, $global_active, $selected, $current_level) {

			$id = $CurrentItem['id'];
			$created = $CurrentItem['created_'.$this->variables['language'][0]['id']];

			$begin = $this -> menu_element_begin;
			$end = $this -> menu_element_end;
			
			if ( $created == 0 ) {
				$begin = str_replace('<blo:title/>', $this->get_title_from_other_language($id), $begin);
				$end = str_replace('<blo:title/>', $this->get_title_from_other_language($id), $end);			
			}	

			if ( $CurrentItem['featured'] ) {
				$begin = str_replace('<blo:featured/>', $this->featured, $begin);
				$end = str_replace('<blo:featured/>', $this->featured, $end);			
			} else {
				$begin = str_replace('<blo:featured/>', '', $begin);
				$end = str_replace('<blo:featured/>', '', $end);			
			}
			
			if ( $CurrentItem['icon'] == '' ) {
				$begin = str_replace('<blo:icon/>', $this->default_icon, $begin);
				$end = str_replace('<blo:icon/>', $this->default_icon, $end);			
			} 	


			foreach($CurrentItem as $index => $val) {
				if ( substr( $index, -2, 1 ) == '_' ) $index_new = substr( $index, 0, strlen($index) - 2 ); 
				else $index_new = $index;
				
				$begin = str_replace('<blo:'.$index_new.'/>', $val, $begin);
				$end = str_replace('<blo:'.$index_new.'/>', $val, $end);
			}	
			
			$begin = str_replace('<blo:level/>', $current_level, $begin);
			$end = str_replace('<blo:level/>', $current_level, $end);
			
			$begin = str_replace('<blo:advanced/>', $this->advanced, $begin);
			$end = str_replace('<blo:advanced/>', $this->advanced, $end);			
			
			if ( $this->per_page_number != '' ){
				$begin = str_replace('<blo:page/>', $this->menu_name.'='.$this->per_page_number.'&', $begin);
				$end = str_replace('<blo:page/>', $this->menu_name.'='.$this->per_page_number.'&', $end);				
			} else {
				$begin = str_replace('<blo:page/>', '', $begin);
				$end = str_replace('<blo:page/>', '', $end);				
			}
			
			if ( $global_active == 0 ) {
				$begin = str_replace('<blo:no_active/>', $this->no_active_element, $begin);
				$end = str_replace('<blo:no_active/>', $this->no_active_element, $end);					
			} else {
				$begin = str_replace('<blo:no_active/>', '', $begin);
				$end = str_replace('<blo:no_active/>', '', $end);
			}
			
			
			
			if ( $this->all_path_elements_selected ) {
			
				if ($selected == 1){
					$begin = str_replace('<blo:selected/>', $this->element_selected, $begin);
					$end = str_replace('<blo:selected/>', $this->element_selected, $end);
				} else {
					$begin = str_replace('<blo:selected/>', '', $begin);
					$end = str_replace('<blo:selected/>', '', $end);			
				}	
						
			} else {
			
				if ( $id == $this->path[count($this->path)-1][id] ) {
					$begin = str_replace('<blo:selected/>', $this->element_selected, $begin);
					$end = str_replace('<blo:selected/>', $this->element_selected, $end);
				} else {
					$begin = str_replace('<blo:selected/>', '', $begin);
					$end = str_replace('<blo:selected/>', '', $end);			
				}	
						
			}
			
			
			
			$current_level++;
			
			$this->finish .= $begin;	
			if ( ( $this->all_opened ) || ( $selected ) ) 
				$this -> menu_print_level( $id, $global_active, $current_level);
			$this->finish .= $end;		
		}	
	
	
	
		
	    function menu_print_level( $this_level_parent_id, $global_active, $current_level ) {
			
			$table = "page";
			$row_item_number = 1;
					
			$this -> db_fields['parent'][0] = $this_level_parent_id;
			$res = $this->read_record( $this -> db_fields, $table, $this -> query_advanced);

			for ($i=0; $i<count($res); $i++) {
				
				if ( ( ( $res[$i]['active_' . $this->variables['language'][0]['id'] ] == 1 ) && ( $res[$i]['created_' . $this->variables['language'][0]['id'] ] == 1 ) ) || ( $this->variables['admin'] ) ) { 
			
					if ( ( $this->level_count == 0 ) || ( $current_level <= $this->level_count   ) ) {
						
						if ( ( $global_active == 0 ) || ( $res[$i]['active_' . $this->variables['language'][0]['id'] ] == 0 ) ) $active = 0;
						else  $active = 1;
						
						$selected = false;
						for ($k=0; $k<count($this->path); $k++)
							if ( $this->path[$k]['id'] == $res[$i]['id'] ) $selected = true;

						if ( ( $this -> column_count != '' ) && ( $row_item_number == 1 ) ) $this->finish .= $this->row_start;
						$this -> menu_print_item( $res[$i], $active, $selected, $current_level );						
						if ( ( $this -> column_count != '' ) && ( $row_item_number == $this -> column_count ) ) {
							$this->finish .= $this -> row_end;
							$row_item_number = 0; 
						}
						$row_item_number++;
					}
				} 		
			}
			
			if ( ( $this -> column_count != '' ) && ( $row_item_number != 1 ) ) { 
				for ($m = $row_item_number; $m <= $this -> column_count; $m++) {
					$this->finish .= $this -> row_item_empty;
				}
				$this->finish .= $this -> row_end;
			}
			
	    }		
		
		
		
		
	    function menu_get() {
			
			$this->finish = '';  
			
			if ( isset( $this -> query_advanced_custom ) ) $this -> query_advanced = $this -> query_advanced_custom;
			else $this -> query_advanced = "ORDER BY priority DESC ";
			
			$replace = '<blo:next>'; 
			
			if ( substr_count( $this->menu_element, $replace) > 0 ) {
				$this -> menu_element_begin = substr( $this->menu_element, 0, strpos( $this->menu_element, $replace ) );
				$this -> menu_element_end = substr( $this->menu_element, strpos( $this->menu_element, $replace ) + strlen($replace), strlen($this->menu_element) );
			} else {
				$this -> menu_element_begin = $this->menu_element;
				$this -> menu_element_end = "";
			}
			
			if ( substr_count( $this->menu_start, $replace) > 0 ) {
				$this -> menu_begin = substr( $this->menu_start, 0, strpos( $this->menu_start, $replace ) );
				$this -> menu_end = substr( $this->menu_start, strpos( $this->menu_start, $replace ) + strlen($replace), strlen($this->menu_start) );
			} else {
				$this -> menu_begin = "";
				$this -> menu_end = $this->menu_start;
			}
			
			if ( isset( $this->page_type ) ) {
				$this -> db_fields['page_type'] = Array( $this->page_type, 0, 1 );
			}	
			
			if ( isset( $this->per_page ) ) {
				$per_page_from = $this->per_page * ( $this->per_page_number - 1 );
				if ( $per_page_from < 0 ) $per_page_from = 0;
				$this -> query_advanced .= ' LIMIT ' . $per_page_from . ' , ' . $this->per_page . ';';
			}				
			
			#
			$start_menu_global_active = 1;
			$menu_root_is_not_in_the_path = 0;
			for ($k=0; $k<count($this->path); $k++ ) {
				if ( $this->path[$k][ 'active_' . $this->variables['language'][0]['id'] ] == 0 ) $start_menu_global_active = 0;
				if ( $this->path[$k]['id'] == $this->menu_parent ) {
					$menu_root_is_not_in_the_path = 1;
					break;
				}
			}
			if ( !$menu_root_is_not_in_the_path ) $start_menu_global_active = 1;
			###

			$this -> menu_print_level($this->menu_parent, $start_menu_global_active, 1);
			
			if ( $this->finish != '' ) {
				return $this -> menu_begin . $this->finish . $this -> menu_end;
			}
				
	    }
		
		
		function menu_print() {
			echo $this->menu_get();
		}


}

?>