|
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/iatax.com.au/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 $imageupload_indexpage = FALSE;
private $image1_filename = NULL;
private $catalog_filename = NULL;
function __construct() {
parent::__construct();
$this->load->model('product_m');
$this->load->library('image_lib');
$this->load->helper('form');
}
//this is to list all products
public function index() {
if(isset($_POST['landing_image'])) {
$this->imageupload_indexpage = TRUE;
//check & processing IMAGE
if ($_FILES['userfile']['size'] !== 0) {
$config['upload_path'] = './uploads/product/';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '500';
$config['max_width'] = '1600';
$config['max_height'] = '460';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$this->session->set_flashdata('banner_error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
//echo $this->upload->display_errors(); exit();
redirect('admin/products');
} else {
$image = $this->upload->data();
$image_filename = $image['file_name'];
}
}
//image upload
if (isset($image_filename)) {
$data['product_landingpage_image'] = $image_filename;
}
$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 Landing Page Edit Successful</p>');
}
//get landingpage banner image, description and link
$this->db->select('product_landingpage_image')->from('configuration')->where('id_configuration', 1);
$this->data['product_landingpage'] = $this->db->get()->row();
//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() {
$this->data['products'] = $this->product_m->get_new();
$this->data['new_product'] = TRUE; //this is to hide quantity discount on edit view.
//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 & processing IMAGE
if ($_FILES['image1']['size'] !== 0) {
$config['upload_path'] = './uploads/product/';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '500';
$config['max_width'] = '500';
$config['max_height'] = '500';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('image1')) {
$this->session->set_flashdata('image1-error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
//echo $this->upload->display_errors(); exit();
redirect('admin/products/add');
} else {
$image1 = $this->upload->data();
$this->image1_filename = $image1['file_name'];
}
}
//check & processing Catalog pdf
if ($_FILES['product_catalog']['size'] !== 0) {
$config['upload_path'] = './uploads/product/';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '20000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('product_catalog')) {
$this->session->set_flashdata('image1-error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">File Upload Error. Wrong format or size. Must be PDF file</p>');
//echo $this->upload->display_errors(); exit();
redirect('admin/products/add');
} else {
$catalog_filename = $this->upload->data();
$this->catalog_filename = $catalog_filename['file_name'];
}
}
$data = $this->table_data_processing(
$this->input->post('product_name'),
$this->input->post('product_code'),
$this->input->post('description'),
$this->input->post('description_en'),
$this->input->post('product_status'),
$this->image1_filename,
$this->catalog_filename,
$this->input->post('meta_description')
);
$product_id = (int) $this->product_m->add_product($data);
$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->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);
//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_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run($this) == TRUE) {
//check & processing IMAGE 1
if ($_FILES['image1']['size'] !== 0) {
$config['upload_path'] = './uploads/product/';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '500';
$config['max_width'] = '500';
$config['max_height'] = '500';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('image1')) {
$this->session->set_flashdata('image1-error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
redirect('admin/products/edit/' . $id);
} else {
$image1 = $this->upload->data();
$this->image1_filename = $image1['file_name'];
}
}
//check & processing Catalog pdf
if ($_FILES['product_catalog']['size'] !== 0) {
$config['upload_path'] = './uploads/product/';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '20000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('product_catalog')) {
$this->session->set_flashdata('image1-error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">File Upload Error. Wrong format or size. Must be PDF file</p>');
//echo $this->upload->display_errors(); exit();
redirect('admin/products/edit/' . $id);
} else {
$catalog_filename = $this->upload->data();
$this->catalog_filename = $catalog_filename['file_name'];
}
}
$data = $this->table_data_processing(
$this->input->post('product_name'),
$this->input->post('product_code'),
$this->input->post('description'),
$this->input->post('description_en'),
$this->input->post('product_status'),
$this->image1_filename,
$this->catalog_filename,
$this->input->post('meta_description')
);
$this->product_m->edit_product($id, $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) {
//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 < 1; $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(FCPATH . 'uploads/product/' . $image["image$i"])) {
unlink(FCPATH .'/uploads/product/'. $image["image$i"]);
}
if (file_exists(FCPATH . 'uploads/product/large/' . $image["image$i"])) {
unlink(FCPATH .'/uploads/product/large/'. $image["image$i"]);
}
if (file_exists(FCPATH . 'uploads/product/small/' . $image["image$i"])) {
unlink(FCPATH .'/uploads/product/small/'. $image["image$i"]);
}
if (file_exists(FCPATH . 'uploads/product/thumbnail/' . $image["image$i"])) {
unlink(FCPATH .'/uploads/product/thumbnail/'. $image["image$i"]);
}
}
}
$current_catalog_file = $this->db->select('product_catalog')->from('products')->where('id_products',$id)->get()->row();
if($current_catalog_file != null){
if (file_exists(FCPATH . 'uploads/product/' . $current_catalog_file->product_catalog)) {
unlink(FCPATH .'/uploads/product/'. $current_catalog_file->product_catalog);
}
}
$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);
}
//callback function validation add new product
public function _cek_existing_product_title($str) {
$num_rows = $this->product_m->cek_existing_product_title($str, $this->product_current_id);
if ($num_rows != 0 ) {
$this->form_validation->set_message('_cek_existing_product_title', 'Product name 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($product_name, $product_code, $description, $description_en, $product_status, $image1_filename, $catalog_filename, $meta_description) {
$data = array(
'title' => $this->security->xss_clean($product_name),
'product_code' => $this->security->xss_clean($product_code),
'alias' => url_title($this->security->xss_clean($product_name)),
'description' => $this->security->xss_clean($description),
'description_en' => $this->security->xss_clean($description_en),
'product_status' => $product_status,
'meta_description' => $this->security->xss_clean($meta_description)
);
//image upload
if (isset($image1_filename)) {
$data['image1'] = $image1_filename;
}
if (isset($catalog_filename)) {
$data['product_catalog'] = $catalog_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(FCPATH . 'uploads/product/' . $image->image1)) {
unlink(FCPATH .'/uploads/product/'. $image->image1);
}
if (file_exists(FCPATH . 'uploads/product/large/' . $image->image1)) {
unlink(FCPATH .'/uploads/product/large/'. $image->image1);
}
if (file_exists(FCPATH . 'uploads/product/small/' . $image->image1)) {
unlink(FCPATH .'/uploads/product/small/'. $image->image1);
}
if (file_exists(FCPATH . 'uploads/product/thumbnail/' . $image->image1)) {
unlink(FCPATH .'/uploads/product/thumbnail/'. $image->image1);
}
//Delete image field from database
$data = array(
'image1' => 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);
}
//To delete product image file from server, and from database
public function delete_product_catalog($id = NULL) {
if ($id == NULL) {
redirect('admin/products');
}
//get image file name for deletion
$this->db->select('product_catalog')->from('products')->where('id_products', (int) $id);
$product_catalog = $this->db->get()->row();
if($product_catalog == null){
redirect('admin/products');
}
if (file_exists(FCPATH . 'uploads/product/' . $product_catalog->product_catalog)) {
unlink(FCPATH .'/uploads/product/'. $product_catalog->product_catalog);
}
$data = array(
'product_catalog' => NULL,
);
$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;">Product Catalog Delete Successful</p>');
redirect('admin/products/edit/' . $id);
}
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 upload_product_photos() {
//upload photos
if(!isset($_POST['upload_photos'])) { show_404(); }
$config['upload_path'] = './uploads/product/';
$config['allowed_types'] = 'zip';
$config['max_size'] = '20000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
//$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">File Upload Error. Wrong format or size.</p>');
redirect('admin/products');
} else {
//EXTRACT ZIP FILE
$data = array('upload_data' => $this->upload->data());
$zip = new ZipArchive;
$file = $data['upload_data']['full_path'];
chmod($file,0777);
if ($zip->open($file) === TRUE) {
$zip->extractTo('./uploads/product/');
$zip->close();
} else {
$this->session->set_flashdata('error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">Sorry Product Photos Upload Fail</p>');
redirect('admin/products');
}
//delete zip files and other files besides jpg and png
//this is yet secured, need to delete unwanted files as well
unlink(FCPATH .'/uploads/product/'. $data['upload_data']['file_name']); //delete zip file
$this->session->set_flashdata('success', '<br>
<p style="background:green; color:white; padding:5px; font-weight:bold;">Product Photos Upload Success</p>');
redirect('admin/products');
}
}
public function delete_landingpage_image() {
//get image file name for deletion
$this->db->select('product_landingpage_image')->from('configuration')->where('id_configuration', 1);
$image = $this->db->get()->row()->product_landingpage_image;
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/product/'. $image);
//Delete image field from database
$data = array(
'product_landingpage_image' => '',
);
$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;">Image Delete Successful</p>');
redirect('admin/products');
}
}