|
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 Categories extends Admin_Controller {
//this property is used for validating existing category title on call back edit category
protected $category_current_id;
function __construct() {
parent::__construct();
$this->load->model('category_m');
$this->load->model('product_m');
}
//this is to list all categories
public function index() {
//Add pagination
$this->load->helper('pagination_helper');
add_pagination(base_url() . 'admin/categories/index', $this->category_m->record_count(), 6, 4);
//get parent categories only
$this->data['parent_categories'] = $this->category_m->get_all_parent_categories(6, $this->uri->segment(4));
//load view
$this->data['subview'] = 'admin/categories/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 category in admin
public function edit($id = NULL) {
$this_case = $this->input->post('this_case');
if (isset($this_case)) {
if ($this_case == 'addcategory_in_product') {
$config = $this->category_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) == FALSE) {
echo json_encode(array(
'result'=>validation_errors(),
));
}
if($this->form_validation->run($this) == TRUE) {
$image_filename1 = $this->image_processing($_FILES['banner_image1']);
$image_filename2 = $this->image_processing($_FILES['banner_image2']);
$data = $this->table_data_processing($image_filename1,$image_filename2,$this_case);
$this->category_m->add_category($data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Kategori Produk berhasil dibuat</p>');
$categories = $this->category_m->get_new();
$parent_categories = $this->category_m->get_parent_categories();
//get ordering number and display at add form
$this->db->select_max('priority')
->from('categories')->where('parent', NULL);
$current_priority = $this->db->get()->row()->priority;
if($current_priority == NULL) {
$categories->priority = 1;
} else {
$categories->priority = $current_priority + 1;
}
$new_category = '';
foreach($parent_categories as $category) :
$new_category .= '<input style="position:relative; bottom:3px; margin-right: 10px;" type="checkbox" name="category_id[]" value="'.$category->id_categories.'"'.set_checkbox('category_id[]', $category->id_categories);
if (isset($chosen_categories)) :
foreach ($chosen_categories as $chosen_category) :
if ($chosen_category->id_category == $category->id_categories) :
$new_category .= ' checked ';
endif;
endforeach;
endif;
$new_category .= '>';
$new_category .= ucfirst($category->category).'<br>';
$this->db->select('id_categories')->from('categories')->where('parent', $category->id_categories);
$count_child = $this->db->get()->num_rows();
if ($count_child > 0) :
$this->db->select('*')->from('categories')->where('parent', $category->id_categories)->order_by('priority', 'ASC');
$child_categories = $this->db->get()->result();
foreach ($child_categories as $child) :
$new_category .= '-- <input style="position:relative; bottom:3px; margin-right: 10px; margin-left:10px;" type="checkbox" name="category_id[]" value="'.$child->id_categories.'"'.set_checkbox('category_id[]', $child->id_categories);
if (isset($chosen_categories)) :
foreach ($chosen_categories as $chosen_category) :
if ($chosen_category->id_category == $child->id_categories) :
$new_category .= ' checked ';
endif;
endforeach;
endif;
$new_category .= '>';
$new_category .= ucfirst($child->category).'<br>';
$this->db->select('id_categories')->from('categories')->where('parent', $child->id_categories);
$count_grandchild = $this->db->get()->num_rows();
if ($count_grandchild > 0) :
$this->db->select('*')->from('categories')->where('parent', $child->id_categories)->order_by('priority', 'ASC');
$grandchild_categories = $this->db->get()->result();
foreach ($grandchild_categories as $grandchild) :
$new_category .= '------- <input style="position:relative; bottom:3px; margin-right: 10px; margin-left:10px;" type="checkbox" name="category_id[]" value="'.$grandchild->id_categories.'"'.set_checkbox('category_id[]', $grandchild->id_categories);
if (isset($chosen_categories)) :
foreach ($chosen_categories as $chosen_category) :
if ($chosen_category->id_category == $grandchild->id_categories) :
$new_category .= ' checked ';
endif;
endforeach;
endif;
$new_category .= '>';
$new_category .= ucfirst($grandchild->category).'<br>';
endforeach;
endif;
endforeach;
endif;
endforeach;
$new_category_in_modal = '';
foreach($parent_categories as $parent) {
$new_category_in_modal .= '<option value="'.$parent->id_categories.'"';
if ($categories->parent == $parent->id_categories){
$new_category_in_modal .= 'selected="selected" ';
}
$new_category_in_modal .= ">".ucfirst($parent->category)."</option>";
$this->db->select('*')->from('categories')->where('parent', $parent->id_categories)->order_by('priority', 'ASC');
$child = $this->db->get()->result();
foreach ($child as $child_category) {
$new_category_in_modal .= '<option value="'.$child_category->id_categories.'"';
if ($categories->parent == $child_category->id_categories){
$new_category_in_modal .= 'selected="selected" ';
}
$new_category_in_modal .= ">-".ucfirst($child_category->category)."</option>";
}
}
echo json_encode(array(
'new_category'=>$new_category,
'new_category_in_modal'=>$new_category_in_modal,
'result'=>'sukses',
));
}
}
}else{
if ($id == NULL) {
$this->data['categories'] = $this->category_m->get_new();
$this->data['parent_categories'] = $this->category_m->get_parent_categories();
//get ordering number and display at add form
$this->db->select_max('priority')->from('categories')->where('parent', NULL);
$current_priority = $this->db->get()->row()->priority;
if($current_priority == NULL) {
$this->data['categories']->priority = 1;
} else {
$this->data['categories']->priority = $current_priority + 1;
}
} else {
//check if id exist. If not exist, redirect
$count = $this->category_m->count_exist($id);
if ($count == 0) { redirect(base_url('admin/categories/edit')); }
$this->data['categories'] = $this->category_m->get($id);
$this->data['parent_categories'] = $this->category_m->get_parent_categories();
$this->category_current_id = (int) $this->data['categories']->id_categories;
}
//validation check
$config = $this->category_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 banner upload files
if ($_FILES['banner_image1']['size'] !== 0) {
//get max image width and height from configuration table
$this->db->select('category_image_width, category_image_height')->from('configuration')->where('id_configuration', 1);
$image_dimension = $this->db->get()->row();
$config['upload_path'] = './uploads/category/';
$config['allowed_types'] = 'png|jpg|jpeg|gif';
$config['max_size'] = '500';
$config['max_width'] = $image_dimension->category_image_width;
$config['max_height'] = $image_dimension->category_image_height;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('banner_image1')) {
$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->category_current_id != NULL) {
redirect('admin/categories/edit/' . $this->category_current_id);
} else {
redirect('admin/categories/edit');
}
} else {
$image1 = $this->upload->data();
$image_filename1 = $image1['file_name'];
}
} else {
$image_filename1 = NULL;
}
if ($_FILES['banner_image2']['size'] !== 0) {
$config['upload_path'] = './uploads/category/';
$config['allowed_types'] = 'png|jpg|jpeg|gif';
$config['max_size'] = '500';
$config['max_width'] = '800';
$config['max_height'] = '500';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('banner_image2')) {
$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->category_current_id != NULL) {
redirect('admin/categories/edit/' . $this->category_current_id);
} else {
redirect('admin/categories/edit');
}
} else {
$image2 = $this->upload->data();
$image_filename2 = $image2['file_name'];
}
} else {
$image_filename2 = NULL;
}
$data = $this->table_data_processing($image_filename1, $image_filename2);
if($this->category_current_id == NULL) {
$this->category_m->add_category($data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Kategori Produk berhasil dibuat</p>');
redirect('admin/categories');
} else {
$this->category_m->edit_category($id, $data);
//change the menu if available
//check if category menu exist..
$this->db->select('id_menus')->from('menus')->where('category_id', $id);
$count_menu = $this->db->get()->num_rows();
if($count_menu > 0) {
//menu exist..so need to change the url path for the menu
$this->db->select('parent')->from('categories')->where('id_categories', $id);
$parent_id = $this->db->get()->row()->parent;
//check parent_id
if($parent_id === NULL) {
//this is level1 category
$menu_link = 'category/' . url_title($this->input->post('category_name'));
} elseif($parent_id !== NULL) {
$this->db->select('parent')->from('categories')->where('id_categories', $parent_id);
$parent2_id = $this->db->get()->row()->parent;
if($parent2_id === NULL) {
//this is level 2 category..
//get level 1 alias
$this->db->select('alias')->from('categories')->where('id_categories', $parent_id);
$alias_level1 = $this->db->get()->row()->alias;
//get level 2 alias
$this->db->select('alias')->from('categories')->where('id_categories', $id);
$alias_level2 = $this->db->get()->row()->alias;
$menu_link = 'category/' . $alias_level1 . '/' . url_title($this->input->post('category_name'));
} else {
//this is level 3 category..
//get level 2 alias
$this->db->select('alias')->from('categories')->where('id_categories', $parent_id);
$alias_level2 = $this->db->get()->row()->alias;
//get level 1 alias
$this->db->select('parent')->from('categories')->where('id_categories', $parent_id);
$level1_id = $this->db->get()->row()->parent;
$this->db->select('alias')->from('categories')->where('id_categories', $level1_id);
$alias_level1 = $this->db->get()->row()->alias;
$menu_link = 'category/' . $alias_level1 . '/' . $alias_level2 . '/' . url_title($this->input->post('category_name'));
}
}
//update menu link
$data = array(
'menu_link' => $menu_link
);
$this->db->where('category_id', $id);
$this->db->update('menus', $data);
}
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Kategori Produk berhasil diubah</p>');
redirect('admin/categories/edit/' . $id);
}
}
$this->data['subview'] = 'admin/categories/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 category
public function delete($id = NULL) {
if($id == NULL) redirect(base_url('admin/categories'));
//check if id exist.
$count = $this->category_m->count_exist($id);
if ($count == 0) { redirect(base_url('admin/categories')); }
//delete image from server
//check if there is an existing image
$this->db->select('image')->from('categories')->where('id_categories', (int) $id);
$image = $this->db->get()->row()->image;
if ($image != '' && $image != NULL) {
if(file_exists(FCPATH .'/uploads/category/' . $image)) {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/category/' . $image);
}
}
//check if there are child categories belong to this parent category. if yes, set the parent category to NULL
$this->db->select('id_categories')->from('categories')->where('parent', $id);
$child_categories = $this->db->get()->result();
if (count($child_categories) > 0) {
//child categories exist, then set their parent to NULL
foreach ($child_categories as $child_category) {
$data = array(
'parent' => NULL,
);
$this->db->where('id_categories', $child_category->id_categories);
$this->db->update('categories', $data);
}
}
//delete parent category
$this->category_m->delete($id);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Kategori Produk berhasil dihapus</p>');
redirect('admin/categories');
}
private function table_data_processing($image_filename1, $image_filename2, $this_case = false) {
$data = array(
'category' => $this->security->xss_clean($this->input->post('category_name')),
'category_en' => $this->security->xss_clean($this->input->post('category_name_en')),
'alias' => url_title($this->security->xss_clean($this->input->post('category_name'))),
'alias_en' => url_title($this->security->xss_clean($this->input->post('category_name_en'))),
'status' => $this->input->post('status'),
'priority' => $this->input->post('priority'),
'meta_title' => $this->security->xss_clean($this->input->post('meta_title')),
'meta_description' => $this->security->xss_clean($this->input->post('meta_description')),
'banner_link' => $this->security->xss_clean($this->input->post('banner_link')),
'updated_by' => $this->session->userdata('admin')['name'],
);
if($this_case != false && $this_case == 'addcategory_in_product'){
$data['description']=$this->security->xss_clean($this->input->post('description_category'));
$data['description_en']=$this->security->xss_clean($this->input->post('description_en_category'));
}else{
$data['description']=$this->security->xss_clean($this->input->post('description'));
$data['description_en']=$this->security->xss_clean($this->input->post('description_en'));
}
if ($this->input->post('parent_id') == 'no-parent') {
$data['parent'] = NULL;
} else {
$data['parent'] = (int) $this->input->post('parent_id');
}
//image upload
if (isset($image_filename1)) { $data['image'] = $image_filename1; }
if (isset($image_filename2)) { $data['image_mobile'] = $image_filename2; }
return $data;
}
//To delete category image file from server, and from database
public function delete_image($id = NULL) {
$count = $this->category_m->count_exist($id);
if ($id == NULL || $count == 0) {
redirect('admin/categories');
}
//get image file name for deletion
$this->db->select('image')->from('categories')->where('id_categories', (int) $id);
$image = $this->db->get()->row()->image;
if(file_exists(FCPATH.'/uploads/category/'.$image)) {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/category/'. $image);
}
//Delete image field from database
$data = array(
'image' => ''
);
$this->db->where('id_categories', (int) $id);
$this->db->update('categories', $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Gambar berhasil dihapus</p>');
redirect('admin/categories/edit/' . $id);
}
//callback function validation add new category
//make it private by adding _
public function _cek_existing_category_title($str) {
$num_rows = $this->category_m->cek_existing_category_title($str, $this->category_current_id);
if ($num_rows != 0 ) {
$this->form_validation->set_message('_cek_existing_category_title', 'category name already exist !');
return FALSE;
} else {
return TRUE;
}
}
public function ajax_get_ordering() {
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
if($this->input->post('id_parentcategory') == 'no-parent') {
//choose select option where value is no-parent at edit view
//get ordering number and display at add form
$this->db->select_max('priority')->from('categories')->where('parent', NULL);
$current_priority = $this->db->get()->row()->priority;
if($current_priority == NULL) {
$next_priority = 1;
} else {
$next_priority = $current_priority + 1;
}
} else {
$id_parentcategory = (int) $this->input->post('id_parentcategory');
//get ordering number and display at add form
$this->db->select_max('priority')->from('categories')->where('parent', $id_parentcategory);
$current_priority = $this->db->get()->row()->priority;
$next_priority = $current_priority + 1;
}
echo $next_priority;
}
public function delete_image_mobile($id = NULL) {
$count = $this->category_m->count_exist($id);
if ($id == NULL || $count == 0) {
redirect('admin/categories');
}
//get image file name for deletion
$this->db->select('image_mobile')->from('categories')->where('id_categories', (int) $id);
$image = $this->db->get()->row()->image;
if(file_exists(FCPATH.'/uploads/category/'.$image)) {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/category/'. $image);
}
//Delete image field from database
$data = array(
'image_mobile' => ''
);
$this->db->where('id_categories', (int) $id);
$this->db->update('categories', $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Gambar berhasil dihapus</p>');
redirect('admin/categories/edit/' . $id);
}
}