|
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 Product_attributes extends Admin_Controller {
//this property is used for validating existing size title on call back edit size
protected $attribute_current_id;
protected $attribute_detail_current_id;
function __construct() {
parent::__construct();
$this->load->model('product_attributes_m');
}
function change_variant_color(){
$t_id = $this->input->post('t_id');
$q_product_attributes = $this->db->get_where("product_attributes",array(
"id"=>$t_id,
// "is_color"=>"yes",
));
// $this->update("product_attributes",array(
// "is_color"=>"no"
// ));
// $this->db->query("UPDATE `product_attributes` SET is_color='no' ");
$t_val = 'no';
if ($q_product_attributes->row()->is_color == 'no') {
$t_val = 'yes';
}
$this->db->update("product_attributes",array(
"is_color"=>$t_val
),array(
"id"=>$t_id,
));
echo json_encode(array(
'res'=>'success'
));
}
//this is to list all brands
function index() {
//Add pagination
$this->load->helper('pagination_helper');
add_pagination(base_url() . 'admin/product_attributes/index', $this->product_attributes_m->record_count(), 6, 4);
//get all attributes
$this->data['attributes'] = $this->product_attributes_m->get_all_attributes(6, $this->uri->segment(4));
//load view
$this->data['subview'] = 'admin/product_attributes/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 edit size in admin
function edit($id = NULL) {
if ($id == NULL) {
//create new attribute
$this->data['attribute'] = $this->product_attributes_m->get_new();
//get ordering number
$this->db->select_max('priority')->from('product_attributes');
$current_priority = $this->db->get()->row()->priority;
if($current_priority == NULL) {
$this->data['attribute']->priority = 1;
} else {
$this->data['attribute']->priority = $current_priority + 1;
}
} else {
//check if id exist. If not exist, redirect to add new
$count = $this->product_attributes_m->count_exist($id);
if ($count == 0) { redirect(base_url('admin/product_attributes/edit')); }
$this->data['attribute'] = $this->product_attributes_m->get($id);
$this->attribute_current_id = (int) $id;
}
//validation check
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); //add class to form validation error, to be styled
$config = $this->product_attributes_m->rules;
$this->form_validation->set_rules($config);
if($this->form_validation->run($this) == TRUE) {
$data = $this->table_data_processing();
if($this->attribute_current_id == NULL) {
$this->product_attributes_m->add_attribute($data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Atribut berhasil dibuat</p>');
redirect('admin/product_attributes');
} else {
$this->product_attributes_m->edit_attribute($id, $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Atribut berhasil diedit</p>');
redirect('admin/product_attributes/edit/' . $id);
}
}
$check_is_color = $this->db->get_where("product_attributes",array(
"is_color"=>"yes"
));
$this->data['check_is_color'] = $check_is_color->num_rows();
$this->data['id_check_is_color'] = null;
if (isset($check_is_color->row()->id)) {
$this->data['id_check_is_color'] = $check_is_color->row()->id;
}
$this->data['subview'] = 'admin/product_attributes/edit';
$this->load->view('admin/templates/header', $this->data_header);
$this->load->view('admin/_layout_main', $this->data);
$this->load->view('admin/templates/footer');
}
//callback function validation add new size
function _cek_existing_attribute($str) {
$num_rows = $this->product_attributes_m->cek_existing_attribute($str, $this->attribute_current_id);
if ($num_rows != 0 ) {
$this->form_validation->set_message('_cek_existing_attribute', 'Nama Atribut Indonesia sudah terdaftar !');
return FALSE;
} else {
return TRUE;
}
}
//callback function validation add new size
function _cek_existing_attribute_en($str) {
if(empty($str)) {
$this->form_validation->set_message('_cek_existing_attribute_en', 'Nama Atribut Inggris tidak boleh kosong');
return FALSE;
}
$num_rows = $this->product_attributes_m->cek_existing_attribute_en($str, $this->attribute_current_id);
if ($num_rows != 0 ) {
$this->form_validation->set_message('_cek_existing_attribute_en', 'Nama Atribut Inggris sudah terdaftar !');
return FALSE;
} else {
return TRUE;
}
}
private function table_data_processing() {
$data = array(
'product_attribute' => $this->security->xss_clean($this->input->post('attribute')),
'product_attribute_en' => $this->security->xss_clean($this->input->post('attribute_en')),
'is_color' => $this->security->xss_clean($this->input->post('is_color')),
'priority' => $this->security->xss_clean($this->input->post('priority'))
);
return $data;
}
//to delete a size
function delete($id) {
if($id == NULL) redirect(base_url('admin/product_attributes'));
//check if id exist.
$count = $this->product_attributes_m->count_exist($id);
if ($count == 0) { redirect(base_url('admin/product_attributes')); }
$this->product_attributes_m->delete($id);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Atribut berhasil dihapus</p>');
redirect('admin/product_attributes');
}
/*-----ATTRIBUTES DETAIL----------*/
function view_detail($attribute_id = NULL) {
if($attribute_id == NULL) redirect(base_url('admin/product_attributes'));
//check if id exist.
$count = $this->product_attributes_m->count_exist($attribute_id);
if ($count == 0) { redirect(base_url('admin/product_attributes')); }
//Add pagination
$this->load->helper('pagination_helper');
add_pagination(base_url() . 'admin/product_attributes/view_detail', $this->product_attributes_m->record_count_detail($attribute_id), 6, 4);
$this->data['details'] = $this->product_attributes_m->get_all_attribute_details($attribute_id);
//pass to view, attribute name and id
$this->data['attribute_id'] = $attribute_id;
$this->db->select('product_attribute')->from('product_attributes')->where('id', $attribute_id);
$this->data['attribute_name'] = $this->db->get()->row()->product_attribute;
//load view
$this->data['subview'] = 'admin/product_attributes/detail_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 edit_detail($attribute_id = NULL, $detail_id = NULL) {
if($attribute_id == NULL && $detail_id == NULL) redirect(base_url('admin/product_attributes'));
if($attribute_id != NULL && $detail_id == NULL) {
//add new detail
$count = $this->product_attributes_m->count_exist($attribute_id);
if ($count == 0) { redirect(base_url('admin/product_attributes')); }
$this->data['detail'] = $this->product_attributes_m->get_new_detail();
$this->data['detail']->product_attribute_id = $attribute_id;
//get ordering number
$this->db->select_max('priority')->from('product_attributes_detail');
$current_priority = $this->db->get()->row()->priority;
if($current_priority == NULL) {
$this->data['detail']->priority = 1;
} else {
$this->data['detail']->priority = $current_priority + 1;
}
} elseif($attribute_id != NULL && $detail_id != NULL) {
//edit current detail
$count_attribute = $this->product_attributes_m->count_exist($attribute_id);
if ($count_attribute == 0) { redirect(base_url('admin/product_attributes')); }
$count_detail = $this->product_attributes_m->count_exist_detail($attribute_id, $detail_id);
if ($count_detail == 0) { redirect(base_url('admin/product_attributes/view_detail/' . $attribute_id)); }
//get attributes
$this->db->select('*');
$this->db->from('product_attributes_detail');
$this->db->where('id', $detail_id);
$this->data['detail'] = $this->db->get()->row();
$this->attribute_detail_current_id = $detail_id;
}
//validation check
$config = $this->product_attributes_m->detail_rules;
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run($this) == TRUE) {
if (isset($_FILES['image1']) && $_FILES['image1']['size'] !== 0) {
$config['upload_path'] = './uploads/varian/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '200';
$config['max_height'] = '200';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image1')) {
//echo $this->upload->display_errors(); die();
$this->session->set_flashdata('error', '<br>
<p style="background:orange; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
if($this->attribute_detail_current_id == NULL) {
redirect('admin/product_attributes/edit_detail/' . $attribute_id);
} else {
redirect('admin/product_attributes/edit_detail/'. $attribute_id . '/' . $detail_id);
}
} else {
$image1 = $this->upload->data();
$image1_filename = $image1['file_name'];
}
}
$data = array(
'attribute_detail' => $this->security->xss_clean($this->input->post('attribute_detail')),
'attribute_detail_en' => $this->security->xss_clean($this->input->post('attribute_detail_en')),
'product_attribute_id' => $attribute_id,
'priority' => $this->security->xss_clean($this->input->post('priority'))
);
$query_attribute = $this->db->get_where('product_attributes',array('id'=>$this->data['detail']->product_attribute_id));
$is_color = 'no';
if ($query_attribute->num_rows()>0) {
$is_color = $query_attribute->row()->is_color;
}
if ($is_color == 'yes') {
$data['color_hex'] = $this->security->xss_clean($this->input->post('color_hex'));
}
if (isset($image1_filename)) {
$data['image'] = $image1_filename;
}
if($this->attribute_detail_current_id == NULL) {
$this->product_attributes_m->add_attribute_detail($data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Atribut Detil berhasil dibuat</p>');
redirect('admin/product_attributes/view_detail/' . $attribute_id);
} else {
$this->product_attributes_m->edit_attribute_detail($detail_id, $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Atribut Detil berhasil diubah</p>');
redirect('admin/product_attributes/view_detail/' . $attribute_id);
}
}
$this->data['attribute_id'] = $attribute_id;
$this->db->select('product_attribute')->from('product_attributes')->where('id', $attribute_id);
$this->data['product_attribute'] = $this->db->get()->row()->product_attribute;
$this->data['subview'] = 'admin/product_attributes/detail_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 _cek_existing_detail_title($str) {
$num_rows = $this->product_attributes_m->cek_existing_detail_title($str, $this->attribute_detail_current_id);
if ($num_rows != 0 ) {
$this->form_validation->set_message('_cek_existing_detail_title', 'Detil Atribut Indonesia sudah terdaftar !');
return FALSE;
} else {
return TRUE;
}
}
public function _cek_existing_detail_title_en($str) {
if(empty($str)) {
$this->form_validation->set_message('_cek_existing_detail_title_en', 'Detail Atribut Inggris tidak boleh kosong');
return FALSE;
}
$num_rows = $this->product_attributes_m->cek_existing_detail_title_en($str, $this->attribute_detail_current_id);
if ($num_rows != 0 ) {
$this->form_validation->set_message('_cek_existing_detail_title_en', 'Detil Atribut Inggris sudah terdaftar !');
return FALSE;
} else {
return TRUE;
}
}
public function delete_detail($detail_id) {
//get attribute id
$this->db->select('product_attribute_id')->from('product_attributes_detail')->where('id', $detail_id);
$attribute_id = $this->db->get()->row()->product_attribute_id;
//delete detail
$this->db->where('id', $detail_id);
$this->db->delete('product_attributes_detail');
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Detil Atribut berhasil dihapus</p>');
redirect('admin/product_attributes/view_detail/' . $attribute_id);
}
}