<?php 
class ControllerSeoProduct extends Controller {
	private $error = array(); 
     
  	public function index() {
		$this->load->language('seo/product');
    	$this->data['linkSuccessImg'] = "/admin/view/image/success.png";
    	
		$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('seo/product');
		$this->model_seo_product->alterTable();
		
		$this->data['export'] = $this->url->link('seo/product/download', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['action'] = $this->url->link('seo/product/import', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->getList();
  	}
  	
  	public function import(){
  		$this->load->model('seo/product');
  		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_product->upload($file)) {
					$this->session->data['success'] = "Import Success";
					$this->redirect($this->url->link('seo/product', 'token=' . $this->session->data['token'], 'SSL'));
				}
				else {
					$this->error['warning'] = $this->language->get('error_upload');
				}
			}
		}
  	}
  	
  	public function download() {
			// send the categories, products and options as a spreadsheet file
			$this->load->model('seo/product');
			$this->model_seo_product->download();
	}

  	public function update() {
    	$this->load->language('seo/product');

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

  	private function getList() {				
		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		} else {
			$filter_name = null;
		}

		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'pd.name';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
						
		$url = '';
						
		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		}
						
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$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/product', 'token=' . $this->session->data['token'] . $url, 'SSL'),       		
      		'separator' => ' :: '
   		);
    	
		$this->data['products'] = array();

		$data = array(
			'filter_name'	  => $filter_name, 
			'sort'            => $sort,
			'order'           => $order,
			'start'           => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'           => $this->config->get('config_admin_limit')
		);
		
		$this->load->model('tool/image');
		
		$product_total = $this->model_seo_product->getTotalProducts($data);
			
		$results = $this->model_seo_product->getProducts($data);
				    	
		foreach ($results as $result) {
			$action = array();
			
			
			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('seo/product/update', 'token=' . $this->session->data['token'] . '&product_id=' . $result['product_id'] . $url, 'SSL')
			);
			
			if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
				$image = $this->model_tool_image->resize($result['image'], 40, 40);
			} else {
				$image = $this->model_tool_image->resize('no_image.jpg', 40, 40);
			}
		
			$result1 = array();
			$result1 = $this->model_seo_product->getUrl($result['product_id']);
			if(!isset($result1['keyword'])){
				$result1['keyword'] = "";
			}
		
      		$this->data['products'][] = array(
      			'product_id' => $result['product_id'],
      			'image'			=> $image,
      			'name'			=> $result['name'],
				'titleTags'		=> $result['meta_titleTags'],
				'description'	=> $result['meta_description'],
				'keywords'      => $result['meta_keyword'],
				'imgAlt'		=> $result['meta_imgAlt'],
				'URL'			=> $result1['keyword'],
				'selected'		=> isset($this->request->post['selected']) && in_array($result['product_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_image'] = $this->language->get('column_image');					
		$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_img_alt'] = $this->language->get('column_img_alt');		
		$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 (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		}
		
		if (isset($this->request->get['filter_model'])) {
			$url .= '&filter_model=' . $this->request->get['filter_model'];
		}
		
		if (isset($this->request->get['filter_price'])) {
			$url .= '&filter_price=' . $this->request->get['filter_price'];
		}
		
		if (isset($this->request->get['filter_quantity'])) {
			$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
		}
		
		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}
								
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

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

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		}
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
				
		$pagination = new Pagination();
		$pagination->total = $product_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('seo/product', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();
	
		$this->data['filter_name'] = $filter_name;
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'seo/product_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
  	}
 
		
	public function autocomplete() {
		$json = array();
		
		if (isset($this->request->get['filter_name']) || isset($this->request->get['filter_model']) || isset($this->request->get['filter_category_id'])) {
			$this->load->model('seo/product');
			
			if (isset($this->request->get['filter_name'])) {
				$filter_name = $this->request->get['filter_name'];
			} else {
				$filter_name = '';
			}
			
			
			if (isset($this->request->get['limit'])) {
				$limit = $this->request->get['limit'];	
			} else {
				$limit = 20;	
			}			
						
			$data = array(
				'filter_name'         => $filter_name,
				'start'               => 0,
				'limit'               => $limit
			);
			
			$results = $this->model_seo_product->getProducts($data);
			
			foreach ($results as $result) {
				$option_data = array();
				
				
				$json[] = array(
					'product_id' => $result['product_id'],
					'name'       => html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'),	
					'model'      => $result['model'],
					'price'      => $result['price']
				);	
			}
		}

		$this->response->setOutput(json_encode($json));
	}
}
?>