<?php
class ControllerModuleCatalog extends Controller {
protected function index($setting) {
$this->document->addScript('catalog/view/javascript/catalog.js');
$this->document->addStyle('catalog/view/theme/default/stylesheet/catalog.css');
$this->language->load('module/catalog');
$this->data['heading_title'] = $this->language->get('heading_title');
if (isset($this->request->get['product_id'])) {
$product_id = (int)$this->request->get['product_id'];
} else {
return;
}
$this->data['product_id'] = $product_id;
$this->load->model('catalog/product');
$this->load->model('catalog/manufacturer');
$product_info = $this->model_catalog_product->getProduct($product_id);
$this->data['active_manufacturer'] = $product_info['manufacturer_id'];
$this->data['active_category'] = $this->getActiveCategory($product_id);
$manufacturers = array ();
$products = $this->getProducts($this->data['active_category']);
$output = array();
$children = array();
$curr_id = $products[0]['manufacturer_id'];
$tmpid = 0;
$tmptotal = 0;
$tmp = array (
'manufacturer_name' => $products[0]['manufacturer_name'],
'manufacturer_id' => $products[0]['manufacturer_id'],
);
foreach ($products as $product) {
$qty = true;
if ((int)$product['quantity'] == 0) {
$qty = false;
}
$special = false;
if (isset($product['special'])) {
$special = true;
}
if ($product['image']) {
$image = $this->model_tool_image->resize($product['image'], 100, 100);
} else {
$image = false;
}
if ($product['manufacturer_id'] != $curr_id) {
$output[$tmpid] = array(
'name' => $tmp['manufacturer_name'] . '<span class="total"> ('.$tmptotal.')</span>',
'manufacturer_id' => $tmp['manufacturer_id'],
'products' => $children
);
$tmp = array (
'manufacturer_name' => $product['manufacturer_name'],
'manufacturer_id' => $product['manufacturer_id'],
);
$children = array();
$curr_id = $product['manufacturer_id'];
$tmpid++;
$tmptotal = 0;
};
$tmptotal++;
$children[] = array(
'product_id' => $product['product_id'],
'name' => $product['product_name'],
'image' => $image,
'qty' => $qty,
'special' => $special,
'href' => $this->url->link('product/product', 'product_id=' . $product['product_id'])
);
}
$output[$tmpid++] = array(
'name' => $tmp['manufacturer_name'] . '<span class="total"> ('.$tmptotal.')</span>',
'manufacturer_id' => $tmp['manufacturer_id'],
'products' => $children
);
$this->data['manufactureres'] = $output;
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/catalog.tpl')) {
$this->template = $this->config->get('config_template') . '/template/module/catalog.tpl';
} else {
$this->template = 'default/template/module/catalog.tpl';
}
$this->render();
}
public function getProducts($active_category, $data = array()) {
$date = date('Y-m-d H:I:s');
if ($this->customer->isLogged()) {
$customer_group_id = $this->customer->getCustomerGroupId();
} else {
$customer_group_id = $this->config->get('config_customer_group_id');
}
$cache = md5($active_category);
$product_data = $this->cache->get('catalog.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . (int)$customer_group_id . '.' . $cache);
if (!$product_data) {
$price_sql = "(SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special,";
$sql = "SELECT p.product_id, pd.name as product_name, p.image, p.quantity, ". $price_sql ." m.name as manufacturer_name, m.manufacturer_id FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id)";
$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id)";
$sql .= " LEFT JOIN " . DB_PREFIX . "manufacturer m ON (m.manufacturer_id = p.manufacturer_id)";
$sql .= " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= UNIX_TIMESTAMP('".$date."') AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";
$sql .= " AND p2c.category_id = '" . (int)$active_category . "'";
$sql .= " ORDER BY m.name ASC";
$query = $this->db->query($sql);
$product_data = $query->rows;
$this->cache->set('catalog.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . (int)$customer_group_id . '.' . $cache, $product_data);
}
return $product_data;
}
public function getActiveCategory($product_id) {
$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' AND main_category = 1 ");
$result = $query->row;
return $result['category_id'];
}
}
?>