<?php
class ControllerCommonColumnLeft extends Controller {
	public function index() {
		if (isset($this->request->get['token']) && isset($this->session->data['token']) && ($this->request->get['token'] == $this->session->data['token'])) {
			$this->load->language('common/column_left');
	
			$this->load->model('user/user');
	
			$this->load->model('tool/image');
	
			$user_info = $this->model_user_user->getUser($this->user->getId());
	
			if ($user_info) {
				$data['firstname'] = $user_info['firstname'];
				$data['lastname'] = $user_info['lastname'];
	
				$data['user_group'] = $user_info['user_group'];
	
				if (is_file(DIR_IMAGE . $user_info['image'])) {
					$data['image'] = $this->model_tool_image->resize($user_info['image'], 45, 45);
				} else {
					$data['image'] = '';
				}
			} else {
				$data['firstname'] = '';
				$data['lastname'] = '';
				$data['user_group'] = '';
				$data['image'] = '';
			}			
		
			// Create a 3 level menu array
			// Level 2 can not have children
			
			// System
			$system = array();
			
			if ($this->user->hasPermission('access', 'setting/setting')) {
				$system[] = array(
					'name'	   => $this->language->get('text_setting'),
					'href'     => $this->url->link('setting/store', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);	
			}
		
			// Users
			$user = array();
			
			if ($this->user->hasPermission('access', 'user/user')) {
				$user[] = array(
					'name'	   => $this->language->get('text_users'),
					'href'     => $this->url->link('user/user', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);	
			}
			
			if ($this->user->hasPermission('access', 'user/user_permission')) {	
				$user[] = array(
					'name'	   => $this->language->get('text_user_group'),
					'href'     => $this->url->link('user/user_permission', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);	
			}
			
			if ($user) {
				$system[] = array(
					'name'	   => $this->language->get('text_users'),
					'href'     => '',
					'children' => $user		
				);
			}

			
			// Tools	
			$tool = array();
			
			if ($this->user->hasPermission('access', 'tool/backup')) {
				$tool[] = array(
					'name'	   => $this->language->get('text_backup'),
					'href'     => $this->url->link('tool/backup', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);
			}
			
			if ($this->user->hasPermission('access', 'tool/log')) {
				$tool[] = array(
					'name'	   => $this->language->get('text_log'),
					'href'     => $this->url->link('tool/log', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);
			}

			if ($this->user->hasPermission('access', 'extension/module/storage_cleaner')) {
				$tool[] = array(
					'name'	   => $this->language->get('text_storage_cleaner'),
					'href'     => $this->url->link('extension/module/storage_cleaner', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);
			}	
			
			if ($tool) {
				$system[] = array(
					'name'	   => $this->language->get('text_tools'),
					'href'     => '',
					'children' => $tool	
				);
			}
			
			if ($system) {
				$data['menus'][] = array(
					'id'       => 'menu-system',
					'icon'	   => 'fa-cog', 
					'name'	   => $this->language->get('text_system'),
					'href'     => '',
					'children' => $system
				);
			}
			
			// custommenu
			$custommenu = array();
			
			if ($this->user->hasPermission('access', 'extension/custommenu')) {
				$custommenu[] = array(
					'name'	   => $this->language->get('text_custommenugl'),
					'href'     => $this->url->link('extension/custommenu', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);	
			}
			if ($custommenu) {
				$data['menus'][] = array(
					'id'       => 'menu-custommenu',
					'icon'	   => 'fa fa-navicon', 
					'name'	   => $this->language->get('text_custommenu'),
					'href'     => '',
					'children' => $custommenu
				);	
			}				

			/* Form Builders */						
			$tmdformbuilder = array();
		
			
			if ($this->user->hasPermission('access', 'tmdform/form')) {		
				$tmdformbuilder[] = array(
					'name'	   => $this->language->get('entry_tmdcreateform'),
					'href'     => $this->url->link('tmdform/form', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);					
			}	
				
			if ($this->user->hasPermission('access', 'tmdform/record')) {		
				$tmdformbuilder[] = array(
					'name'	   => $this->language->get('entry_tmdformrecord'),
					'href'     => $this->url->link('tmdform/record', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);					
			}	
			
			/* Filed name */
			$this->load->model('tmdform/form');
			$form_infos = $this->model_tmdform_form->getHeaderlinks();
			
			if(isset($form_infos)) {
				foreach($form_infos as $form_info) {
				
			if ($this->user->hasPermission('access', 'tmdform/record')) {		
				$tmdformbuilder[] = array(
					'name'	   => $form_info['title'],
					'href'     => $this->url->link('tmdform/filedrecord', 'token=' . $this->session->data['token'] .'&form_id=' . $form_info['form_id'] .'&filter_title=' . $form_info['form_id'], true),
					'children' => array()		
				);	
			
			}
			}
			}
			/* Filed name */
			
			if ($tmdformbuilder) {					
				$data['menus'][] = array(
					'id'       => 'menu-tmdformbuilder',
					'icon'	   => 'fa fa-files-o', 
					'name'	   => $this->language->get('text_tmdform'),
					'href'     => '',
					'children' => $tmdformbuilder
				);		
			}
			
			/* Form Builders */
			
			// Blog
			$this->load->language('blog/blog');
			$blog = array();
			if ($this->user->hasPermission('access', 'blog/blog_setting')) {
				$blog[] = array(
					'name'	   => $this->language->get('text_blog_setting'),
					'href'     => $this->url->link('blog/blog_setting', 'token=' . $this->session->data['token'], true),
					'children' => array()
			);}
			if ($this->user->hasPermission('access', 'blog/blog_category')) {
				$blog[] = array(
					'name'	   => $this->language->get('text_blog_category'),
					'href'     => $this->url->link('blog/blog_category', 'token=' . $this->session->data['token'], true),
					'children' => array()		
			);}
			if ($this->user->hasPermission('access', 'blog/blog')) {
				$blog[] = array(
					'name'	   => $this->language->get('text_blog_post'),
					'href'     => $this->url->link('blog/blog', 'token=' . $this->session->data['token'], true),
					'children' => array()		
			);}
			if ($this->user->hasPermission('access', 'blog/blog_comment')) {
				$blog[] = array(
					'name'	   => $this->language->get('text_blog_comment'),
					'href'     => $this->url->link('blog/blog_comment', 'token=' . $this->session->data['token'], true),
					'children' => array()		
			);}
			if ($blog) {
				$data['menus'][] = array(
					'id'       => 'menu-blog',
					'icon'	   => 'fa-newspaper-o', 
					'name'	   => $this->language->get('text_blog'),
					'href'     => '',
					'children' => $blog
			);}
			
			// Catalog
			$catalog = array();	
			if ($this->user->hasPermission('access', 'catalog/category')) {
				$catalog[] = array(
					'name'	   => $this->language->get('text_category'),
					'href'     => $this->url->link('catalog/category', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);
			}
			
			if ($this->user->hasPermission('access', 'catalog/product')) {
				$catalog[] = array(
					'name'	   => $this->language->get('text_product'),
					'href'     => $this->url->link('catalog/product', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);
			}

			if ($catalog) {
				$data['menus'][] = array(
					'id'       => 'menu-catalog',
					'icon'	   => 'fa-laptop', 
					'name'	   => $this->language->get('text_catalog'),
					'href'     => '',
					'children' => $catalog
				);		
			}	
			
			// Extension
			$extension = array();
			
			if ($this->user->hasPermission('access', 'extension/modification')) {
				$extension[] = array(
					'name'	   => $this->language->get('text_modification'),
					'href'     => $this->url->link('extension/modification', 'token=' . $this->session->data['token'], true),
					'children' => array()		
				);
			}					
			if ($extension) {					
				$data['menus'][] = array(
					'id'       => 'menu-extension',
					'icon'	   => 'fa-registered', 
					'name'	   => $this->language->get('text_extension'),
					'href'     => '',
					'children' => $extension
				);		
			}
			
			// Stats
			$data['text_complete_status'] = $this->language->get('text_complete_status');
			$data['text_processing_status'] = $this->language->get('text_processing_status');
			$data['text_other_status'] = $this->language->get('text_other_status');
	
			$this->load->model('sale/order');
			$order_total = $this->model_sale_order->getTotalOrders();
	
			$complete_total = $this->model_sale_order->getTotalOrders(array('filter_order_status' => implode(',', $this->config->get('config_complete_status'))));
			
			if ($complete_total) {
				$data['complete_status'] = round(($complete_total / $order_total) * 100);
			} else {
				$data['complete_status'] = 0;
			}
	
			$processing_total = $this->model_sale_order->getTotalOrders(array('filter_order_status' => implode(',', $this->config->get('config_processing_status'))));
	
			if ($processing_total) {
				$data['processing_status'] = round(($processing_total / $order_total) * 100);
			} else {
				$data['processing_status'] = 0;
			}
	
			$this->load->model('localisation/order_status');
	
			$order_status_data = array();
	
			$results = $this->model_localisation_order_status->getOrderStatuses();
	
			foreach ($results as $result) {
				if (!in_array($result['order_status_id'], array_merge($this->config->get('config_complete_status'), $this->config->get('config_processing_status')))) {
					$order_status_data[] = $result['order_status_id'];
				}
			}
	
			$other_total = $this->model_sale_order->getTotalOrders(array('filter_order_status' => implode(',', $order_status_data)));
	
			if ($other_total) {
				$data['other_status'] = round(($other_total / $order_total) * 100);
			} else {
				$data['other_status'] = 0;
			}
			
			return $this->load->view('common/column_left', $data);
		}
	}
}