<?php
class ControllerExtensionModuleSalescombopge extends Controller {
	private $error = array();

	public function index() {

		$this->load->language('extension/module/salescombopge');

		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('extension/module/salescombopge');
		$this->model_extension_module_salescombopge->createTable();

		$this->getList();
	}

	public function add() {
		$this->load->language('extension/module/salescombopge');

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

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

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_extension_module_salescombopge->addsalescombopge($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			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->response->redirect($this->url->link('extension/module/salescombopge', 'user_token=' . $this->session->data['user_token'] . $url, TRUE));
		}

		$this->getForm();
	}

	public function edit() {
		$this->load->language('extension/module/salescombopge');

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

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

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_extension_module_salescombopge->editsalescombopge($this->request->get['salescombopge_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			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->response->redirect($this->url->link('extension/module/salescombopge', 'user_token=' . $this->session->data['user_token'] . $url, TRUE));
		}

		$this->getForm();
	}

	public function delete() {
		$this->load->language('extension/module/salescombopge');

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

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

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $salescombopge_id) {
				$this->model_extension_module_salescombopge->deletesalescombopge($salescombopge_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			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->response->redirect($this->url->link('extension/module/salescombopge', 'user_token=' . $this->session->data['user_token'] . $url, TRUE));
		}

		$this->getList();
	}

	protected function getList() {

		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'id.title';
		}

		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['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'];
		}

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

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

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('extension/module/salescombopge', 'user_token=' . $this->session->data['user_token'] . $url, TRUE)
		);

		$data['add'] = $this->url->link('extension/module/salescombopge/add', 'user_token=' . $this->session->data['user_token'] . $url, TRUE);
		$data['delete'] = $this->url->link('extension/module/salescombopge/delete', 'user_token=' . $this->session->data['user_token'] . $url, TRUE);

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

		$filter_data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_limit_admin'),
			'limit' => $this->config->get('config_limit_admin')
		);

		$salescombopge_total = $this->model_extension_module_salescombopge->getTotalsalescombopges();

		$results = $this->model_extension_module_salescombopge->getsalescombopges($filter_data);

		foreach ($results as $result) {
			$data['salescombopges'][] = array(
				'salescombopge_id' => $result['salescombopge_id'],
				'title'          => $result['title'],
				'sort_order'     => $result['sort_order'],
				'edit'           => $this->url->link('extension/module/salescombopge/edit', 'user_token=' . $this->session->data['user_token'] . '&salescombopge_id=' . $result['salescombopge_id'] . $url, TRUE)
			);
		}

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

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

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

		if (isset($this->request->post['selected'])) {
			$data['selected'] = (array)$this->request->post['selected'];
		} else {
			$data['selected'] = array();
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

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

		$data['sort_title'] = $this->url->link('extension/module/salescombopge', 'user_token=' . $this->session->data['user_token'] . '&sort=id.title' . $url, TRUE);
		$data['sort_sort_order'] = $this->url->link('extension/module/salescombopge', 'user_token=' . $this->session->data['user_token'] . '&sort=i.sort_order' . $url, TRUE);

		$url = '';

		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 = $salescombopge_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_limit_admin');
		$pagination->url = $this->url->link('extension/module/salescombopge', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', TRUE);

		$data['pagination'] = $pagination->render();

		$data['results'] = sprintf($this->language->get('text_pagination'), ($salescombopge_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($salescombopge_total - $this->config->get('config_limit_admin'))) ? $salescombopge_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $salescombopge_total, ceil($salescombopge_total / $this->config->get('config_limit_admin')));

		$data['sort'] = $sort;
		$data['order'] = $order;

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('extension/module/salescombopge_list', $data));
	}

	protected function getForm() {

		$data['version'] = str_replace(".","",VERSION);
		$data['text_form'] = !isset($this->request->get['salescombopge_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->error['title'])) {
			$data['error_title'] = $this->error['title'];
		} else {
			$data['error_title'] = array();
		}

		if (isset($this->error['description'])) {
			$data['error_description'] = $this->error['description'];
		} else {
			$data['error_description'] = array();
		}

		if (isset($this->error['meta_title'])) {
			$data['error_meta_title'] = $this->error['meta_title'];
		} else {
			$data['error_meta_title'] = array();
		}

		if (isset($this->error['keyword'])) {
			$data['error_keyword'] = $this->error['keyword'];
		} else {
			$data['error_keyword'] = '';
		}

		$url = '';

		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'];
		}

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

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

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('extension/module/salescombopge', 'user_token=' . $this->session->data['user_token'] . $url, TRUE)
		);

		if (!isset($this->request->get['salescombopge_id'])) {
			$data['action'] = $this->url->link('extension/module/salescombopge/add', 'user_token=' . $this->session->data['user_token'] . $url, TRUE);
		} else {
			$data['action'] = $this->url->link('extension/module/salescombopge/edit', 'user_token=' . $this->session->data['user_token'] . '&salescombopge_id=' . $this->request->get['salescombopge_id'] . $url, TRUE);
		}

		$data['cancel'] = $this->url->link('extension/module/salescombopge', 'user_token=' . $this->session->data['user_token'] . $url, TRUE);

		if (isset($this->request->get['salescombopge_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$salescombopge_info = $this->model_extension_module_salescombopge->getsalescombopge($this->request->get['salescombopge_id']);
		}

		$data['user_token'] = $this->session->data['user_token'];

		$this->load->model('localisation/language');

		$data['languages'] = $this->model_localisation_language->getLanguages();

		if (isset($this->request->post['salescombopge_description'])) {
			$data['salescombopge_description'] = $this->request->post['salescombopge_description'];
		} elseif (isset($this->request->get['salescombopge_id'])) {
			$data['salescombopge_description'] = $this->model_extension_module_salescombopge->getsalescombopgeDescriptions($this->request->get['salescombopge_id']);
		} else {
			$data['salescombopge_description'] = array();
		}

		$this->load->model('setting/store');

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

		$data['stores'][] = array(
			'store_id' => 0,
			'name'     => $this->language->get('text_default')
		);

		$stores = $this->model_setting_store->getStores();

		foreach ($stores as $store) {
			$data['stores'][] = array(
				'store_id' => $store['store_id'],
				'name'     => $store['name']
			);
		}

		if (isset($this->request->post['salescombopge_store'])) {
			$data['salescombopge_store'] = $this->request->post['salescombopge_store'];
		} elseif (isset($this->request->get['salescombopge_id'])) {
			$data['salescombopge_store'] = $this->model_extension_module_salescombopge->getsalescombopgeStores($this->request->get['salescombopge_id']);
		} else {
			$data['salescombopge_store'] = array(0);
		}

		if (isset($this->request->post['bottom'])) {
			$data['bottom'] = $this->request->post['bottom'];
		} elseif (!empty($salescombopge_info)) {
			$data['bottom'] = $salescombopge_info['bottom'];
		} else {
			$data['bottom'] = 1;
		}

		if (isset($this->request->post['autopopup'])) {
			$data['autopopup'] = $this->request->post['autopopup'];
		} elseif (!empty($salescombopge_info)) {
			$data['autopopup'] = $salescombopge_info['autopopup'];
		} else {
			$data['autopopup'] = 0;
		}

		if (isset($this->request->post['top'])) {
			$data['top'] = $this->request->post['top'];
		} elseif (!empty($salescombopge_info)) {
			$data['top'] = $salescombopge_info['top'];
		} else {
			$data['top'] = 0;
		}

		if (isset($this->request->post['image'])) {
			$data['image'] = $this->request->post['image'];
		} elseif (!empty($salescombopge_info)) {
			$data['image'] = $salescombopge_info['image'];
		} else {
			$data['image'] = '';
		}

		$this->load->model('tool/image');

		if (isset($this->request->post['image']) && is_file(DIR_IMAGE . $this->request->post['image'])) {
			$data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100);
		} elseif (!empty($salescombopge_info) && is_file(DIR_IMAGE . $salescombopge_info['image'])) {
			$data['thumb'] = $this->model_tool_image->resize($salescombopge_info['image'], 100, 100);
		} else {
			$data['thumb'] = $this->model_tool_image->resize('no_image.png', 100, 100);
		}

		$data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);

		if (isset($this->request->post['backgroundcolor'])) {
			$data['backgroundcolor'] = $this->request->post['backgroundcolor'];
		} elseif (!empty($salescombopge_info)) {
			$data['backgroundcolor'] = $salescombopge_info['backgroundcolor'];
		} else {
			$data['backgroundcolor'] = '#eeeeee';
		}


		if (isset($this->request->post['fontcolor'])) {
			$data['fontcolor'] = $this->request->post['fontcolor'];
		} elseif (!empty($salescombopge_info)) {
			$data['fontcolor'] = $salescombopge_info['fontcolor'];
		} else {
			$data['fontcolor'] = '#000000';
		}

		if (isset($this->request->post['status'])) {
			$data['status'] = $this->request->post['status'];
		} elseif (!empty($salescombopge_info)) {
			$data['status'] = $salescombopge_info['status'];
		} else {
			$data['status'] = true;
		}

		if (isset($this->request->post['keyword'])) {
			$data['keyword'] = $this->request->post['keyword'];
		} elseif (isset($this->request->get['salescombopge_id'])) {
			$data['keyword'] = $this->model_extension_module_salescombopge->getSalesComboSeoUrls($this->request->get['salescombopge_id']);
		} else {
			$data['keyword'] = array();
		}

		$this->load->model('customer/customer_group');
		$data['customergroups'] = $this->model_customer_customer_group->getCustomerGroups();
		$data['customerlink'] = "customer/customer/";

		if (isset($this->request->post['customergroupcst'])) {
			$data['customergroupcst'] = $this->request->post['customergroupcst'];
		} elseif (isset($this->request->get['salescombopge_id'])) {
			$data['customergroupcst'] = $this->model_extension_module_salescombopge->getCustomerGroups($this->request->get['salescombopge_id']);
		} else {
			$data['customergroupcst'] = array();
		}

		if (isset($this->request->post['customers'])) {
			$data['customers'] = $this->request->post['customers'];
		} elseif (isset($this->request->get['salescombopge_id'])) {
			$data['customers'] = $this->model_extension_module_salescombopge->getCustomerNames($this->request->get['salescombopge_id']);
		} else {
			$data['customers'] = array();
		}

		if (isset($this->request->post['sort_order'])) {
			$data['sort_order'] = $this->request->post['sort_order'];
		} elseif (!empty($salescombopge_info)) {
			$data['sort_order'] = $salescombopge_info['sort_order'];
		} else {
			$data['sort_order'] = '';
		}

		if (isset($this->request->post['salescombopge_layout'])) {
			$data['salescombopge_layout'] = $this->request->post['salescombopge_layout'];
		} elseif (isset($this->request->get['salescombopge_id'])) {
			$data['salescombopge_layout'] = $this->model_extension_module_salescombopge->getsalescombopgeLayouts($this->request->get['salescombopge_id']);
		} else {
			$data['salescombopge_layout'] = array();
		}

		// Categories
		$this->load->model('catalog/category');

		if (isset($this->request->post['salescombopge_category'])) {
			$categories = $this->request->post['salescombopge_category'];
		} elseif (isset($this->request->get['salescombopge_id'])) {
			$categories = $this->model_extension_module_salescombopge->getsalescombopgeCategories($this->request->get['salescombopge_id']);
		} else {
			$categories = array();
		}

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

		foreach ($categories as $category_id) {
			$category_info = $this->model_catalog_category->getCategory($category_id);

			if ($category_info) {
				$data['product_categories'][] = array(
					'category_id' => $category_info['category_id'],
					'name' => ($category_info['path']) ? $category_info['path'] . ' &gt; ' . $category_info['name'] : $category_info['name']
				);
			}
		}

		//Products
		$this->load->model('catalog/product');
		if (isset($this->request->post['salescombopge_product'])) {
			$products = $this->request->post['salescombopge_product'];
		} elseif (isset($this->request->get['salescombopge_id'])) {
			$products = $this->model_extension_module_salescombopge->getsalescombopgeProducts($this->request->get['salescombopge_id']);
		} else {
			$products = array();
		}

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

		foreach ($products as $product_id) {
			$related_info = $this->model_catalog_product->getProduct($product_id);

			if ($related_info) {
				$data['salescombopge_product'][] = array(
					'product_id' => $related_info['product_id'],
					'name'       => $related_info['name']
				);
			}
		}


		$this->load->model('design/layout');

		$data['layouts'] = $this->model_design_layout->getLayouts();

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('extension/module/salescombopge_form', $data));
	}

	protected function validateForm() {
		if (!$this->user->hasPermission('modify', 'extension/module/salescombopge')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		foreach ($this->request->post['salescombopge_description'] as $language_id => $value) {
			if ((utf8_strlen($value['title']) < 3) || (utf8_strlen($value['title']) > 64)) {
				$this->error['title'][$language_id] = $this->language->get('error_title');
			}

			if ((utf8_strlen($value['meta_title']) < 3) || (utf8_strlen($value['meta_title']) > 255)) {
				$this->error['meta_title'][$language_id] = $this->language->get('error_meta_title');
			}

		}

		if ($this->request->post['keyword']) {
			$this->load->model('design/seo_url');

			foreach ($this->request->post['keyword'] as $store_id => $language) {
				foreach ($language as $language_id => $keyword) {
					if (!empty($keyword)) {
						if (count(array_keys($language, $keyword)) > 1) {
							$this->error['keyword'][$store_id][$language_id] = $this->language->get('error_unique');
						}

						$seo_urls = $this->model_design_seo_url->getSeoUrlsByKeyword($keyword);

						foreach ($seo_urls as $seo_url) {
							if (($seo_url['store_id'] == $store_id) && (!isset($this->request->get['salescombopge_id']) || (($seo_url['query'] != 'salescombopge_id=' . $this->request->get['salescombopge_id'])))) {
								$this->error['keyword'][$store_id][$language_id] = $this->language->get('error_keyword');

								break;
							}
						}
					}
				}
			}
		}

		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}

		return !$this->error;
	}

	protected function validateDelete() {
		if (!$this->user->hasPermission('modify', 'extension/module/salescombopge')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return !$this->error;
	}
}
