<?php  
class ControllerCommonContentBottom extends Controller {
	protected function index() {
		$this->load->model('design/layout');
		$this->load->model('catalog/category');
		$this->load->model('catalog/product');
		$this->load->model('catalog/information');
		
		if (isset($this->request->get['route'])) {
			$route = (string)$this->request->get['route'];
		} else {
			$route = 'common/home';
		}
		
		$layout_id = 0;
		
		if ($route == 'product/category' && isset($this->request->get['path'])) {
			$path = explode('_', (string)$this->request->get['path']);
				
			$layout_id = $this->model_catalog_category->getCategoryLayoutId(end($path));			
		}
		
		if ($route == 'product/product' && isset($this->request->get['product_id'])) {
			$layout_id = $this->model_catalog_product->getProductLayoutId($this->request->get['product_id']);
		}
		
		if ($route == 'information/information' && isset($this->request->get['information_id'])) {
			$layout_id = $this->model_catalog_information->getInformationLayoutId($this->request->get['information_id']);
		}
		
		if (!$layout_id) {
			$layout_id = $this->model_design_layout->getLayout($route);
		}
						
		if (!$layout_id) {
			$layout_id = $this->config->get('config_layout_id');
		}

		$module_data = array();
		
		$this->load->model('setting/extension');
		
		$extensions = $this->model_setting_extension->getExtensions('module');		
		
//OMF start
			$store_id = $this->config->get('config_store_id') ? $this->config->get('config_store_id') : 0;
			//OMF end 
		foreach ($extensions as $extension) {
//OMF start
			$isMobileExtension = false;

			if(strpos($extension['code'], '_mobile') !== false) {
				$isMobileExtension = true;
			}//OMF end 
			
			$modules = $this->config->get($extension['code'] . '_module');
			
			if ($modules) {
				foreach ($modules as $module) {
//OMF start
					if($this->isVisitorMobile()) { //OMF If our client is a mobile device
						if ($module['layout_id'] == $layout_id && $module['position'] == 'content_bottom' && $module['status'] && ($isMobileExtension || (isMobile() && $this->config->get('config_modules_' . $extension['code'] . '_smartphones' . $store_id)) || (isTablet() && $this->config->get('config_modules_' . $extension['code'] . '_tablets' . $store_id)))) { //load only enabled an mobile extensions
							$module_data[] = array(
								'code'       => $extension['code'],
								'setting'    => $module,
								'sort_order' => $module['sort_order']
							);
						}
					} else if(!$isMobileExtension) { // Load extensions normally 
				//OMF end  
			
					if ($module['layout_id'] == $layout_id && $module['position'] == 'content_bottom' && $module['status']) {
						$module_data[] = array(
							'code'       => $extension['code'],
							'setting'    => $module,
							'sort_order' => $module['sort_order']
						);				
					}
				}
			}
		}
		
//OMF start
		}//OMF end 
			
		$sort_order = array(); 
	  
		foreach ($module_data as $key => $value) {
      		$sort_order[$key] = $value['sort_order'];
    	}
		
		array_multisort($sort_order, SORT_ASC, $module_data);
		
		$this->data['modules'] = array();
		
		foreach ($module_data as $module) {
			$module = $this->getChild('module/' . $module['code'], $module['setting']);
			
			if ($module) {
				$this->data['modules'][] = $module;
			}
		}

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/content_bottom.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/content_bottom.tpl';
		} else {
			$this->template = 'default/template/common/content_bottom.tpl';
		}
								
		$this->render();
	}
}
?>