<?php

//modules/sitepanel/cotrollers/newproducts.php
Class NewProducts extends Admin_Controller {

  public function __construct() {
    parent::__construct();
    //$this->load->helper('products_helper');
    $this->load->model(array('products/product_model', 'size/size_model', 'color/color_model'));
    $this->load->helper('category/category');
    //$this->load->library('excel');
  }

  public function index() {
    return $this->load->view('dashboard/new_product');
  }

  public function get() {
    $this->upload();
    $file = $this->input->post('file');
  }

  public function upload() {
    if (true) {
      $this->form_validation->set_rules('excel_file', 'Upload Excel File', 'required|callback_check_upload_excel');
      if ($this->form_validation->run() == TRUE) {
        require_once FCPATH . 'apps/third_party/Excel/reader.php';
        $data = new Spreadsheet_Excel_Reader();
        $data->setOutputEncoding('CP1251');
        //$data->setUTFEncoder('');
        chmod($_FILES["excel_file"]["tmp_name"], 0777);
        $data->read($_FILES["excel_file"]["tmp_name"]);
        $worksheet = $data->sheets[0]['cells'];



        for ($i = 2; $i <= count($worksheet); $i++) {
          //get the product id from sheet
          $product_id = (!isset($worksheet[$i][1])) ? '' : addslashes(trim($worksheet[$i][1]));
          $cat_id = (!isset($worksheet[$i][2])) ? '' : addslashes(trim($worksheet[$i][2]));
          $new_cat_name = (!isset($worksheet[$i][3])) ? '' : addslashes(trim($worksheet[$i][3]));
          $customer_id = (!isset($worksheet[$i][4])) ? '' : addslashes(trim($worksheet[$i][4]));
          $prod_name = (!isset($worksheet[$i][5])) ? '' : addslashes(trim($worksheet[$i][5]));
          $url = (!isset($worksheet[$i][7])) ? '' : addslashes(trim($worksheet[$i][7]));
          $details = (!isset($worksheet[$i][8])) ? '' : addslashes(trim($worksheet[$i][8]));

          //Delete the entries
          if ($new_cat_name == "Delete") {
            //Delete From Products
            $this->db->where('products_id', $product_id);
            $this->db->delete('wps_products');
            //Delete from media
            $this->db->where('products_id', $product_id);
            $this->db->delete('wps_products_media');
            //Delete from meta_tags
            $this->db->where('entity_id', $product_id);
            $this->db->where('entity_type', 'products/detail');
            $this->db->delete('wps_meta_tags');

            $worksheet[$i]['update_status'] = 0;
          } else {
            //Add the entries to products table;
            error_reporting(E_ALL);
            $this->db->where('category_name', $new_cat_name);
            $result = $this->db->get('wps_categories')->row();
            if ($result != null) {
              $cat_id = $result->category_id;
              //trace($cat_id);
              if ($cat_id != null) {
                //trace($cat_id);
                $postCategory = $cat_id;
                $catlink = get_parent_categories($postCategory, "AND status='1'", "category_id,parent_id");
                $ctl = array_keys($catlink);
                $category_links = implode(",", $ctl) . ',';
                $category_links = substr($category_links, 0, -1);

                /* echo $category_links;
                  echo "<br>";
                  exit; */
              }
              //Apply Changes
              $posted_data = array(
                  'category_id' => $cat_id,
                  'category_links' => $category_links,
              );
              trace($posted_data);              
              $where = "products_id = '" . $product_id . "'";
              $this->product_model->safe_update('wps_products', $posted_data, $where, FALSE);
              echo_sql();
              
              //$this->db->replace('wps_products', $data);
              $worksheet[$i]['update_status'] = 1;
            }
          }
        }
        die;
      }
      $siteData['worksheet'] = $worksheet;
      return $this->load->view('dashboard/new_product', $siteData);
    }
  }

  /* public function uploadConfig(){

    $config['upload_path'] = './csv_uploads/';
    $config['allowed_types'] = 'xlsx|csv|xls';
    $config['max_size'] = '10000';
    $config['overwrite'] = true;
    $config['encrypt_name'] = FALSE;
    $config['remove_spaces'] = TRUE;

    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    if (!is_dir('csv_uploads'))
    {
    mkdir('./csv_uploads', 0777, true);
    }
    // gif|jpg|jpeg|png
    } */
}
