<?php
class ControllerCommonDashboard extends Controller {
	public function index() {
	    
	      $this->load->model('user/user');
	      $user_info = $this->model_user_user->getUser($this->user->getId());
	       if ($user_info && $user_info['user_group_id']=='8') {
	          $this->response->redirect($this->url->link('sale/couponused_order', 'token=' . $this->session->data['token'] , true));
	       }elseif ($user_info && $user_info['user_group_id']=='9') {
               $this->response->redirect($this->url->link('sale/coupon_tt_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='10') {
               $this->response->redirect($this->url->link('sale/coupon_hopi_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='11') {
               $this->response->redirect($this->url->link('sale/coupon_denebunu_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='12') {
               $this->response->redirect($this->url->link('sale/coupon_fastpay_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='13') {
               $this->response->redirect($this->url->link('sale/coupon_ing_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='14') {
               $this->response->redirect($this->url->link('sale/coupon_borusan_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='15') {
               $this->response->redirect($this->url->link('sale/coupon_tosla_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='16') {
               $this->response->redirect($this->url->link('sale/coupon_grupanya_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='17') {
               $this->response->redirect($this->url->link('sale/coupon_onlyforyou_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='18') {
               $this->response->redirect($this->url->link('sale/coupon_itu_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='19') {
               $this->response->redirect($this->url->link('sale/coupon_hacettepe_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='20') {
               $this->response->redirect($this->url->link('sale/coupon_garanti_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='21') {
               $this->response->redirect($this->url->link('sale/coupon_yildizteknik_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='22') {
               $this->response->redirect($this->url->link('sale/coupon_koc_uni_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='21') {
               $this->response->redirect($this->url->link('sale/coupon_dokuzeylul_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='22') {
               $this->response->redirect($this->url->link('sale/coupon_gmthacettepe_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='23') {
               $this->response->redirect($this->url->link('sale/coupon_uludaguni_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='24') {
               $this->response->redirect($this->url->link('sale/coupon_duzceuni_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='25') {
               $this->response->redirect($this->url->link('sale/coupon_balikesir_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='26') {
               $this->response->redirect($this->url->link('sale/coupon_yildizgirisim_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='27') {
               $this->response->redirect($this->url->link('sale/coupon_eskisehirand_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='31') {
               $this->response->redirect($this->url->link('sale/coupon_yasam_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='32') {
               $this->response->redirect($this->url->link('sale/coupon_radiobilkent_order', 'token=' . $this->session->data['token'] , true));
           }elseif ($user_info && $user_info['user_group_id']=='33') {
               $this->response->redirect($this->url->link('sale/coupon_vodafone_order', 'token=' . $this->session->data['token'] , true));
           }

		$this->load->language('common/dashboard');

		$this->document->setTitle($this->language->get('heading_title'));

		$data['heading_title'] = $this->language->get('heading_title');

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

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
		);

		// Check install directory exists
		if (is_dir(dirname(DIR_APPLICATION) . '/install')) {
			$data['error_install'] = $this->language->get('error_install');
		} else {
			$data['error_install'] = '';
		}
		// Harun Yaptı Burayı Token İçin Sakat OLabilir!!
		$data['token'] = $this->session->data['token'];
		// Harun Yaptı Burayı Token İçin Sakat OLabilir!!

		// Dashboard Extensions
		$dashboards = array();

		$this->load->model('extension/extension');

		// Get a list of installed modules
		$extensions = $this->model_extension_extension->getInstalled('dashboard');
		
		// Add all the modules which have multiple settings for each module
		foreach ($extensions as $code) {
			if($code != 'order' && $code != 'sale' && $code != 'customer' && $code != 'online' && $code != 'recent' && $code != 'chart' && $code != 'notes'){
				if ($this->config->get('dashboard_' . $code . '_status') && $this->user->hasPermission('access', 'extension/dashboard/' . $code)) {
					$output = $this->load->controller('extension/dashboard/' . $code . '/dashboard');
					
					if ($output) {
						$dashboards[] = array(
							'code'       => $code,
							'width'      => $this->config->get('dashboard_' . $code . '_width'),
							'sort_order' => $this->config->get('dashboard_' . $code . '_sort_order'),
							'output'     => $output
						);
					}
				}
			}
		}

		$sort_order = array();

		foreach ($dashboards as $key => $value) {
			$sort_order[$key] = $value['sort_order'];
		}

		array_multisort($sort_order, SORT_ASC, $dashboards);
		
		// Split the array so the columns width is not more than 12 on each row.
		$width = 0;
		$column = array();
		$data['rows'] = array();
		
		foreach ($dashboards as $dashboard) {
			$column[] = $dashboard;
			
			$width = ($width + $dashboard['width']);
			
			if ($width >= 3) {
				$data['rows'][] = $column;
				
				$width = 0;
				$column = array();
			}
		}
		
		
		$data['user'] = $this->config->get('helpdesk_login');
		
		$data['pass'] = $this->config->get('helpdesk_key');
		
		if($this->config->get('helpdesk_licence_key')){
			$data['refresh_Licence_Link'] = $this->config->get('helpdesk_licence_key');	
		}else{
			$data['refresh_Licence_Link'] = '#';
		}
		
		
		$data['days'] = $this->load->controller('extension/module/helpdesk/domain_verification');
		
		if ($this->config->get('dashboard_notes_status') && $this->user->hasPermission('access', 'extension/dashboard/notes')) {
			$data['notes'] = $this->load->controller('extension/dashboard/notes/dashboard');
		}
		
		if ($this->config->get('dashboard_order_status') && $this->user->hasPermission('access', 'extension/dashboard/order')) {
			$data['order'] = $this->load->controller('extension/dashboard/order/dashboard');
		}
		if ($this->config->get('dashboard_sale_status') && $this->user->hasPermission('access', 'extension/dashboard/sale')) {
			$data['sale'] = $this->load->controller('extension/dashboard/sale/dashboard');
		}
		if ($this->config->get('dashboard_customer_status') && $this->user->hasPermission('access', 'extension/dashboard/customer')) {
			$data['customer'] = $this->load->controller('extension/dashboard/customer/dashboard');
		}
		if ($this->config->get('dashboard_online_status') && $this->user->hasPermission('access', 'extension/dashboard/online')) {
			$data['online'] = $this->load->controller('extension/dashboard/online/dashboard');
		}
		//$data['map'] = $this->load->controller('dashboard/map');
		if ($this->config->get('dashboard_chart_status') && $this->user->hasPermission('access', 'extension/dashboard/chart')) {
			$data['chart'] = $this->load->controller('extension/dashboard/chart/dashboard');
			//$data['activity'] = $this->load->controller('dashboard/activity');
		}
		if ($this->config->get('dashboard_recent_status') && $this->user->hasPermission('access', 'extension/dashboard/recent')) {
			$data['recent'] = $this->load->controller('extension/dashboard/recent/dashboard');
		}
		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		// Run currency update
		if ($this->config->get('config_currency_auto')) {
			$this->load->model('localisation/currency');

			$this->model_localisation_currency->refresh();
		}

		if(file_exists(DIR_LOCAL_TEMPLATE .'common/dashboard.tpl')){
		    $this->response->setOutput($this->load->view( DIR_LOCAL_TEMPLATE .'common/dashboard', $data));
		}else{ 
		    $this->response->setOutput($this->load->view(DIR_TEMPLATE . 'common/dashboard', $data));
		}

		
	}
}