|
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/serbaantik.com/public_html/application/controllers/ |
Upload File : |
<?php defined('BASEPATH') || exit('No direct script access allowed');
/**
* @property Pagination $pagination
* @property DB $db
* @property Session $session
*/
class Products extends Public_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('pagination');
}
public function index()
{
$category = $this->db->select('alias')->from('categories')->where('status', '1')->order_by('priority', 'ASC')->get()->row_array();
if(!$category)
{
redirect(base_url());
}
$this->category($category['alias']);
}
public function category($alias = null, $reset = 'false')
{
if($reset === 'true')
{
$this->session->unset_userdata('varian_attributes');
}
$this->data['category'] = $this->db->select('id_categories, category, alias, image_banner, can_download_catalog, min_show_qty')->from('categories')->where('status', '1')->where('alias', $alias)->get()->row_array();
if(!$this->data['category'])
{
redirect(base_url());
}
if($this->session->userdata('sess_category_id') && $this->session->userdata('sess_category_id') != $this->data['category']['id_categories'])
{
$this->session->unset_userdata('varian_attributes');
}
$this->data['varians'] = $this->get_varians($this->data['category']['id_categories']);
$this->data_header['browser_title'] = 'Products from Serba Antik - '. ucwords($this->data['category']['category']);
$this->data_header['meta_description'] = 'Products from Serba Antik - '. ucwords($this->data['category']['category']);
//categories
$this->data['categories'] = $this->db
->select('category, alias, id_categories, min_show_qty')
->from('categories')
->where('status', '1')
->order_by('priority', 'ASC')
->get()
->result_array();
//get slideshows
$this->db->select('title, image, text_content')->from('home_slideshow')->where('status', '1')->where('type', 'products')->where('id', $this->data['category']['id_categories'])->order_by('priority', 'ASC');
$this->data['slideshows'] = $this->db->get()->result_array();
$this->load->view('template/header', $this->data_header);
$this->load->view('products', $this->data);
$this->load->view('template/footer', $this->data_footer);
}
private function get_varians($category_id)
{
$varians_attrs = array();
//get varians and attributes
$varians = $this->db
->select('id, varian, type')
->from('varians')
->where('category_id', $category_id)
->where('display', 1)
->order_by('priority', 'ASC')
->get()
->result_array();
foreach($varians as $var_key => $varian)
{
$varians_attrs[$var_key]['id'] = $varian['id'];
$varians_attrs[$var_key]['varian'] = $varian['varian'];
$varians_attrs[$var_key]['type'] = $varian['type'];
//get attributes for each varian
$attributes = $this->db->select('id, attribute')->from('attributes')->where('varian_id', $varian['id'])->where('display', 1)->order_by('priority', 'ASC')->get()->result_array();
foreach($attributes as $attr_key => $attribute)
{
$varians_attrs[$var_key]['attributes'][$attr_key]['id'] = $attribute['id'];
$varians_attrs[$var_key]['attributes'][$attr_key]['attribute'] = $attribute['attribute'];
}
}
return $varians_attrs;
}
public function detail($alias)
{
$data['product'] = $this->db->select('*')
->from('products')
->where('alias', $alias)
->where('product_status', 1)
->get()
->row_array();
if (!$data['product'])
{
redirect(base_url('products'));
}
//get product category
$data['category'] = $this->db->select('id_category, category, alias, can_request_sample')
->from('category_product')
->join('categories', 'categories.id_categories = category_product.id_category')
->where('id_product', $data['product']['id_products'])
->get()->row_array();
if (!$data['category'])
{
redirect(base_url('products'));
}
//get varian & attribute
$varian_ids = array();
$varians = $this->db->select('varian_id')
->from('attribute_product')
->where('product_id', $data['product']['id_products'])
->get()->result_array();
foreach($varians as $varian)
{
if(!in_array($varian['varian_id'], $varian_ids))
{
array_push($varian_ids , $varian['varian_id']);
}
}
$data['varians_attributes'] = array();
foreach($varian_ids as $key => $varian_id)
{
//get varian name and get attribute
$varian = $this->db->select('varian')->from('varians')->where('id', (int) $varian_id)->get()->row_array();
if(!is_null($varian))
{
$data['varians_attributes'][$key]['varian_name'] = $varian['varian'];
//get attribute id
$atrribute = $this->db->select('attribute_id')->from('attribute_product')->where('product_id', $data['product']['id_products'])->where('varian_id', (int) $varian_id)->get()->row_array();
if(!is_null($atrribute))
{
//get attribute name
$attribute_name = $this->db->select('attribute')->from('attributes')->where('id', (int) $atrribute['attribute_id'])->get()->row_array();
if(!is_null($attribute_name))
{
$data['varians_attributes'][$key]['attribute_name'] = $attribute_name['attribute'];
}
}
}
}
//get 4 related products
$this->db->select('title, alias, image1')
->from('products')
->join('category_product', 'category_product.id_product = products.id_products')
->where('id_category', $data['category']['id_category'])
->where('product_status', '1')
->where('products.id_products !=', $data['product']['id_products'])
->limit(4);
$data['related_products'] = $this->db->get()->result_array();
$this->data_header['browser_title'] = 'Product - ' . ucwords($data['product']['title']);
$this->data_header['meta_description'] = 'Product - ' . ucwords($data['product']['title']);
$this->load->view('template/header', $this->data_header);
$this->load->view('product-detail', $data);
$this->load->view('template/footer', $this->data_footer);
}
public function ajax_getproducts()
{
$this->load->helper('form');
if (!$this->input->is_ajax_request())
{
exit('No direct script access allowed');
}
$category_id = (int) $this->input->post('category_id');
$varians_attributes = $this->input->post('attributes');
$offset = (int) $this->input->post('offset');
$load_type = $this->input->post('load_type');
if($this->input->post('search_term'))
{
$search_term = $this->security->xss_clean($this->input->post('search_term'));
}
$this->data['category'] = $this->db->select('id_categories, category, alias, image_banner, can_download_catalog, min_show_qty')->from('categories')->where('status', '1')->where('id_categories', $category_id)->get()->row_array();
if(is_null($this->data['category']))
{
http_response_code(400);
}
$var_number_arr = array();
if(is_array($varians_attributes) && count($varians_attributes) > 0)
{
$this->session->set_userdata('varian_attributes', $varians_attributes);
$this->session->set_userdata('sess_category_id', $category_id);
foreach($varians_attributes as $var_attribute)
{
$var_attribute_arr = explode('|', $var_attribute);
$varian = $var_attribute_arr[0];
$attribute = $var_attribute_arr[1];
//get number from varian_field_numbers
$var_number = $this->db->select('number')->from('varian_field_numbers')->where('category_id', $category_id)->where('varian', $varian)->get()->row_array();
if($var_number['number'] > 0)
{
array_push($var_number_arr, array(
'varian' => $varian,
'attribute' => $attribute,
'var_number' => $var_number['number'])
);
}
}
}
else
{
$this->session->unset_userdata('varian_attributes');
}
$exist_var_arr = array();
if(count($var_number_arr) > 0)
{
foreach($var_number_arr as $var_number)
{
if(!in_array($var_number['varian'], $exist_var_arr))
{
array_push($exist_var_arr, $var_number['attribute']);
}
}
}
//get count of products
$this->db->select('id_products');
$this->db->from('products');
$this->db->join('category_product', 'category_product.id_product = products.id_products');
$this->db->where('id_category', $this->data['category']['id_categories']);
if(isset($search_term))
{
$this->db->like('products.title', $search_term);
}
$stocks_for_category_ids =[6, 7, 8, 9];
if (in_array((int) $this->data['category']['id_categories'], $stocks_for_category_ids))
{
$this->db->where('stock >=', $this->data['category']['min_show_qty']);
}
$this->db->where('product_status', '1');
if(count($var_number_arr) > 0)
{
foreach($var_number_arr as $var_number)
{
$this->db->where_in('attribute' . $var_number['var_number'], $exist_var_arr);
}
}
$total_products = $this->db->get()->num_rows();
//get actual products
$this->db->select('title, alias, image1');
$this->db->from('products');
$this->db->join('category_product', 'category_product.id_product = products.id_products');
$this->db->where('id_category', $this->data['category']['id_categories']);
if(isset($search_term))
{
$this->db->like('products.title', $search_term);
}
$stocks_for_category_ids =[6, 7, 8, 9];
if (in_array((int) $this->data['category']['id_categories'], $stocks_for_category_ids))
{
$this->db->where('stock >=', $this->data['category']['min_show_qty']);
}
$this->db->where('product_status', '1');
if(count($var_number_arr) > 0)
{
foreach($var_number_arr as $var_number)
{
$this->db->where_in('attribute' . $var_number['var_number'], $exist_var_arr);
}
}
$this->db->limit(21, $offset);
$this->data['products'] = $this->db->get()->result_array();
$this->data['total_products'] = $total_products;
//get chosen attributes name, to display selected attributes box
$this->data['attributes'] = array();
if(is_array($varians_attributes) && count($varians_attributes) > 0)
{
foreach($varians_attributes as $var_attribute)
{
if (!in_array($var_attribute, $this->data['attributes']))
{
array_push($this->data['attributes'], $var_attribute);
}
}
}
$res_data = array();
$res_data['attributes'] = $this->load->view('ajax_products_showattributes', $this->data, true);
$res_data['products'] = $this->load->view('ajax_getproducts', $this->data, true);
$res_data['pagination'] = $this->pagination->create_links();
$res_data['count'] = $this->data['products'];
$res_data['load_type'] = $load_type;
echo json_encode($res_data);
}
private function ajax_getproducts_usingpagination()
{
$this->load->helper('form');
if (!$this->input->is_ajax_request())
{
exit('No direct script access allowed');
}
$category_id = (int) $this->input->post('category_id');
$varians_attributes = $this->input->post('attributes');
$offset = (int) $this->input->post('offset');
$load_type = $this->input->post('load_type');
if($this->input->post('search_term'))
{
$search_term = $this->security->xss_clean($this->input->post('search_term'));
}
$this->data['category'] = $this->db->select('id_categories, category, alias, image_banner, can_download_catalog, min_show_qty')->from('categories')->where('status', '1')->where('id_categories', $category_id)->get()->row_array();
if(is_null($this->data['category']))
{
http_response_code(400);
}
$var_number_arr = array();
if(is_array($varians_attributes) && count($varians_attributes) > 0)
{
$this->session->set_userdata('varian_attributes', $varians_attributes);
$this->session->set_userdata('sess_category_id', $category_id);
foreach($varians_attributes as $var_attribute)
{
$var_attribute_arr = explode('|', $var_attribute);
$varian = $var_attribute_arr[0];
$attribute = $var_attribute_arr[1];
//get number from varian_field_numbers
$var_number = $this->db->select('number')->from('varian_field_numbers')->where('category_id', $category_id)->where('varian', $varian)->get()->row_array();
if($var_number['number'] > 0)
{
array_push($var_number_arr, array(
'varian' => $varian,
'attribute' => $attribute,
'var_number' => $var_number['number'])
);
}
}
}
else
{
$this->session->unset_userdata('varian_attributes');
}
$exist_var_arr = array();
if(count($var_number_arr) > 0)
{
foreach($var_number_arr as $var_number)
{
if(!in_array($var_number['varian'], $exist_var_arr))
{
array_push($exist_var_arr, $var_number['attribute']);
}
}
}
//pagination
$this->load->helper('pagination_helper');
$config = pagination_format_fe();
$config['base_url'] = base_url() . 'products/category/' . $this->data['category']['alias'];
$config['uri_segment'] = 4;
$config['per_page'] = 21;
if($offset != 0)
{
$offset = ($offset - 1) * $config['per_page'];
}
$config['cur_page'] = $offset;
//get total rows of products
$this->db->select('id_products');
$this->db->from('products');
$this->db->join('category_product', 'category_product.id_product = products.id_products');
$this->db->where('id_category', $this->data['category']['id_categories']);
if(isset($search_term))
{
$this->db->like('products.title', $search_term);
}
$stocks_for_category_ids =[6, 7, 8, 9];
if ((int) in_array($this->data['category']['id_categories'], $stocks_for_category_ids))
{
$this->db->where('stock >=', $this->data['category']['min_show_qty']);
}
$this->db->where('product_status', '1');
if(count($var_number_arr) > 0)
{
foreach($var_number_arr as $var_number)
{
$this->db->where_in('attribute' . $var_number['var_number'], $exist_var_arr);
}
}
$config['total_rows'] = $this->db->get()->num_rows();
$this->pagination->initialize($config);
//get count of products
$this->db->select('id_products');
$this->db->from('products');
$this->db->join('category_product', 'category_product.id_product = products.id_products');
$this->db->where('id_category', $this->data['category']['id_categories']);
if(isset($search_term))
{
$this->db->like('products.title', $search_term);
}
$stocks_for_category_ids =[6, 7, 8, 9];
if (in_array((int) $this->data['category']['id_categories'], $stocks_for_category_ids))
{
$this->db->where('stock >=', $this->data['category']['min_show_qty']);
}
$this->db->where('product_status', '1');
if(count($var_number_arr) > 0)
{
foreach($var_number_arr as $var_number)
{
$this->db->where_in('attribute' . $var_number['var_number'], $exist_var_arr);
}
}
$total_products = $this->db->get()->num_rows();
//get actual products
$this->db->select('title, alias, image1');
$this->db->from('products');
$this->db->join('category_product', 'category_product.id_product = products.id_products');
$this->db->where('id_category', $this->data['category']['id_categories']);
if(isset($search_term))
{
$this->db->like('products.title', $search_term);
}
$stocks_for_category_ids =[6, 7, 8, 9];
if (in_array((int) $this->data['category']['id_categories'], $stocks_for_category_ids))
{
$this->db->where('stock >=', $this->data['category']['min_show_qty']);
}
$this->db->where('product_status', '1');
if(count($var_number_arr) > 0)
{
foreach($var_number_arr as $var_number)
{
$this->db->where_in('attribute' . $var_number['var_number'], $exist_var_arr);
}
}
$this->db->limit(21, $offset);
$this->data['products'] = $this->db->get()->result_array();
$this->data['total_products'] = $total_products;
$this->data['curr_products'] = count($this->data['products']);
//get chosen attributes name, to display selected attributes box
$this->data['attributes'] = array();
if(is_array($varians_attributes) && count($varians_attributes) > 0)
{
foreach($varians_attributes as $var_attribute)
{
if (!in_array($var_attribute, $this->data['attributes']))
{
array_push($this->data['attributes'], $var_attribute);
}
}
}
$res_data = array();
$res_data['attributes'] = $this->load->view('ajax_products_showattributes', $this->data, true);
$res_data['products'] = $this->load->view('ajax_getproducts', $this->data, true);
$res_data['pagination'] = $this->pagination->create_links();
$res_data['count'] = $this->data['products'];
$res_data['load_type'] = $load_type;
echo json_encode($res_data);
}
}