|
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 defined('BASEPATH') or exit('No direct script access allowed');
class Campaign extends Admin_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model([
'customer_m',
'product_m',
'category_m',
'Subscriber_m',
'Subscriber_group_m',
'Campaign_email_template_m',
'Campaign_m',
'Campaign_email_log_m',
'Campaign_email_queue_m',
'Promotional_campaign_m',
]);
$this->load->helper(['url', 'security']);
$this->load->library('form_validation');
// $this->load->library('phpmailer_lib');
$this->load->library('upload');
}
public function subscriber()
{
$data['userdata'] = $this->session->userdata();
$data['title'] = 'Campaign Subscriber | Laciasmara';
$data['subscribers'] = $this->Subscriber_m->get_all([]);
// Send all subscriber
$this->load->view('admin_new/layouts/header', $data);
$this->load->view('admin_new/campaign/subscriber/index.php');
$this->load->view('admin_new/layouts/footer');
}
// Website Campaign
public function website()
{
$data_header['userdata'] = $this->session->userdata();
$data_header['title'] = 'Website Campaign | Laciasmara';
$campaigns = $this->Campaign_m->get_all_website_campaigns();
$data['campaigns'] = $campaigns;
$data['total_campaign'] = count($campaigns);
$data['active_campaign'] = $this->Campaign_m->count_website_campaigns(1);
$data['inactive_campaign'] = $this->Campaign_m->count_website_campaigns(0);
$this->load->view('admin_new/layouts/header', $data_header);
$this->load->view('admin_new/campaign/website/index.php', $data);
$this->load->view('admin_new/layouts/footer');
}
public function edit_website_campaign($campaign_id)
{
$data_header['userdata'] = $this->session->userdata();
$data_header['title'] = 'Edit Campaign | Laciasmara';
$data['products'] = $this->product_m->all_products();
$data['categories'] = $this->category_m->get_all_categories();
$data['campaign'] = $this->Promotional_campaign_m->get_by_id($campaign_id);
$this->load->view('admin_new/layouts/header', $data_header);
$this->load->view('admin_new/campaign/website/edit.php', $data);
$this->load->view('admin_new/layouts/footer');
}
public function process_update_website_campaign()
{
$this->output->set_content_type('application/json');
$campaign_id = $this->input->post('id');
try {
// Cek apakah campaign exists
$existing_campaign = $this->Promotional_campaign_m->get_by_id($campaign_id);
if (!$existing_campaign) {
$this->output->set_output(json_encode([
'success' => false,
'message' => 'Campaign not found',
'campaign_id' => $campaign_id
]));
return;
}
$this->db->trans_begin();
$banner_image = $this->_handle_file_upload_edit('banner_image', 'banner', $existing_campaign->banner_image);
$thumbnail_image = $this->_handle_file_upload_edit('thumbnail', 'thumbnail', $existing_campaign->thumbnail);
$campaign_data = $this->_prepare_campaign_data_edit($banner_image, $thumbnail_image);
// Update campaign
$update_result = $this->Promotional_campaign_m->update_campaign($campaign_id, $campaign_data);
if (!$update_result) {
throw new Exception('Failed to update campaign');
}
// Commit transaction
if ($this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
throw new Exception('Database transaction failed');
} else {
$this->db->trans_commit();
}
// Success response
$this->output->set_output(json_encode([
'success' => true,
'message' => 'Campaign updated successfully',
'data' => [
'campaign_id' => $campaign_id,
'code' => $campaign_data['code'],
]
]));
} catch (Exception $e) {
$this->db->trans_rollback();
$this->output->set_output(json_encode([
'success' => false,
'message' => 'Error: ' . $e->getMessage()
]));
}
}
public function create_website_campaign()
{
$data_header['userdata'] = $this->session->userdata();
$data_header['title'] = 'Create Campaign | Laciasmara';
// Product data
$data['products'] = $this->product_m->all_products();
$data['categories'] = $this->category_m->get_all_categories();
// Category data
$this->load->view('admin_new/layouts/header', $data_header);
$this->load->view('admin_new/campaign/website/create.php', $data);
$this->load->view('admin_new/layouts/footer');
}
public function process_create_website_campaign()
{
$this->output->set_content_type('application/json');
try {
// Validate required fields
$validation_result = $this->_validate_campaign_data();
if (!$validation_result['status']) {
$this->output->set_output(json_encode([
'success' => false,
'message' => $validation_result['message'],
'errors' => $validation_result['errors']
]));
return;
}
// Start database transaction
$this->db->trans_begin();
// Handle file uploads
$banner_image = $this->_handle_file_upload('banner_image', 'banner');
$thumbnail_image = $this->_handle_file_upload('thumbnail', 'thumbnail');
$campaign_data = $this->_prepare_campaign_data($banner_image, $thumbnail_image);
// Insert campaign
$campaign_id = $this->Promotional_campaign_m->insert_campaign($campaign_data);
if (!$campaign_id) {
throw new Exception('Failed to create campaign');
}
// Commit transaction
if ($this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
throw new Exception('Database transaction failed');
} else {
$this->db->trans_commit();
}
// Success response
$this->output->set_output(json_encode([
'success' => true,
'message' => 'Campaign created successfully',
'data' => [
'campaign_id' => $campaign_id,
'code' => $campaign_data['code'],
]
]));
} catch (Exception $e) {
}
}
/**
* Validate campaign input data
*/
private function _validate_campaign_data()
{
$errors = [];
// Required fields validation
if (empty($_POST['name'])) {
$errors['name'] = 'Campaign name is required';
}
if (empty($_POST['code'])) {
$errors['code'] = 'Code is required';
}
if (!empty($errors)) {
return [
'status' => false,
'message' => 'Validation failed',
'errors' => $errors
];
}
return ['status' => true];
}
/**
* Handle file upload
*/
private function _handle_file_upload($field_name, $prefix)
{
if (empty($_FILES[$field_name]['name'])) {
return null;
}
// Tentukan upload path berdasarkan prefix
$upload_paths = [
'banner' => './uploads/campaign/full/',
'thumbnail' => './uploads/campaign/thumbnail/',
];
$config['upload_path'] = isset($upload_paths[$prefix]) ? $upload_paths[$prefix] : './uploads/campaigns/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|webp';
$config['max_size'] = 2048;
$config['file_name'] = $prefix . '_' . time() . '_' . uniqid();
// Buat folder kalau belum ada
if (!is_dir($config['upload_path'])) {
mkdir($config['upload_path'], 0755, true);
}
$this->upload->initialize($config);
if ($this->upload->do_upload($field_name)) {
$upload_data = $this->upload->data();
return $upload_data['file_name'];
} else {
throw new Exception('File upload failed: ' . $this->upload->display_errors());
}
}
/**
* Prepare campaign data for insertion
*/
private function _prepare_campaign_data($banner_image, $thumbnail_image)
{
// Mengambil array data dari POST menggunakan method CodeIgniter
$applicable_categories = $this->input->post('applicable_categories');
$applicable_products = $this->input->post('applicable_products');
$excluded_products = $this->input->post('excluded_products');
// Description belom masuk
$data = [
'name' => $this->input->post('name'),
'code' => $this->input->post('code'),
'description' => $this->input->post('description'),
'discount_type' => $this->input->post('discount_type'),
'discount_value' => $this->input->post('discount_value'),
'min_purchase' => $this->input->post('min_purchase') ?: null,
'max_discount' => $this->input->post('max_discount') ?: null,
'start_date' => $this->input->post('start_date'),
'end_date' => $this->input->post('end_date'),
'max_usage' => $this->input->post('max_usage') ?: null,
'max_usage_per_user' => $this->input->post('max_usage_per_user') ?: null,
'is_active' => $this->input->post('is_active') ? 1 : 0,
'is_stackable' => $this->input->post('is_stackable') ? 1 : 0,
'priority' => $this->input->post('priority') ?: $this->_get_next_priority(),
'banner_image' => $banner_image,
'thumbnail' => $thumbnail_image,
// Convert arrays to JSON (hanya jika ada data)
'applicable_categories' => !empty($applicable_categories) ? json_encode(array_values($applicable_categories)) : null,
'applicable_products' => !empty($applicable_products) ? json_encode(array_values($applicable_products)) : null,
'excluded_products' => !empty($excluded_products) ? json_encode(array_values($excluded_products)) : null,
'created_at' => date('Y-m-d H:i:s'),
'created_by' => $this->session->userdata('user_id') ?: 'Admin',
];
return $data;
}
/**
* Handle file upload for edit (dengan option keep existing file)
*/
private function _handle_file_upload_edit($field_name, $prefix, $existing_file = null)
{
// Jika tidak ada file baru yang diupload
if (empty($_FILES[$field_name]['name'])) {
// Cek apakah user ingin menghapus file (bisa lewat checkbox atau input hidden)
$remove_file = $this->input->post('remove_' . $field_name);
if ($remove_file) {
// Hapus file lama jika ada
if ($existing_file) {
$this->_delete_file($prefix, $existing_file);
}
return null;
}
// Jika tidak ada file baru dan tidak dihapus, keep existing file
return $existing_file;
}
// Ada file baru, hapus file lama
if ($existing_file) {
$this->_delete_file($prefix, $existing_file);
}
// Upload file baru
$upload_paths = [
'banner' => './uploads/campaign/full/',
'thumbnail' => './uploads/campaign/thumbnail/',
];
$config['upload_path'] = isset($upload_paths[$prefix]) ? $upload_paths[$prefix] : './uploads/campaigns/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|webp';
$config['max_size'] = 2048;
$config['file_name'] = $prefix . '_' . time() . '_' . uniqid();
// Buat folder kalau belum ada
if (!is_dir($config['upload_path'])) {
mkdir($config['upload_path'], 0755, true);
}
$this->upload->initialize($config);
if ($this->upload->do_upload($field_name)) {
$upload_data = $this->upload->data();
return $upload_data['file_name'];
} else {
throw new Exception('File upload failed: ' . $this->upload->display_errors());
}
}
/**
* Delete old file
*/
private function _delete_file($prefix, $filename)
{
if (empty($filename)) {
return;
}
$upload_paths = [
'banner' => './uploads/campaign/full/',
'thumbnail' => './uploads/campaign/thumbnail/',
];
$file_path = $upload_paths[$prefix] . $filename;
if (file_exists($file_path)) {
unlink($file_path);
}
}
/**
* Prepare campaign data for update
*/
private function _prepare_campaign_data_edit($banner_image, $thumbnail_image)
{
// Mengambil array data dari POST
$applicable_categories = $this->input->post('applicable_categories');
$applicable_products = $this->input->post('applicable_products');
$excluded_products = $this->input->post('excluded_products');
// Combine date and time untuk start_date dan end_date
$start_date = $this->input->post('start_date');
$start_time = $this->input->post('start_time');
$end_date = $this->input->post('end_date');
$end_time = $this->input->post('end_time');
$data = [
'name' => $this->input->post('name'),
'code' => $this->input->post('code'),
'description' => $this->input->post('description'),
'discount_type' => $this->input->post('discount_type'),
'discount_value' => $this->input->post('discount_value') ?: 0,
'min_purchase' => $this->input->post('min_purchase') ?: null,
'max_discount' => $this->input->post('max_discount') ?: null,
'start_date' => $start_date . ' ' . $start_time . ':00',
'end_date' => $end_date . ' ' . $end_time . ':00',
'max_usage' => $this->input->post('max_usage') ?: null,
'max_usage_per_user' => $this->input->post('max_usage_per_user') ?: null,
'is_active' => $this->input->post('is_active') ? 1 : 0,
'is_stackable' => $this->input->post('is_stackable') ? 1 : 0,
'priority' => $this->input->post('priority') ?: 0,
// Convert arrays to JSON (hanya jika ada data)
'applicable_categories' => !empty($applicable_categories) ? json_encode(array_values($applicable_categories)) : null,
'applicable_products' => !empty($applicable_products) ? json_encode(array_values($applicable_products)) : null,
'excluded_products' => !empty($excluded_products) ? json_encode(array_values($excluded_products)) : null,
'updated_at' => date('Y-m-d H:i:s'),
];
// Hanya update image jika ada perubahan
if ($banner_image !== null) {
$data['banner_image'] = $banner_image;
}
if ($thumbnail_image !== null) {
$data['thumbnail'] = $thumbnail_image;
}
return $data;
}
private function _get_next_priority()
{
$this->db->select_max('priority', 'max_priority');
$this->db->from('promotional_campaigns');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$result = $query->row();
$max_priority = $result->max_priority;
return (int)$max_priority + 1;
}
return 1;
}
}