<?php 
class ControllerSeoCategory extends Controller {
	private $error = array(); 
     
  	public function index() {
		$this->load->language('seo/category');
		$this->data['linkSuccessImg'] = "/admin/view/image/success.png";
    	
		$this->document->setTitle($this->language->get('heading_title')); 
	
		$this->load->model('seo/category');
		$this->model_seo_category->alterTable();
		
		$this->data['export'] = $this->url->link('seo/category/download', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['action'] = $this->url->link('seo/category/import', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->getList();
  	}
  	
  	public function import(){
  		$this->load->model('seo/category');
  		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			if ((isset( $this->request->files['upload'] )) && (is_uploaded_file($this->request->files['upload']['tmp_name']))) {
				$file = $this->request->files['upload']['tmp_name'];
				if ($this->model_seo_category->upload($file)) {
					$this->session->data['success'] = "Import Success";
					$this->redirect($this->url->link('seo/category', 'token=' . $this->session->data['token'], 'SSL'));
				}
				else {
					$this->error['warning'] = $this->language->get('error_upload');
				}
			}
		}
  	}
  	
  	public function download() {
			// set appropriate memory and timeout limits
			//ini_set("memory_limit","128M");
			//set_time_limit( 1800 );

			// send the categories, categorys and options as a spreadsheet file
			$this->load->model('seo/category');
			$this->model_seo_category->download();
	}
  	
  	public function update() {
    	$this->load->language('seo/category');

    	$this->load->model('seo/category');
	
    	if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
    		print_r($_POST);
			$this->model_seo_category->editCategory($_POST);
		}
  	}
  	

  	private function getList() {				
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
						
		$url = '';
						
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

  		$this->data['breadcrumbs'] = array();

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

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('seo/category', 'token=' . $this->session->data['token'] . $url, 'SSL'),       		
      		'separator' => ' :: '
   		);
    	
		$this->data['categories'] = array();

		$this->load->model('tool/image');
		
		$categories_total = $this->model_seo_category->getTotalCategories();
			
		$results = $this->model_seo_category->getCategories(0, $order);	    
				    	
		foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('seo/category/update', 'token=' . $this->session->data['token'] . '&category_id=' . $result['category_id'] . $url, 'SSL')
			);


			$result['name'] = $this->model_seo_category->getPath($result['category_id']);

			$result1 = array();
			$result1 = $this->model_seo_category->getUrl($result['category_id']);
			if(!isset($result1['keyword'])){
				$result1['keyword'] = "";
			}
		
      		$this->data['categories'][] = array(
      			'category_id' => $result['category_id'],
      			'name'			=> $result['name'],
				'titleTags'		=> $result['meta_titleTags'],
				'description'	=> $result['meta_description'],
				'keywords'      => $result['meta_keyword'],
				'URL'			=> $result1['keyword'],
				'selected'		=> isset($this->request->post['selected']) && in_array($result['category_id'], $this->request->post['selected']),
				'action'		=> $action
			);
    	}
		
		$this->data['heading_title'] = $this->language->get('heading_title');		
				
		$this->data['text_enabled'] = $this->language->get('text_enabled');		
		$this->data['text_disabled'] = $this->language->get('text_disabled');		
		$this->data['text_no_results'] = $this->language->get('text_no_results');		
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');		

		$this->data['column_name'] = $this->language->get('column_name');		
		$this->data['column_title_tags'] = $this->language->get('column_title_tags');		
		$this->data['column_description'] = $this->language->get('column_description');		
		$this->data['column_keywords'] = $this->language->get('column_keywords');		
		$this->data['column_URL'] = $this->language->get('column_URL');		
		$this->data['column_action'] = $this->language->get('column_action');		
					
		$this->data['button_filter'] = $this->language->get('button_filter');
		 
 		$this->data['token'] = $this->session->data['token'];
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';
			
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}
					
		$this->data['sort_name'] = $this->url->link('seo/category', 'token=' . $this->session->data['token'] . '&sort=pd.name' . $url, 'SSL');
		$this->data['sort_order'] = $this->url->link('seo/category', 'token=' . $this->session->data['token'] . '&sort=p.sort_order' . $url, 'SSL');
		
		$url = '';
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
				
		$this->data['order'] = $order;

		$this->template = 'seo/category_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
  	}
}
?>