<?php
class ControllerExtensionModuleCustommenu extends Controller {		
	private $error = array();
	private $modpath = 'module/custommenu'; 
	private $modtpl = 'default/template/module/custommenu.tpl'; 
	private $modname = 'custommenu';
	private $modtext = 'Custom Menu Navbar';
	private $modid = '26286';
	private $modssl = 'SSL';
	private $modemail = 'opencarttools@gmail.com';
	
	public function __construct($registry) {
		parent::__construct($registry);
 		
		if(substr(VERSION,0,3)>='3.0' || substr(VERSION,0,3)=='2.3') { 
			$this->modtpl = 'extension/module/custommenu';
			$this->modpath = 'extension/module/custommenu';
		} else if(substr(VERSION,0,3)=='2.2') {
			$this->modtpl = 'module/custommenu';
		} 
		
		if(substr(VERSION,0,3)>='3.0') { 
			$this->modname = 'module_custommenu';
		}  
		
		if(substr(VERSION,0,3)>='3.0' || substr(VERSION,0,3)=='2.3' || substr(VERSION,0,3)=='2.2') { 
			$this->modssl = true;
		} 
 	} 
	
	public function index() {
 		$data = $this->load->language($this->modpath);
 				
		$data['text_custommenu'] = $this->language->get('text_custommenu');
		
		$data['custommenus'] = array();
		
		$data[$this->modname.'_status'] = $this->setvalue($this->modname.'_status');
		
		if($data[$this->modname.'_status']) {
			// Menu
			$this->load->model($this->modpath);
	
 			$data['custommenus'] = array();
			
			if(substr(VERSION,0,3)>='3.0' || substr(VERSION,0,3)=='2.3') { 
				$custommenus = $this->model_extension_module_custommenu->getCategories(0);
			} else {
				$custommenus = $this->model_module_custommenu->getCategories(0);
			}
	
			foreach ($custommenus as $custommenu) {
				if ($custommenu['top']) {
					// Level 2
					$children_data = array();
					
					if(substr(VERSION,0,3)>='3.0' || substr(VERSION,0,3)=='2.3') { 	
						$children = $this->model_extension_module_custommenu->getCategories($custommenu['custommenu_id']);
					} else {
						$children = $this->model_module_custommenu->getCategories($custommenu['custommenu_id']);
					}
	
					foreach ($children as $child) {
						$filter_data = array(
							'filter_custommenu_id'  => $child['custommenu_id'],
							'filter_sub_custommenu' => true
						);
	
						$children_data[] = array(
							'name'  => $child['name'] ,
							'custlink'  => $child['custlink']
						);
					}
	
					// Level 1
					$data['custommenus'][] = array(
						'name'     => $custommenu['name'],
						'children' => $children_data,
						'target' => $custommenu['target'],
						'column'   => $custommenu['column'] ? $custommenu['column'] : 1,
						'custlink'     => $custommenu['custlink']
					);
				}
			}
		}
		
		if($data['custommenus']) {
			return $this->load->view($this->modtpl, $data);
 		}
	}
	
	protected function setvalue($postfield) {
		return $this->config->get($postfield);
	}
}