<?php

class ControllerCatalogSuperProduct extends Controller {

    private $error = array();

    //super_product
    //SuperProduct
    public function index() {
        $this->language->load('catalog/super_product');

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

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

        $this->getList();
    }

    protected 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['filter_category_id'])) {
            $filter_category_id = $this->request->get['filter_category_id'];
        } else {
            $filter_category_id = null;
        }

        if (isset($this->request->get['filter_manufacturer_id'])) {
            $filter_manufacturer_id = $this->request->get['filter_manufacturer_id'];
        } else {
            $filter_manufacturer_id = null;
        }

        if (isset($this->request->get['filter_other'])) {
            $filter_other = $this->request->get['filter_other'];
        } else {
            $filter_other = null;
        }


        if (isset($this->request->get['filter_model'])) {
            $filter_model = $this->request->get['filter_model'];
        } else {
            $filter_model = null;
        }

        if (isset($this->request->get['filter_price'])) {
            $filter_price = $this->request->get['filter_price'];
        } else {
            $filter_price = null;
        }

        if (isset($this->request->get['filter_quantity'])) {
            $filter_quantity = $this->request->get['filter_quantity'];
        } else {
            $filter_quantity = null;
        }

        if (isset($this->request->get['filter_status'])) {
            $filter_status = $this->request->get['filter_status'];
        } else {
            $filter_status = 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=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
        }
        if (isset($this->request->get['filter_category_id'])) {
            $url .= '&filter_category_id=' . $this->request->get['filter_category_id'];
        }
        if (isset($this->request->get['filter_manufacturer_id'])) {
            $url .= '&filter_manufacturer_id=' . $this->request->get['filter_manufacturer_id'];
        }

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

        if (isset($this->request->get['filter_model'])) {
            $url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
        }

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

        $url .= '&super_product=true';
        
        $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('catalog/super_product', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );

        $this->data['ajax_edit'] = $this->url->link('catalog/super_product/ajax_edit', 'token=' . $this->session->data['token'] . $url, 'SSL');
        $this->data['insert'] = $this->url->link('catalog/product/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
        $this->data['copy'] = $this->url->link('catalog/product/copy', 'token=' . $this->session->data['token'] . $url, 'SSL');
        $this->data['delete'] = $this->url->link('catalog/product/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

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



        $data = array(
            'filter_other' => $filter_other,
            'filter_name' => $filter_name,
            'filter_manufacturer_id' => $filter_manufacturer_id,
            'filter_category_id' => $filter_category_id,
            'filter_model' => $filter_model,
            'filter_price' => $filter_price,
            'filter_quantity' => $filter_quantity,
            'filter_status' => $filter_status,
            '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_catalog_super_product->getTotalProducts($data);


        $this->load->model('catalog/category');
        $this->load->model('catalog/manufacturer');
        $this->load->model('catalog/option');
        $other_info = array('product_id', 'sku', 'upc', 'ean', 'jan', 'isbn', 'mpn', 'location');

        $results = $this->model_catalog_super_product->getProducts($data);

        if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
            $store = HTTPS_CATALOG;
        } else {
            $store = HTTP_CATALOG;
        }

        foreach ($results as $result) {
            $action = array();

            $action[] = array(
                'text' => $this->language->get('text_edit'),
                'target' => '',
                'href' => $this->url->link('catalog/product/update', 'token=' . $this->session->data['token'] . '&product_id=' . $result['product_id'] . $url, 'SSL')
            );
            $action[] = array(
                'text' => 'View',
                'target' => '_blank',
                'href' => $store . 'index.php?route=product/product&product_id=' . $result['product_id']
            );

            if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
                $image = $this->model_tool_image->resize($result['image'], 80, 80);
            } else {
                $image = $this->model_tool_image->resize('no_image.jpg', 80, 80);
            }
            $special = false;

            $product_specials = $this->model_catalog_super_product->getProductSpecials($result['product_id']);

            foreach ($product_specials as $product_special) {
                if (($product_special['date_start'] == '0000-00-00' || $product_special['date_start'] < date('Y-m-d')) && ($product_special['date_end'] == '0000-00-00' || $product_special['date_end'] > date('Y-m-d'))) {
                    $special = $product_special['price'];

                    break;
                }
            }

            $product_categories = array();
            $categories = $this->model_catalog_super_product->getProductCategories($result['product_id']);
            foreach ($categories as $category_id) {
                $category_info = $this->model_catalog_category->getCategory($category_id);

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

            $manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($result['manufacturer_id']);


            $product_optionsA = array();

            $product_options = $this->model_catalog_super_product->getProductOptions($result['product_id']);
            foreach ($product_options as $product_option) {
                if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
                    $product_option_value_data = array();

                    foreach ($product_option['product_option_value'] as $product_option_value) {
                        $product_option_value_data[] = array(
                            'product_option_value_id' => $product_option_value['product_option_value_id'],
                            'option_value_id' => $product_option_value['option_value_id'],
                            'quantity' => $product_option_value['quantity']
                        );
                    }

                    $product_optionsA[] = array(
                        'product_option_id' => $product_option['product_option_id'],
                        'product_option_value' => $product_option_value_data,
                        'option_id' => $product_option['option_id'],
                        'type' => $product_option['type'],
                        'name' => $product_option['name']
                    );
                } else {
                    $product_optionsA[] = array(
                        'product_option_id' => $product_option['product_option_id'],
                        'option_id' => $product_option['option_id'],
                        'name' => $product_option['name'],
                        'type' => $product_option['type'],
                        'option_value' => $product_option['option_value']
                    );
                }
            }

            $option_values = array();

            foreach ($product_optionsA as $product_option) {
                if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
                    if (!isset($option_values[$product_option['option_id']])) {
                        $option_values[$product_option['option_id']] = $this->model_catalog_option->getOptionValues($product_option['option_id']);
                    }
                }
            }

            $other_infoArray = array();
            foreach ($other_info as $other_infoRow) {
                if (isset($result[$other_infoRow]) && !empty($result[$other_infoRow])) {
                    $other_infoArray[$other_infoRow] = $result[$other_infoRow];
                }
            }

            $this->data['products'][] = array(
                'product_id' => $result['product_id'],
                'name' => $result['name'],
                'model' => $result['model'],
                'other_info' => $other_infoArray,
                'price' => $result['price'],
                'categories' => $product_categories,
                'special' => $special,
                'product_options' => $product_optionsA,
                'option_values' => $option_values,
                'manufacturer' => $manufacturer_info,
                'image' => $image,
                'quantity' => $result['quantity'],
                'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
                'selected' => isset($this->request->post['selected']) && in_array($result['product_id'], $this->request->post['selected']),
                'action' => $action
            );
        }

        //pre($this->data['products'][0]);

        $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['entry_sku'] = $this->language->get('entry_sku');
        $this->data['entry_upc'] = $this->language->get('entry_upc');
        $this->data['entry_ean'] = $this->language->get('entry_ean');
        $this->data['entry_jan'] = $this->language->get('entry_jan');
        $this->data['entry_isbn'] = $this->language->get('entry_isbn');
        $this->data['entry_mpn'] = $this->language->get('entry_mpn');
        $this->data['entry_location'] = $this->language->get('entry_location');
        $this->data['entry_product_id'] = $this->language->get('entry_product_id');

        $this->data['column_image'] = $this->language->get('column_image');
        $this->data['column_name'] = $this->language->get('column_name');
        $this->data['column_model'] = $this->language->get('column_model');
        $this->data['column_price'] = $this->language->get('column_price');
        $this->data['column_quantity'] = $this->language->get('column_quantity');
        $this->data['column_status'] = $this->language->get('column_status');
        $this->data['column_action'] = $this->language->get('column_action');

        $this->data['button_copy'] = $this->language->get('button_copy');
        $this->data['button_insert'] = $this->language->get('button_insert');
        $this->data['button_delete'] = $this->language->get('button_delete');
        $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=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
        }

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

        if (isset($this->request->get['filter_manufacturer_id'])) {
            $url .= '&filter_manufacturer_id=' . $this->request->get['filter_manufacturer_id'];
        }
        if (isset($this->request->get['filter_model'])) {
            $url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
        }

        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('catalog/super_product', 'token=' . $this->session->data['token'] . '&sort=pd.name' . $url, 'SSL');
        $this->data['sort_model'] = $this->url->link('catalog/super_product', 'token=' . $this->session->data['token'] . '&sort=p.model' . $url, 'SSL');
        $this->data['sort_price'] = $this->url->link('catalog/super_product', 'token=' . $this->session->data['token'] . '&sort=p.price' . $url, 'SSL');
        $this->data['sort_quantity'] = $this->url->link('catalog/super_product', 'token=' . $this->session->data['token'] . '&sort=p.quantity' . $url, 'SSL');
        $this->data['sort_status'] = $this->url->link('catalog/super_product', 'token=' . $this->session->data['token'] . '&sort=p.status' . $url, 'SSL');
        $this->data['sort_order'] = $this->url->link('catalog/super_product', 'token=' . $this->session->data['token'] . '&sort=p.sort_order' . $url, 'SSL');

        $url = '';

        if (isset($this->request->get['filter_name'])) {
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_model'])) {
            $url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
        }
        if (isset($this->request->get['filter_category_id'])) {
            $url .= '&filter_category_id=' . $this->request->get['filter_category_id'];
        }

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

        if (isset($this->request->get['filter_other'])) {
            $url .= '&filter_other=' . $this->request->get['filter_other'];
        }
        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 (isset($this->request->get['filter_manufacturer_id'])) {
            $url .= '&filter_manufacturer_id=' . $this->request->get['filter_manufacturer_id'];
        }
        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('catalog/super_product', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

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

        $this->data['filter_category_id'] = $filter_category_id;
        $this->data['filter_manufacturer_id'] = $filter_manufacturer_id;
        $this->data['filter_other'] = $filter_other;
        $this->data['filter_name'] = $filter_name;
        $this->data['filter_model'] = $filter_model;
        $this->data['filter_price'] = $filter_price;
        $this->data['filter_quantity'] = $filter_quantity;
        $this->data['filter_status'] = $filter_status;

        $this->data['sort'] = $sort;
        $this->data['order'] = $order;

        $this->data['all_categories'] = $this->model_catalog_category->getCategories(0);
        $this->data['all_manufacturer'] = $this->model_catalog_manufacturer->getManufacturers(0);

        $this->template = 'catalog/super_product_list.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    public function ajax_edit() {
        if ($this->request->post['product_id'] && $this->request->post['action'] && isset($this->request->post['value'])) {
            $product_id = $this->request->post['product_id'];
            $action = $this->request->post['action'];
            $value = $this->request->post['value'];

            if ($action == 'quantityExt' && isset($this->request->post['option_id'])) {
              $option_id = $this->request->post['option_id'];
             
              $sql = "UPDATE " . DB_PREFIX . "product_option_value SET quantity = '".(int) $value. "' WHERE product_id = '$product_id' and option_value_id='".(int) $option_id. "' ";
                  //  echo '1';
             
              $query = $this->db->query($sql);
                   
                    echo '1';
                    $this->cache->delete('product');
            } else {
                $catT = false;
                switch ($action) {
                    case 'name':
                        $sql = "SELECT language_id FROM " . DB_PREFIX . "language l LEFT JOIN " . DB_PREFIX . "setting s ON(l.code = s.value) WHERE s.key = 'config_admin_language'";
                        $query = $this->db->query($sql);
                        $language_id = $query->row['language_id'];
                        $sql = "UPDATE " . DB_PREFIX . "product_description SET name = '" . $this->db->escape($value) . "' WHERE product_id = $product_id AND language_id = $language_id";
                        break;
                    case 'model':
                        $sql = "UPDATE " . DB_PREFIX . "product SET model = '$value' WHERE product_id = $product_id";
                        break;
                    case 'quantity':
                        $sql = "UPDATE " . DB_PREFIX . "product SET quantity = $value WHERE product_id = $product_id";
                        break;
                    case 'manufacturer':
                        $sql = "UPDATE " . DB_PREFIX . "product SET manufacturer_id = '$value' WHERE product_id = $product_id";
                        break;
                    case 'category':

                        $this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int) $product_id . "'");
                        if ($value != '') {
                            $data['product_category'] = explode(',', $value);
                            foreach ($data['product_category'] as $category_id) {
                                $this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int) $product_id . "', category_id = '" . (int) $category_id . "'");
                            }
                        }
                        $catT = true;
                        $sql = '';
                        break;
                    case 'price':
                        $sql = "UPDATE " . DB_PREFIX . "product SET price = $value WHERE product_id = $product_id";
                        break;
                    case 'image':
                        $sql = "UPDATE " . DB_PREFIX . "product SET image = '$value' WHERE product_id = $product_id";
                        break;
                    case 'special':
                        $sql = "UPDATE " . DB_PREFIX . "product_special SET price = $value WHERE product_id = $product_id ORDER BY priority, price LIMIT 1";
                        break;
                    case 'status':
                        $sql = "UPDATE " . DB_PREFIX . "product SET status = $value WHERE product_id = $product_id";
                        break;
                    default:
                        $other_info = array('sku', 'upc', 'ean', 'jan', 'isbn', 'mpn', 'location');
                        if (in_array($action, $other_info)) {
                            $sql = "UPDATE " . DB_PREFIX . "product SET " . $action . " = '$value' WHERE product_id = $product_id";
                        } else {
                            $sql = '';
                        }
                        break;
                }
                if ($sql != '' || $catT) {
                    if (!$catT) {
                        $query = $this->db->query($sql);
                    }
                    echo '1';
                    $this->cache->delete('product');
                } else {
                    echo $product_id . ' ' . $action . ' ' . $value;
                }
            }
        }
    }

    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('catalog/super_product');
            $this->load->model('catalog/option');

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

            if (isset($this->request->get['filter_model'])) {
                $filter_model = $this->request->get['filter_model'];
            } else {
                $filter_model = '';
            }

            if (isset($this->request->get['limit'])) {
                $limit = $this->request->get['limit'];
            } else {
                $limit = 20;
            }

            $data = array(
                'filter_name' => $filter_name,
                'filter_BT_model' => $filter_name,
                'filter_model' => $filter_model,
                'start' => 0,
                'limit' => $limit
            );

            $results = $this->model_catalog_super_product->getProducts($data);

            foreach ($results as $result) {
                $option_data = array();

                $product_options = $this->model_catalog_super_product->getProductOptions($result['product_id']);

                foreach ($product_options as $product_option) {
                    $option_info = $this->model_catalog_option->getOption($product_option['option_id']);

                    if ($option_info) {
                        if ($option_info['type'] == 'select' || $option_info['type'] == 'radio' || $option_info['type'] == 'checkbox' || $option_info['type'] == 'image') {
                            $option_value_data = array();

                            foreach ($product_option['product_option_value'] as $product_option_value) {
                                $option_value_info = $this->model_catalog_option->getOptionValue($product_option_value['option_value_id']);

                                if ($option_value_info) {
                                    $option_value_data[] = array(
                                        'product_option_value_id' => $product_option_value['product_option_value_id'],
                                        'option_value_id' => $product_option_value['option_value_id'],
                                        'name' => $option_value_info['name'],
                                        'price' => (float) $product_option_value['price'] ? $this->currency->format($product_option_value['price'], $this->config->get('config_currency')) : false,
                                        'price_prefix' => $product_option_value['price_prefix']
                                    );
                                }
                            }

                            $option_data[] = array(
                                'product_option_id' => $product_option['product_option_id'],
                                'option_id' => $product_option['option_id'],
                                'name' => $option_info['name'],
                                'type' => $option_info['type'],
                                'option_value' => $option_value_data,
                                'required' => $product_option['required']
                            );
                        } else {
                            $option_data[] = array(
                                'product_option_id' => $product_option['product_option_id'],
                                'option_id' => $product_option['option_id'],
                                'name' => $option_info['name'],
                                'type' => $option_info['type'],
                                'option_value' => $product_option['option_value'],
                                'required' => $product_option['required']
                            );
                        }
                    }
                }

                $this->load->model('tool/image');
                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);
                }

                $json[] = array(
                    'product_id' => $result['product_id'],
                    'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),
                    'model' => $result['model'],
                    'option' => $option_data,
                    'image' => $image,
                    'price' => $result['price']
                );
            }
        }

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

}

?>