|
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/angkasapuraretail.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;
//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('brand_m');
$this->load->model('product_m');
if (!in_array('brands', $this->data['allowed_module'])) {
$this->data['allowed'] = false;
} else {
$this->data['allowed'] = true;
}
}
//this is to list all brands
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_filename1 = $this->image_processing($_FILES['image1'], 'image1');
$image_filename2 = $this->image_processing($_FILES['image2'], 'image2');
$image_filename3 = $this->image_processing($_FILES['image3'], 'image3');
$image_filename4 = $this->image_processing($_FILES['image4'], 'image4');
$image_filename5 = $this->image_processing($_FILES['image5'], 'image5');
//store image information to configuration table
$data = array(
'brand_landingpage_description' => $this->security->xss_clean($this->input->post('description')),
);
//image upload
if (isset($image_filename1)) {
$data['brand_landingpage_image1'] = $image_filename1;
}
if (isset($image_filename2)) {
$data['brand_landingpage_image2'] = $image_filename2;
}
if (isset($image_filename3)) {
$data['brand_landingpage_image3'] = $image_filename3;
}
if (isset($image_filename4)) {
$data['brand_landingpage_image4'] = $image_filename4;
}
if (isset($image_filename5)) {
$data['brand_landingpage_image5'] = $image_filename5;
}
$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;">Brand Landing Page Edit Successful</p>');
}
//get landingpage banner image, description and link
$this->db->select('brand_landingpage_description, brand_landingpage_image1, brand_landingpage_image2, brand_landingpage_image3, brand_landingpage_image4, brand_landingpage_image5')->from('configuration')->where('id_configuration', 1);
$this->data['brand_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();
$config['base_url'] = base_url() . 'admin/brands/index';
$config['total_rows'] = $this->brand_m->record_count();
$config['per_page'] = 100;
$config['uri_segment'] = 4;
$this->pagination->initialize($config);
$this->data['brands'] = $this->brand_m->get_all_brands($config['per_page'],
$this->uri->segment($config['uri_segment']));
//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 a new brand
public function add() {
if($this->data['allowed'] == false) { redirect('admin/dashboard'); }
$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;
}
//validation in action
//validation check in action
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); //above is to add class to form validation error, to be styled
$config = $this->brand_m->rules;
$this->form_validation->set_rules($config);
//add $this because we use hmvc
if($this->form_validation->run($this) == TRUE) {
$image_filename = $this->image_processing($_FILES['userfile'], 'userfile');
$logo_filename = $this->logo_processing($_FILES['userfile2'], 'userfile2');
$data = $this->table_data_processing(
$this->input->post('brand_name'),
$this->input->post('status'),
$image_filename,
$logo_filename,
$this->input->post('priority'));
$this->brand_m->add_brand($data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Brand Add Successful</p>');
redirect('admin/brands');
}
$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 edit brand in admin
public function edit($id = NULL) {
if ($id == NULL) { show_404(); }
//check if id exist. If not exist, show 404.
$count = $this->brand_m->count_exist($id);
if ($count == 0) { show_404(); }
$this->data['brands'] = $this->brand_m->get($id);
$this->brand_current_id = (int) $id;
//validation check in action
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); //above is to 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'], 'userfile');
$logo_filename = $this->logo_processing($_FILES['userfile2'], 'userfile2');
$data = $this->table_data_processing(
$this->input->post('brand_name'),
$this->input->post('status'),
$image_filename,
$logo_filename,
$this->input->post('priority'));
$this->brand_m->edit_brand($id, $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Brand Edit Successful</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) {
if($this->data['allowed'] == false || $this->data['role'] == 'admin') { redirect('admin/dashboard'); }
//check if id exist. If not exist, show 404.
$count = $this->brand_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('brands')->where('id_brands', (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/brand/'. $image->image);
}
//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 Delete Successful</p>');
redirect('admin/brands');
}
//image upload processing
private function image_processing($image_file, $input_name) {
//check & processing image banner upload files
if ($image_file['size'] !== 0) {
$config = array();
$config['upload_path'] = './uploads/brand/';
$config['allowed_types'] = 'jpg|png|jpeg|gif|webp';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($input_name)) {
echo $this->upload->display_errors(); die();
$this->session->set_flashdata('banner_error', '<br>
<p style="background:orange; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
if ($this->brand_current_id != NULL) {
redirect('admin/brands/edit/' . $this->brand_current_id);
} elseif($this->brand_current_id == NULL && $this->imageupload_indexpage == FALSE) {
redirect('admin/brands/add');
} elseif($this->brand_current_id == NULL && $this->imageupload_indexpage == TRUE) {
redirect('admin/brands');
}
} else {
$image = $this->upload->data();
$image_filename = $image['file_name'];
return $image_filename;
}
}
}
//logo upload processing
private function logo_processing($image_file, $input_name) {
//check & processing image banner upload files
if ($image_file['size'] !== 0) {
$config = array();
$config['upload_path'] = './uploads/brand/';
$config['allowed_types'] = 'jpg|png|jpeg|gif|webp';
$config['max_size'] = '1000';
// $config['max_width'] = $logo_dimension->brand_logo_width;
// $config['max_height'] = $logo_dimension->brand_logo_height;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($input_name)) {
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->brand_current_id != NULL) {
redirect('admin/brands/edit/' . $this->brand_current_id);
} else {
redirect('admin/brands/add');
}
} else {
$logo = $this->upload->data();
$logo_filename = $logo['file_name'];
return $logo_filename;
}
}
}
private function table_data_processing($brand_name, $status, $image_filename, $logo_filename, $priority) {
$data = array(
'brand' => $this->security->xss_clean($brand_name),
'alias' => url_title($this->security->xss_clean($brand_name)),
'status' => $status,
'description' => $this->security->xss_clean($this->input->post('description')),
'priority' => $priority,
'type' => $this->input->post('type')
);
//image upload
if (isset($image_filename)) {
$data['image'] = $image_filename;
}
//logo upload
if (isset($logo_filename)) {
$data['logo'] = $logo_filename;
}
return $data;
}
//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', 'Brand name already exist!');
return FALSE;
} else {
return TRUE;
}
}
//To delete brand landingpage banner image file from server, and from database
public function delete_landingpage_image() {
//get image file name for deletion
$this->db->select('brand_landingpage_image')->from('configuration')->where('id_configuration', 1);
$image = $this->db->get()->row()->brand_landingpage_image;
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/brand/'. $image);
//Delete image field from database
$data = array(
'brand_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/brands');
}
//To delete brand banner image file from server, and from database
public function delete_image($id = NULL) {
$count = $this->brand_m->count_exist($id);
if ($id == NULL || $count == 0) {
redirect('admin/brands');
}
//get image file name for deletion
$this->db->select('image')->from('brands')->where('id_brands', (int) $id);
$image = $this->db->get()->row();
//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);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Image Delete Successful</p>');
redirect('admin/brands/edit/' . $id);
}
//To delete brand logo file from server, and from database
public function delete_logo($id = NULL) {
$count = $this->brand_m->count_exist($id);
if ($id == NULL || $count == 0) {
redirect('admin/brands');
}
//get logo file name for deletion
$this->db->select('logo')->from('brands')->where('id_brands', (int) $id);
$image = $this->db->get()->row();
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/brand/'. $image->logo);
//Delete image 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;">Logo Delete Successful</p>');
redirect('admin/brands/edit/' . $id);
}
//BRAND LOCATIONS
public function locations($brand_id = NULL) {
//check if brand_id exist
$this->data['brand'] = $this->db->select('brand')->from('brands')->where('id_brands', $brand_id)->get()->row_array();
if(!$this->data['brand'])
{
redirect('admin/brands');
}
$this->load->helper('form');
//pagination in action. 100 results per page
$this->load->library('pagination');
$config = array();
$this->load->helper('pagination_helper');
$config = pagination_format();
$config['base_url'] = base_url() . 'admin/brands/locations/' . $brand_id;
//get total rows
$config['total_rows'] = $this->db->select('id')->from('brand_locations')->where('brand_id', $brand_id)->get()->num_rows();
$config['per_page'] = 100;
$config['uri_segment'] = 5;
$this->pagination->initialize($config);
$this->db->select('*');
$this->db->from('brand_locations');
$this->db->where('brand_id', $brand_id);
$this->db->order_by('priority', 'ASC');
$this->db->limit($config['per_page'], $this->uri->segment($config['uri_segment']));
$query = $this->db->get();
$this->data['locations'] = $query->result();
$this->data['brand_id'] = $brand_id;
//load view
$this->data['subview'] = 'admin/brands/location_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 brand
public function add_location($brand_id = NULL) {
if($this->data['allowed'] == false) { redirect('admin/dashboard'); }
$this->data['brand_id'] = $brand_id;
$this->data['location'] = new stdClass();
$this->data['location']->location = '';
$this->data['location']->title = '';
$this->data['location']->description = '';
$this->data['location']->display = '';
$this->data['location']->priority = '';
//get ordering number and display at add form
$this->db->select_max('priority')->from('brand_locations');
$current_priority = $this->db->get()->row()->priority;
if($current_priority == NULL) {
$this->data['location']->priority = 1;
} else {
$this->data['location']->priority = $current_priority + 1;
}
//validation in action
//validation check in action
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); //above is to add class to form validation error, to be styled
$config = array(
array(
'field' => 'location',
'label' => 'location Name',
'rules' => 'trim|required'
),
array(
'field' => 'title',
'label' => 'title',
'rules' => 'trim|required'
),
array(
'field' => 'description',
'label' => 'description',
'rules' => 'trim|required'
),
array(
'field' => 'priority',
'label' => 'priority',
'rules' => 'trim|required|numeric'
),
array(
'field' => 'status',
'label' => 'status',
'rules' => 'trim|required'
),
);
$this->form_validation->set_rules($config);
//add $this because we use hmvc
if($this->form_validation->run($this) == TRUE) {
$data = array(
'brand_id' => $brand_id,
'location' => $this->input->post('location'),
'display' => $this->input->post('status'),
'title' => $this->security->xss_clean($this->input->post('title')),
'description' => $this->security->xss_clean($this->input->post('description')),
'priority' => $this->security->xss_clean($this->input->post('priority')),
);
$this->db->insert('brand_locations', $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Brand Location Add Successful</p>');
redirect('admin/brands/locations/' . $brand_id);
}
$this->data['subview'] = 'admin/brands/edit_location';
$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 brand
public function edit_location($id = NULL) {
if($this->data['allowed'] == false) { redirect('admin/dashboard'); }
$this->data['location'] = $this->db->select('*')->from('brand_locations')->where('id', $id)->get()->row();
if(!$this->data['location']) {
redirect('admin/brands');
}
$this->data['brand_id'] = $this->data['location']->brand_id;
//validation in action
//validation check in action
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); //above is to add class to form validation error, to be styled
$config = array(
array(
'field' => 'location',
'label' => 'location Name',
'rules' => 'trim|required'
),
array(
'field' => 'title',
'label' => 'title',
'rules' => 'trim|required'
),
array(
'field' => 'description',
'label' => 'description',
'rules' => 'trim|required'
),
array(
'field' => 'priority',
'label' => 'priority',
'rules' => 'trim|required|numeric'
),
array(
'field' => 'status',
'label' => 'status',
'rules' => 'trim|required'
),
);
$this->form_validation->set_rules($config);
//add $this because we use hmvc
if($this->form_validation->run($this) == TRUE) {
$data = array(
'location' => $this->input->post('location'),
'display' => $this->input->post('status'),
'title' => $this->security->xss_clean($this->input->post('title')),
'description' => $this->security->xss_clean($this->input->post('description')),
'priority' => $this->security->xss_clean($this->input->post('priority')),
);
$this->db->where('id', $id);
$this->db->update('brand_locations', $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Brand Location Edit Successful</p>');
redirect('admin/brands/edit_location/' . $id);
}
$this->data['subview'] = 'admin/brands/edit_location';
$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_location($id) {
if($this->data['allowed'] == false || $this->data['role'] == 'admin') { redirect('admin/dashboard'); }
$this->data['location'] = $this->db->select('*')->from('brand_locations')->where('id', $id)->get()->row();
if(!$this->data['location']) {
redirect('admin/brands');
}
//delete
$this->db->where('id', $id);
$this->db->delete('brand_locations');
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Brand Location Delete Successful</p>');
redirect('admin/brands/locations/' . $this->data['location']->brand_id);
}
}