<?php

class categories_menu {

	public $depth = 0;
	public $server = 0;
	public $base_href = 0;
	public $items = array();
	public $first_items = array();
	public $last_items = array();

	public function __construct( $depth = 0 ){
		global $languages_id, $db, $request_type;

		$this->depth = $depth;
	
    $this->server    = ((ENABLE_SSL == true && $request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER);
    $this->base_href = ((ENABLE_SSL == true && $request_type == 'SSL') ? DIR_WS_HTTPS_CATALOG : DIR_WS_CATALOG);

		$categories_query = "select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd " . 
                        "where c.categories_id = cd.categories_id and c.categories_status=1 and c.categories_id NOT IN (select categories_id from " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCT_TYPES . " pt, " . TABLE_PRODUCT_TYPES_TO_CATEGORY . " ptc where pt.type_master_type = 3 and ptc.product_type_id = pt.type_id and c.categories_id = ptc.category_id and c.categories_status=1) and cd.language_id = '" . (int)$_SESSION['languages_id']  . "'" .
                        "order by c.parent_id, c.sort_order, cd.categories_name";

    $categories = $db->Execute($categories_query);

    while (!$categories->EOF) {
      $products_in_category = (SHOW_COUNTS == 'true' ? zen_count_products_in_category($categories->fields['categories_id']) : 0);
      
      $this->items[$categories->fields['parent_id']][ $categories->fields['categories_id'] ] = array(
      	'id'				=> $categories->fields['categories_id'], 
      	'label'			=> $categories->fields['categories_name'], 
      	'href'			=> zen_href_link( FILENAME_DEFAULT, zen_get_path( $categories->fields['categories_id'] ) ), 
      	'parent'		=> $categories->fields['parent_id'], 
      	'count'			=> $products_in_category
      );

      $categories->MoveNext();
    }

		return $this;
	}

	public function prepend_items( $item = array() ){
		if( !is_array( $item ) ) return;

		/*$defaults = array(
    	'label'			=> HEADER_TITLE_CATALOG, 
    	'href'			=> $this->server . $this->base_href, 
		);*/

		$this->first_items = $item;

		return $this;
	}

	public function append_items( $item = array() ){
		if( !is_array( $item ) ) return;

		/*$defaults = array(
    	'label'			=> HEADER_TITLE_CATALOG, 
    	'href'			=> $this->server . $this->base_href, 
		);*/

		$this->last_items = $item;

		return $this;
	}	

	public function normalize( $parent = 0, $path = array() ){

		$output = sprintf( '<ul %s>', ( $parent == 0 ? 'id="main-menu" class="menu collapse"' : 'id="main-menu-parent-' . $parent . '" class="menu collapse"' ) );
		//print"<pre>";
		//print_r($item);
		if( zen_not_null( $this->first_items ) && $parent == 0 ){
			foreach ($this->first_items as $id => $item) {
				
				$output .= sprintf( '<li><a href="%s">%s</a></li>', $item['href'], $item['label'] );
			}
		}

		if( zen_not_null( $this->items[ $parent ] ) ){
			foreach ($this->items[ $parent ] as $id => $item) {

				$new_path = array();
				if($parent == 0){
					$new_path[] = $item['id'];
				}else{
					$new_path = array_merge( $path, array( $item['id'] ) );
				}

				$item['href'] = $this->buildPath( $new_path );
				//echo $item['href']."-".$item['label']."<br />";
				if( $this->items[ $id ] && $id != 0 ){
					$output .= sprintf( '<li class="has-child"><a href="%s">%s</a>', $item['href'], $item['label'] );
					$output .= '<span class="expand-btn collapsed" data-toggle="collapse" data-target="#main-menu-parent-'.$id.'"></span>'; 
					$output .= $this->normalize( $id, $new_path );
				}else{
					$output .= sprintf( '<li><a href="%s">%s</a>', $item['href'], $item['label'] );
				}
				$output .= '</li>';
			}
		}

		if( zen_not_null( $this->last_items ) && $parent == 0 ){
			foreach ($this->last_items as $id => $item) {
				$output .= sprintf( '<li><a href="%s">%s</a></li>', $item['href'], $item['label'] );
			}
		}

		$output .= '</ul>';

		return $output;
	}

	public function buildPath( $path = array() ){
		$cPath = implode( '_', $path );
		$href = zen_href_link(FILENAME_DEFAULT, 'cPath=' . $cPath);

		return $href;
	}

	public function render(){
		return $this->normalize( 0 );
	}

}