<?php
class ControllerCommonMenu extends Controller {
	public function index() {
		$this->load->language('common/menu');

		// Menu
		$this->load->model('catalog/category');

		$this->load->model('catalog/product');

		$data['categories'] = array();

		$categories = $this->model_catalog_category->getCategories(0);

		foreach ($categories as $category) {
			if ($category['top']) {
				// Level 2
				$children_data = array();

				$children = $this->model_catalog_category->getCategories($category['category_id']);

				foreach ($children as $child) {

		$this->load->model('tool/image');
		$subchildren_data = array();
		$subchildren = $this->model_catalog_category->getCategories($child['category_id']);
		foreach ($subchildren as $subchild) {
		$filter_data = array(
		'filter_category_id'  => $subchild['category_id'],
		'filter_sub_category' => true
		);
		$subchildren_data[] = array(
		'name'  => $subchild['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
		'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'] . '_' . $subchild['category_id']),
		//'image' => $subchild['image'] ? $this->model_tool_image->resize($subchild['image'], $this->config->get($this->config->get('config_theme') . '_image_additional_width'), $this->config->get($this->config->get('config_theme') . '_image_additional_height')) : ''
		);
		}
		
					$filter_data = array(
						'filter_category_id'  => $child['category_id'],
						'filter_sub_category' => true
					);

					$children_data[] = array(

		'subchildren' => $this->config->get('theme_madeshop_menu_subchildren') ? $subchildren_data : '',
	    'image' => $this->config->get('theme_madeshop_menu_image') && $child['image'] ? '<img src="' . $this->model_tool_image->resize($child['image'], $this->config->get('theme_madeshop_menu_width'), $this->config->get('theme_madeshop_menu_height')) . '" title="'.$child['name'].'" alt="'.$child['name'].'">' : '',
		
						'name'  => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
						'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
					);
				}

				// Level 1
				$data['categories'][] = array(
					'name'     => $category['name'],
					'children' => $children_data,
					'column'   => $category['column'] ? $category['column'] : 1,
					'href'     => $this->url->link('product/category', 'path=' . $category['category_id'])
				);
			}
		}


		$data['telephone'] = $telephone = $this->config->get('config_telephone');
		$data['mtelephone'] = 'tel:+'.preg_replace("/[^0-9]/", '', $telephone);
		$data['email'] = $this->config->get('config_email');
		$data['address'] = $this->config->get('config_address');
		$data['madeshop_call'] = $this->config->get('call_status') ? "1" : "";
		$data['text_call'] = $this->language->get('text_call');
		$data['see'] = $this->config->get('theme_madeshop_menu_see') ? $this->config->get('theme_madeshop_menu_see') : 0;
		$data['pimage'] = $this->config->get('theme_madeshop_menu_pimage') ? $this->config->get('theme_madeshop_menu_pimage') : 0;
		$data['target'] = $this->config->get('theme_madeshop_menu_target') ? $this->config->get('theme_madeshop_menu_target') : 0;
		
		$data['theme_madeshop_mail'] = $this->config->get('theme_madeshop_mail') ? $this->config->get('theme_madeshop_mail') : 0;
		$data['theme_madeshop_phone'] = $this->config->get('theme_madeshop_phone') ? $this->config->get('theme_madeshop_phone') : 0;
		$data['theme_madeshop_address'] = $this->config->get('theme_madeshop_address') ? $this->config->get('theme_madeshop_address') : 0;
		
		$data['language'] = $this->load->controller('common/language');
		$data['currency'] = $this->load->controller('common/currency');
		if($this->config->get('theme_madeshop_menu_brand')){		
        $this->load->model('catalog/manufacturer');       
       
        $data['manufacturer'] = array();

        $results = $this->model_catalog_manufacturer->getManufacturers();
       
        foreach ($results as $result) {
            $data['manufacturer'][] = array(
                'name' => $this->config->get('theme_madeshop_brand_name') ? $result['name'] : '',
                'image' => $this->config->get('theme_madeshop_brand_image') && $result['image'] ? '<img src="' . $this->model_tool_image->resize($result['image'], $this->config->get('theme_madeshop_brand_width'), $this->config->get('theme_madeshop_brand_height')) . '" title="'.$result['name'].'" alt="'.$result['name'].'">' : '',
                'subchildren' => '',
                'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id'])
            );
        }
              
        $data['categories'][] = array(
            'name'     => $this->language->get('text_man'),
            'children' => $data['manufacturer'],
            'column'   => $this->config->get('theme_madeshop_column') ? $this->config->get('theme_madeshop_column') : 1,
            'href'     => $this->url->link('product/manufacturer')
        );
        }
		
        if ($this->config->get('theme_madeshop_url')) {
		    $madeshop_url = $this->config->get('theme_madeshop_url');
            foreach ($madeshop_url as $result) {
            $data['categories'][] = array(
                'name'     => $result['name'][$this->config->get('config_language_id')],
                'children' => '',
                'column'   => '',
                'href'     => $result['url'][$this->config->get('config_language_id')]
            );
            }
        }
		
		$data['madeshop_url0'] = $this->config->get('theme_madeshop_html0')[$this->config->get('config_language_id')]['url'];
		$data['madeshop_name0'] = $this->config->get('theme_madeshop_html0')[$this->config->get('config_language_id')]['name'];
		$data['madeshop_des0'] =  html_entity_decode($this->config->get('theme_madeshop_html0')[$this->config->get('config_language_id')]['des'], ENT_QUOTES, 'UTF-8');
		
		$data['madeshop_url1'] = $this->config->get('theme_madeshop_html1')[$this->config->get('config_language_id')]['url'];
		$data['madeshop_name1'] = $this->config->get('theme_madeshop_html1')[$this->config->get('config_language_id')]['name'];
		$data['madeshop_des1'] =  html_entity_decode($this->config->get('theme_madeshop_html1')[$this->config->get('config_language_id')]['des'], ENT_QUOTES, 'UTF-8');
		
		$data['madeshop_url2'] = $this->config->get('theme_madeshop_html2')[$this->config->get('config_language_id')]['url'];
		$data['madeshop_name2'] = $this->config->get('theme_madeshop_html2')[$this->config->get('config_language_id')]['name'];
		$data['madeshop_des2'] =  html_entity_decode($this->config->get('theme_madeshop_html2')[$this->config->get('config_language_id')]['des'], ENT_QUOTES, 'UTF-8');
		
		return $this->load->view('common/menu', $data);
	}
}
