|
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/kamariallee.com/public_html/application/controllers/ |
Upload File : |
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Ajax extends Public_Controller {
public function __construct() {
parent::__construct();
}
//ajax product page select size
public function ajax_select_size() {
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$quantity = (int) $this->input->post('quantity');
$id_product = (int) $this->input->post('id_product');
//get product base price
$this->db->select('price')->from('products')->where('id_products', $id_product);
$base_price = $this->db->get()->row()->price;
//check if the id_product has quantity discount
$this->db->select('id_quantity_discount')->from('quantity_discount')->where('product_id', $id_product);
$count_quantity_discount = $this->db->get()->num_rows();
if ($count_quantity_discount > 0) {
//get discount for chosen quantity, choosing the closest quantity
$query = $this->db->query("SELECT discount_percentage FROM quantity_discount WHERE min_quantity <= '$quantity' ORDER BY ABS(min_quantity - '$quantity') LIMIT 1");
$row = $query->row();
if (count($row) > 0) {
$data['quantity_discounted_price'] = $base_price - ($base_price * $row->discount_percentage / 100);
$data['quantity_discount_percentage'] = $row->discount_percentage;
} else {
//quantity is less than minimum discount rule
//check if have base normal discount
$this->db->select('discount_price')->from('products')->where('id_products', $id_product);
$discount_price = $this->db->get()->row()->discount_price;
if ($discount_price != 0) {
$data['discounted_price'] = $base_price - ($base_price * $discount_price / 100);
$data['discount_percentage'] = $discount_price;
}
}
} else {
//no quantity discount
//check if have base normal discount
$this->db->select('discount_price')->from('products')->where('id_products', $id_product);
$discount_price = $this->db->get()->row()->discount_price;
if ($discount_price != 0) {
$data['discounted_price'] = $base_price - ($base_price * $discount_price / 100);
$data['discount_percentage'] = $discount_price;
}
}
$data['price'] = $base_price;
$data['id_product'] = $id_product;
$this->load->view('ajax_select_size', $data);
}
//ajax product page add product review
public function ajax_addproductreview() {
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
//CPATCHA VALIDATION
// First, delete old captchas
$expiration = time() - 7200; // Two hour limit
$this->db->where('captcha_time < ', $expiration)
->delete('captcha');
// Then see if a captcha exists and match
$sql = 'SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?';
$binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
$query = $this->db->query($sql, $binds);
$row = $query->row();
if ($row->count == 0) {
echo '<p style="background-color:red; color:white; padding:5px;">Mohon masukan kode yang benar.</p>';
exit();
}
$product_id = (int) $this->input->post('product_id');
$rating = $this->input->post('rating');
$review = $this->security->xss_clean($this->input->post('review'));
if($this->input->post('customer_id')) {
//if customer act as a registered during product review
$customer_id = (int) $this->input->post('customer_id');
//get customer name and email
$this->db->select('name, email')->from('customers')->where('id_customers', $customer_id);
$customer_data = $this->db->get()->row();
$data = array(
'product_id' => $product_id,
'review_date' => date('j M Y'),
'is_registered' => 'yes',
'customer_id' => $customer_id,
'name' => $customer_data->name,
'email' => $customer_data->email,
'rating' => $rating,
'review' => $review
);
} else {
//customer act as a guest during product review
//get value from serialize form data ajax
$name = $this->security->xss_clean($this->input->post('name'));
$email = $this->security->xss_clean($this->input->post('email'));
$data = array(
'product_id' => $product_id,
'review_date' => date('j M Y'),
'is_registered' => 'no',
'name' => $name,
'email' => $email,
'rating' => $rating,
'review' => $review
);
}
$this->db->insert('product_review', $data);
//get all product reviews
$this->db->select('*')->from('product_review')->where('product_id', $product_id)->order_by('review_date', 'DESC');
$data['product_reviews'] = $this->db->get()->result();
$data['product_id'] = $product_id;
$this->load->view('ajax_addproductreview', $data);
}
//ajax get price
public function ajax_get_price() {
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$id_product_details = (int) $this->input->post('id_product_details');
//get pricing details
$this->db->select('price, discounted_price')->from('product_details')->where('id_product_details', $id_product_details);
$prices = $this->db->get()->row();
$data['price'] = $prices->price;
$data['discounted_price'] = $prices->discounted_price;
$this->load->view('ajax_get_price', $data);
}
//ajax product page add to cart
public function ajax_add_to_cart() {
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$this->load->library('form_validation');
$this->load->library('cart');
//validation check in action
$config = array(
array(
'field' => 'product_size',
'label' => 'product Size',
'rules' => 'trim|required|callback_cek_stock'
)
);
$this->form_validation->set_rules($config);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if($this->form_validation->run($this) == TRUE) {
$id_product_details = (int) $this->input->post('product_size');
//get product details
$this->db->select('*')->from('product_details')->where('id_product_details', $id_product_details);
$product_details = $this->db->get()->row();
$data['id'] = (int) $this->input->post('product_id');
$data['name'] = '<strong>' . $this->input->post('product_name') . '</strong>' . '<br>Option: ' . $product_details->attributes . '<br>Product Code: ' . $product_details->sku;
$data['qty'] = (int) $this->input->post('qty');
$data['price'] = (int) $this->input->post('price');
$data['options']['size'] = $product_details->attributes;
$data['options']['sku'] = $product_details->sku;
$this->cart->product_name_rules = '[:print:]'; //this is to eliminate cart product name restriction on special characters
$this->cart->insert($data);
echo count($this->cart->contents());
} else {
echo 0; //means not enough stock
}
}
//callback function validation cek stock available when add to cart
public function cek_stock() {
$id_product_details = (int) $this->input->post('product_size');
$chosen_quantity = (int) $this->input->post('qty'); ;
//get current stock froms product_details table
$this->db->select('stock');
$this->db->from('product_details');
$this->db->where('id_product_details', $id_product_details);
$query = $this->db->get();
$current_stock = (int) $query->row()->stock;
//check if quantity is less or equal to current stock
if ($chosen_quantity > $current_stock) {
return FALSE;
} else {
return TRUE;
}
}
public function ajax_get_district() {
//if(!$_POST) { show_404(); }
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$this->load->helper('rajaongkir');
$province_id = (int) $this->input->post('id_province');
//get list of districts from RajaOngkir.com API
$data['districts'] = get_rajaongkir_data('city?province=' . $province_id); //get from helper file
$this->load->view('ajax_get_district', $data);
}
public function ajax_get_shipping_district() {
//if(!$_POST) { show_404(); }
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$this->load->helper('rajaongkir');
$shipping_province_id = (int) $this->input->post('id_shipping_province');
//get list of districts from RajaOngkir.com API
$data['shipping_districts'] = get_rajaongkir_data('city?province=' . $shipping_province_id); //get from helper file
$this->load->view('ajax_get_shipping_district', $data);
}
public function ajax_get_subdistrict() {
//if(!$_POST) { show_404(); }
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$this->load->helper('rajaongkir');
$district_id = (int) $this->input->post('id_district');
//get list of subdistricts from RajaOngkir.com API
$data['subdistricts'] = get_rajaongkir_data('subdistrict?city=' . $district_id); //get from helper file
$this->load->view('ajax_get_subdistrict', $data);
}
public function ajax_get_shipping_subdistrict() {
//if(!$_POST) { show_404(); }
//test if ajax call to prevent direct access
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
$this->load->helper('rajaongkir');
$shipping_district_id = (int) $this->input->post('id_shipping_district');
//get list of subdistricts from RajaOngkir.com API
$data['shipping_subdistricts'] = get_rajaongkir_data('subdistrict?city=' . $shipping_district_id); //get from helper file
$this->load->view('ajax_get_shipping_subdistrict', $data);
}
public function ajax_subscribe()
{
//test if ajax call to prevent direct access
if(!$this->input->is_ajax_request())
{
exit('No direct script access allowed');
}
$email = $this->security->xss_clean($this->input->post('email'));
//check if email exist
$email_data = $this->db->select('email')->from('subscriptions')->where('email', $email)->get()->row_array();
if($email_data)
{
return $this->output
->set_content_type('application/json')
->set_status_header(200)
->set_output(json_encode([
'message' => 'Email already exist',
]));
}
else
{
//add new email
$data = array(
'email' => $email
);
$this->db->insert('subscriptions', $data);
return $this->output
->set_content_type('application/json')
->set_status_header(201)
->set_output(json_encode([
'message' => 'Subscription successful',
]));
}
}
}