|
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/mesinpolesshinemate.com/application/controllers/admin/ |
Upload File : |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Customers extends Admin_Controller {
//this property is used for validating existing customer title on call back edit customer
private $customer_current_id = NULL;
function __construct() {
parent::__construct();
$this->load->model('customer_m');
}
//this is to list all customers
public function index() {
/*----FILTER SEARCH PRODUCT--*/
if(isset($_POST['search_customer'])) {
//get product name from form
$this->data['keyword'] = $this->security->xss_clean($this->input->post('customer'));
//get all customers
$this->db->select('*');
$this->db->from('customers');
$this->db->like('name', $this->data['keyword']);
$this->db->order_by('join_date', 'DESC');
$this->data['customers'] = $this->db->get()->result();
} else {
//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/customers/index';
$config['total_rows'] = $this->customer_m->record_count();
$config['per_page'] = 200;
$config['uri_segment'] = 4;
$this->pagination->initialize($config);
//fetch all customers
$this->data['customers'] = $this->customer_m->get_all_customers($config["per_page"],
$this->uri->segment(4));
$this->data['use_pagination'] = 'yes';
}
//load view
$this->data['subview'] = 'admin/customers/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 customer
public function add() {
$this->load->helper('rajaongkir');
$this->data['customers'] = $this->customer_m->get_new();
//get all province data from RajaOngkir.com API
//$this->data['provinces'] = get_rajaongkir_data('province'); //get from helper file
//get all provinces data from provinces table
$this->db->select('rajaongkir_province_id, province')->from('indonesia_provinces')->order_by('rajaongkir_province_id', 'ASC');
$this->data['provinces'] = $this->db->get()->result();
//get all countries data from RajaOngkir.com API
//$this->data['countries'] = get_rajaongkir_data('v2/internationalDestination'); //get from helper file
//get all countries data from countries table
$this->db->select('*')->from('countries')->order_by('id_countries', 'ASC');
$this->data['countries'] = $this->db->get()->result();
//validation in action
//if country id 0 (indonesia) regular validation rule
if($this->input->post('country') == '0') {
$config = $this->customer_m->admin_rules;
} else {
//rules for international country
$config = $this->customer_m->admin_rules_international;
}
$config['register_password']['rules'] .= '|required'; //for new user password is required
$config['retype_register_password']['rules'] .= '|required|matches[register_password]'; //for new user password is required
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run($this) == TRUE) {
//hash input password
$password = $this->customer_m->hash($this->input->post('register_password'));
$data = array(
'name' => $this->security->xss_clean($this->input->post('name')),
'shipping_name' => $this->security->xss_clean($this->input->post('name')),
'recipient_name' => $this->security->xss_clean($this->input->post('name')),
'title' => $this->input->post('title'),
'email' => $this->security->xss_clean($this->input->post('email')),
'password' => $password,
'birthday' => $this->security->xss_clean($this->input->post('birthday')),
'address' => $this->security->xss_clean($this->input->post('address')),
'shipping_address' => $this->security->xss_clean($this->input->post('address')),
'postcode' => $this->security->xss_clean($this->input->post('postcode')),
'shipping_postcode' => $this->security->xss_clean($this->input->post('postcode')),
'phone' => $this->security->xss_clean($this->input->post('phone')),
'shipping_phone' => $this->security->xss_clean($this->input->post('phone')),
'status' => $this->input->post('status'),
'dropship' => $this->input->post('dropship')
);
if($this->input->post('reseller_id')) {
$data['reseller_id'] = $this->input->post('reseller_id');
} else {
$data['reseller_id'] = NULL;
}
if($this->input->post('country') == '0') {
//this is indonesia
$data['id_province'] = (int) $this->input->post('province');
$data['shipping_id_province'] = (int) $this->input->post('province');
$data['id_district'] = (int) $this->input->post('district');
$data['shipping_id_district'] = (int) $this->input->post('district');
$data['id_subdistrict'] = (int) $this->input->post('subdistrict');
$data['shipping_id_subdistrict'] = (int) $this->input->post('subdistrict');
$data['id_country'] = 0;
$data['shipping_id_country'] = 0;
$data['country'] = 'Indonesia';
$data['shipping_country'] = 'Indonesia';
//get province name
$this->db->select('province')->from('indonesia_provinces')->where('rajaongkir_province_id', (int) $this->input->post('province'));
$data['province'] = $this->db->get()->row()->province;
$data['shipping_province'] = $data['province'];
//get district name
$this->db->select('district')->from('indonesia_districts')->where('rajaongkir_id_district', (int) $this->input->post('district'));
$data['district'] = $this->db->get()->row()->district;
$data['shipping_district'] = $data['district'];
//get subdistrict name
$this->db->select('subdistrict')->from('indonesia_subdistricts')->where('rajaongkir_id_subdistrict', (int) $this->input->post('subdistrict'));
$data['subdistrict'] = $this->db->get()->row()->subdistrict;
$data['shipping_subdistrict'] = $data['subdistrict'];
} else {
//this is not indonesia
//get country name
$this->db->select('country')->from('countries')->where('id_countries', $this->input->post('country'));
$data['country'] = $this->db->get()->row()->country;
$data['shipping_country'] = $data['country'];
$data['id_country'] = (int) $this->input->post('country');
$data['shipping_id_country'] = (int) $this->input->post('country');
}
//add new point rewards to customer if rule exist
$this->db->select('first_customer')->from('point_rewards')->where('id_point_rewards', 1);
$point_rewards = (int) $this->db->get()->row()->first_customer;
if($point_rewards > 0) {
$data['current_pointreward'] = $point_rewards;
}
//save customer data
$this->db->insert('customers', $data);
$customer_id = $this->db->insert_id();
//email welcome message to customer..
//get customer data
$this->db->select('name, email')->from('customers')->where('id_customers', $customer_id);
$customer_data = $this->db->get()->row();
$data['customer_name'] = $customer_data->name;
$data['customer_email'] = $customer_data->email;
//get website data
$this->db->select('logo, from_email, website_name, email_smtp_host, email_smtp_port, email_smtp_password, email_smtp')->from('configuration')->where('id_configuration', 1);
$website_data = $this->db->get()->row();
$data['logo'] = $website_data->logo;
$data['website_name'] = $website_data->website_name;
$data['register_password'] = $this->input->post('register_password');
//get bonus pointrewards
if($point_rewards > 0) {
$data['pointrewards'] = $point_rewards;
}
$this->load->library('email');
//get email setting
$config['protocol'] = 'smtp';
$config['smtp_host'] = $website_data->email_smtp_host;
$config['smtp_port'] = $website_data->email_smtp_port;
$config['smtp_user'] = $website_data->email_smtp;
$config['smtp_pass'] = $website_data->email_smtp_password;
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
$this->email->initialize($config);
$this->email->from($website_data->from_email, $website_data->website_name);
$this->email->to($data['customer_email']);
$this->email->subject('Welcome');
$email = $this->load->view('email/account', $data, TRUE);
$this->email->message($email);
$this->email->send();
//----end send email
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Customer Add Successful</p>');
redirect('admin/customers');
}
$this->data['subview'] = 'admin/customers/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 customer in admin
public function edit($id = NULL) {
//check if id exist. If not exist, show 404.
$this->db->select('id_customers')->from('customers')->where('id_customers', $id);
$count_customer = $this->db->get()->num_rows();
if ($count_customer == 0) {
//customer not exist
show_404();
}
$this->data['shipping'] = $this->customer_m->get_shipping($id);
//get all provinces data from provinces table
$this->db->select('rajaongkir_province_id, province')->from('indonesia_provinces')->order_by('rajaongkir_province_id', 'ASC');
$this->data['provinces'] = $this->db->get()->result();
//get all countries data from countries table
$this->db->select('*')->from('countries')->order_by('id_countries', 'ASC');
$this->data['countries'] = $this->db->get()->result();
//get current country
$current_country_id = $this->data['shipping']->id_country;
$this->data['current_country_id'] = $current_country_id;
//get current province
$current_province_id = $this->data['shipping']->id_province;
$this->data['current_province_id'] = $current_province_id;
//get district lists
$this->db->select('rajaongkir_id_district, district')->from('indonesia_districts')->where('indonesia_id_province', $current_province_id);
$this->data['district_lists'] = $this->db->get()->result();
$current_district_id = $this->data['shipping']->id_district;
$this->data['current_district_id'] = $current_district_id;
//get all initial subdistricts lists
$this->db->select('rajaongkir_id_subdistrict, subdistrict')->from('indonesia_subdistricts')->where('indonesia_id_district', $current_district_id);
$this->data['subdistrict_lists'] = $this->db->get()->result();
$current_subdistrict_id = $this->data['shipping']->id_subdistrict;
$this->data['current_subdistrict_id'] = $current_subdistrict_id;
//get customer detail data
$this->db->select('*')->from('customers')->where('id_customers', $id);
$this->data['customers'] = $this->db->get()->row();
$this->customer_current_id = (int) $id;
//validation check in action
//if country id 0 (indonesia) regular validation rule
if($this->input->post('country') == '0') {
$config = $this->customer_m->admin_rules;
} else {
//rules for international country
$config = $this->customer_m->admin_rules_international;
}
if($this->input->post('register_password')) {
$config['retype_register_password']['rules'] .= '|matches[register_password]'; //for new user password is required
}
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run($this) == TRUE) {
$data = array(
'name' => $this->security->xss_clean($this->input->post('name')),
'shipping_name' => $this->security->xss_clean($this->input->post('name')),
'recipient_name' => $this->security->xss_clean($this->input->post('name')),
'title' => $this->input->post('title'),
'email' => $this->security->xss_clean($this->input->post('email')),
'birthday' => $this->security->xss_clean($this->input->post('birthday')),
'address' => $this->security->xss_clean($this->input->post('address')),
'shipping_address' => $this->security->xss_clean($this->input->post('address')),
'postcode' => $this->security->xss_clean($this->input->post('postcode')),
'shipping_postcode' => $this->security->xss_clean($this->input->post('postcode')),
'phone' => $this->security->xss_clean($this->input->post('phone')),
'shipping_phone' => $this->security->xss_clean($this->input->post('phone')),
'status' => $this->input->post('status'),
'dropship' => $this->input->post('dropship')
);
if($this->input->post('reseller_id')) {
$data['reseller_id'] = $this->input->post('reseller_id');
} else {
$data['reseller_id'] = NULL;
}
if($this->input->post('register_password')) {
//hash input password
$password = $this->customer_m->hash($this->input->post('register_password'));
$data['password'] = $password;
}
if($this->input->post('country') == '0') {
//this is indonesia
$data['id_province'] = (int) $this->input->post('province');
$data['shipping_id_province'] = (int) $this->input->post('province');
$data['id_district'] = (int) $this->input->post('district');
$data['shipping_id_district'] = (int) $this->input->post('district');
$data['id_subdistrict'] = (int) $this->input->post('subdistrict');
$data['shipping_id_subdistrict'] = (int) $this->input->post('subdistrict');
$data['id_country'] = 0;
$data['shipping_id_country'] = 0;
$data['country'] = 'Indonesia';
$data['shipping_country'] = 'Indonesia';
//get province name
$this->db->select('province')->from('indonesia_provinces')->where('rajaongkir_province_id', (int) $this->input->post('province'));
$data['province'] = $this->db->get()->row()->province;
$data['shipping_province'] = $data['province'];
//get district name
$this->db->select('district')->from('indonesia_districts')->where('rajaongkir_id_district', (int) $this->input->post('district'));
$data['district'] = $this->db->get()->row()->district;
$data['shipping_district'] = $data['district'];
//get subdistrict name
$this->db->select('subdistrict')->from('indonesia_subdistricts')->where('rajaongkir_id_subdistrict', (int) $this->input->post('subdistrict'));
$data['subdistrict'] = $this->db->get()->row()->subdistrict;
$data['shipping_subdistrict'] = $data['subdistrict'];
} else {
//this is not indonesia
//get country name by country from RajaOngkir
//get country name
$this->db->select('country')->from('countries')->where('id_countries', $this->input->post('country'));
$data['country'] = $this->db->get()->row()->country;
$data['shipping_country'] = $data['country'];
$data['id_country'] = (int) $this->input->post('country');
$data['shipping_id_country'] = (int) $this->input->post('country');
$data['id_province'] = NULL;
$data['id_district'] = NULL;
$data['id_subdistrict'] = NULL;
$data['province'] = NULL;
$data['district'] = NULL;
$data['subdistrict'] = NULL;
$data['shipping_id_province'] = NULL;
$data['shipping_id_district'] = NULL;
$data['shipping_id_subdistrict'] = NULL;
$data['shipping_province'] = NULL;
$data['shipping_district'] = NULL;
$data['shipping_subdistrict'] = NULL;
}
$this->db->where('id_customers', $id);
$this->db->update('customers', $data);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Customer Edit Successful</p>');
redirect('admin/customers');
}
$this->data['subview'] = 'admin/customers/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 customer
public function delete($id) {
//check if id exist. If not exist, show 404.
$this->db->select('id_customers')->from('customers')->where('id_customers', $id);
$count = $this->db->get()->num_rows();
if ($count == 0) { show_404(); }
//delete customer
$this->customer_m->delete($id);
$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Customer Delete Successful</p>');
redirect('admin/customers');
}
//callback function validation register new email
public function cek_email($str) {
$num_rows = $this->customer_m->cek_existing_email($str, $this->customer_current_id);
if ($num_rows != 0 ) {
$this->form_validation->set_message('cek_email', 'Email already exist !');
return FALSE;
} else {
return TRUE;
}
}
}