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/serbaantik.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; //this property is to mark whether we are uploading image in the index page private $imageupload_indexpage = FALSE; function __construct() { parent::__construct(); if (!in_array('product categories', $this->allowed_sections)) redirect('admin/dashboard'); $this->load->model('category_m'); $this->load->model('product_m'); } //this is to list all categories public function index() { $this->load->helper('form'); if (isset($_POST['submit'])) { $this->imageupload_indexpage = TRUE; //if there is a form submit banner image description at landing page $image_filename = $this->image_processing($_FILES['userfile']['size']); //store image information to configuration table $data = array( 'category_landingpage_link' => $this->security->xss_clean($this->input->post('banner_link')), 'category_landingpage_description' => $this->security->xss_clean($this->input->post('description')), 'category_landingpage_description_en' => $this->security->xss_clean($this->input->post('description')), ); //image upload if (isset($image_filename)) { $data['category_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;">Category Landing Page Edit Successful</p>'); } //get landingpage banner image, description and link $this->db->select('category_landingpage_description, category_landingpage_description_en, category_landingpage_image, category_landingpage_link')->from('configuration')->where('id_configuration', 1); $this->data['category_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/categories/index'; $config['per_page'] = 100; $config['uri_segment'] = 4; //fetch all categories $config['total_rows'] = $this->category_m->record_count(); $this->pagination->initialize($config); //get parent categories only $this->data['parent_categories'] = $this->category_m->get_all_parent_categories($config["per_page"], $this->uri->segment($config['uri_segment'])); //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 a new category public function add() { $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; } //validation in action //validation check in action $config = $this->category_m->rules; $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); $this->form_validation->set_rules($config); if($this->form_validation->run($this) == TRUE) { $data = $this->table_data_processing(); $image_filename = $this->upload_files('image'); $image_banner_filename = $this->upload_files('image_banner'); $catalog_filename = $this->upload_files('catalog'); if (isset($image_filename)) { $data['image'] = $image_filename; } if (isset($image_banner_filename)) { $data['image_banner'] = $image_banner_filename; } if (isset($catalog_filename)) { $data['catalog'] = $catalog_filename; } $this->category_m->add_category($data); $this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Category Add Successful</p>'); redirect('admin/categories'); } $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 edit category in admin public function edit($id = NULL) { //check if id exist. If not exist, show 404. $count = $this->category_m->count_exist($id); if ($count == 0) { show_404(); } $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 in action $config = $this->category_m->rules; $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); $this->form_validation->set_rules($config); if($this->form_validation->run($this) == TRUE) { $data = $this->table_data_processing(); $image_filename = $this->upload_files('image'); $image_banner_filename = $this->upload_files('image_banner'); $catalog_filename = $this->upload_files('catalog'); if (isset($image_filename)) { $data['image'] = $image_filename; } if (isset($image_banner_filename)) { $data['image_banner'] = $image_banner_filename; } if (isset($catalog_filename)) { $data['catalog'] = $catalog_filename; } $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;">Category Edit Successful</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) { //check if id exist. If not exist, show 404. $count = $this->category_m->count_exist($id); if ($count == 0) { //page not exist show_404(); } //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(); if ($image->image != '' || $image->image != NULL) { //Delete the actual image file from server. FCPATH is codeigniter base path unlink(FCPATH .'/uploads/category/'. $image->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); $count_child_categories = $this->db->get()->num_rows(); if ($count_child_categories > 0) { //child categories exist, then set their parent to NULL $this->db->select('id_categories')->from('categories')->where('parent', $id); $child_categories = $this->db->get()->result(); 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;">Category Delete Successful</p>'); redirect('admin/categories'); } //image upload processing private function image_processing($image_file_size) { //check & processing image banner upload files if ($image_file_size !== 0) { $config = array(); $config['upload_path'] = './uploads/category/'; $config['allowed_types'] = '*'; // $config['allowed_types'] = 'jpg|png|jpeg|webp'; $config['max_size'] = '500'; $this->load->library('upload', $config); $this->upload->initialize($config); if ( ! $this->upload->do_upload()) { //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->category_current_id != NULL) { redirect('admin/categories/edit/' . $this->category_current_id); } else { redirect('admin/categories/add'); } } else { $image = $this->upload->data(); $image_filename = $image['file_name']; return $image_filename; } } } private function table_data_processing() { $data = array( 'category' => $this->security->xss_clean($this->input->post('category_name')), 'alias' => url_title($this->security->xss_clean($this->input->post('category_name'))), 'status' => $this->input->post('status'), 'priority' => $this->input->post('priority'), 'min_show_qty' => $this->input->post('min_show_qty'), 'can_download_catalog' => $this->input->post('can_download_catalog'), 'can_request_sample' => $this->input->post('can_request_sample'), ); if ($this->input->post('parent_id') == 'no-parent') { $data['parent'] = NULL; } else { $data['parent'] = (int) $this->input->post('parent_id'); } return $data; } //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; } } //To delete category landingpage banner image file from server, and from database public function delete_landingpage_image() { //get image file name for deletion $this->db->select('category_landingpage_image')->from('configuration')->where('id_configuration', 1); $image = $this->db->get()->row()->category_landingpage_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( 'category_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/categories'); } //To delete category image file from server, and from database public function delete_image($id = NULL, $type) { $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(); //Delete the actual image file from server. FCPATH is codeigniter base path unlink(FCPATH .'/uploads/category/'. $image->image); //Delete image field from database if($type == 'image') { $data = array( 'image' => '', ); } elseif($type == 'image_banner') { $data = array( 'image_banner' => '', ); } else { $data = array( 'catalog' => '', ); } $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;">Deletion Successful</p>'); redirect('admin/categories/edit/' . $id); } 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; } private function upload_files($file_name, $id = null) { if ($_FILES[$file_name]['size'] !== 0) { $config = array(); $config['upload_path'] = './uploads/categories/'; if($file_name == 'image' || $file_name == 'image_banner') { $config['allowed_types'] = '*'; // $config['allowed_types'] = 'jpg|png|jpeg|webp'; } elseif($file_name == 'catalog') { $config['allowed_types'] = 'pdf'; } //$config['max_size'] = '800'; $this->load->library('upload', $config); $this->upload->initialize($config); if (!$this->upload->do_upload($file_name)) { echo $this->upload->display_errors(); die(); if($id) { $this->session->set_flashdata('error', '<br> <p style="background:orange; color:white; padding:5px; font-weight:bold;">Kesalahan Unggah Gambar. Format atau ukuran salah.</p>'); redirect('admin/categories/edit/' . $id); } else { $this->session->set_flashdata('error-' . $file_name, '<br> <p style="background:orange; color:white; padding:5px; font-weight:bold;">Kesalahan Unggah Gambar. Format atau ukuran salah.</p>'); redirect('admin/categories/add'); } } else { $image = $this->upload->data(); return $image['file_name']; } } } //Varians public function varians($category_id = null) { if ($category_id == null) { redirect('admin/categories'); } $category = $this->db->select('category, id_categories')->from('categories')->where('id_categories', $category_id)->get()->row_array(); if($category === null) { redirect('admin/categories'); } $this->data['category_id'] = $category['id_categories']; $this->data['category'] = $category['category']; $this->data['varians'] = $this->db->select('id, varian, type, display')->from('varians')->where('display', 1)->where('category_id', $category_id)->order_by('priority', 'ASC')->get()->result(); //load view $this->data['subview'] = 'admin/categories/varians_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 varians_edit($category_id = null, $id = null) { if ($category_id === null) { redirect('admin/categories'); } if($id === null) { $varian = new stdClass(); $varian->id = ''; $varian->varian = ''; $varian->category_id = ''; $varian->type = ''; $varian->display = 1; $this->data['varian'] = $varian; //get ordering number and display at add form $this->db->select_max('priority')->from('varians'); $current_priority = $this->db->get()->row()->priority; if($current_priority === null) { $this->data['varian']->priority = 1; } else { $this->data['varian']->priority = $current_priority + 1; } } else { $this->data['varian'] = $this->db->select('*')->from('varians')->where('id', $id)->get()->row(); if($this->data['varian'] === null) { redirect('admin/categories'); } } //validation check in action $rules = array( array( 'field' => 'varian', 'label' => 'varian', 'rules' => 'trim|required', ), array( 'field' => 'category_id', 'label' => 'category_id', 'rules' => 'trim|required', ), array( 'field' => 'type', 'label' => 'type', 'rules' => 'trim|required', ), array( 'field' => 'display', 'label' => 'display', 'rules' => 'trim|required' ), array( 'field' => 'priority', 'label' => 'priority', 'rules' => 'trim|required|numeric' ), ); $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); $this->form_validation->set_rules($rules); if($this->form_validation->run($this) == TRUE) { $data = array( 'varian' => $this->security->xss_clean($this->input->post('varian')), 'category_id' => $category_id, 'type' => $this->security->xss_clean($this->input->post('type')), 'display' => $this->input->post('display'), 'priority' => $this->input->post('priority'), ); if($id) { $this->db->where('id', $id); $this->db->update('varians', $data); $this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Varian Edit Successful</p>'); redirect('admin/categories/varians_edit/' . $category_id . '/' . $id); } else { $this->db->insert('varians', $data); $this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Varian Added Successful</p>'); redirect('admin/categories/varians/' . $category_id); } } $this->data['category_id'] = $category_id; $this->data['subview'] = 'admin/categories/varians_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_varian($category_id = null, $id = null) { $count = $this->db->select('id')->from('varians')->where('id', $id)->where('category_id', $category_id)->get()->num_rows(); if($count === 0) { redirect('admin/categories'); } $this->db->where('id', $id); $this->db->where('category_id', $category_id); $this->db->delete('varians'); $this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Variant Deleted Successful</p>'); redirect('admin/categories/varians/' . $category_id); } //Attributes public function attributes($varian_id = null) { if ($varian_id == null) { redirect('admin/categories'); } $varian = $this->db->select('id, varian, category_id')->from('varians')->where('id', $varian_id)->get()->row_array(); if($varian === null) { redirect('admin/categories'); } $this->data['varian_id'] = $varian['id']; $this->data['varian'] = $varian['varian']; $this->data['category_id'] = $varian['category_id']; $this->data['attributes'] = $this->db->select('id, attribute, display')->from('attributes')->where('display', 1)->where('varian_id', $varian['id'])->order_by('priority', 'ASC')->get()->result(); //load view $this->data['subview'] = 'admin/categories/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'); } public function attributes_edit($varian_id = null, $id = null) { if ($varian_id === null) { redirect('admin/categories'); } $varian = $this->db->select('id, varian')->from('varians')->where('id', $varian_id)->get()->row_array(); if($varian === null) { redirect('admin/categories'); } if($id === null) { $attribute = new stdClass(); $attribute->id = ''; $attribute->attribute = ''; $attribute->varian_id = ''; $attribute->display = 1; $this->data['attribute'] = $attribute; //get ordering number and display at add form $this->db->select_max('priority')->from('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 { $this->data['attribute'] = $this->db->select('*')->from('attributes')->where('id', $id)->get()->row(); if($this->data['attribute'] === null) { redirect('admin/categories'); } } //validation check in action $rules = array( array( 'field' => 'attribute', 'label' => 'attribute', 'rules' => 'trim|required', ), array( 'field' => 'varian_id', 'label' => 'varian_id', 'rules' => 'trim|required', ), array( 'field' => 'display', 'label' => 'display', 'rules' => 'trim|required' ), array( 'field' => 'priority', 'label' => 'priority', 'rules' => 'trim|required|numeric' ), ); $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); $this->form_validation->set_rules($rules); if($this->form_validation->run($this) == TRUE) { $data = array( 'attribute' => $this->security->xss_clean($this->input->post('attribute')), 'varian_id' => $varian_id, 'display' => $this->input->post('display'), 'priority' => $this->input->post('priority'), ); if($id) { $this->db->where('id', $id); $this->db->update('attributes', $data); $this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Attribute Edit Successful</p>'); redirect('admin/categories/attributes_edit/' . $varian_id . '/' . $id); } else { $this->db->insert('attributes', $data); $this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Attribute Added Successful</p>'); redirect('admin/categories/attributes/' . $varian_id); } } $this->data['varian'] = $varian['varian']; $this->data['varian_id'] = $varian['id']; $this->data['subview'] = 'admin/categories/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'); } public function delete_attribute($varian_id = null, $id = null) { $count = $this->db->select('id')->from('attributes')->where('id', $id)->where('varian_id', $varian_id)->get()->num_rows(); if($count === 0) { redirect('admin/categories'); } $this->db->where('id', $id); $this->db->where('varian_id', $varian_id); $this->db->delete('attributes'); $this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Attribute Deleted Successful</p>'); redirect('admin/categories/attributes/' . $varian_id); } }