|
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 Spaces extends Admin_Controller {
//this property is used for validating existing spaces title on call back edit spaces
private $spaces_current_id = NULL;
private $image1_filename = NULL;
private $image2_filename = NULL;
private $image3_filename = 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('spaces_m');
$this->load->helper('form');
if (!in_array('spaces', $this->data['allowed_module'])) {
$this->data['allowed'] = false;
} else {
$this->data['allowed'] = true;
}
}
//this is to list all spaces
public function index() {
if (isset($_POST['submit'])) {
$this->imageupload_indexpage = TRUE;
//check & processing image banner upload files
if ($_FILES['image1']['size'] !== 0) {
$config = array();
$config['upload_path'] = './uploads/spaces/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|webp';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image1')) {
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->spaces_current_id != NULL) {
redirect('admin/spaces/edit/' . $this->spaces_current_id);
} elseif($this->spaces_current_id == NULL && $this->imageupload_indexpage == FALSE) {
redirect('admin/spaces/add');
} elseif($this->spaces_current_id == NULL && $this->imageupload_indexpage == TRUE) {
redirect('admin/spaces');
}
} else {
$image1 = $this->upload->data();
$image_filename1 = $image1['file_name'];
}
}
//check & processing image banner upload files
if ($_FILES['image2']['size'] !== 0) {
$config = array();
$config['upload_path'] = './uploads/spaces/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|webp';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image2')) {
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->spaces_current_id != NULL) {
redirect('admin/spaces/edit/' . $this->spaces_current_id);
} elseif($this->spaces_current_id == NULL && $this->imageupload_indexpage == FALSE) {
redirect('admin/spaces/add');
} elseif($this->spaces_current_id == NULL && $this->imageupload_indexpage == TRUE) {
redirect('admin/spaces');
}
} else {
$image2 = $this->upload->data();
$image_filename2 = $image2['file_name'];
}
}
//check & processing image banner upload files
if ($_FILES['image3']['size'] !== 0) {
$config = array();
$config['upload_path'] = './uploads/spaces/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|webp';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image3')) {
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->spaces_current_id != NULL) {
redirect('admin/spaces/edit/' . $this->spaces_current_id);
} elseif($this->spaces_current_id == NULL && $this->imageupload_indexpage == FALSE) {
redirect('admin/spaces/add');
} elseif($this->spaces_current_id == NULL && $this->imageupload_indexpage == TRUE) {
redirect('admin/spaces');
}
} else {
$image3 = $this->upload->data();
$image_filename3 = $image3['file_name'];
}
}
//store image information to configuration table
$data = array(
'spaces_landingpage_description1' => $this->security->xss_clean($this->input->post('description1')),
'spaces_landingpage_description2' => $this->security->xss_clean($this->input->post('description2')),
'spaces_landingpage_description3' => $this->security->xss_clean($this->input->post('description3')),
);
//image upload
if (isset($image_filename1)) {
$data['spaces_landingpage_image1'] = $image_filename1;
}
if (isset($image_filename2)) {
$data['spaces_landingpage_image2'] = $image_filename2;
}
if (isset($image_filename3)) {
$data['spaces_landingpage_image3'] = $image_filename3;
}
$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;">Spaces Landing Page Edit Successful</p>');
}
//get landingpage banner image, description and link
$this->db->select('spaces_landingpage_description1, spaces_landingpage_description2, spaces_landingpage_description3, spaces_landingpage_image1, spaces_landingpage_image2, spaces_landingpage_image3')->from('configuration')->where('id_configuration', 1);
$this->data['spaces_landingpage'] = $this->db->get()->row();
//pagination in action. 100 results per page
$this->load->library('pagination');
$config['base_url'] = base_url() . 'admin/spaces/index';
$config['total_rows'] = $this->spaces_m->record_count();
$config['per_page'] = 200;
$config["uri_segment"] = 4;
$config['num_tag_open'] = '<span style="padding-left:10px; padding-right:10px">';
$config['num_tag_close'] = '</span>';
$this->pagination->initialize($config);
$this->data['spaces'] = $this->spaces_m->get_all_spaces($config["per_page"],
$this->uri->segment(4));
//load view
$this->data['subview'] = 'admin/spaces/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 spaces
public function add() {
if($this->data['allowed'] == false) { redirect('admin/dashboard'); }
$this->data['spaces'] = $this->spaces_m->get_new();
//get ordering number and display at add form
$this->db->select_max('priority')->from('spaces');
$current_priority = $this->db->get()->row()->priority;
if($current_priority == NULL) {
$this->data['spaces']->priority = 1;
} else {
$this->data['spaces']->priority = $current_priority + 1;
}
//validation in action
//validation check in action
$config = $this->spaces_m->rules;
$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
$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 INTRO
if ($_FILES['image1']['size'] !== 0) {
$config = array();
$config['upload_path'] = './uploads/spaces/';
$config['allowed_types'] = 'jpg|png|jpeg|gif|webp';
$config['max_size'] = '1000';
// $config['max_width'] = $image_dimension->spaces_image_width;
// $config['max_height'] = $image_dimension->spaces_image_height;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image1')) {
// return the error message and kill the script
//echo $this->upload->display_errors();
$this->session->set_flashdata('image1-error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
redirect('admin/spaces/add');
} else {
$image1 = $this->upload->data();
$this->image1_filename = $image1['file_name'];
}
}
//check & processing IMAGE TOP BANNER
if ($_FILES['image2']['size'] !== 0) {
$config = array();
$config['upload_path'] = './uploads/spaces/';
$config['allowed_types'] = 'jpg|png|jpeg|gif|webp';
$config['max_size'] = '1000';
// $config['max_width'] = $image_dimension->spaces_image_width;
// $config['max_height'] = $image_dimension->spaces_image_height;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image2')) {
// return the error message and kill the script
//echo $this->upload->display_errors();
$this->session->set_flashdata('image2-error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
redirect('admin/spaces/add');
} else {
$image2 = $this->upload->data();
$this->image2_filename = $image2['file_name'];
}
}
//check & processing IMAGE CONTENT
if ($_FILES['image3']['size'] !== 0) {
$config = array();
$config['upload_path'] = './uploads/spaces/';
$config['allowed_types'] = 'jpg|png|jpeg|gif|webp';
$config['max_size'] = '1000';
// $config['max_width'] = $image_dimension->spaces_image_width;
// $config['max_height'] = $image_dimension->spaces_image_height;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image3')) {
// return the error message and kill the script
//echo $this->upload->display_errors();
$this->session->set_flashdata('image3-error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
redirect('admin/spaces/add');
} else {
$image3 = $this->upload->data();
$this->image3_filename = $image3['file_name'];
}
}
$data = $this->table_data_processing($this->input->post('spaces_name'), $this->input->post('status'), $this->input->post('description'), $this->image1_filename, $this->image2_filename, $this->image3_filename);
$this->spaces_m->add_spaces($data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Spaces Add Successful</p>');
redirect('admin/spaces');
}
$this->data['subview'] = 'admin/spaces/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 spaces in admin
public function edit($id = NULL) {
if ($id == NULL) { show_404(); }
//check if id exist. If not exist, show 404.
$count = $this->spaces_m->count_exist($id);
if ($count == 0) {
//page not exist
show_404();
}
$this->data['spaces'] = $this->spaces_m->get($id);
$this->spaces_current_id = (int) $id;
//validation check in action
$config = $this->spaces_m->rules;
$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
$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 INTRO
if ($_FILES['image1']['size'] !== 0) {
$config = array();
$config['upload_path'] = './uploads/spaces/';
$config['allowed_types'] = 'jpg|png|jpeg|gif|webp';
$config['max_size'] = '1000';
// $config['max_width'] = $image_dimension->spaces_image_width;
// $config['max_height'] = $image_dimension->spaces_image_height;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image1')) {
// return the error message and kill the script
echo $this->upload->display_errors();
exit();
$this->session->set_flashdata('image1-error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
redirect('admin/spaces/edit/' . $id);
} else {
$image1 = $this->upload->data();
$this->image1_filename = $image1['file_name'];
}
}
//check & processing IMAGE TOP BANNER
if ($_FILES['image2']['size'] !== 0) {
$config = array();
$config['upload_path'] = './uploads/spaces/';
$config['allowed_types'] = 'jpg|png|jpeg|gif|webp';
$config['max_size'] = '1000';
// $config['max_width'] = $image_dimension->spaces_image_width;
// $config['max_height'] = $image_dimension->spaces_image_height;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image2')) {
// return the error message and kill the script
echo $this->upload->display_errors();
exit();
$this->session->set_flashdata('image2-error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
redirect('admin/spaces/edit/' . $id);
} else {
$image2 = $this->upload->data();
$this->image2_filename = $image2['file_name'];
}
}
//check & processing IMAGE CONTENT
if ($_FILES['image3']['size'] !== 0) {
$config = array();
$config['upload_path'] = './uploads/spaces/';
$config['allowed_types'] = 'jpg|png|jpeg|gif|webp';
$config['max_size'] = '1000';
// $config['max_width'] = $image_dimension->spaces_image_width;
// $config['max_height'] = $image_dimension->spaces_image_height;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image3')) {
// return the error message and kill the script
echo $this->upload->display_errors();
exit();
$this->session->set_flashdata('image3-error', '<br>
<p style="background:red; color:white; padding:5px; font-weight:bold;">Image Upload Error. Wrong format or size.</p>');
redirect('admin/spaces/edit/' . $id);
} else {
$image3 = $this->upload->data();
$this->image3_filename = $image3['file_name'];
}
}
$data = $this->table_data_processing($this->input->post('spaces_name'), $this->input->post('status'), $this->input->post('description'), $this->image1_filename, $this->image2_filename, $this->image3_filename);
$this->spaces_m->edit_spaces($id, $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Spaces Edit Successful</p>');
redirect('admin/spaces/edit/' . $id);
}
$this->data['subview'] = 'admin/spaces/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 spaces
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->spaces_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('image1, image2')->from('spaces')->where('id', (int) $id);
$image = $this->db->get()->row();
if ($image->image1 != '') {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/spaces/'. $image->image1);
}
if ($image->image2 != '') {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/spaces/'. $image->image2);
}
//delete spaces
$this->spaces_m->delete($id);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Spaces Delete Successful</p>');
redirect('admin/spaces');
}
private function table_data_processing($spaces_name, $status, $description,
$image1_filename, $image2_filename, $image3_filename) {
$data = array(
'title' => $this->security->xss_clean($spaces_name),
'alias' => url_title(strtolower($spaces_name)),
'status' => $status,
'description' => $description,
'type' => $this->input->post('type'),
'priority' => $this->input->post('priority'),
);
//image upload
if (isset($image1_filename)) {
$data['image1'] = $image1_filename;
}
//image upload
if (isset($image2_filename)) {
$data['image2'] = $image2_filename;
}
//image upload
if (isset($image3_filename)) {
$data['image3'] = $image3_filename;
}
return $data;
}
//callback function validation add new spaces
//make it private by adding _
public function _cek_existing_spaces_title($str) {
$num_rows = $this->spaces_m->cek_existing_spaces_title($str, $this->spaces_current_id);
if ($num_rows != 0 ) {
$this->form_validation->set_message('_cek_existing_spaces_title', 'spaces name already exist !');
return FALSE;
} else {
return TRUE;
}
}
public function _cek_existing_spaces_title_en($str) {
$num_rows = $this->spaces_m->cek_existing_spaces_title_en($str, $this->spaces_current_id);
if ($num_rows != 0 ) {
$this->form_validation->set_message('_cek_existing_spaces_title_en', 'spaces name english already exist !');
return FALSE;
} else {
return TRUE;
}
}
//To delete spaces landingpage banner image file from server, and from database
public function delete_landingpage_image() {
//get image file name for deletion
$this->db->select('spaces_landingpage_image')->from('configuration')->where('id_configuration', 1);
$image = $this->db->get()->row()->spaces_landingpage_image;
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/spaces/'. $image);
//Delete image field from database
$data = array(
'spaces_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/spaces');
}
//To delete spaces image file from server, and from database
public function delete_image($id = NULL, $image_type = NULL) {
$count = $this->spaces_m->count_exist($id);
if ($id == NULL || $count == 0) { redirect('admin/spaces'); }
if ($image_type == NULL) {redirect('admin/spaces'); }
//get image file name for deletion
$this->db->select('image1, image2')->from('spaces')->where('id', (int) $id);
$image = $this->db->get()->row();
if($image_type == 'image1') {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/spaces/'. $image->image1);
//Delete image field from database
$data = array(
'image1' => '',
);
} elseif($image_type == 'image2') {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/spaces/'. $image->image2);
//Delete image field from database
$data = array(
'image2' => '',
);
} elseif($image_type == 'image3') {
//Delete the actual image file from server. FCPATH is codeigniter base path
unlink(FCPATH .'/uploads/spaces/'. $image->image3);
//Delete image field from database
$data = array(
'image3' => '',
);
}
$this->db->where('id', (int) $id);
$this->db->update('spaces', $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Image Delete Successful</p>');
redirect('admin/spaces/edit/' . $id);
}
}