<?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) {

					// Level 3
          $grandchildren_data = array();

          $grandchildren = $this->model_catalog_category->getCategories($child['category_id']);

          foreach ($grandchildren as $grandchild) {

              $grandchild_filter_data = array(
                  'filter_category_id'  => $grandchild['category_id'],
                  'filter_sub_category' => true
              );

              $grandchildren_data[] = array(
                  'name'  => $grandchild['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($grandchild_filter_data) . ')' : ''),
                  'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $grandchild['category_id'])
              );
          }

					$filter_data = array(
						'filter_category_id'  => $child['category_id'],
						'filter_sub_category' => true
					);

					$children_data[] = array(
						'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']),
						'children' => $grandchildren_data
					);
				}

				// 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'])
				);
			}
		}

		$this->load->language('extension/module/salescombopge');

		$data['categories'][] = array(
		  'name'     => $this->language->get('text_salescombopge_heading'),
		  'children' => '',
		  'column'   => 1,
		  'href'     => $this->url->link("extension/module/promotion")
		);


        if($this->config->get("offerpage_installed")) {
          $this->load->language('extension/module/salescombopge');
          $data['salescombopge_info'] = array();
          $this->load->model('extension/module/salescombopge');
          $salescombopge_info = $this->model_extension_module_salescombopge->getPages();
          foreach ($salescombopge_info as $key => $value) {
            if($value['top']) {
              $data['salescombopge_info'][] = array(
                'name'=> $value['title'],
                'href' => $this->url->link('extension/module/salescombopge', 'page_id=' .  $value['salescombopge_id']),
                'id' => "",
                'children_level2' => array()
              );
            }  
          }
          if(!empty($data['salescombopge_info'])) {
            $data['categories'][] = array(
              'name'     => $this->language->get('text_salescombopge_heading'),
              'children' => $data['salescombopge_info'],
              'column'   => 1,
              'href'     => $this->url->link("extension/module/alloffers")
            );
          }
        }  

        
		return $this->load->view('common/menu', $data);
	}
}
