|
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/laciasmara.com/public_html/shop/application/controllers/admin/ |
Upload File : |
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Reviews extends Admin_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('review_m');
$this->load->model('customer_m');
$this->load->model('product_m');
}
public function index()
{
redirect('admin/reviews/manage');
}
//this is to list all resellers
public function manage()
{
$data['userdata'] = $this->session->userdata();
$data['title'] = 'Daftar Review | Laciasmara';
$data['reviews'] = $this->review_m->get_all_reviews();
$this->load->view('admin_new/layouts/header', $data);
$this->load->view('admin_new/reviews/manage_reviews');
$this->load->view('admin_new/layouts/footer');
}
public function add()
{
$data['userdata'] = $this->session->userdata();
$data['title'] = 'Tambah Review | Laciasmara';
$data['products'] = $this->product_m->all_products();
$this->load->view('admin_new/layouts/header', $data);
$this->load->view('admin_new/reviews/add_review');
$this->load->view('admin_new/layouts/footer');
}
public function edit_review($id_review = null)
{
if (!$id_review) {
redirect('admin/marketing/voucher');
}
$data['userdata'] = $this->session->userdata();
$data['title'] = 'Ubah Review | Laciasmara';
$data['products'] = $this->product_m->all_products_with_no_stocks();
$data['review'] = $this->review_m->get_review_by_id($id_review);
$this->load->view('admin_new/layouts/header', $data);
$this->load->view('admin_new/reviews/edit_review');
$this->load->view('admin_new/layouts/footer');
}
public function update_review($id)
{
$this->db->where('id', $id);
$existing_review = $this->db->get('product_review')->row();
if (!$existing_review) {
$this->session->set_flashdata('message', 'Review tidak ditemukan');
$this->session->set_flashdata('message_type', 'error');
redirect('admin/customer-services/review');
return;
}
// Validasi input
$this->form_validation->set_rules('product_id', 'Produk', 'required|trim|numeric');
$this->form_validation->set_rules('reviewer', 'Nama Reviewer', 'required|trim|max_length[100]');
$this->form_validation->set_rules('rating', 'Rating', 'required|trim|numeric|greater_than[0]|less_than_equal_to[5]');
$this->form_validation->set_rules('review_subject', 'Judul Review', 'required|trim|max_length[200]');
$this->form_validation->set_rules('review_description', 'Deskripsi Review', 'required|trim|max_length[2000]');
$this->form_validation->set_rules('status', 'Status', 'required|trim|in_list[pending,approved,rejected]');
$this->form_validation->set_rules('moderation_notes', 'Catatan Moderasi', 'trim|max_length[1000]');
$this->form_validation->set_message('required', '{field} tidak boleh kosong');
$this->form_validation->set_message('min_length', '{field} minimal {param} karakter');
$this->form_validation->set_message('max_length', '{field} maksimal {param} karakter');
$this->form_validation->set_message('numeric', '{field} harus berupa angka');
$this->form_validation->set_message('in_list', '{field} harus salah satu dari: pending, approved, rejected');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('message', validation_errors());
$this->session->set_flashdata('message_type', 'error');
redirect('admin/reviews/edit_review/' . $id);
} else {
$this->db->where('id_products', $this->input->post('product_id'));
$product_exists = $this->db->get('products')->num_rows() > 0;
if (!$product_exists) {
$this->session->set_flashdata('message', 'Produk yang dipilih tidak valid');
$this->session->set_flashdata('message_type', 'error');
redirect('admin/reviews/edit_review/' . $id);
return;
}
// Handle checkbox is_verified_purchase
$is_verified_purchase = $this->input->post('is_verified_purchase') ? 1 : 0;
// Data yang akan diupdate
$review_data = array(
'product_id' => $this->input->post('product_id'),
'display_name' => htmlspecialchars($this->input->post('reviewer'), ENT_QUOTES, 'UTF-8'),
'rating' => (int)$this->input->post('rating'),
'subject' => htmlspecialchars($this->input->post('review_subject'), ENT_QUOTES, 'UTF-8'),
'review' => htmlspecialchars($this->input->post('review_description'), ENT_QUOTES, 'UTF-8'),
'status' => $this->input->post('status'),
'is_verified_purchase' => $is_verified_purchase,
'moderated_by' => $this->session->userdata('user_id'),
'moderation_notes' => $this->input->post('moderation_notes') ? htmlspecialchars($this->input->post('moderation_notes'), ENT_QUOTES, 'UTF-8') : null,
'moderated_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
);
$this->db->trans_start();
// Update data review di database
$this->db->where('id', $id);
$update_result = $this->db->update('product_review', $review_data);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE || !$update_result) {
// Rollback jika ada error
$this->db->trans_rollback();
$this->session->set_flashdata('message', 'Gagal mengubah review. Silakan coba lagi.');
$this->session->set_flashdata('message_type', 'error');
} else {
// Success
$reviewer_name = htmlspecialchars($this->input->post('reviewer'), ENT_QUOTES, 'UTF-8');
$this->session->set_flashdata('message', 'Berhasil mengubah review dari ' . $reviewer_name);
$this->session->set_flashdata('message_type', 'success');
}
// Redirect ke halaman daftar review
redirect('admin/customer-services/review');
}
}
public function create_review()
{
// Validasi form
$this->load->library('form_validation');
$this->form_validation->set_rules('product_id', 'Produk', 'required|numeric');
$this->form_validation->set_rules('review_subject', 'Judul Review', 'required|max_length[255]');
$this->form_validation->set_rules('review_description', 'Deskripsi Review', 'required');
$this->form_validation->set_rules('rating', 'Rating', 'required|numeric|greater_than[0]|less_than_equal_to[5]');
if ($this->form_validation->run() == FALSE) {
// Jika validasi gagal, kembalikan ke halaman form dengan pesan error
$this->session->set_flashdata('message', validation_errors());
$this->session->set_flashdata('message_type', 'error');
redirect('admin/customer-services/review/add');
}
$is_verified_purchase = $this->input->post('is_verified_purchase') ? 1 : 0;
// Siapkan data untuk disimpan
$review_data = [
'product_id' => $this->input->post('product_id'),
'customer_id' => NULL,
'rating' => $this->input->post('rating'),
'subject' => $this->input->post('review_subject'),
'review' => $this->input->post('review_description'),
'display_name' => $this->input->post('reviewer'),
'review_date' => date('Y-m-d H:i:s'),
'is_verified_purchase' => $is_verified_purchase,
'moderated_by' => $this->session->userdata('user_id'),
'moderation_notes' => $this->input->post('moderation_notes') ? htmlspecialchars($this->input->post('moderation_notes'), ENT_QUOTES, 'UTF-8') : null,
'moderated_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
];
// Simpan review ke database
$this->db->insert('product_review', $review_data);
$review_id = $this->db->insert_id();
if ($review_id) {
// Jika berhasil disimpan
$this->session->set_flashdata('message', 'Berhasil menambah review baru');
$this->session->set_flashdata('message_type', 'success');
} else {
// Jika gagal disimpan
$this->session->set_flashdata('message', 'Gagal menambah review');
$this->session->set_flashdata('message_type', 'error');
}
// Redirect ke halaman review
redirect('admin/customer-services/review');
}
public function get_reviews()
{
$sort = $this->input->get('sort', true);
$dateFilter = $this->input->get('date_filter', true);
$startDate = $this->input->get('start_date', true);
$endDate = $this->input->get('end_date', true);
$is5Star = filter_var($this->input->get('is5Star', true), FILTER_VALIDATE_BOOLEAN);
$is4Star = filter_var($this->input->get('is4Star', true), FILTER_VALIDATE_BOOLEAN);
$is3Star = filter_var($this->input->get('is3Star', true), FILTER_VALIDATE_BOOLEAN);
$is2Star = filter_var($this->input->get('is2Star', true), FILTER_VALIDATE_BOOLEAN);
$is1Star = filter_var($this->input->get('is1Star', true), FILTER_VALIDATE_BOOLEAN);
$page = (int) ($this->input->get('page', true) ?? 1);
$limit = (int) ($this->input->get('limit', true) ?? 10);
$offset = ($page - 1) * $limit;
$searchTerm = $this->input->get('search', true);
// Membuat query dasar
$this->db->select('pr.*, p.title as product_title, IFNULL(c.name, "Guest") as customer_name, IFNULL(c.email, "Guest") as customer_email');
$this->db->from('product_review pr');
$this->db->join('products p', 'p.id_products = pr.product_id', 'left');
$this->db->join('customers c', 'c.id_customers = pr.customer_id', 'left');
// Filter berdasarkan rating bintang
$ratingFilters = [];
if ($is5Star) $ratingFilters[] = 5;
if ($is4Star) $ratingFilters[] = 4;
if ($is3Star) $ratingFilters[] = 3;
if ($is2Star) $ratingFilters[] = 2;
if ($is1Star) $ratingFilters[] = 1;
if (!empty($ratingFilters)) {
$this->db->where_in('pr.rating', $ratingFilters);
}
// Filter berdasarkan tanggal
if ($dateFilter) {
switch ($dateFilter) {
case 'today':
$this->db->where('DATE(pr.review_date) = CURDATE()');
break;
case 'yesterday':
$this->db->where('DATE(pr.review_date) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)');
break;
case 'last7days':
$this->db->where('pr.review_date >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)');
break;
case 'last30days':
$this->db->where('pr.review_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)');
break;
case 'thisMonth':
$this->db->where('MONTH(pr.review_date) = MONTH(CURDATE()) AND YEAR(pr.review_date) = YEAR(CURDATE())');
break;
case 'thisYear':
$this->db->where('YEAR(pr.review_date) = YEAR(CURDATE())');
break;
case 'custom':
if (!empty($startDate) && !empty($endDate)) {
$this->db->where('pr.review_date >=', $startDate);
$this->db->where('pr.review_date <=', $endDate);
}
break;
}
}
// Filter berdasarkan tanggal
if ($dateFilter == 'custom' && !empty($startDate) && !empty($endDate)) {
$this->db->where('pr.review_date >=', $startDate);
$this->db->where('pr.review_date <=', $endDate);
}
// Filter berdasarkan pencarian
if (!empty($searchTerm)) {
$this->db->group_start();
$this->db->like('p.title', $searchTerm);
$this->db->or_like('c.name', $searchTerm);
$this->db->or_like('c.email', $searchTerm);
$this->db->or_like('pr.subject', $searchTerm);
$this->db->or_like('pr.review', $searchTerm);
$this->db->or_like('pr.display_name', $searchTerm);
$this->db->group_end();
}
// Menghitung total data
$totalRecords = $this->db->count_all_results('', false);
// Sorting
switch ($sort) {
case 'paling_baru':
$this->db->order_by('pr.review_date', 'DESC');
break;
case 'paling_lama':
$this->db->order_by('pr.review_date', 'ASC');
break;
case 'tertinggi':
$this->db->order_by('pr.rating', 'DESC');
break;
case 'terendah':
$this->db->order_by('pr.rating', 'ASC');
break;
default:
$this->db->order_by('pr.review_date', 'DESC'); // Default sorting
}
// Pagination
$this->db->limit($limit, $offset);
// Eksekusi query final
$query = $this->db->get();
$reviews = $query->result();
// Hasil akhir
$pagination = [
'total_records' => $totalRecords,
'total_pages' => ceil($totalRecords / $limit),
'current_page' => $page,
'limit' => $limit
];
foreach ($reviews as $review) {
$review->_pagination = $pagination;
}
echo json_encode($reviews);
}
public function delete_review($id)
{
if ($this->input->is_ajax_request()) {
// Process for AJAX request
if ($this->db->delete('product_review', ['id' => $id])) {
$response = [
'status' => true,
'message' => 'Berhasil menghapus review dengan ID: ' . $id
];
} else {
$response = [
'status' => false,
'message' => 'Gagal menghapus review.'
];
}
// Send JSON response
$this->output
->set_content_type('application/json')
->set_output(json_encode($response));
} else {
if ($this->db->delete('product_review', ['id' => $id])) {
$this->session->set_flashdata('message', 'Berhasil menghapus review dengan ID: ' . $id);
$this->session->set_flashdata('message_type', 'success');
} else {
$this->session->set_flashdata('message', 'Gagal menghapus review.');
$this->session->set_flashdata('message_type', 'error');
}
redirect('admin/customer-services/review');
}
}
}