|
Server : Apache/2.4.18 (Ubuntu) System : Linux canvaswebdesign 3.13.0-71-generic #114-Ubuntu SMP Tue Dec 1 02:34:22 UTC 2015 x86_64 User : oppastar ( 1041) PHP Version : 7.0.33-0ubuntu0.16.04.15 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, Directory : /var/www/angkasapuraretail.com/public_html/application/controllers/admin/ |
Upload File : |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Products extends Admin_Controller {
//this property is used for validating existing category title on call back edit category
private $product_current_id;
private $image1_filename = NULL;
private $image2_filename = NULL;
private $image3_filename = NULL;
private $image4_filename = NULL;
private $image5_filename = NULL;
function __construct() {
parent::__construct();
$this->load->model('product_m');
$this->load->model('category_m');
$this->load->model('configuration_m');
$this->load->model('brand_m');
$this->load->model('size_m');
$this->load->library('image_lib');
$this->load->helper('form');
if (!in_array('products', $this->data['allowed_module'])) {
$this->data['allowed'] = false;
} else {
$this->data['allowed'] = true;
}
}
//this is to list all products
public function index() {
/*----FILTER SEARCH PRODUCT--*/
if(isset($_POST['search_product'])) {
//get product name from form
$this->data['keyword'] = $this->security->xss_clean($this->input->post('product'));
$this->data['products'] = $this->product_m->get_all_products_search_product($this->data['keyword']);
} else {
//pagination in action. 100 results per page
$this->load->library('pagination');
$config = array();
$this->load->helper('pagination_helper');
$config = pagination_format(); //function from helper file
$config['base_url'] = base_url() . 'admin/products/index/';
$config['per_page'] = 100;
$config['uri_segment'] = 4;
$config['total_rows'] = $this->product_m->record_count();
$this->pagination->initialize($config);
$this->data['products'] = $this->product_m->get_all_products($config['per_page'],$this->uri->segment($config['uri_segment']));
$this->data['use_pagination'] = 'yes';
}
//get website product ordering
$this->db->select('website_product_ordering')->from('configuration')->where('id_configuration', 1);
$this->data['website_product_ordering'] = $this->db->get()->row()->website_product_ordering;
//load view
$this->data['subview'] = 'admin/products/index';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
}
//to ADD a new product
public function add() {
if($this->data['allowed'] == false) { redirect('admin/dashboard'); }
$this->data['new_product'] = 'yes';
/*get product data*/
$this->data['products'] = $this->product_m->get_new();
$this->data['parent_categories'] = $this->category_m->get_parent_categories();
$this->data['brands'] = $this->brand_m->get_brands_onlinestore();
/*get stock*/
$this->data['stock'] = $this->db->select('*')->from('warehouse')->order_by('id','ASC')->get()->result();
/*get shipment method*/
$this->data['shipment'] = $this->db->select('*')->from('shipment_method')->order_by('id','ASC')->get()->result();
//get ordering number and display at add form
$this->db->select_max('priority')->from('products');
$current_priority = $this->db->get()->row()->priority;
if($current_priority == NULL) {
$this->data['products']->priority = 1;
} else {
$this->data['products']->priority = $current_priority + 1;
}
//validation in action
//validation check in action
$config = $this->product_m->rules;
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); //above is to add class to form validation error, to be styled
$this->form_validation->set_rules($config);
if($this->form_validation->run($this) == TRUE) {
//check if product code already exist
$product_code = $this->input->post('product_code');
$this->db->select('product_code')->from('products')->where('product_code', $product_code);
$count_product_code = $this->db->get()->num_rows();
if($count_product_code > 0) {
//means product code already exist...must exit with error notification
// $this->session->set_flashdata('result', "<br><p style='background:red; color:white; padding:5px; font-weight:bold;'>Product Code already exist.</p>");
// redirect('admin/products/add');
}
//check if product code already exist
$sku = $this->input->post('sku');
$this->db->select('sku')->from('products')->where('sku', $sku);
$count_sku = $this->db->get()->num_rows();
if($count_sku > 0) {
//means product code already exist...must exit with error notification
$this->session->set_flashdata('result', "<br><p style='background:red; color:white; padding:5px; font-weight:bold;'>SKU already exist.</p>");
//$this->add();
redirect('admin/products/add');
}
//Get product image dimensions from configuration table
$this->db->select('
product_image_width,
product_image_height,
product_image_large_width,
product_image_large_height,
product_image_small_width,
product_image_small_height,
product_image_thumbnail_width,
product_image_thumbnail_height
')
->from('configuration')
->where('id_configuration', 1);
$image_dimension = $this->db->get()->row();
for($i = 1; $i <= 5; $i++) {
if ($_FILES['image' . $i]['size'] !== 0) {
//use variables variables
$this->{'image' . $i . '_filename'} = $this->image_processing('image' . $i, $image_dimension->product_image_width, $image_dimension->product_image_height);
}
}
$data = $this->table_data_processing();
$product_id = (int) $this->product_m->add_product($data);
/*INSERT INTO SHIPMENT*/
$shipments_id = $this->input->post('shipment_id');
if (!in_array("3", $shipments_id))
{
$shipments_id[] = '3';
}
$shipments_id_count = count($shipments_id);
if ($shipments_id_count > 0) {
$shipments_keywords = '';
foreach ($shipments_id as $shipment_id) {
$data_shipment = array(
'product_id' => $product_id,
'shipment_method_id' => $shipment_id,
);
$this->db->insert('shipment_method_product', $data_shipment);
$this->db->select('name')->from('shipment_method')->where('id', $shipment_id);
$shipments_keywords .= $this->db->get()->row()->name . ',';
}
$data_shipments = array(
'shipment_method' => $shipments_keywords,
);
$this->db->where('id_products', $product_id);
$this->db->update('products', $data_shipments);
}
else {
$data_shipment = array(
'product_id' => $product_id,
'shipment_method_id'=> NULL,
);
$this->db->insert('shipment_method_product', $data_shipment);
}
/*INSERT INTO CATEGORY*/
//get category_id from view, then insert together with product_id to category_product table
$categories_id = $this->input->post('category_id');
//check id there is content inside category array
$categories_id_count = count($categories_id);
if ($categories_id_count > 0) {
//add to category_product table
$categories_keywords = '';
foreach ($categories_id as $category_id) {
$data = array(
'id_product' => $product_id,
'id_category' => $category_id,
);
$this->db->insert('category_product', $data);
//add to products table, categories column
$this->db->select('category')->from('categories')->where('id_categories', $category_id);
//append all categories keywords to this variable
$categories_keywords .= $this->db->get()->row()->category . ',';
}
$data = array(
'categories' => $categories_keywords,
);
$this->db->where('id_products', $product_id);
$this->db->update('products', $data);
} else {
$data = array(
'id_product' => $product_id,
'id_category' => NULL,
);
$this->db->insert('category_product', $data);
}
/*INSERT INTO CATEGORY*/
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Product Added Successful</p>');
redirect('admin/products');
}
$this->data['subview'] = 'admin/products/edit';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
}
//to EDIT product in admin
public function edit($id) {
$this->data['new_product'] = 'no';
$this->db->select('id_products')->from('products')->where('id_products', $id);
$count_product = $this->db->get()->num_rows();
if($count_product === 0) {show_404();}
$this->data['products'] = $this->product_m->get($id);
$this->data['parent_categories'] = $this->category_m->get_parent_categories();
//get all chosen (active) categories
$this->db->select('*')->from('category_product')->where('id_product', $id);
$this->data['chosen_categories'] = $this->db->get()->result();
//get all brands
$this->data['brands'] = $this->brand_m->get_brands_onlinestore();
//get current brand id
$this->db->select('brand_id')->from('products')->where('id_products', (int) $id);
$products = $this->db->get()->row();
$this->data['brand_id'] = $products->brand_id;
/*get stock*/
$this->data['stock'] = $this->db->select('*')->from('warehouse')->order_by('id','ASC')->get()->result();
/*get shipment method*/
$this->data['shipment'] = $this->db->select('*')->from('shipment_method')->order_by('id','ASC')->get()->result();
/*get all chosen (active) shipment*/
$this->db->select('*')->from('shipment_method_product')->where('product_id', $id);
$this->data['chosen_shipments'] = $this->db->get()->result();
//assign to properties, used for custom callback validation
$this->product_current_id = (int) $this->data['products']->id_products;
//validation check in action
$config = $this->product_m->rules;
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); //above is to add class to form validation error, to be styled
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run($this) == TRUE) {
//check if product code already exist
$product_code = $this->input->post('product_code');
$this->db->select('product_code')->from('products')->where('product_code', $product_code)->where('id_products !=',$id);
$count_product_code = $this->db->get()->num_rows();
if($count_product_code > 0) {
//means product code already exist...must exit with error notification
$this->session->set_flashdata('result', "<br><p style='background:red; color:white; padding:5px; font-weight:bold;'>Product Code already exist.</p>");
redirect('admin/products/edit/'.$id);
}
//check if product code already exist
$sku = $this->input->post('sku');
$this->db->select('sku')->from('products')->where('sku', $sku)->where('id_products !=',$id);
$count_sku = $this->db->get()->num_rows();
if($count_sku > 0) {
//means product code already exist...must exit with error notification
$this->session->set_flashdata('result', "<br><p style='background:red; color:white; padding:5px; font-weight:bold;'>SKU already exist.</p>");
//$this->add();
redirect('admin/products/edit/'.$id);
}
/*UPDATE INTO WAREHOUSE*/
if($this->data['role'] == 'super admin'){
/*delete current stock*/
$this->db->where('id_product', $id);
$this->db->delete('stock');
/*delete current stock*/
$warehouse_id = $this->input->post('warehouse_id');
$warehouse_stock = $this->input->post('warehouse_stock');
for($a=0;$a<count($warehouse_id);$a++){
$new_stock = array(
'id_product' => $id,
'warehouse_id' => $warehouse_id[$a],
'stock' => $warehouse_stock[$a]
);
$this->db->insert('stock', $new_stock);
}
}
/*UPDATE INTO WAREHOUSE*/
//Get product image dimensions from configuration table
$this->db->select('product_image_width, product_image_height, product_image_large_width, product_image_large_height, product_image_small_width, product_image_small_height, product_image_thumbnail_width, product_image_thumbnail_height')->from('configuration')->where('id_configuration', 1);
$image_dimension = $this->db->get()->row();
for($i = 1; $i <= 5; $i++) {
if ($_FILES['image' . $i]['size'] !== 0) {
//use variables variables
$this->{'image' . $i . '_filename'} = $this->image_processing('image' . $i, $image_dimension->product_image_width, $image_dimension->product_image_height);
}
}
$data = $this->table_data_processing();
$this->product_m->edit_product($id, $data);
/*UPDATE INTO SHIPMENT*/
$shipments_id = $this->input->post('shipment_id');
if (!in_array("3", $shipments_id))
{
$shipments_id[] = '3';
}
$shipments_id_count = count($shipments_id);
if ($shipments_id_count > 0) {
//firstly, we delete all existing records inside current table
$this->db->where('product_id', $id);
$this->db->delete('shipment_method_product');
$shipments_keywords = '';
foreach ($shipments_id as $shipment_id) {
$data_shipment = array(
'product_id' => $id,
'shipment_method_id' => $shipment_id,
);
$this->db->insert('shipment_method_product', $data_shipment);
$this->db->select('name')->from('shipment_method')->where('id', $shipment_id);
$shipments_keywords .= $this->db->get()->row()->name . ',';
}
$data_shipments = array(
'shipment_method' => $shipments_keywords,
);
$this->db->where('id_products', $id);
$this->db->update('products', $data_shipments);
}
else {
//firstly, we delete all existing records inside current table
$this->db->where('product_id', $id);
$this->db->delete('shipment_method_product');
$data_shipment = array(
'product_id' => $id,
'shipment_method_id'=> NULL,
);
$this->db->insert('shipment_method_product', $data_shipment);
}
//get category_id from view, then insert together with product_id to category_product table
$categories_id = $this->input->post('category_id');
//check id there is content inside category array
$categories_id_count = count($categories_id);
if ($categories_id_count > 0) {
//firstly, we delete all existing category records inside category_product table
$this->db->where('id_product', $id);
$this->db->delete('category_product');
//secondly, we insert new category_id
$categories_keywords = '';
foreach ($categories_id as $category_id) {
$data = array(
'id_product' => $id,
'id_category' => $category_id,
);
$this->db->insert('category_product', $data);
//add to products table, categories column
$this->db->select('category')->from('categories')->where('id_categories', $category_id);
//append all categories keywords to this variable
$categories_keywords .= $this->db->get()->row()->category . ',';
}
$data = array(
'categories' => $categories_keywords,
);
$this->db->where('id_products', $id);
$this->db->update('products', $data);
} else {
//no category is checked at edit page
//firstly, we delete all existing category records inside category_product table
$this->db->where('id_product', $id);
$this->db->delete('category_product');
//secondly, we insert new category_id of NULL
$data = array(
'id_product' => $id,
'id_category' => NULL,
);
$this->db->insert('category_product', $data);
}
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Product Edit Successful</p>');
redirect('admin/products/edit/' . $id);
}
$this->data['subview'] = 'admin/products/edit';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
}
//to delete a product
public function delete($id) {
if($this->data['allowed'] == false || $this->data['role'] == 'admin') { redirect('admin/dashboard'); }
//check if id exist. If not exist, show 404.
$count = $this->product_m->count_exist($id);
if ($count == 0) {
//page not exist
show_404();
}
//delete image from server
for ($i = 1; $i <= 5; $i++) {
//check if there is an existing image on product table
$this->db->select("image$i")->from('products')->where('id_products', (int) $id);
$image = $this->db->get()->row_array(); //use array insted of object
if ($image["image$i"] != '' || $image["image$i"] != NULL) {
//Delete the actual image file from server. FCPATH is codeigniter base path
if (file_exists(base_url() . 'uploads/product/' . $image["image$i"])) {
unlink(FCPATH .'/uploads/product/'. $image["image$i"]);
}
if (file_exists(base_url() . 'uploads/product/large/' . $image["image$i"])) {
unlink(FCPATH .'/uploads/product/large/'. $image["image$i"]);
}
if (file_exists(base_url() . 'uploads/product/small/' . $image["image$i"])) {
unlink(FCPATH .'/uploads/product/small/'. $image["image$i"]);
}
if (file_exists(base_url() . 'uploads/product/thumbnail/' . $image["image$i"])) {
unlink(FCPATH .'/uploads/product/thumbnail/'. $image["image$i"]);
}
}
}
$this->product_m->delete($id);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Product Deleted Successful</p>');
redirect('admin/products');
}
//to delete a product
public function delete_product_detail($id_product, $id_product_detail) {
//check if id_product_detail exist. If not exist, show 404.
$this->db->select('id_product_details')->from('product_details')->where('id_product_details', $id_product_detail);
$count_product_detail = $this->db->get()->num_rows();
if ($count_product_detail == 0) { show_404(); }
//check if id_product. If not exist, show 404.
$this->db->select('id_products')->from('products')->where('id_products', $id_product);
$count_product = $this->db->get()->num_rows();
if ($count_product == 0) { show_404(); }
$this->db->where('id_product_details', $id_product_detail);
$this->db->delete('product_details');
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Product Detail Deleted Successfully</p>');
redirect('admin/products/edit/' . $id_product);
}
//process image
private function image_processing($image, $max_width, $max_height) {
//check & processing IMAGE 1
if ($_FILES[$image]['size'] !== 0) {
$config['upload_path'] = './uploads/product/';
$config['allowed_types'] = 'jpg|png|jpeg|gif|webp';
$config['max_size'] = '500';
$config['max_width'] = $max_width;
$config['max_height'] = $max_height;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($image)) {
echo $this->upload->display_errors(); exit();
$this->session->set_flashdata('{$image}-error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
redirect('admin/products/add');
} else {
$image = $this->upload->data();
$this->resize_image($image['file_name']);
return $image['file_name'];
}
}
}
//callback function validation add new product
public function _cek_existing_product_code($str) {
$num_rows = $this->product_m->cek_existing_product_code($str, $this->product_current_id);
if ($num_rows != 0 ) {
$this->form_validation->set_message('_cek_existing_product_code', 'Product code already exist !');
return FALSE;
} else {
return TRUE;
}
}
//NOT USED CURRENTLY ! callback function validation add new product check SKU
public function cek_existing_sku() {
$sku = $this->input->post('sku');
$this->db->select('sku')->from('products')->where('sku', $sku);
$count_sku = $this->db->get()->num_rows();
if($count_sku > 0) {
$cek_sku = 1;
} else {
$cek_sku = 0;
}
echo json_encode(array('sku'=>$cek_sku));
}
public function cek_existing_product_code() {
$product_code = $this->input->post('product_code');
$this->db->select('product_code')->from('products')->where('LOWER(product_code)', strtolower($product_code));
$count_product_code = $this->db->get()->num_rows();
if($count_product_code > 0) {
$cek_product_code = 1;
} else {
$cek_product_code = 0;
}
echo json_encode(array('product_code'=>$cek_product_code));
}
//NOT USED CURRENTLY ! callback function validation add new product check SKU
public function _cek_existing_sku($str) {
//check if the code is already exist in products detail table..
$this->db->select('sku')->from('product_details')->where('sku', $str);
$count_code_productstable = $this->db->get()->num_rows();
//check if the code is already exist in stocks table..
$this->db->select('sku')->from('stocks')->where('sku', $str);
$count_code_stockstable = $this->db->get()->num_rows();
if ($count_code_productstable != 0 || $count_code_stockstable != 0) {
$this->form_validation->set_message('_cek_existing_product_code', 'Product Code (SKU) already exist !');
return FALSE;
} else {
return TRUE;
}
}
private function table_data_processing() {
$data = array(
'sku' => $this->security->xss_clean($this->input->post('sku')),
'title' => $this->security->xss_clean($this->input->post('product_name')),
'alias' => url_title($this->security->xss_clean($this->input->post('product_name'))),
'description' => $this->security->xss_clean($this->input->post('description')),
'description_en' => $this->security->xss_clean($this->input->post('description_en')),
'long_description' => $this->security->xss_clean($this->input->post('long_description')),
'long_description_en' => $this->security->xss_clean($this->input->post('long_description_en')),
'additional_information' => $this->security->xss_clean($this->input->post('additional_information')),
'additional_information_en' => $this->security->xss_clean($this->input->post('additional_information_en')),
'payment_information' => $this->security->xss_clean($this->input->post('payment_information')),
'payment_information_en' => $this->security->xss_clean($this->input->post('payment_information_en')),
'brand_id' => (int) $this->input->post('brand_id'),
'sale_price' => $this->security->xss_clean($this->input->post('sale_price')),
'discounted_price' => $this->security->xss_clean($this->input->post('discounted_price')),
'is_sale' => $this->input->post('is_sale'),
'minimum_stock' => $this->security->xss_clean($this->input->post('minimum_stock')),
'warranty' => $this->security->xss_clean($this->input->post('warranty')),
'dimension_weight' => $this->security->xss_clean($this->input->post('dimension_weight')),
'dimension_length' => $this->security->xss_clean($this->input->post('dimension_length')),
'dimension_width' => $this->security->xss_clean($this->input->post('dimension_width')),
'dimension_height' => $this->security->xss_clean($this->input->post('dimension_height')),
'product_status' => $this->input->post('product_status'),
'priority' => $this->input->post('priority'),
'new_arrival' => $this->input->post('new_arrival'),
'best_seller' => $this->input->post('best_seller'),
'is_backorder' => $this->input->post('is_backorder'),
'is_discontinue' => $this->input->post('is_discontinue'),
'popular_product' => $this->input->post('popular_product'),
'meta_description' => $this->security->xss_clean($this->input->post('meta_description')),
'indent_duration' => $this->security->xss_clean($this->input->post('indent_duration')),
'sold' => $this->input->post('sold')
);
//image upload
if (isset($this->image1_filename)) {
$data['image1'] = $this->image1_filename;
}
if (isset($this->image2_filename)) {
$data['image2'] = $this->image2_filename;
}
if (isset($this->image3_filename)) {
$data['image3'] = $this->image3_filename;
}
if (isset($this->image4_filename)) {
$data['image4'] = $this->image4_filename;
}
if (isset($this->image5_filename)) {
$data['image5'] = $this->image5_filename;
}
return $data;
}
//To delete product image file from server, and from database
public function delete_image($id = NULL, $image_name) {
$count = $this->product_m->count_exist($id);
if ($id == NULL || $image_name == NULL || $count == 0) {
redirect('admin/brands');
}
//get image file name for deletion
$this->db->select($image_name)->from('products')->where('id_products', (int) $id);
$image = $this->db->get()->row();
switch ($image_name) {
case 'image1':
//Delete the actual image file from server. FCPATH is codeigniter base path
if (file_exists(base_url() . 'uploads/product/' . $image->image1)) {
unlink(FCPATH .'/uploads/product/'. $image->image1);
}
if (file_exists(base_url() . 'uploads/product/large/' . $image->image1)) {
unlink(FCPATH .'/uploads/product/large/'. $image->image1);
}
if (file_exists(base_url() . 'uploads/product/small/' . $image->image1)) {
unlink(FCPATH .'/uploads/product/small/'. $image->image1);
}
if (file_exists(base_url() . 'uploads/product/thumbnail/' . $image->image1)) {
unlink(FCPATH .'/uploads/product/thumbnail/'. $image->image1);
}
//Delete image field from database
$data = array(
'image1' => NULL,
);
break;
case 'image2':
//Delete the actual image file from server. FCPATH is codeigniter base path
if (file_exists(base_url() . 'uploads/product/' . $image->image2)) {
unlink(FCPATH .'/uploads/product/'. $image->image2);
}
if (file_exists(base_url() . 'uploads/product/large/' . $image->image2)) {
unlink(FCPATH .'/uploads/product/large/'. $image->image2);
}
if (file_exists(base_url() . 'uploads/product/small/' . $image->image2)) {
unlink(FCPATH .'/uploads/product/small/'. $image->image2);
}
if (file_exists(base_url() . 'uploads/product/thumbnail/' . $image->image2)) {
unlink(FCPATH .'/uploads/product/thumbnail/'. $image->image2);
}
//Delete image field from database
$data = array(
'image2' => NULL,
);
break;
case 'image3':
//Delete the actual image file from server. FCPATH is codeigniter base path
if (file_exists(base_url() . 'uploads/product/' . $image->image3)) {
unlink(FCPATH .'/uploads/product/'. $image->image3);
}
if (file_exists(base_url() . 'uploads/product/large/' . $image->image3)) {
unlink(FCPATH .'/uploads/product/large/'. $image->image3);
}
if (file_exists(base_url() . 'uploads/product/small/' . $image->image3)) {
unlink(FCPATH .'/uploads/product/small/'. $image->image3);
}
if (file_exists(base_url() . 'uploads/product/thumbnail/' . $image->image3)) {
unlink(FCPATH .'/uploads/product/thumbnail/'. $image->image3);
}
//Delete image field from database
$data = array(
'image3' => NULL,
);
break;
case 'image4':
//Delete the actual image file from server. FCPATH is codeigniter base path
if (file_exists(base_url() . 'uploads/product/' . $image->image4)) {
unlink(FCPATH .'/uploads/product/'. $image->image4);
}
if (file_exists(base_url() . 'uploads/product/large/' . $image->image4)) {
unlink(FCPATH .'/uploads/product/large/'. $image->image4);
}
if (file_exists(base_url() . 'uploads/product/small/' . $image->image4)) {
unlink(FCPATH .'/uploads/product/small/'. $image->image4);
}
if (file_exists(base_url() . 'uploads/product/thumbnail/' . $image->image4)) {
unlink(FCPATH .'/uploads/product/thumbnail/'. $image->image4);
}
//Delete image field from database
$data = array(
'image4' => NULL,
);
break;
case 'image5':
//Delete the actual image file from server. FCPATH is codeigniter base path
if (file_exists(base_url() . 'uploads/product/' . $image->image5)) {
unlink(FCPATH .'/uploads/product/'. $image->image5);
}
if (file_exists(base_url() . 'uploads/product/large/' . $image->image5)) {
unlink(FCPATH .'/uploads/product/large/'. $image->image5);
}
if (file_exists(base_url() . 'uploads/product/small/' . $image->image5)) {
unlink(FCPATH .'/uploads/product/small/'. $image->image5);
}
if (file_exists(base_url() . 'uploads/product/thumbnail/' . $image->image5)) {
unlink(FCPATH .'/uploads/product/thumbnail/'. $image->image5);
}
//Delete image field from database
$data = array(
'image5' => NULL,
);
break;
}
$this->db->where('id_products', (int) $id);
$this->db->update('products', $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Image Delete Successful</p>');
redirect('admin/products/edit/' . $id);
}
public function search_brand() {
if(!isset($_POST['search_brand'])) {
show_404();
}
//get product name from form
$this->data['brand_id'] = $this->input->post('brand', TRUE);
if ($this->data['brand_id'] != '') {
//get all brands which only consist this brand_id
$this->db->select('*')->from('products')->where('brand_id', (int) $this->data['brand_id']);
$this->db->order_by('title', 'ASC');
$this->data['products'] = $this->db->get()->result();
} else {
//get all brands
$this->db->select('*')->from('products');
$this->db->order_by('priority', 'ASC');
$this->data['products'] = $this->db->get()->result();
}
//load view
$this->data['subview'] = 'admin/products/index';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
}
public function search_category() {
if(!isset($_POST['search_category'])) {
show_404();
}
//get product name from form
$this->data['category_id'] = $this->input->post('category', TRUE);
if ($this->data['category_id'] == '') {
$this->db->select('*')->from('products')->order_by('priority', 'ASC');
$this->data['products'] = $this->db->get()->result();
//load view
$this->data['subview'] = 'admin/products/index';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
} else {
$this->db->select('*');
$this->db->from('products');
$this->db->join('category_product', 'category_product.id_product = products.id_products');
$this->db->where('category_product.id_category', (int) $this->data['category_id']);
$this->db->order_by('products.priority', 'ASC');
$this->data['products'] = $this->db->get()->result();
//load view
$this->data['subview'] = 'admin/products/index';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
}
}
public function change_product_ordering() {
if(!isset($_POST['change_ordering'])) {
show_404();
}
$data = array(
'website_product_ordering' => $this->input->post('change_product_ordering')
);
$this->db->where('id_configuration', 1);
$this->db->update('configuration', $data);
$this->session->set_flashdata('success', "<br><p style='background:green; color:white; padding:5px; font-weight:bold;'>Product Ordering changed to {$this->input->post('change_product_ordering')}</p>");
redirect('admin/products');
}
public function ajax_getproductdetails() {
//test if ajax call to prevent direct access
//this script causing error, ajax cannot request
/* if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
} */
if (empty($_POST)) {show_404();}
$product_id = (int) $this->input->post('id_product');
//get product detail
$this->db->select('id_products, title, alias, product_status')->from('products')->where('id_products', $product_id);
$data['product'] = $this->db->get()->row();
$this->load->view('ajax/ajax_quickedit_product', $data);
}
public function quick_update_product() {
if(!isset($_POST['quickeditproduct'])) {
show_404();
}
$product_id = (int) $this->input->post('product_id');
$product_status = $this->input->post('product_status');
//update product
$data = array(
'product_status' => $product_status
);
$this->db->where('id_products', $product_id);
$this->db->update('products', $data);
//UPDATE STOCK & SKU
$sku_array = $this->input->post('sku');
$price_array = $this->input->post('price');
$discounted_price_array = $this->input->post('discounted_price');
$weight_array = $this->input->post('weight');
$stock_array = $this->input->post('stock');
$count_stock = count($stock_array);
//firstly, we delete all existing records inside stocks table
$this->db->where('product_id', $product_id);
$this->db->delete('product_details');
//then we fill in the data in
for($i = 0; $i < 10; $i++ ) {
if ($stock_array[$i] != '') {
$attributes_array = $_POST["select{$i}"];
$attributes_text = implode(' ',$attributes_array);
$data = array(
'product_id' => $product_id,
'sku' => $sku_array[$i],
'price' => $price_array[$i],
'stock' => $stock_array[$i],
'attributes' => $attributes_text,
'weight' => $weight_array[$i],
'discounted_price' => $discounted_price_array[$i]
);
$this->db->insert('product_details', $data);
}
}
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Edit Product Successful.</p>');
redirect('admin/products');
}
//duplicate products
public function duplicate_product($product_id = NULL) {
if ($product_id == NULL) { show_404();}
//check if id exist in current table
$this->db->select('id_products')->from('products')->where('id_products', $product_id);
$count = $this->db->get()->num_rows();
if($count == 0) {show_404();}
//select current chosen product
$this->db->select('*')->from('products')->where('id_products', $product_id);
$current_product = $this->db->get()->row();
//get title, and check the title how many title is similar exist..
$current_product_title = $current_product->title;
$this->db->select('id_products')->from('products')->like('title', $current_product_title);
$count_title = $this->db->get()->num_rows();
//insert to new row
$data = array(
'brand_id' => $current_product->brand_id,
'title' => $current_product->title . ' (' . ($count_title + 1) . ')',
'alias' => $current_product->alias . '-' . ($count_title + 1),
'description'=> $current_product->description,
'description_en'=> $current_product->description_en,
'long_description'=> $current_product->long_description,
'long_description_en'=> $current_product->long_description_en,
'additional_information'=> $current_product->additional_information,
'additional_information_en'=> $current_product->additional_information_en,
'created_at' => $current_product->created_at,
'image1' => $current_product->image1,
'image2' => $current_product->image2,
'image3' => $current_product->image3,
'image4' => $current_product->image4,
'image5' => $current_product->image5,
'product_status' => '0',
'new_arrival' => $current_product->new_arrival,
'best_seller' => $current_product->best_seller,
'popular_product' => $current_product->popular_product,
'meta_description' => $current_product->meta_description,
'meta_keywords' => $current_product->meta_keywords,
);
$this->db->insert('products', $data);
$new_product_id = $this->db->insert_id();
//copy category as well
$this->db->select('id_category')->from('category_product')->where('id_product', $product_id);
$current_categories = $this->db->get()->result();
foreach ($current_categories as $current_category) {
$data = array(
'id_product' => $new_product_id,
'id_category' => $current_category->id_category
);
$this->db->insert('category_product', $data);
}
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Product Duplicate Successful.</p>');
redirect('admin/products');
}
public function ajax_add_quantitydiscount() {
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$id_product = (int) $this->input->post('id_product');
$min_quantity = (int) $this->input->post('min_quantity');
$quantity_discount = $this->input->post('quantity_discount');
//check if min quantity already exist
$this->db->select('id_quantity_discount')->from('quantity_discount')->where('product_id', $id_product)->where('min_quantity', $min_quantity);
$count = $this->db->get()->num_rows();
if ($count == 0) {
//add quantity discount to quantity discount table
$data = array(
'product_id' => $id_product,
'min_quantity' => $min_quantity,
'discount_percentage' => $quantity_discount
);
$this->db->insert('quantity_discount', $data);
} else {
//update quantity discount
$data = array(
'discount_percentage' => $quantity_discount
);
$this->db->where('product_id', $id_product);
$this->db->where('min_quantity', $min_quantity);
$this->db->update('quantity_discount', $data);
}
//get all quantity discount
$this->db->select('*')->from('quantity_discount')->where('product_id', $id_product)->order_by('min_quantity', 'ASC');
$data['quantity_discount'] = $this->db->get()->result();
$this->load->view('admin/products/ajax_addquantitydiscount', $data);
}
public function ajax_delete_quantitydiscount($id_quantitydiscount = NULL) {
if ($id_quantitydiscount == NULL) { show_404(); }
//check if product exist
$this->db->select('id_quantity_discount')->from('quantity_discount')->where('id_quantity_discount', $id_quantitydiscount);
$count = $this->db->get()->num_rows();
if($count == 0) {show_404();}
//get product id
$this->db->select('product_id')->from('quantity_discount')->where('id_quantity_discount', $id_quantitydiscount);
$product_id = $this->db->get()->row()->product_id;
//delete id
$this->db->where('id_quantity_discount', $id_quantitydiscount);
$this->db->delete('quantity_discount');
//get all quantity discount
$this->db->select('*')->from('quantity_discount')->where('product_id', $product_id)->order_by('min_quantity', 'ASC');
$data['quantity_discount'] = $this->db->get()->result();
$this->load->view('admin/products/ajax_addquantitydiscount', $data);
}
public function ajax_get_link_products() {
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$category_id = (int) $this->input->post('category_id');
//get all products belongs to the category
$this->db->select('id_product')->from('category_product')->where('id_category', $category_id);
$data['id_products'] = $this->db->get()->result();
$this->load->view('admin/products/ajax_get_link_products', $data);
}
public function ajax_add_link_products() {
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$data['current_product_id'] = (int) $this->input->post('current_product_id');
$link_product_id = (int) $this->input->post('link_product_id');
$data_insert = array(
'product_id' => $data['current_product_id'],
'link_to_product_id' => $link_product_id
);
$this->db->insert('product_link', $data_insert);
$this->load->view('admin/products/ajax_add_link_products', $data);
}
public function delete_link_product($link_id, $product_id) {
$this->db->where('id_product_link', $link_id);
$this->db->delete('product_link');
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Product Link Deleted Successful.</p>');
redirect('admin/products/edit/' . $product_id);
}
public function uploadHarga(){
$config['upload_path'] = 'uploads/excel/';
$config['allowed_types'] = 'xls|xlsx';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')){
$this->session->set_flashdata('upload_excel_info', '<p style="background:red; color:white; padding:5px; font-weight:bold;">'.strip_tags($this->upload->display_errors()).'</p>');
redirect('admin/products');
}
else{
require_once APPPATH . 'third_party/PHPExcel/IOFactory.php';
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data(); //Mengambil detail data yang di upload
$filename = $upload_data['file_name'];//Nama File
ini_set('memory_limit', '-1');
$inputFileName = 'uploads/excel/'.$filename;
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
}
catch(Exception $e) {
die('Error loading file :' . $e->getMessage());
}
$worksheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$numRows = count($worksheet);
// $this->db->empty_table('sales_list');
for ($i=1; $i < ($numRows+1) ; $i++) {
if($i != 1){
//check if product code already exist, if already exist, update the row
$this->db->select('product_code, sku')->from('products')->where('product_code', $worksheet[$i]["A"])->where('sku', $worksheet[$i]["B"]);
$count_sku = $this->db->get()->num_rows();
if($count_sku > 0) {
//Product Code already exist. so update only...
//add to products table
$data = array(
"sale_price" => $worksheet[$i]["D"],
"discounted_price" => $worksheet[$i]["E"],
);
//update into products table
$this->db->where('product_code', $worksheet[$i]["A"]);
$this->db->where('sku', $worksheet[$i]["B"]);
$this->db->update('products', $data);
} else {
unlink('uploads/excel/'.$filename);
$this->session->set_flashdata('upload_excel_info', '<p style="background:red; color:white; padding:5px; font-weight:bold;">Failed</p>');
redirect('admin/products');
}
}
}
unlink('uploads/excel/'.$filename);
$this->session->set_flashdata('success', '<p style="background:green; color:white; padding:5px; font-weight:bold;">Upload Products File Success</p>');
redirect('admin/products');
}
}
public function uploadStock(){
$config['upload_path'] = 'uploads/excel/';
$config['allowed_types'] = 'xls|xlsx';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')){
$this->session->set_flashdata('upload_excel_info', '<p style="background:red; color:white; padding:5px; font-weight:bold;">'.strip_tags($this->upload->display_errors()).'</p>');
redirect('admin/products');
}
else{
require_once APPPATH . 'third_party/PHPExcel/IOFactory.php';
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data(); //Mengambil detail data yang di upload
$filename = $upload_data['file_name'];//Nama File
ini_set('memory_limit', '-1');
$inputFileName = 'uploads/excel/'.$filename;
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
}
catch(Exception $e) {
die('Error loading file :' . $e->getMessage());
}
$worksheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$numRows = count($worksheet);
// $this->db->empty_table('sales_list');
for ($i=1; $i < ($numRows+1) ; $i++) {
if($i != 1){
//check if product code already exist, if already exist, update the row
$this->db->select('product_code, sku')->from('products')->where('product_code', $worksheet[$i]["A"])->where('sku', $worksheet[$i]["B"]);
$count_sku = $this->db->get()->num_rows();
if($count_sku > 0) {
$product_data = $this->db->select('product_code, sku, id_products')->from('products')->where('product_code', $worksheet[$i]["A"])->where('sku', $worksheet[$i]["B"])->get()->row();
// update into stock table, currently for Jakarta & Makassar
//Jakarta
//check if Jakarta row already exist
$count_jakarta_row = $this->db->select('id')->from('stock')->where('warehouse_id', 1)->where('id_product', $product_data->id_products)->get()->num_rows();
if($count_jakarta_row ==1) {
$this->db->set('stock', $worksheet[$i]["D"]);
$this->db->where('warehouse_id', 1);
$this->db->where('id_product', $product_data->id_products);
$this->db->update('stock');
} else {
$data = array(
'stock' => $worksheet[$i]["D"],
'warehouse_id' => 1,
'id_product' => $product_data->id_products
);
$this->db->insert('stock', $data);
}
//Makassar
//check if makassar row already exist
$count_makassar_row = $this->db->select('id')->from('stock')->where('warehouse_id', 2)->where('id_product', $product_data->id_products)->get()->num_rows();
if($count_makassar_row ==1) {
$this->db->set('stock', $worksheet[$i]["E"]);
$this->db->where('warehouse_id', 2);
$this->db->where('id_product', $product_data->id_products);
$this->db->update('stock');
} else {
$data = array(
'stock' => $worksheet[$i]["E"],
'warehouse_id' => 2,
'id_product' => $product_data->id_products
);
$this->db->insert('stock', $data);
}
} else {
unlink('uploads/excel/'.$filename);
$this->session->set_flashdata('upload_excel_info', '<p style="background:red; color:white; padding:5px; font-weight:bold;">Failed</p>');
redirect('admin/products');
}
}
}
unlink('uploads/excel/'.$filename);
$this->session->set_flashdata('success', '<p style="background:green; color:white; padding:5px; font-weight:bold;">Upload Stock File Success</p>');
redirect('admin/products');
}
}
public function upload_products() {
//upload products data
if(!isset($_POST['upload_csv'])) { show_404(); }
//check if the uploaded file is csv format
$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
if(in_array($_FILES['userfile']['type'],$mimes)){
ini_set('auto_detect_line_endings', TRUE); //to detect line ending
//continue import operation
//open the csv file and put into variable
$file = fopen($_FILES['userfile']['tmp_name'],'r') or die('cannot open file');
//initialize array
$csv_data = array();
//if not reach end of file...
while (!feof($file)) {
//put the csv into array..
$csv_data[] = fgetcsv($file, 10000, '~'); //file, length, separator..
}
fclose($file); //close the file
/* echo '<pre>';
print_r($csv_data);
echo '</pre>';
exit(); */
$row_number = 0;
foreach ($csv_data as $item) {
$row_number = $row_number + 1;
//skip the 1st row...
if($row_number != 1) {
if(empty($item)) {continue;} //if array is empty, then continue to next iteration and bypass below script..
//check if product code already exist, if already exist, update the row, else, insert new
$this->db->select('product_code')->from('products')->where('product_code', $item[0]);
$count_sku = $this->db->get()->num_rows();
if($count_sku > 0) {
//Product Code already exist. so update only...
//add to products table
$data = array(
'title' => $item[1],
'alias' => url_title(trim($item[1])),
'description' => $item[2],
'long_description' => $item[3],
'image1' => trim($item[4]),
'image2' => trim($item[5]),
'image3' => trim($item[6]),
'image4' => trim($item[7]),
'image5' => trim($item[8]),
'product_type' => $item[9],
'competitor_price' => $item[11],
'sale_price' => $item[12],
'discounted_price' => $item[13],
'minimum_stock' => $item[14],
'dimension_weight' => $item[17],
'dimension_length' => $item[18],
'dimension_width' => $item[19],
'dimension_height' => $item[20],
'best_seller' => $item[21],
'product_status' => $item[24],
'new_arrival' => $item[25],
'popular_product' => $item[26],
'is_backorder' => $item[27],
'is_discontinue' => $item[28],
'is_backorder' => $item[27],
'is_delete' => 'no',
'meta_description' => strip_tags($item[2]), //need to remove html tag
'text_satuan_jual' => $item[29],
'golongan_obat' => $item[30],
'is_sale' => $item[33]
);
//BRANDS
$this->db->select('id_brands')->from('brands')->where('brand', trim($item[10]));
$brand = $this->db->get()->row();
if(count($brand) > 0) {
$data['brand_id'] = $brand->id_brands;
} else {
$data['brand_id'] = NULL;
}
//update into products table
$this->db->where('product_code', $item[0]);
$this->db->update('products', $data);
//get product_id
$this->db->select('id_products')->from('products')->where('product_code', $item[0]);
$product_id = $this->db->get()->row()->id_products;
//SATUAN JUAL & SATUAN BELI
//first, delete all satuan jual beli dari tabel unit_conversion
$this->db->where('product_id', $product_id);
$this->db->delete('unit_convertion');
$satuan_jual = $item[31];
$satuan_beli = $item[32];
$data_conversion = array();
$data_conversion['product_id'] = $product_id;
//get satuan jual id
$this->db->select('id_conversion')->from('conversion')->where('conversion', $satuan_jual);
$satuan_jual_id = $this->db->get()->row();
if(count($satuan_jual_id)>0) {
$data_conversion['satuan_jual_id'] = $satuan_jual_id->id_conversion;
}
//satuan beli
if (strpos($satuan_beli, '|') == true) {
$satuan_beli_array = explode('|', $satuan_beli);
foreach ($satuan_beli_array as $item_satuan_beli) {
$satuan_beli_content = explode('-', $item_satuan_beli);
$this->db->select('id_conversion')->from('conversion')->where('conversion', trim($satuan_beli_content[0]));
$satuan_beli_id = $this->db->get()->row();
if(count($satuan_beli_id)>0) {
$data_conversion['satuan_beli_id'] = $satuan_beli_id->id_conversion;
$data_conversion['konversi'] = trim($satuan_beli_content[1]);
//insert into unit conversion
$this->db->insert('unit_convertion', $data_conversion);
}
}
} else {
//satuan beli hanya ada 1
$satuan_beli_array = explode('-', $satuan_beli);
//get satuan beli id
$this->db->select('id_conversion')->from('conversion')->where('conversion', trim($satuan_beli_array[0]));
$satuan_beli_id = $this->db->get()->row();
if(count($satuan_beli_id)>0) {
$data_conversion['satuan_beli_id'] = $satuan_beli_id->id_conversion;
$data_conversion['konversi'] = trim($satuan_beli_array[1]);
//insert into unit conversion
$this->db->insert('unit_convertion', $data_conversion);
}
}
//ACTIVE INGREDIENTS
//first, delete all active ingredients dari tabel active_ingredients_products
$this->db->where('product_id', $product_id);
$this->db->delete('active_ingredients_products');
$active_ingredients = $item[23];
if (strpos($active_ingredients, '|') == true) {
//this product has more than 1 active ingredients
$active_ingredients_array = explode('|', $active_ingredients);
for ($i=0; $i<count($active_ingredients_array); $i++) {
//get active ingredient id
$this->db->select('id')->from('active_ingredients')->where('name', trim($active_ingredients_array[$i]));
$active_ingredients_id = $this->db->get()->row();
if(count($active_ingredients_id) > 0) {
$data = array(
'product_id' => $product_id,
'active_ingredients_id' => $active_ingredients_id->id,
);
$this->db->insert('active_ingredients_products', $data);
}
}
} else {
//this product has only 1 active ingredients
//get active ingredient id
$this->db->select('id')->from('active_ingredients')->where('name', trim($active_ingredients));
$active_ingredients_id = $this->db->get()->row();
if(count($active_ingredients_id) > 0) {
$data = array(
'product_id' => $product_id,
'active_ingredients_id' => $active_ingredients_id->id,
);
$this->db->insert('active_ingredients_products', $data);
}
}
//SHIPMENT METHOD
//first, delete all shipment method dari tabel shipment_method_product
$this->db->where('product_id', $product_id);
$this->db->delete('shipment_method_product');
$shipment_method = $item[16];
if(strpos($shipment_method, '|') == true) {
$shipment_method_array = explode('|', $shipment_method);
for ($i=0; $i<count($shipment_method_array); $i++) {
$data = array(
'product_id' => $product_id,
'shipment_method_id' => $shipment_method_array[$i],
);
$this->db->insert('shipment_method_product', $data);
}
} else {
$data = array(
'product_id' => $product_id,
'shipment_method_id' => $shipment_method,
);
$this->db->insert('shipment_method_product', $data);
}
//CATEGORY PRODUCT
//first, delete all category dari tabel category_product
$this->db->where('id_product', $product_id);
$this->db->delete('category_product');
$category_product = $item[22];
if (strpos($category_product, '|') == true) {
//this category has more than 1 parent category
$category_group = explode('|', $item[22]);
foreach($category_group as $group) {
//check if has sub categories
if (strpos($group, '-') == true) {
//has sub categories
$categories = explode('-', $group);
if(isset($categories[0])) {
//get parent category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[0]))->where('parent', NULL);
$parent_category_id = $this->db->get()->row();
if(count($parent_category_id) > 0) {
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $parent_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
if(isset($categories[1])) {
//get level1 category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[1]))->where('parent', $parent_category_id->id_categories);
$level1_category_id = $this->db->get()->row();
if(count($level1_category_id) > 0) {
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $level1_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
if(isset($categories[2])) {
//get level2 category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[2]))->where('parent', $level1_category_id->id_categories);
$level2_category_id = $this->db->get()->row();
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $level2_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
} else {
$this->db->select('id_categories')->from('categories')->where('category', trim($group));
$category_id = $this->db->get()->row();
if(count($category_id) >0) {
//dont have sub categories
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
}
} else {
//this only have 1 category
//check if has sub categories
if (strpos($category_product, '-') == true) {
//has sub categories
$categories = explode('-', $category_product);
if(isset($categories[0])) {
//category exist
//get parent category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[0]))->where('parent', NULL);
$parent_category_id = $this->db->get()->row();
//add to category_product
if(count($parent_category_id)>0) {
$data = array(
'id_product' => $product_id,
'id_category' => $parent_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
if(isset($categories[1])) {
//get level1 category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[1]))->where('parent', $parent_category_id->id_categories);
$level1_category_id = $this->db->get()->row();
if(count($level1_category_id) >0) {
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $level1_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
if(isset($categories[2])) {
//get level2 category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[2]))->where('parent', $level1_category_id->id_categories);
$level2_category_id = $this->db->get()->row();
if(count($level2_category_id) >0) {
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $level2_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
} else {
$this->db->select('id_categories')->from('categories')->where('category', trim($category_product));
$category_id = $this->db->get()->row();
if(count($category_id) >0) {
//dont have sub categories
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
}
} else {
//insert new
//add to products table
$data = array(
'product_code' => $item[0],
'title' => $item[1],
'alias' => url_title(trim($item[1])),
'description' => $item[2],
'long_description' => $item[3],
'image1' => trim($item[4]),
'image2' => trim($item[5]),
'image3' => trim($item[6]),
'image4' => trim($item[7]),
'image5' => trim($item[8]),
'product_type' => $item[9],
'competitor_price' => $item[11],
'sale_price' => $item[12],
'discounted_price' => $item[13],
'minimum_stock' => $item[14],
'dimension_weight' => $item[17],
'dimension_length' => $item[18],
'dimension_width' => $item[19],
'dimension_height' => $item[20],
'best_seller' => $item[21],
'product_status' => $item[24],
'new_arrival' => $item[25],
'popular_product' => $item[26],
'is_backorder' => $item[27],
'is_discontinue' => $item[28],
'is_backorder' => $item[27],
'is_delete' => 'no',
'meta_description' => strip_tags($item[2]), //need to remove html tag
'text_satuan_jual' => $item[29],
'golongan_obat' => $item[30],
'is_sale' => $item[33]
);
//BRANDS
$this->db->select('id_brands')->from('brands')->where('brand', trim($item[10]));
$brand = $this->db->get()->row();
if(count($brand) > 0) {
$data['brand_id'] = $brand->id_brands;
} else {
$data['brand_id'] = NULL;
}
//insert into products table
$this->db->insert('products', $data);
$product_id = $this->db->insert_id();
//SATUAN JUAL & SATUAN BELI
$satuan_jual = $item[31];
$satuan_beli = $item[32];
$data_conversion = array();
$data_conversion['product_id'] = $product_id;
//get satuan jual id
$this->db->select('id_conversion')->from('conversion')->where('conversion', $satuan_jual);
$satuan_jual_id = $this->db->get()->row();
if(count($satuan_jual_id)>0) {
$data_conversion['satuan_jual_id'] = $satuan_jual_id->id_conversion;
}
//satuan beli
if (strpos($satuan_beli, '|') == true) {
$satuan_beli_array = explode('|', $satuan_beli);
foreach ($satuan_beli_array as $item_satuan_beli) {
$satuan_beli_content = explode('-', $item_satuan_beli);
$this->db->select('id_conversion')->from('conversion')->where('conversion', trim($satuan_beli_content[0]));
$satuan_beli_id = $this->db->get()->row();
if(count($satuan_beli_id)>0) {
$data_conversion['satuan_beli_id'] = $satuan_beli_id->id_conversion;
$data_conversion['konversi'] = trim($satuan_beli_content[1]);
//insert into unit conversion
$this->db->insert('unit_convertion', $data_conversion);
}
}
} else {
//satuan beli hanya ada 1
$satuan_beli_array = explode('-', $satuan_beli);
//get satuan beli id
$this->db->select('id_conversion')->from('conversion')->where('conversion', trim($satuan_beli_array[0]));
$satuan_beli_id = $this->db->get()->row();
if(count($satuan_beli_id)>0) {
$data_conversion['satuan_beli_id'] = $satuan_beli_id->id_conversion;
$data_conversion['konversi'] = trim($satuan_beli_array[1]);
//insert into unit conversion
$this->db->insert('unit_convertion', $data_conversion);
}
}
//ACTIVE INGREDIENTS
$active_ingredients = $item[23];
if (strpos($active_ingredients, '|') == true) {
//this product has more than 1 active ingredients
$active_ingredients_array = explode('|', $active_ingredients);
for ($i=0; $i<count($active_ingredients_array); $i++) {
//get active ingredient id
$this->db->select('id')->from('active_ingredients')->where('name', trim($active_ingredients_array[$i]));
$active_ingredients_id = $this->db->get()->row();
if(count($active_ingredients_id) > 0) {
$data = array(
'product_id' => $product_id,
'active_ingredients_id' => $active_ingredients_id->id,
);
$this->db->insert('active_ingredients_products', $data);
}
}
} else {
//this product has only 1 active ingredients
//get active ingredient id
$this->db->select('id')->from('active_ingredients')->where('name', trim($active_ingredients));
$active_ingredients_id = $this->db->get()->row();
if(count($active_ingredients_id) > 0) {
$data = array(
'product_id' => $product_id,
'active_ingredients_id' => $active_ingredients_id->id,
);
$this->db->insert('active_ingredients_products', $data);
}
}
//SHIPMENT METHOD
$shipment_method = $item[16];
if(strpos($shipment_method, '|') == true) {
$shipment_method_array = explode('|', $shipment_method);
for ($i=0; $i<count($shipment_method_array); $i++) {
$data = array(
'product_id' => $product_id,
'shipment_method_id' => $shipment_method_array[$i],
);
$this->db->insert('shipment_method_product', $data);
}
} else {
$data = array(
'product_id' => $product_id,
'shipment_method_id' => $shipment_method,
);
$this->db->insert('shipment_method_product', $data);
}
//CATEGORY PRODUCT
$category_product = $item[22];
if (strpos($category_product, '|') == true) {
//this category has more than 1 parent category
$category_group = explode('|', $item[22]);
foreach($category_group as $group) {
//check if has sub categories
if (strpos($group, '-') == true) {
//has sub categories
$categories = explode('-', $group);
if(isset($categories[0])) {
//get parent category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[0]))->where('parent', NULL);
$parent_category_id = $this->db->get()->row();
if(count($parent_category_id) > 0) {
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $parent_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
if(isset($categories[1])) {
//get level1 category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[1]))->where('parent', $parent_category_id->id_categories);
$level1_category_id = $this->db->get()->row();
if(count($level1_category_id) > 0) {
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $level1_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
if(isset($categories[2])) {
//get level2 category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[2]))->where('parent', $level1_category_id->id_categories);
$level2_category_id = $this->db->get()->row();
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $level2_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
} else {
$this->db->select('id_categories')->from('categories')->where('category', trim($group));
$category_id = $this->db->get()->row();
if(count($category_id) >0) {
//dont have sub categories
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
}
} else {
//this only have 1 category
//check if has sub categories
if (strpos($category_product, '-') == true) {
//has sub categories
$categories = explode('-', $category_product);
if(isset($categories[0])) {
//category exist
//get parent category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[0]))->where('parent', NULL);
$parent_category_id = $this->db->get()->row();
//add to category_product
if(count($parent_category_id)>0) {
$data = array(
'id_product' => $product_id,
'id_category' => $parent_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
if(isset($categories[1])) {
//get level1 category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[1]))->where('parent', $parent_category_id->id_categories);
$level1_category_id = $this->db->get()->row();
if(count($level1_category_id) >0) {
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $level1_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
if(isset($categories[2])) {
//get level2 category_id
$this->db->select('id_categories')->from('categories')->where('category', trim($categories[2]))->where('parent', $level1_category_id->id_categories);
$level2_category_id = $this->db->get()->row();
if(count($level2_category_id) >0) {
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $level2_category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
} else {
$this->db->select('id_categories')->from('categories')->where('category', trim($category_product));
$category_id = $this->db->get()->row();
if(count($category_id) >0) {
//dont have sub categories
//add to category_product
$data = array(
'id_product' => $product_id,
'id_category' => $category_id->id_categories
);
$this->db->insert('category_product', $data);
}
}
}
}
} /*---end if($row_number != 1) --*/
} /*---end foreach ($csv_data as $item) ---*/
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Products CSV Imported.</p>');
redirect('admin/products');
} else {
//not a csv file. Not allowed.
die('Sorry, file type not allowed. Please upload only CSV file.');
}
}
public function upload_stocks() {
//upload products data
if(!isset($_POST['upload_csv'])) { show_404(); }
//check if the uploaded file is csv format
$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
if(in_array($_FILES['userfile']['type'],$mimes)){
ini_set('auto_detect_line_endings', TRUE); //to detect line ending
//continue import operation
//open the csv file and put into variable
$file = fopen($_FILES['userfile']['tmp_name'],'r') or die('cannot open file');
//initialize array
$csv_data = array();
//if not reach end of file...
while (!feof($file)) {
//put the csv into array..
$csv_data[] = fgetcsv($file, 10000, '~'); //file, length, separator..
}
fclose($file); //close the file
/* echo '<pre>';
print_r($csv_data);
echo '</pre>';
exit(); */
$row_number = 0;
foreach ($csv_data as $item) {
$row_number = $row_number + 1;
//skip the 1st row...
if($row_number != 1) {
if(empty($item)) {continue;} //if array is empty, then continue to next iteration and bypass below script..
//check if product id is exist, if already exist, update the row, else, insert new
$this->db->select('id_products')->from('products')->where('title', $item[0]);
$count_product = $this->db->get()->num_rows();
if($count_product > 0) {
//Product Code is exist. so can proceed...
//get product id
$this->db->select('id_products')->from('products')->where('title', $item[0]);
$product_id = $this->db->get()->row()->id_products;
//firstly, delete all record
$this->db->where('id_product', $product_id);
$this->db->delete('stock');
//add new record jakarta
$data1 = array(
'id_product' => $product_id,
'warehouse_id' => 1,
'stock' => $item[1]
);
$this->db->insert('stock', $data1);
//add new record jakarta
$data2 = array(
'id_product' => $product_id,
'warehouse_id' => 2,
'stock' => $item[2]
);
$this->db->insert('stock', $data2);
}
} /*---end if($row_number != 1) --*/
} /*---end foreach ($csv_data as $item) ---*/
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Stock CSV Imported.</p>');
redirect('admin/products');
} else {
//not a csv file. Not allowed.
die('Sorry, file type not allowed. Please upload only CSV file.');
}
}
private function resize_image($image) {
//Get product image dimensions from configuration table
$this->db->select('product_image_width, product_image_height, product_image_large_width, product_image_large_height, product_image_small_width, product_image_small_height, product_image_thumbnail_width, product_image_thumbnail_height')->from('configuration')->where('id_configuration', 1);
$image_dimension = $this->db->get()->row();
//image resizing (LARGE IMAGE)
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/product/' . $image;
$config['new_image'] = './uploads/product/large/';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $image_dimension->product_image_large_width;
$config['height'] = $image_dimension->product_image_large_height;
$this->image_lib->initialize($config); //firstly autoload image_lib, then initialize it. Dont repeatly load it.
$this->image_lib->resize();
//image resizing (SMALL IMAGE)
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/product/' . $image;
$config['new_image'] = './uploads/product/small/';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $image_dimension->product_image_small_width;
$config['height'] = $image_dimension->product_image_small_height;
$this->image_lib->initialize($config); //firstly autoload image_lib, then initialize it. Dont repeatly load it.
$this->image_lib->resize();
//image resizing (THUMBNAIL)
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/product/' . $image;
$config['new_image'] = './uploads/product/thumbnail/';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $image_dimension->product_image_thumbnail_width;
$config['height'] = $image_dimension->product_image_thumbnail_height;
$this->image_lib->initialize($config); //firstly autoload image_lib, then initialize it. Dont repeatly load it.
$this->image_lib->resize();
}
/*new ajax by riantk*/
public function ajax_get_view_satuan_beli(){
$data['id_satuan_jual'] = $this->input->post('id_satuan_jual');
$data['conversion'] = $this->db->select('*')->from('conversion')->get()->result();
$this->load->view('admin/products/ajax_get_view_satuan_beli', $data);
}
/*new ajax by riantk*/
function changeStatusAct(){
$this_id = $this->input->post('this_id');
$toStat = $this->input->post('toStat');
$codeStat = null;
if ($toStat == "Ya") {
$codeStat = '1';
}else{
$codeStat = '0';
}
$data = array(
"product_status"=>$codeStat,
);
$upd = $this->db->update('products', $data, array('id_products' => $this_id));
if ($upd) {
echo json_encode(array(
"res"=>"sukses",
));
}
}
function refreshDisplayPriority(){
$this_data = $this->input->post('this_data');
foreach ($this_data as $key) {
$u_data = array(
'priority'=>$key['val'],
);
$this->db->update('products', $u_data, array('id_products' => $key['id']));
}
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Prioritas produk berhasil diubah</p>');
echo json_encode(array(
"res"=>"sukses",
// "data"=>$this_data,
));
}
}