|
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/ptbos.biz/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();
$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'))
);
//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;">Service 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_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run($this) == TRUE) {
$image_filename = $this->image_processing($_FILES['userfile']['size']);
$thumbnail_filename = $this->thumbnail_processing($_FILES['userfile2']['size']);
$data = $this->table_data_processing($image_filename, $thumbnail_filename);
$this->category_m->add_category($data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Service 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->db->select('*')->from('categories')->where('id_categories',$id)->get()->row();
$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_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run($this) == TRUE) {
$image_filename = $this->image_processing($_FILES['userfile']['size']);
$thumbnail_filename = $this->thumbnail_processing($_FILES['userfile2']['size']);
$data = $this->table_data_processing($image_filename, $thumbnail_filename);
$this->category_m->edit_category($id, $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Service 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();
//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;">Service Delete Successful</p>');
redirect('admin/categories');
}
//image upload processing
private function image_processing($image_file_size) {
//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();
//check & processing image banner upload files
if ($image_file_size !== 0) {
$config['upload_path'] = './uploads/category/';
$config['allowed_types'] = 'png|jpg';
$config['max_size'] = '600';
$config['max_width'] = '1600';
$config['max_height'] = '640';
$this->load->library('upload', $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;
}
}
}
//logo upload processing
private function thumbnail_processing($thumbnail_file_size) {
//get max thumbnail width and height from configuration table
$this->db->select('category_thumbnail_width, category_thumbnail_height')->from('configuration')->where('id_configuration', 1);
$thumbnail_dimension = $this->db->get()->row();
//check & processing image banner upload files
if ($thumbnail_file_size !== 0) {
$config['upload_path'] = './uploads/category/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '500';
$config['max_width'] = '650';
$config['max_height'] = '448';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile2')) {
echo $this->upload->display_errors(); die();
$this->session->set_flashdata('logo_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 {
$thumbnail = $this->upload->data();
$thumbnail_filename = $thumbnail['file_name'];
return $thumbnail_filename;
}
}
}
private function table_data_processing($image_filename, $thumbnail_filename) {
$data = array(
'category' => $this->security->xss_clean($this->input->post('category_name')),
'category_en' => $this->security->xss_clean($this->input->post('category_name')),
'short_description' => $this->security->xss_clean($this->input->post('short_description')),
'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'))),
'status' => $this->input->post('status'),
'description' => $this->security->xss_clean($this->input->post('description')),
'description_en' => $this->security->xss_clean($this->input->post('description')),
'priority' => $this->input->post('priority'),
'meta_description' => $this->security->xss_clean($this->input->post('meta_description')),
'meta_keywords' => $this->security->xss_clean($this->input->post('meta_keywords'))
);
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_filename)) {
$data['image'] = $image_filename;
}
//thumbnail upload
if (isset($thumbnail_filename)) {
$data['thumbnail'] = $thumbnail_filename;
}
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) {
$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
$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;">Image Delete Successful</p>');
redirect('admin/categories/edit/' . $id);
}
//To delete category thumbnail file from server, and from database
public function delete_thumbnail($id = NULL) {
$count = $this->category_m->count_exist($id);
if ($id == NULL || $count == 0) {
redirect('admin/categories');
}
//get thumbnail file name for deletion
$this->db->select('thumbnail')->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->thumbnail);
//Delete image field from database
$data = array(
'thumbnail' => '',
);
$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;">Thumbnail Delete 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;
}
}