function saveCsvData($type = 0, $contain_header = 0, $start_index = 0) { if ($this->input->post('cols_count')) { $dataArray = array(); $dataIndexes = array(); $dataValues = array(); $data_save = array(); $json_decoded_array = array(); $data_row_error = array(); $headerArray = array(); $data_csv_save = ""; $cols_count = $this->input->post('cols_count'); $rows_count = $this->input->post('rows_count'); $hasHeader = $this->input->post('hasHeader'); $rowStart = $hasHeader ? 1 : 0; $json_decoded_array = json_decode($this->input->post('json_array')); $json_decoded_array = $json_decoded_array->myArray; // Prep csv data $this->data->headerColumns = array(); for ($i = $rowStart; $i < $rows_count; $i++) { for ($j = 0; $j < $cols_count; $j++) { $dataArray[$i][$j] = $json_decoded_array[$i][$j]; } if (count($json_decoded_array[$i]) > count($this->data->headerColumns)) { $this->data->headerColumns = array_fill(0, count($json_decoded_array[$i]), ''); } } for ($i = 0; $i < $cols_count; $i++) { $name = 'hid_' . $i; if (isset($_POST[$name])) { $dataIndexes[] = $i; $dataValues[] = $_POST[$name]; } if ($hasHeader) { $name = 'hid_' . $i; $post_index = "header_0_" . $i; if (isset($_POST[$post_index])) { $headerArray[0][$i] = $_POST[$post_index]; } if (isset($_POST[$name])) { $headerArray[1][$i] = $_POST[$name]; } } else { if (isset($_POST[$name])) { $headerArray[1][$i] = $_POST[$name]; $headerArray[0][$i] = $_POST[$name]; } else { $headerArray[1][$i] = ''; $headerArray[0][$i] = ''; } } } for ($i = 0, $n = count($headerArray[0]); $i < $n; $i++) { $val = ""; if (isset($headerArray[1][$i])) { $val = $headerArray[1][$i]; } else { $val = ""; } if ($i == $n - 1) { $data_csv_save .= $i . "=>" . $val . "~" . preg_replace('/[=>%~&]/', '', $headerArray[0][$i]); } else { $data_csv_save .= $i . "=>" . $val . "~" . preg_replace('/[=>%~&]/', '', $headerArray[0][$i]) . "&"; } } for ($j = $rowStart; $j < $rows_count; $j++) { for ($i = 0, $m = count($dataIndexes); $i < $m; $i++) { $data_save[$j]['store_id'] = $this->store_id; if ($dataValues[$i] == 'upc_code') { if (is_numeric(preg_replace('/[^0-9]/', '', $dataArray[$j][$dataIndexes[$i]]))) { $data_save[$j][$dataValues[$i]] = htmlentities(round(preg_replace('/[^0-9]/', '', $dataArray[$j][$dataIndexes[$i]]))); } else { if (is_numeric(preg_replace('/[^0-9]/', '', $dataArray[$j][$dataIndexes[$i]]))) { $data_save[$dataValues[$i]] = htmlentities(round(preg_replace('/[^0-9]/', '', $dataArray[$j][$dataIndexes[$i]]))); $data_save[$j]['is_tracked'] = 1; $data_save[$j]['status'] = 1; } else { $this->data->error = 'Please select correct UPC.'; $data_row_error[] = $j; $data_save[$j]['is_tracked'] = 0; $data_save[$j]['status'] = 0; } } } elseif ($dataValues[$i] == 'price_floor' || $dataValues[$i] == 'retail_price' || $dataValues[$i] == 'wholesale_price') { $priceWithoutDollar = trim(str_replace('$', '', $dataArray[$j][$dataIndexes[$i]])); if (!is_numeric($priceWithoutDollar)) { $priceWithoutDollar = ''; } if ($priceWithoutDollar == '' || is_numeric($priceWithoutDollar)) { if ($priceWithoutDollar == '') { $priceWithoutDollar = 0.0; } $data_save[$j][$dataValues[$i]] = round($priceWithoutDollar, 2); } else { $this->data->error = 'Please select correct numeric value for prices.'; $data_row_error[] = $j; $data_save[$j]['is_tracked'] = 0; $data_save[$j]['status'] = 0; } } else { $data_save[$j][$dataValues[$i]] = htmlentities($dataArray[$j][$dataIndexes[$i]]); } } } // start saving products $total_saved = $total_not_saved = 0; $errors = array(); $brand = $this->Store->get_brand_by_store($this->store_id); if (isset($headerArray[1]) && in_array('upc_code', $headerArray[1])) { for ($i = $rowStart; $i < $rows_count; $i++) { if (isset($data_save[$i]['upc_code'])) { $data_save[$i]['brand'] = $brand; $data_save[$i]['created_at'] = date('Y-m-d H:i:s'); if ($this->Items->product_exists_by_upc($data_save[$i]['upc_code'])) { $product_id = $this->Items->get_product_id_from_upc($data_save[$i]['upc_code']); if (!is_null($product_id)) { $newValues = array(); if (isset($data_save[$i]['title']) and $data_save[$i]['title'] != '') { $newValues['title'] = $data_save[$i]['title']; } if (isset($data_save[$i]['sku']) and $data_save[$i]['sku'] != '') { $newValues['sku'] = $data_save[$i]['sku']; } if (sizeof($newValues) > 0) { $this->Items->update($product_id, $newValues); } $pricing_start = date('Y-m-d h:i:s'); foreach (array('wholesale_price', 'retail_price', 'price_floor') as $pricing_type) { if (!isset($data_save[$i][$pricing_type]) && $data_save[$i][$pricing_type] == '' && !is_numeric($data_save[$i][$pricing_type])) { continue; } $this->db->insert('products_pricing', array('product_id' => $product_id, 'pricing_type' => $pricing_type, 'pricing_value' => $data_save[$i][$pricing_type], 'pricing_start' => $pricing_start)); } $total_saved++; log_message('success', 'Product updated failed: ' . print_r($data_save[$i], true)); } else { $total_not_saved++; $errors[] = $data_save[$i]['upc_code'] . ' is already in our database but was unable to be updated.'; log_message('error', $data_save[$i]['upc_code'] . ' is already in our database but was unable to be updated.'); } } elseif ($this->db->insert('products', $data_save[$i])) { $product_id = mysql_insert_id(); $pricing_start = date('Y-m-d h:i:s'); foreach (array('wholesale_price', 'retail_price', 'price_floor') as $pricing_type) { $this->db->insert('products_pricing', array('product_id' => $product_id, 'pricing_type' => $pricing_type, 'pricing_value' => $data_save[$i][$pricing_type], 'pricing_start' => $pricing_start)); } $total_saved++; } else { log_message('error', 'Product insert failed: ' . print_r($data_save[$i], true)); } } } if (count($errors)) { $this->session->set_flashdata('error', implode("\n<br>", $errors)); } } else { if ($hasHeader) { $dataArray[0] = array_fill(0, count($this->data->headerColumns), ''); for ($j = 0; $j < $cols_count; $j++) { $post_name = "header_0_" . $j; $dataArray[0][$j] = $this->input->post($post_name); } $this->data->headerColumns = $dataArray[0]; } elseif (isset($headerArray[1])) { $this->data->headerColumns = $headerArray[1]; } $this->data->dataArray = $dataArray; $headerArray = generateHeaderPostArray($data_csv_save); $this->data->headerArray = $headerArray; $this->data->filename = 'filename'; $this->data->hasHeader = $hasHeader ? 1 : 0; $this->data->error = 'Please select UPC column to import data.'; $this->load->view('front/merchant/import', $this->data); } $this->saveDefaultCSVSettings($data_csv_save); $this->session->set_flashdata("products_saved", $total_saved); $this->session->set_flashdata("products_not_saved", $total_not_saved); if ($total_saved > 0) { $this->session->set_flashdata("success", 'File import success.'); } elseif ($total_not_saved === 0) { $this->session->set_flashdata("error", 'File import failed.'); } } redirect(base_url() . 'catalog/'); }
/** * Page where users can add new products. * * @author unknown, Christophe */ public function products() { if (!$this->store_id) { $this->session->set_flashdata('error_msg', 'Error: Store not selected.'); redirect('/'); } if ($this->role_id != 2) { $this->session->set_flashdata('error_msg', 'Error: Your account does not have access to this item.'); redirect('/'); exit; } $this->javascript('views/add_products.js.php'); $this->data->error = ''; $this->data->product_count = 1; $this->data->product_method = $this->input->post('product_method') == FALSE ? 'bycsv' : $this->input->post('product_method'); $this->data->file_processing = false; //var_dump($this->data->product_method); exit(); $this->data->title = $this->input->post('title'); $this->data->upc_code = $this->input->post('upc_code'); $this->data->sku = $this->input->post('sku'); $this->data->retail_price = $this->input->post('retail_price'); $this->data->wholesale_price = $this->input->post('wholesale_price'); $this->data->price_floor = $this->input->post('price_floor'); if ($this->input->post('product_method') == 'byform') { // this will be an array of x products... $this->data->product_count = sizeof($this->data->title); $rules = array(array('field' => 'title[]', 'label' => 'Title', 'rules' => 'trim|required'), array('field' => 'upc_code[]', 'label' => 'UPC Code', 'rules' => 'trim|required'), array('field' => 'retail_price[]', 'label' => 'Retail Price', 'rules' => 'trim|required'), array('field' => 'price_floor[]', 'label' => 'Price Floor', 'rules' => 'trim|required')); $this->form_validation->set_rules($rules); $this->form_validation->set_fields(); if ($this->form_validation->run()) { for ($i = 0; $i < $this->data->product_count; $i++) { $existing_product_id = $this->Product->check_product_exist($this->store_id, $this->data->upc_code[$i], $this->data->sku[$i]); if (!$existing_product_id) { $data = array('store_id' => $this->store_id, 'title' => $this->data->title[$i], 'upc_code' => $this->data->upc_code[$i], 'brand' => '', 'sku' => $this->data->sku[$i], 'retail_price' => $this->data->retail_price[$i], 'price_floor' => $this->data->price_floor[$i], 'is_tracked' => 1, 'created_at' => date('Y-m-d H:i:s'), 'wholesale_price' => $this->data->wholesale_price[$i], 'status' => 1); $newProdId = $this->Product->add_product($data); $iT = time(); if (isset($data['retail_price'])) { $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'retail_price', 'pricing_value' => $data['retail_price'], 'pricing_start' => date('Y-m-d H:i:s', $iT))); } if (isset($data['wholesale_price'])) { $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'wholesale_price', 'pricing_value' => $data['wholesale_price'], 'pricing_start' => date('Y-m-d H:i:s', $iT))); } if (isset($data['price_floor'])) { $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'price_floor', 'pricing_value' => $data['price_floor'], 'pricing_start' => date('Y-m-d H:i:s', $iT))); } } else { $data = array('title' => $this->data->title[$i], 'upc_code' => $this->data->upc_code[$i], 'brand' => '', 'sku' => $this->data->sku[$i], 'retail_price' => $this->data->retail_price[$i], 'price_floor' => $this->data->price_floor[$i], 'is_tracked' => 1, 'wholesale_price' => $this->data->wholesale_price[$i], 'created_at' => date('Y-m-d H:i:s'), 'status' => 1); $this->Product->update_product($existing_product_id, $data); $newProdId = $existing_product_id; $iT = time(); if (isset($data['retail_price'])) { $this->db->where('product_id', $newProdId); $this->db->where('pricing_type', 'retail_price'); $this->db->update($this->_table_products_pricing, array('pricing_value' => $data['retail_price'], 'pricing_start' => date('Y-m-d H:i:s', $iT))); } if (isset($data['wholesale_price'])) { $this->db->where('product_id', $newProdId); $this->db->where('pricing_type', 'wholesale_price'); $this->db->update($this->_table_products_pricing, array('pricing_value' => $data['wholesale_price'], 'pricing_start' => date('Y-m-d H:i:s', $iT))); } if (isset($data['price_floor'])) { $this->db->where('product_id', $newProdId); $this->db->where('pricing_type', 'price_floor'); $this->db->update($this->_table_products_pricing, array('pricing_value' => $data['price_floor'], 'pricing_start' => date('Y-m-d H:i:s', $iT))); } } } redirect(base_url() . 'catalog'); } else { //we're going back to the form to fix error... $this->data->error = 'There is an error with one or more of your products. The title, UPC, Retail & MAP price values are required.'; } } elseif ($this->input->post('product_method') == 'bycsv') { if ($_FILES['csv_file']['tmp_name']) { $config['upload_path'] = $this->config->item('uploaded_files') . 'brand_csv_uploads/' . $this->store_id; $brand_file = $this->config->item('uploaded_files') . 'brand_csv_uploads'; $config['allowed_types'] = 'csv'; $config['overwrite'] = true; $config['remove_spaces'] = true; //$config['file_name'] = $_FILES['csv_file']['name']; //if(!is_dir($brand_file)) mkdir($brand_file, 0777); if (!is_dir($config['upload_path'])) { mkdir($config['upload_path'], 0777, true); chmod($config['upload_path'], 0777); } $this->load->library('upload', $config); if ($this->upload->do_upload('csv_file')) { //we've posted a file ini_set("auto_detect_line_endings", "1"); $this->data->file_name = $this->upload->file_name; //$config['file_name']; $this->data->file_processing = true; $dataArray = $headerArray = array(); $has_header = $this->input->post("has_header"); // Does user want to archive all existing products in system that are NOT contained in this CSV file? $archive_products = $this->input->post("archive_products"); $this->data->has_header = $has_header; $this->data->archive_products = $archive_products; $this->data->headerColumns = array(); $handle = fopen($config['upload_path'] . '/' . $this->data->file_name, "r"); while (($data = fgetcsv($handle, 9999999, ",")) !== FALSE) { $dataArray[] = $data; if (count($data) > count($this->data->headerColumns)) { $this->data->headerColumns = $data; } } fclose($handle); if ($this->data->has_header) { if (isset($dataArray[0])) { if (count($dataArray[0]) < count($this->data->headerColumns)) { $missing_indexes = count($this->data->headerColumns) - count($dataArray[0]); $this->data->headerColumns = array_merge($dataArray[0], array_fill(count($dataArray[0]), $missing_indexes, '')); } else { $this->data->headerColumns = $dataArray[0]; } } $var_to_match = ''; for ($i = 0, $n = count($this->data->headerColumns); $i < $n; $i++) { if ($n - 1 == $i) { $var_to_match .= $i . '=>%~' . preg_replace('/[=>%~&]/', '', $this->data->headerColumns[$i]); } else { $var_to_match .= $i . '=>%~' . preg_replace('/[=>%~&]/', '', $this->data->headerColumns[$i]) . '&'; } } $headerArray = generateHeaderArray($var_to_match); if ($headerArray != '') { $headerArray = generateHeaderPostArray($headerArray); } else { $headerArray = generateHeaderPostArray($var_to_match); } } $this->data->dataArray = $dataArray; $this->data->headerArray = $headerArray; $this->javascript('views/import.js.php'); } else { $this->data->error = $this->upload->display_errors(); } } else { $this->data->error = 'No file was uploaded.'; } } }
function products($store_id = null) { $this->data->store_id = $this->store_id; $this->data->dataArray = array(); $this->error = ''; $brand = $this->Store->get_brand_by_store($this->store_id); if ($this->input->post('rdProduct') === 'byform') { $title = $this->input->post('title'); $upc_code = $this->input->post('upc_code'); $sku = $this->input->post('sku'); $retail_price = $this->input->post('retail_price'); $price_floor = $this->input->post('price_floor'); $wholesale_price = $this->input->post('wholesale_price'); $added_item = FALSE; $errors = array(); for ($i = 0, $n = count($sku); $i < $n; $i++) { if (!$this->Product->product_exists_by_upc($upc_code[$i])) { // product not in system, add it $iT = time(); $data = array('store_id' => $this->store_id, 'title' => $title[$i], 'upc_code' => $upc_code[$i], 'brand' => $brand, 'sku' => $sku[$i], 'wholesale_price' => $wholesale_price[$i], 'retail_price' => $retail_price[$i], 'price_floor' => $price_floor[$i], 'created_at' => date('Y-m-d H:i:s', $iT)); $newProdId = $this->Product->add_product($data); $added_item = ($added_item or $newProdId); $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'retail_price', 'pricing_value' => $retail_price[$i], 'pricing_start' => date('Y-m-d H:i:s', $iT))); $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'wholesale_price', 'pricing_value' => $wholesale_price[$i], 'pricing_start' => date('Y-m-d H:i:s', $iT))); $this->db->insert($this->_table_products_pricing, array('product_id' => $newProdId, 'pricing_type' => 'price_floor', 'pricing_value' => $price_floor[$i], 'pricing_start' => date('Y-m-d H:i:s', $iT))); } else { // product in system, generate notice $errors[] = $upc_code[$i] . ' is already in our database.'; } } if ($added_item) { $this->session->set_flashdata("success", 'Product(s) successfully added.'); } if (count($errors)) { $this->session->set_flashdata('error', implode("\n<br>", $errors)); } redirect(base_url() . 'catalog/catalog_add/' . $this->store_id); } elseif ($this->input->post('rdProduct') === 'bycsv') { array_push($this->javascript_files, 'views/import.php'); $headerArray = array(); if ($_FILES['file']['tmp_name']) { $ext = explode('.', $_FILES['file']['name']); $ext = $ext[count($ext) - 1]; if ($ext === 'csv') { ini_set("auto_detect_line_endings", "1"); $theFileSize = $_FILES['file']['size']; if ($theFileSize < 6 * 1024 * 1024) { $path = $this->config->item('csv_upload_path'); move_uploaded_file($_FILES['file']['tmp_name'], $path . $_FILES['file']['name']); $extArray = explode('.', $_FILES['file']['tmp_name']); $ext = $extArray[count($extArray) - 1]; if (strtolower($ext) != 'csv') { $this->error = false; } $handle = fopen($path . $_FILES['file']['name'], "r"); $check = 0; $dataArray = array(); $this->data->filename = $_FILES['file']['name']; $this->data->hasHeader = 0; $this->data->headerColumns = array(); $hasHeader = $this->input->post("hasHeader"); $this->data->hasHeader = $hasHeader; while (($data = fgetcsv($handle, 9999999, ",")) !== FALSE) { $dataArray[] = $data; if (count($data) > count($this->data->headerColumns)) { $this->data->headerColumns = $data; } } fclose($handle); if ($hasHeader) { if (isset($dataArray[0])) { if (count($dataArray[0]) < count($this->data->headerColumns)) { $missing_indexes = count($this->data->headerColumns) - count($dataArray[0]); $this->data->headerColumns = array_merge($dataArray[0], array_fill(count($dataArray[0]), $missing_indexes, '')); } else { $this->data->headerColumns = $dataArray[0]; } } $var_to_match = ''; for ($i = 0, $n = count($this->data->headerColumns); $i < $n; $i++) { if ($n - 1 == $i) { $var_to_match .= $i . '=>%~' . preg_replace('/[=>%~&]/', '', $this->data->headerColumns[$i]); } else { $var_to_match .= $i . '=>%~' . preg_replace('/[=>%~&]/', '', $this->data->headerColumns[$i]) . '&'; } } $headerArray = generateHeaderArray($var_to_match); if ($headerArray != '') { $headerArray = generateHeaderPostArray($headerArray); } else { $headerArray = generateHeaderPostArray($var_to_match); } } $this->data->dataArray = $dataArray; $this->data->headerArray = $headerArray; $this->load->view('front/merchant/import', $this->data); } else { $this->session->set_flashdata('error', 'Please upload file size must be less than 6MB.'); redirect(base_url() . 'catalog/products/'); } } else { $this->session->set_flashdata('error', 'Please provide your catalog in csv format.'); redirect(base_url() . 'catalog/products/'); } } else { //echo "last else";exit; $this->error = false; } if ($this->error) { echo 'error'; exit; redirect(base_url() . 'dashboard'); } } else { array_push($this->javascript_files, 'views/import.php'); array_push($this->javascript_files, 'views/add_products.php'); $this->load->view('front/merchant/add_products', $this->data); } }