|
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/rabbithabit.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;
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('product_attributes_m');
$this->load->helper('form');
}
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 {
//Add pagination
$this->load->helper('pagination_helper');
add_pagination(base_url() . 'admin/products/index', $this->product_m->record_count(), 100, 4);
$this->data['products'] = $this->product_m->get_all_products(100,$this->uri->segment(4));
$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 or EDIT a product content
function edit($id = NULL) {
if($id != NULL) {
$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 is an existing product
$this->data['new_product'] = 'no';
$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();
//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 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;
} else {
if (($this->data['membership_type'] == "starter" && $this->data['jml_produk'] >= 100) || ($this->data['membership_type'] == "business" && $this->data['jml_produk'] >= 500)) {
redirect('admin/products');
}
//id is null. this is a new product
$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();
/*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 SKU already exist in product details table
foreach($this->input->post('product_details') as $product_detail) {
if($id == NULL) {
$this->db->select('sku')->from('product_details')->where('sku', $product_detail['sku']);
} else {
$this->db->select('sku')->from('product_details')->where('product_id !=', $id)->where('sku', $product_detail['sku']);
}
$count_sku = $this->db->get()->num_rows();
if($count_sku > 0) {
//means sku already exist...must exit with error notification
$this->session->set_flashdata('sku_error', "<br><p style='background:red; color:white; padding:5px; font-weight:bold;'>Kode SKU {$product_detail['sku']} sudah ada. Gunakan SKU/Kode Produk unik lainnya.</p>");
if($id == NULL) {
redirect('admin/products/edit');
} else {
redirect('admin/products/edit/' . $id);
}
}
}
$data = $this->table_data_processing($id);
if($id == NULL) {
$product_id = (int) $this->product_m->add_product($data);
} else {
$this->product_m->edit_product($id, $data);
}
/*INSERT INTO SHIPMENT*/
$shipments_id = $this->input->post('shipment_id');
if (!in_array("3", $shipments_id)) {
$shipments_id[] = '3';//add regular shipping method
}
if($id != NULL) {
//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(
'shipment_method_id' => $shipment_id,
);
if($id != NULL) {
$data_shipment['product_id'] = $id;
} else {
$data_shipment['product_id'] = $product_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,
);
if($id != NULL) {
$this->db->where('id_products', $id);
} else {
$this->db->where('id_products', $product_id);
}
$this->db->update('products', $data_shipments);
/*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) {
if($id != NULL) {
//firstly, we delete all existing category records inside category_product table
$this->db->where('id_product', $id);
$this->db->delete('category_product');
}
foreach ($categories_id as $category_id) {
$data = array(
'id_category' => $category_id,
);
if($id != NULL) {
$data['id_product'] = $id;
} else {
$data['id_product'] = $product_id;
}
$this->db->insert('category_product', $data);
}
} else {
if($id != NULL) {
//firstly, we delete all existing category records inside category_product table
$this->db->where('id_product', $id);
$this->db->delete('category_product');
}
$data = array(
'id_category' => NULL,
);
if($id != NULL) {
$data['id_product'] = $id;
} else {
$data['id_product'] = $product_id;
}
$this->db->insert('category_product', $data);
}
//Varians and attributes
foreach ($this->input->post('product_details') as $product_detail) {
if (!empty($product_detail['sku'])) {
$data = array(
'sku' => $product_detail['sku'],
'price' => $product_detail['price'],
'discounted_price' => $product_detail['discounted_price'],
'length' => $product_detail['length'],
'width' => $product_detail['width'],
'height' => $product_detail['height'],
'is_indent' => $product_detail['is_indent'],
);
if($id != NULL) {
$data['product_id'] = $id;
} else {
$data['product_id'] = $product_id;
}
if(empty($product_detail['weight'])) {
$data['weight'] = 1;
} else {
$data['weight'] = $product_detail['weight'];
}
if($id != NULL) {
//check if sku with current product id already exist in table. If already exist, then update, else, insert new
$this->db->select('sku')->from('product_details')->where('sku', $product_detail['sku'])->where('product_id', $id);
$count_sku = $this->db->get()->num_rows();
} else {
$count_sku = 0;
}
if($count_sku > 0) {
//update product detail
$this->db->where('sku', $product_detail['sku']);
$this->db->where('product_id', $id);
$this->db->update('product_details', $data);
//get $product_details_id
$this->db->select('id')->from('product_details')->where('sku', $product_detail['sku'])->where('product_id', $id);
$product_details_id = $this->db->get()->row()->id;
} else {
//insert new product detail
$this->db->insert('product_details', $data);
$product_details_id = $this->db->insert_id();
}
//adit product_combination table
//first, delete combination table
$this->db->where('product_details_id', $product_details_id);
$this->db->delete('product_combination');
for($i = 0; $i < 20; $i++) {
if(!empty($product_detail['variant'][$i]) && !empty($product_detail['attribute'][$i])) {
$data = array(
'product_details_id' => $product_details_id,
'attribute_id' => $product_detail['variant'][$i],
'attribute_detail_id' => $product_detail['attribute'][$i],
);
if($id == NULL) {
$data['product_id'] = $product_id;
} else {
$data['product_id'] = $id;
}
$this->db->insert('product_combination', $data);
}
}
}
}
if($id != NULL) {
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Produk berhasil diedit</p>');
redirect('admin/products/edit/' . $id);
} else {
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Produk berhasil dibuat</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');
}
public function delete_all(){
$checkbox_for_del = $this->input->post('checkbox_del');
if (empty($checkbox_for_del)) {
redirect('admin/products');
}
for ($i=0; $i<count($checkbox_for_del) ; $i++) {
$id = $checkbox_for_del[$i];
$this->product_m->delete($id);
}
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Produk berhasil dihapus</p>');
echo json_encode(array(
'result'=>'sukses',
));
}
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;">Produk Priority berhasil diubah</p>');
echo json_encode(array(
"res"=>"sukses",
// "data"=>$this_data,
));
}
//to delete a product
function delete($id) {
//check if id exist. If not exist, show 404.
$count = $this->product_m->count_exist($id);
if ($count == 0) {
//page not exist
show_404();
}
$this->product_m->delete($id);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Produk berhasil dihapus</p>');
redirect('admin/products');
}
//to delete a product
function delete_product_detail($id_product, $id_product_detail) {
//check if id_product_detail exist. If not exist, show 404.
$this->db->select('id')->from('product_details')->where('id', $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', $id_product_detail);
$this->db->delete('product_details');
$this->db->where('product_id', $id_product);
$this->db->where('product_details_id', $id_product_detail);
$this->db->delete('product_combination');
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Produk Varian berhasil dihapus</p>');
redirect('admin/products/edit/' . $id_product);
}
function product_images($product_id = NULL) {
if($product_id == NULL) {redirect(base_url('admin/products'));}
//pagination in action. 100 results per page
$this->load->library('pagination');
$config['base_url'] = base_url() . 'admin/products/product_images';
$config['per_page'] = 100;
$config['uri_segment'] = 3;
//fetch all product images
$this->db->select('id')->from('product_images')->where('product_id', $product_id);
$config['total_rows'] = $this->db->get()->num_rows();
$this->pagination->initialize($config);
$this->db->select('*');
$this->db->from('product_images');
$this->db->where('product_id', $product_id);
$this->db->order_by('product_details_id', 'ASC');
$this->db->order_by('priority', 'ASC');
$this->db->limit($config['per_page'], $this->uri->segment($config['uri_segment']));
$this->data['product_images'] = $this->db->get()->result();
$this->data['product_id'] = $product_id;
//get product name
$this->db->select('title')->from('products')->where('id_products', $product_id);
$this->data['product_name'] = $this->db->get()->row()->title;
//load view
$this->data['subview'] = 'admin/products/product_images';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
}
function edit_product_image($product_id = NULL, $product_image_id = NULL, $case_addnew = false) {
if($product_id == NULL) {redirect(base_url('admin/products'));}
$case_save = $this->input->post('case_save');
if (isset($case_save)) {
if ($case_save == 'addnew') {
$config = array(
array(
'field' => 'title',
'label' => 'Banner Title',
'rules' => 'trim|required'
),
array(
'field' => 'title_en',
'label' => 'Banner Title English',
'rules' => 'trim|required'
),
array(
'field' => 'product_detail_id',
'label' => 'Varian',
'rules' => 'required'
),
array(
'field' => 'priority[]',
'label' => 'Priority',
'rules' => 'trim|required|numeric'
),
array(
'field' => 'status[]',
'label' => 'status',
'rules' => 'trim|required'
)
);
$this->load->library('form_validation');
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run() == TRUE){
$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();
$ar_status = $this->input->post('status[]');
$ar_priority = $this->input->post('priority[]');
$cpt = count($_FILES['image']['name']);
$config['upload_path'] = './uploads/product/';
$config['allowed_types'] = 'jpg|png|gif|jpeg';
$config['max_size'] = '600';
$config['max_width'] = $image_dimension->product_image_width;
$config['max_height'] = $image_dimension->product_image_height;
$this->load->library('upload', $config);
$files = $_FILES;
for($i=0; $i<$cpt; $i++)
{
if ($_FILES['image']['size'][$i] !== 0) {
$_FILES['image']['name']= $files['image']['name'][$i];
$_FILES['image']['type']= $files['image']['type'][$i];
$_FILES['image']['tmp_name']= $files['image']['tmp_name'][$i];
$_FILES['image']['error']= $files['image']['error'][$i];
$_FILES['image']['size']= $files['image']['size'][$i];
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('image')) {
// echo $this->upload->display_errors(); die();
$this->session->set_flashdata('image_error', '<br>
<p style="background:orange; color:white; padding:5px; font-weight:bold;">Image Upload Error. Salah format atau ukuran.</p>');
redirect('admin/products/edit_product_image/' . $product_id . '/' . $this->input->post('correct_product_details_id').'/addnew');
} else {
$image = $this->upload->data();
$image_filename[$i] = $image['file_name'];
}
$this->load->library('image_lib');
//image resizing (LARGE IMAGE)
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/product/' . $image_filename[$i];
$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);
$this->image_lib->resize();
//image resizing (SMALL IMAGE)
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/product/' . $image_filename[$i];
$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);
$this->image_lib->resize();
//image resizing (THUMBNAIL)
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/product/' . $image_filename[$i];
$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);
$this->image_lib->resize();
}
$data = array(
'product_id' => $product_id,
'product_details_id' =>
$this->input->post('correct_product_details_id'),
'title' =>
$this->security->xss_clean($this->input->post('title')),
'title_en' =>
$this->security->xss_clean($this->input->post('title_en')),
'priority' =>
$this->input->post('priority')[$i],
'status' =>
$this->input->post('status')[$i]
);
if (isset($image_filename[$i])) {
$data['image'] = $image_filename[$i];
}
$this->db->insert('product_images', $data);
}
redirect(base_url('admin/products/product_images/' . $product_id));
}
// else{
// // jika error
// // echo validation_errors();
// redirect('admin/products/edit_product_image/' . $product_id . '/' . $this->input->post('correct_product_details_id').'/addnew');
// }
}
// return false;
}
if($product_image_id == NULL) {
//create new photo
$product_image = new stdClass();
$product_image->title = '';
$product_image->title_en = '';
$product_image->image = '';
$product_image->priority = '';
$product_image->status = '';
$product_image->product_details_id = '';
$this->data['product_image'] = $product_image;
//get ordering number and display at add form
$this->db->select_max('priority')->from('product_images');
$current_priority = $this->db->get()->row()->priority;
if($current_priority == NULL) {
$this->data['product_image']->priority = 1;
} else {
$this->data['product_image']->priority = $current_priority + 1;
}
} else {
//edit current photo
$this->db->select('*')->from('product_images')->where('id', $product_image_id);
$this->data['product_image'] = $this->db->get()->row();
if(count($this->data['product_image']) == 0) {
if (empty($case_addnew) || $case_addnew != 'addnew') {
redirect(base_url('admin/products/product_images/' . $product_id));
}
}
}
//validation check in action
$config = array(
array(
'field' => 'title',
'label' => 'Banner Title',
'rules' => 'trim|required'
),
array(
'field' => 'product_detail_id',
'label' => 'Varian',
'rules' => 'required'
),
array(
'field' => 'priority',
'label' => 'Priority',
'rules' => 'trim|required|numeric'
),
array(
'field' => 'status',
'label' => 'status',
'rules' => 'trim|required'
)
);
$this->load->library('form_validation');
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run() == TRUE) {
$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();
if ($_FILES['image']['size'] !== 0) {
$config['upload_path'] = './uploads/product/';
$config['allowed_types'] = 'jpg|png|gif|jpeg';
$config['max_size'] = '600';
$config['max_width'] = $image_dimension->product_image_width;
$config['max_height'] = $image_dimension->product_image_height;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image')) {
//echo $this->upload->display_errors(); die();
$this->session->set_flashdata('image_error', '<br>
<p style="background:orange; color:white; padding:5px; font-weight:bold;">Image Upload Error. Salah format atau ukuran.</p>');
redirect('admin/products/edit_product_image/' . $product_id);
} else {
$image = $this->upload->data();
$image_filename = $image['file_name'];
}
$this->load->library('image_lib');
//image resizing (LARGE IMAGE)
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/product/' . $image_filename;
$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_filename;
$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_filename;
$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();
}
$data = array(
'product_id' => $product_id,
'product_details_id' => $this->input->post('product_detail_id'),
'title' => $this->security->xss_clean($this->input->post('title')),
'title_en' => $this->security->xss_clean($this->input->post('title_en')),
'priority' => $this->input->post('priority'),
'status' => $this->input->post('status')
);
if (isset($image_filename)) {
$data['image'] = $image_filename;
}
if($product_image_id == NULL) {
$this->db->insert('product_images', $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Foto produk berhasil ditambahkan</p>');
redirect('admin/products/product_images/' . $product_id);
} else {
$this->db->where('id', $product_image_id);
$this->db->update('product_images', $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Foto produk berhasil diedit</p>');
redirect('admin/products/edit_product_image/' . $product_id . '/' . $product_image_id);
}
}
$this->data['product_id'] = $product_id;
//get product name
$this->db->select('title')->from('products')->where('id_products', $product_id);
$this->data['product_name'] = $this->db->get()->row()->title;
//get product details
$this->db->select('id, product_id, sku')->from('product_details')->where('product_id', $product_id)->order_by('id', 'ASC');
$this->data['product_details'] = $this->db->get()->result();
if ($case_addnew == 'addnew') {
$this->data['case_addnew'] = $case_addnew;
}
$this->data['product_image_id'] = $product_image_id;
$this->data['subview'] = 'admin/products/edit_product_image';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
}
function delete_product_image($product_id = NULL, $product_image_id = NULL) {
if($product_id == NULL || $product_image_id == NULL) {redirect(base_url('admin/products'));}
//get image file name for deletion
$this->db->select('image')->from('product_images')->where('id', $product_image_id);
$image = $this->db->get()->row()->image;
if (file_exists(base_url() . 'uploads/product/' . $image)) {
unlink(FCPATH .'/uploads/product/'. $image);
}
if (file_exists(base_url() . 'uploads/product/large/' . $image)) {
unlink(FCPATH .'/uploads/product/large/'. $image);
}
if (file_exists(base_url() . 'uploads/product/small/' . $image)) {
unlink(FCPATH .'/uploads/product/small/'. $image);
}
if (file_exists(base_url() . 'uploads/product/thumbnail/' . $image)) {
unlink(FCPATH .'/uploads/product/thumbnail/'. $image);
}
$this->db->where('id', $product_image_id);
$this->db->delete('product_images');
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Foto produk berhasil dihapus</p>');
redirect('admin/products/product_images/' . $product_id);
}
//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($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($id) {
$active_product_link = $this->input->post('active_product_link');
if (empty($active_product_link)) {
$active_product_link = 'no';
}
$data = array(
'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')),
'additional_information' => $this->security->xss_clean($this->input->post('additional_information')),
'additional_information_en' => $this->security->xss_clean($this->input->post('additional_information_en')),
'ingredients' => $this->security->xss_clean($this->input->post('ingredients')),
'ingredients_en' => $this->security->xss_clean($this->input->post('ingredients_en')),
'link_video' => $this->security->xss_clean($this->input->post('link_video')),
'active_link_video' => $this->input->post('active_link_video'),
'brand_id' => (int) $this->input->post('brand_id'),
'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'),
'popular_product' => $this->input->post('popular_product'),
'meta_description' => $this->security->xss_clean($this->input->post('meta_description')),
'meta_title' => $this->security->xss_clean($this->input->post('meta_title')),
'indent_time' => $this->security->xss_clean($this->input->post('indent_time')),
'indent_dp' => $this->security->xss_clean($this->input->post('indent_dp')),
'product_link' => $this->security->xss_clean($this->input->post('product_link')),
'active_product_link' => $this->security->xss_clean($active_product_link),
);
$config['upload_path'] = './uploads/banners/';
$config['allowed_types'] = 'jpg|png|gif|jpeg';
// $config['max_size'] = '600';
// $config['max_width'] = '640';
// $config['max_height'] = '330';
$this->load->library('upload', $config);
if ($_FILES['banner_link_video']['size'] !== 0) {
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('banner_link_video')) {
// echo $this->upload->display_errors(); die();
$this->session->set_flashdata('error', '
<p style="background:orange; color:white; padding:5px; font-weight:bold;">Image Upload Error. Salah format atau ukuran.</p>');
redirect('admin/products/edit/' . $id);
} else {
$image = $this->upload->data();
$image_filename = $image['file_name'];
$data['banner_link_video'] = $image_filename;
}
}
return $data;
}
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->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');
}
}
function ajax_get_attributes() {
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$varian_id = $this->input->post('id_varian');
//get all attribute details
$this->db->select('id, attribute_detail')->from('product_attributes_detail')->where('product_attribute_id', $varian_id)->order_by('priority', 'ASC');
$data['attributes'] = $this->db->get()->result();
$this->load->view('admin/products/ajax_get_attributes', $data);
}
function upload_products() {
if(!$_POST) {
redirect('admin/products');
}
//ini hanya contoh saja, perlu di mofif sesuai excel yg baru...
$config['upload_path'] = 'uploads/excel/';
$config['allowed_types'] = 'xlsx|xls';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')){
$this->session->set_flashdata('error', '<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);
for ($i=1; $i < ($numRows+1) ; $i++) {
if ($i > 50) {
break;
}
if($i != 1){
$nama_product = $worksheet[$i]["A"];
$sku = $worksheet[$i]["B"];
$merek = $worksheet[$i]["C"];
$kategori = $worksheet[$i]["D"];
$harga_normal = $worksheet[$i]["E"];
$harga_diskon = $worksheet[$i]["F"];
$panjang_cm = $worksheet[$i]["G"];
$lebar_cm = $worksheet[$i]["H"];
$tinggi_cm = $worksheet[$i]["I"];
$berat_cm = $worksheet[$i]["J"];
$is_indent = $worksheet[$i]["K"];
$varian = $worksheet[$i]["L"];
$short_descr_id = $worksheet[$i]["M"];
$short_descr_en = $worksheet[$i]["N"];
$long_descr_id = $worksheet[$i]["O"];
$long_descr_en = $worksheet[$i]["P"];
$kurir_pengiriman = $worksheet[$i]["Q"];
$is_new_arrivals = $worksheet[$i]["R"];
$is_featured = $worksheet[$i]["S"];
$is_best_sellers = $worksheet[$i]["T"];
$seo_judul = $worksheet[$i]["U"];
$seo_descr_meta = $worksheet[$i]["V"];
$dp_produk_persen = $worksheet[$i]["W"];
$durasi_info_indent = $worksheet[$i]["X"];
$is_product_active = $worksheet[$i]["Y"];
// query product
$query_products = $this->db->get_where('products',array(
'title'=>$nama_product,
));
// query brands
$query_brands = $this->db->get_where('brands',array(
'brand'=>$merek,
));
$id_brands = null;
if ($query_brands->num_rows()>0) {
$id_brands = $query_brands->row()->id_brands;
}
// shipment method
$output_kurir_pengiriman = '';
$kurir_pengiriman = str_replace('.', ',', (string)$kurir_pengiriman);
$id_kurir_pengiriman = explode(',', $kurir_pengiriman);
// query shipment method
for ($j=0; $j<count($id_kurir_pengiriman); $j++) {
$query_shipment_method = $this->db->get_where('shipment_method',array(
'id'=>$id_kurir_pengiriman[$j],
));
if ($query_shipment_method->num_rows()>0) {
$output_kurir_pengiriman .= $query_shipment_method->row()->name.",";
}
}
$output_kurir_pengiriman = substr($output_kurir_pengiriman,0,-1);
// data categories
$data_group_categories = [];
$group_categories = explode(', ', $kategori);
$cntr_categories = -1;
for ($j=0; $j<count($group_categories); $j++) {
$each_categories = explode('>', $group_categories[$j]);
for ($k=0; $k<count($each_categories); $k++) {
$cntr_categories++;
$data_group_categories[$cntr_categories]=$each_categories[$k];
}
}
// data varian
$data_group_varians = [];
$group_varians = explode(', ', $varian);
for ($j=0; $j<count($group_varians); $j++) {
$data_group_varians[$j] = explode('>', $group_varians[$j]);
}
// status produk
if ($is_product_active == 'ya' || $is_product_active == 'yes') {
$is_product_active = 1;
}else{
$is_product_active = 0;
}
// best seller
if ($is_best_sellers == 'ya' || $is_best_sellers == 'yes') {
$is_best_sellers = 'yes';
}else{
$is_best_sellers = 'no';
}
// new arrivals
if ($is_new_arrivals == 'ya' || $is_new_arrivals == 'yes') {
$is_new_arrivals = 'yes';
}else{
$is_new_arrivals = 'no';
}
// featured
if ($is_featured == 'ya' || $is_featured == 'yes') {
$is_featured = 'yes';
}else{
$is_featured = 'no';
}
// is_indent
if ($is_indent == 'ya' || $is_indent == 'yes') {
$is_indent = 'yes';
}else{
$is_indent = 'no';
}
// data products
$data_products = array(
"brand_id"=>$id_brands,
"alias"=>preg_replace('/\s+/', '-', $nama_product),
"description"=>$short_descr_id,
"description_en"=>$short_descr_en,
"long_description"=>$long_descr_id,
"long_description_en"=>$long_descr_en,
"shipment_method"=>$output_kurir_pengiriman,
"best_seller"=>$is_best_sellers,
"new_arrival"=>$is_new_arrivals,
"popular_product"=>$is_featured,
"indent_dp"=>$dp_produk_persen,
"meta_title"=>$seo_judul,
"meta_description"=>$seo_descr_meta,
"indent_time"=>$durasi_info_indent,
"product_status"=>$is_product_active,
);
// data product details
$data_product_details = array(
"weight"=>$berat_cm,
"length"=>$panjang_cm,
"width"=>$lebar_cm,
"height"=>$tinggi_cm,
"price"=>$harga_normal,
"discounted_price"=>$harga_diskon,
"is_indent"=>$is_indent,
);
$id_product = null;
if ($query_products->num_rows()>0) {
// update products
$id_product = $query_products->row()->id_products;
$upd_products = $this->db->update("products",$data_products,array('title'=>$nama_product));
}else{
// insert products
$data_products['title']=$nama_product;
if ($data_products['title'] != null) {
$this->db->insert("products",$data_products);
$id_product = $this->db->insert_id();
}
}
// query product details
$query_product_details = $this->db->get_where(
'product_details',array(
'sku'=>$sku,
));
$id_product_details = null;
if ($query_product_details->num_rows()>0) {
// update product details
$id_product_details = $query_product_details->row()->id;
$upd_products = $this->db->update("product_details",$data_product_details,array(
'id'=>$id_product_details,
'sku'=>$sku,
));
}else{
// insert product details
$data_product_details['sku']=$sku;
$data_product_details['product_id']=$id_product;
if ($data_product_details['weight'] != null) {
$this->db->insert("product_details",$data_product_details);
$id_product_details = $this->db->insert_id();
}
}
if ($id_product != null) {
// delete categories by id product
if($i <= 2){
$del_categories = $this->db->delete('category_product',array('id_product'=>$id_product));
$del_shipment = $this->db->delete('shipment_method_product',array('product_id'=>$id_product));
}
for ($j=0; $j<count($id_kurir_pengiriman); $j++) {
// insert shipment product
$query_check_shipment_product =
$this->db->get_where('shipment_method_product',array(
"product_id"=>$id_product,
"shipment_method_id"=>$id_kurir_pengiriman[$j],
));
if ($query_check_shipment_product->num_rows() == 0) {
$ins_shipment_product = $this->db->insert('shipment_method_product',array(
"product_id"=>$id_product,
"shipment_method_id"=>$id_kurir_pengiriman[$j],
));
}
}
for ($j=0; $j<count($data_group_categories); $j++) {
$this_val_for_search = strtolower($data_group_categories[$j]);
$query_categories = $this->db->select('*')
->from('categories')
->where('category',$this_val_for_search)
->or_where('category_en',$this_val_for_search)
->or_where('alias',$this_val_for_search)
->or_where('alias_en',$this_val_for_search)
->get();
if ($query_categories->num_rows()>0) {
$id_categories = $query_categories->row()->id_categories;
// insert category product
$ins_category_product = $this->db->insert('category_product',array(
"id_product"=>$id_product,
"id_category"=>$id_categories,
));
}
}
}
if ($id_product != null && $id_product_details != null) {
// delete varians by id product
if($i <= 2){
$del_varians = $this->db->delete('product_combination',array(
'product_id'=>$id_product,
'product_details_id'=>$id_product_details,
));
}
for ($j=0; $j<count($data_group_varians); $j++) {
$key_product_attributes = strtolower($data_group_varians[$j][0]);
$key_product_attributes_detail = strtolower($data_group_varians[$j][1]);
$query_product_attributes = $this->db->select("*")
->from("product_attributes")
->where('product_attribute',$key_product_attributes)
->or_where('product_attribute_en',$key_product_attributes)->get();
if ($query_product_attributes->num_rows()>0) {
$attribute_id = $query_product_attributes->row()->id;
$query_product_detail_attributes = $this->db->select("*")
->from("product_attributes_detail")
->where('attribute_detail',$key_product_attributes_detail)
->or_where('attribute_detail_en',$key_product_attributes_detail)
->where('product_attribute_id',$attribute_id)->get();
if ($query_product_detail_attributes->num_rows()>0) {
$attribute_detail_id = $query_product_detail_attributes->row()->id;
$ins_p_combination = $this->db->insert('product_combination',array(
'product_id'=>$id_product,
'product_details_id'=>$id_product_details,
'attribute_id'=>$attribute_id,
'attribute_detail_id'=>$attribute_detail_id,
));
}
}
}
}
}
}
unlink('uploads/excel/'.$filename);
$this->session->set_flashdata('success', '<p style="background:green; color:white; padding:5px; font-weight:bold;">Upload produk Sukses</p>');
redirect('admin/products');
}
}
}