<?php
//-----------------------------------------
// Author: Qphoria@gmail.com
// Web: http://www.OpenCartGuru.com/
//-----------------------------------------
class ControllerModuleSplitCategory extends Controller {
	protected function index($setting) {

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

		$this->data['css_class'] = (isset($setting['css_class'])) ? $setting['css_class'] : '';

    	$this->data['heading_title'] = $setting['language_id'][$this->config->get('config_language_id')];

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

		if (isset($this->request->get['path'])) {
			$parts = explode('_', (string)$this->request->get['path']);
		} else {
			$parts = array();
		}

		if (isset($parts[0])) {
			$this->data['category_id'] = $parts[0];
		} else {
			$this->data['category_id'] = 0;
		}

		if (isset($parts[1])) {
			$this->data['child_id'] = $parts[1];
		} else {
			$this->data['child_id'] = 0;
		}

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

		if (isset($setting['style'])) {
			$this->data['style'] = $setting['style'];
		} else {
			$this->data['style'] = 'list';
		}

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

		//$categories = $this->model_catalog_category->getCategories(0);
		$categories = $this->getCategoryInId(implode("','", $setting['category_id']));

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

		foreach ($categories as $category) {

			// Skip the ones with sort order = -1
			if ($category['sort_order'] === '-1') { continue; }

			$children_data = array();

			$children = $this->model_catalog_category->getCategories($category['category_id']);

			foreach ($children as $child) {

				// Skip the ones with sort order = -1
				if ($child['sort_order'] === '-1') { continue; }

				$data = array(
					'filter_category_id'  => $child['category_id'],
					'filter_sub_category' => true
				);

				if ($setting['count']) {
					$product_total = $this->model_catalog_product->getTotalProducts($data);
					$child['name'] = $child['name'] . ' (' . $product_total . ')';
				}

				$children_data[] = array(
					'category_id' => $child['category_id'],
					'name'        => $child['name'],
					'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
				);
			}

			$data = array(
				'filter_category_id'  => $category['category_id'],
				'filter_sub_category' => true
			);

			if ($setting['count']) {
				$product_total = $this->model_catalog_product->getTotalProducts($data);
				$category['name'] = $category['name'] . ' (' . $product_total . ')';
			}

			if ($category['image']) {
				$image = $category['image'];
			} else {
				$image = 'no_image.jpg';
			}

			$this->data['categories'][] = array(
				'category_id' => $category['category_id'],
				'name'        => $category['name'],
				'children'    => $children_data,
				'href'        => $this->url->link('product/category', 'path=' . $category['category_id']),
				'thumb' 	  => $this->model_tool_image->resize($image, $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'))
			);
		}

		if (isset($setting['expand'])) {
			$this->data['always_expand'] = $setting['expand'];
		} else {
			$this->data['always_expand'] = false;
		}

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/split_category.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/split_category.tpl';
		} else {
			$this->template = 'default/template/module/split_category.tpl';
		}

		$this->render();
  	}

  	private function getCategoryInId($str) {
		$category_data = array();

		$category_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.category_id IN ('" . $str . "') AND cd.language_id = '" . $this->config->get('config_language_id') . "' AND c2s.store_id = '" . $this->config->get('config_store_id') . "' ORDER BY c.sort_order, LCASE(cd.name)");
		//$category_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.category_id IN ('" . $str . "') AND cd.language_id = '" . $this->config->get('config_language_id') . "'");

		return $category_query->rows;
	}
}
?>