|
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 Brands extends Admin_Controller {
//this property is used for validating existing brand title on call back edit brand
private $brand_current_id = NULL;
function __construct() {
parent::__construct();
$this->load->model('brand_m');
$this->load->model('product_m');
}
public function delete_all(){
$checkbox_for_del = $this->input->post('checkbox_del');
if (empty($checkbox_for_del)) {
redirect('admin/brands');
}
for ($i=0; $i<count($checkbox_for_del) ; $i++) {
$id = $checkbox_for_del[$i];
//delete image from server
//check if there is an existing image
$this->db->select('image, logo')->from('brands')->where('id_brands', (int) $id);
$image = $this->db->get()->row();
$banner = $image->image;
$logo = $image->logo;
if ($banner != '' && $banner != NULL) {
if(file_exists(FCPATH .'/uploads/brand/' . $banner)) {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/brand/'. $banner);
}
}
if ($logo != '' && $logo != NULL) {
if(file_exists(FCPATH .'/uploads/brand/' . $logo)) {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/brand/'. $logo);
}
}
//delete brand
$this->brand_m->delete($id);
}
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Merek 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(
"status"=>$codeStat,
);
$upd = $this->db->update('brands', $data, array('id_brands' => $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('brands', $u_data, array('id_brands' => $key['id']));
}
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Prioritas merek berhasil diubah</p>');
echo json_encode(array(
"res"=>"sukses",
// "data"=>$this_data,
));
}
//this is to list all brands
public function index() {
//Add pagination
$this->load->helper('pagination_helper');
add_pagination(base_url() . 'admin/brands/index', $this->brand_m->record_count(), 6, 4);
//get all brands
$this->data['brands'] = $this->brand_m->get_all_brands(6, $this->uri->segment(4));
//load view
$this->data['subview'] = 'admin/brands/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 & edit brand in admin
public function edit($id = NULL) {
$this_case = $this->input->post('this_case');
if (isset($this_case)) {
if ($this_case == 'addmerk_in_product') {
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$config = $this->brand_m->rules;
// array_push($config,array(
// 'field' => 'description_en',
// 'label' => 'Description English',
// 'rules' => 'trim'
// ),array(
// 'field' => 'description',
// 'label' => 'Description Indonesia',
// 'rules' => 'trim'
// ));
$this->form_validation->set_rules($config);
if ($this->form_validation->run($this) == FALSE) {
echo json_encode(array('sukses'=>validation_errors()));
}
if($this->form_validation->run($this) == TRUE) {
$image_filename = $this->image_processing($_FILES['userfile'], 'banner');
$logo_filename = $this->image_processing($_FILES['userfile2'], 'logo');
$data = $this->table_data_processing($image_filename, $logo_filename,$this_case);
$this->brand_m->add_brand($data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Merek berhasil dibuat</p>');
$get_all_brands = $this->brand_m->get_brands();
// $output_opt = '<option value="" disabled selected>Pilih Merek...</option>';
$get_all_brands = $this->brand_m->get_brands();
// foreach ($get_all_brands as $key) {
// $output_opt .= '<option value="'.$key->id_brands.'">'.$key->brand.'</option>';
// }
echo json_encode(array(
// 'config'=>$config,
'sukses'=>'sukses',
'get_all_brands'=>$get_all_brands,
));
}
}
}else{
if ($id == NULL) {
//create new brand
$this->data['brands'] = $this->brand_m->get_new();
//get ordering number and display at add form
$this->db->select_max('priority')->from('brands');
$current_priority = $this->db->get()->row()->priority;
if($current_priority == NULL) {
$this->data['brands']->priority = 1;
} else {
$this->data['brands']->priority = $current_priority + 1;
}
} else {
//check if id exist. If not exist, redirect to add new
$count = $this->brand_m->count_exist($id);
if ($count == 0) { redirect(base_url('admin/brands/edit')); }
$this->data['brands'] = $this->brand_m->get($id);
$this->brand_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->brand_m->rules;
$this->form_validation->set_rules($config);
if($this->form_validation->run($this) == TRUE) {
$image_filename = $this->image_processing($_FILES['userfile'], 'banner');
$logo_filename = $this->image_processing($_FILES['userfile2'], 'logo');
$data = $this->table_data_processing($image_filename, $logo_filename);
if($this->brand_current_id == NULL) {
$this->brand_m->add_brand($data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Merek berhasil dibuat</p>');
redirect('admin/brands');
} else {
$this->brand_m->edit_brand($id, $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Merek berhasil diedit</p>');
redirect('admin/brands/edit/' . $id);
}
}
$this->data['subview'] = 'admin/brands/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 brand
public function delete($id = NULL) {
if($id == NULL) redirect(base_url('admin/brands'));
//check if id exist.
$count = $this->brand_m->count_exist($id);
if ($count == 0) { redirect(base_url('admin/brands')); }
//delete image from server
//check if there is an existing image
$this->db->select('image, logo')->from('brands')->where('id_brands', (int) $id);
$image = $this->db->get()->row();
$banner = $image->image;
$logo = $image->logo;
if ($banner != '' && $banner != NULL) {
if(file_exists(FCPATH .'/uploads/brand/' . $banner)) {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/brand/'. $banner);
}
}
if ($logo != '' && $logo != NULL) {
if(file_exists(FCPATH .'/uploads/brand/' . $logo)) {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/brand/'. $logo);
}
}
//delete brand
$this->brand_m->delete($id);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Brand berhasil dihapus</p>');
redirect('admin/brands');
}
//image upload processing
private function image_processing($image_file, $image_type) {
if($image_type == 'banner') {
$banner_input_name = 'userfile';
$max_size = '500';
//get max image width and height from configuration table
$this->db->select('brand_image_width, brand_image_height')->from('configuration')->where('id_configuration', 1);
$image_dimension = $this->db->get()->row();
$max_width = $image_dimension->brand_image_width;
$max_height = $image_dimension->brand_image_height;
} elseif($image_type == 'logo') {
$banner_input_name = 'userfile2';
$max_size = '200';
//get max logo width and height from configuration table
$this->db->select('brand_logo_width, brand_logo_height')->from('configuration')->where('id_configuration', 1);
$image_dimension = $this->db->get()->row();
$max_width = $image_dimension->brand_logo_width;
$max_height = $image_dimension->brand_logo_height;
}
//check & processing image banner upload files
if ($image_file['size'] > 0) {
$config['upload_path'] = './uploads/brand/';
$config['allowed_types'] = 'png|jpg|jpeg|gif';
$config['max_size'] = $max_size;
$config['max_width'] = $max_width;
$config['max_height'] = $max_height;
$this->load->library('upload', $config);
if (!$this->upload->do_upload($banner_input_name)) {
$error = array('error' => $this->upload->display_errors());
$error_message = $error['error'];
$this->session->set_flashdata('success', "<div style='background:red; color:white; padding:5px; font-weight:bold;'>$error_message</div>");
if ($this->brand_current_id != NULL) {
redirect('admin/brands/edit/' . $this->brand_current_id);
} elseif($this->brand_current_id == NULL) {
redirect('admin/brands/edit');
}
} else {
$image = $this->upload->data();
return $image['file_name'];
}
}
}
private function table_data_processing($image_filename, $logo_filename, $this_case = false) {
$data = array(
'brand' => $this->security->xss_clean($this->input->post('brand_name')),
'alias' => url_title($this->security->xss_clean($this->input->post('brand_name'))),
'status' => $this->input->post('status'),
'priority' => $this->input->post('priority'),
'meta_description' => $this->security->xss_clean($this->input->post('meta_description')),
'meta_title' => $this->security->xss_clean($this->input->post('meta_title')),
'banner_link' => $this->security->xss_clean($this->input->post('banner_link')),
'updated_by' => $this->session->userdata('admin')['name'],
);
$data['description'] = $this->security->xss_clean($this->input->post('description'));
$data['description_en'] = $this->security->xss_clean($this->input->post('description_en'));
if($this_case == 'addmerk_in_product' && $this_case != false ){
$data['description']=$this->security->xss_clean($this->input->post('description_merk'));
$data['description_en']=$this->security->xss_clean($this->input->post('description_en_merk'));
}
//image upload
if (isset($image_filename)) { $data['image'] = $image_filename; }
//logo upload
if (isset($logo_filename)) { $data['logo'] = $logo_filename; }
return $data;
}
//To delete brand banner image file from server, and from database
public function delete_image($id = NULL, $image_type) {
$count = $this->brand_m->count_exist($id);
if ($id == NULL || $count == 0) {redirect('admin/brands');}
if($image_type == 'banner') {
//if image type is banner
//get image file name for deletion
$this->db->select('image')->from('brands')->where('id_brands', (int) $id);
$image = $this->db->get()->row();
if(file_exists(FCPATH.'/uploads/brand/'.$image->image)) {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/brand/'. $image->image);
}
//Delete image field from database
$data = array(
'image' => ''
);
$this->db->where('id_brands', (int) $id);
$this->db->update('brands', $data);
} else {
//if image type is logo
//get image file name for deletion
$this->db->select('logo')->from('brands')->where('id_brands', (int) $id);
$image = $this->db->get()->row();
if(file_exists(FCPATH.'/uploads/brand/'.$image->logo)) {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/brand/'. $image->logo);
}
//Delete logo field from database
$data = array(
'logo' => ''
);
$this->db->where('id_brands', (int) $id);
$this->db->update('brands', $data);
}
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Gambar berhasil dihapus</p>');
redirect('admin/brands/edit/' . $id);
}
//callback function validation add new brand
//make it private by adding _
public function _cek_existing_brand_title($str) {
$num_rows = $this->brand_m->cek_existing_brand_title($str, $this->brand_current_id);
if ($num_rows != 0 ) {
$this->form_validation->set_message('_cek_existing_brand_title', 'Nama Brand sudah terdaftar');
return FALSE;
} else {
return TRUE;
}
}
}