<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class LiteCirPublish extends ADMIN_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model(array('lite_cir_model','header_model'));
}
public function index()
{
$this->login_check();
$data = $head = array();
$head['title'] = 'CostMasters Admin - Lite CIR Report Publish';
$head['description'] = '!';
$head['keywords'] = '';
$head['profile'] = $this->header_model->profile($this->user_id);
$data['lite_cir_reports'] = $this->lite_cir_model->get_reports();
$this->load->view('parts/header', $head);
$this->load->view('lite_cir_publish/upload_lite_cir', $data);
$this->load->view('parts/footer');
}
public function do_upload()
{
$check_data = [
'subscribe_month' => $this->input->post('month'),
'subscribe_year' => $this->input->post('year')
];
// Validate input data
if (!$this->validate_upload()) {
return $this->upload();
}
// Retrieve product and subscriber data
$id_arr = $this->get_product_ids();
$lite_subscribers = $this->lite_cir_model->lite_cir_subscribers($id_arr);
$checkcir=$this->lite_cir_model->check_cir_report($check_data);
if (empty($lite_subscribers)) {
return $this->set_error_and_redirect('No CIR Lite report subscriber found!');
}
if ($this->lite_cir_model->check_report($check_data)) {
return $this->set_error_and_redirect('CIR Lite report is already published!');
}
// Handle file upload
$this->handle_file_upload($lite_subscribers,$checkcir);
redirect('admin/lite-cir-publish');
}
private function validate_upload()
{
$this->form_validation->set_rules('month', 'Month', 'trim|required');
$this->form_validation->set_rules('year', 'Year', 'trim|required');
$this->form_validation->set_rules('last_updation', 'Last updation date', 'trim|required');
$this->form_validation->set_rules('lite_cirfile', 'CIR Lite Report File', 'callback_file_check');
return $this->form_validation->run();
}
private function get_product_ids()
{
$id_arr = [];
$products = $this->lite_cir_model->lite_cir_products();
foreach ($products as $product) {
$id_arr[] = $product->product_id;
}
return $id_arr;
}
private function set_error_and_redirect($message)
{
$this->session->set_flashdata('error', $message);
redirect('admin/lite-cir-publish');
}
public function do_upload_with_data($month, $year, $last_updation, $file_path)
{
// Prepare check data
$check_data = [
'subscribe_month' => $month,
'subscribe_year' => $year
];
// Retrieve product and subscriber data
$id_arr = $this->get_product_ids();
$lite_subscribers = $this->lite_cir_model->lite_cir_subscribers($id_arr);
$checkcir=$this->lite_cir_model->check_cir_report($check_data);
if (empty($lite_subscribers)) {
$this->session->set_flashdata('error', 'No CIR Lite report subscriber found!');
$this->session->set_flashdata('message', '');
//redirect('admin/lite-cir-publish');
}
if ($this->lite_cir_model->check_report($check_data)) {
$this->session->set_flashdata('error', 'CIR Lite report is already published!');
$this->session->set_flashdata('message', '');
//redirect('admin/lite-cir-publish');
}
// Handle file upload from the specified path
$this->handle_file_upload($lite_subscribers,$checkcir, $file_path, $month, $year, $last_updation);
// redirect('admin/lite-cir-publish');
}
private function handle_file_upload($lite_subscribers,$checkcir, $file_path = null, $month = null, $year = null, $last_updation = null)
{
$config = [
'upload_path' => './uploads/lite_cir_report/',
'allowed_types' => 'pdf',
'max_size' => 1024 * 12, // 12 MB
'max_width' => 1500,
'max_height' => 1500
];
$subscribers = $this->lite_cir_model->subscribers();
// echo $this->db->last_query(); die;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($file_path === null) {
if (!$this->upload->do_upload('lite_cirfile')) {
return $this->set_error_and_redirect($this->upload->display_errors());
}
$report_name = $this->upload->data('file_name');
} else {
$report_name = $this->upload_file_from_path($file_path);
}
$input_data = [
'subscriber_id' => implode(',', array_column($lite_subscribers, 'id')),
'subscribe_month' => $month ?? $this->input->post('month'),
'subscribe_year' => $year ?? $this->input->post('year'),
'last_updation' => $last_updation ?? $this->input->post('last_updation'),
'subscribe_link' => $report_name
];
log_message('info', 'Input Data: ' . json_encode($input_data));
// Insert report data into the database
if ($this->lite_cir_model->set_report($input_data)) {
$this->session->set_flashdata('message', 'LITE CIR report is successfully published.');
$this->schedule_email_notifications($lite_subscribers, $subscribers, $checkcir, $report_name);
} else {
$this->set_error_and_redirect('Could not publish report!');
}
}
private function upload_file_from_path($file_path)
{
$absolute_file_path = FCPATH . $file_path;
if (!file_exists($absolute_file_path)) {
throw new Exception('File does not exist at the provided path: ' . $absolute_file_path);
}
$destination = FCPATH . 'uploads/lite_cir_report/' . basename($absolute_file_path);
if (copy($absolute_file_path, $destination)) {
return basename($absolute_file_path);
}
throw new Exception('File could not be copied to the upload directory.');
}
private function schedule_email_notifications($lite_subscribers, $subscribers, $checkcir, $report_name)
{
// Save scheduling data to database
$data = [
'lite_subscribers' => json_encode($lite_subscribers),
'subscribers' => json_encode($subscribers),
'checkcir' => json_encode($checkcir),
'report_name' => $report_name, // Save original report name for reference
'scheduled_at' => date('Y-m-d H:i:s', strtotime('+12 hours'))
];
if ($this->db->insert('email_scheduling_queue', $data)) {
log_message('info', 'Email scheduling data saved successfully.');
} else {
log_message('error', 'Failed to save email scheduling data.');
}
}
public function edit($id)
{
$this->login_check();
$data = $head = array();
$head['title'] = 'CostMasters Admin - Lite CIR Report Publish';
$head['description'] = '!';
$head['keywords'] = '';
$head['profile'] = $this->header_model->profile($this->user_id);
$report_data = $this->lite_cir_model->get_report($id);
if(count($report_data)==0)
{
$this->session->set_flashdata('error','lite cir report not found !!');
$this->session->set_flashdata('message','');
redirect('admin/lite-cir-publish');
}
$data['report']=$report_data;
$this->load->view('parts/header', $head);
$this->load->view('lite_cir_publish/edit_lite_cir',$data);
$this->load->view('parts/footer');
}
public function update()
{
$id=$this->input->post('cir_lite_id');
$report_data = $this->lite_cir_model->get_report($id);
$check=0;
$month = (empty($this->input->post('month')))?'':$this->input->post('month');
$year = (empty($this->input->post('year')))?'':$this->input->post('year');
if($report_data[0]->subscribe_month !=$month || $report_data[0]->subscribe_year !=$year)
{
$check_data= array(
'subscribe_month' => $month,
'subscribe_year' => $year
);
$check=$this->lite_cir_model->check_report($check_data);
}
$this->form_validation->set_rules('month', 'Month', 'trim|required');
$this->form_validation->set_rules('year', 'Year', 'trim|required');
$this->form_validation->set_rules('last_updation', 'Last updation date', 'trim|required');
if (!empty($_FILES['lite_cirfile']['name']))
{
$this->form_validation->set_rules('lite_cirfile', 'Lite CIR Report File', 'callback_file_check');
}
if ($this->form_validation->run() == FALSE)
{
$this->edit($id);
}
else if(count($report_data)==0)
{
$this->session->set_flashdata('error','lite cir report not found !!');
$this->session->set_flashdata('message','');
redirect('admin/lite-cir-publish');
}
else if($check)
{
$this->session->set_flashdata('error','lite cir report is already published !!');
$this->session->set_flashdata('message','');
redirect('admin/lite-cir-publish');
}
else
{
if (!empty($_FILES['lite_cirfile']['name']))
{
$target_dir = "./uploads/lite_cir_report/";
$target_file = $target_dir.basename($_FILES["lite_cirfile"]["name"]);
$filename = strtolower(pathinfo($target_file,PATHINFO_FILENAME));
$extension = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$new_name = $filename.time().'.'.$extension;
$config['upload_path'] = './uploads/lite_cir_report/';
$config['allowed_types'] = 'pdf';
$config['max_size'] = 1024*12;
$config['max_width'] = 1500;
$config['max_height'] = 1500;
$config['file_name'] = $new_name;
$this->upload->initialize($config);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('lite_cirfile'))
{
$upload_arr = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error',$upload_arr['error']);
$this->session->set_flashdata('message','');
redirect('admin/lite-cir-publish');
}
else
{
$upload_arr = $this->upload->data();
$report_name=$upload_arr['file_name'];
}
}
else
{
$report_name = $report_data[0]->subscribe_link;
}
$input_data = array(
'id' => $id,
'subscribe_month' => $this->input->post('month'),
'subscribe_year' => $this->input->post('year'),
'last_updation' => $this->input->post('last_updation'),
'subscribe_link' => $report_name
);
$response=$this->lite_cir_model->update_report($input_data);
if($response)
{
$this->session->set_flashdata('message','lite cir report is successfully updated.');
$this->session->set_flashdata('error','');
redirect('admin/lite-cir-publish');
}
else
{
$this->session->set_flashdata('error','could not update report !!');
$this->session->set_flashdata('message','');
redirect('admin/lite-cir-publish');
}
}
}
public function file_check($str)
{
$allowed_mime_type_arr = array('application/pdf');
$mime = get_mime_by_extension($_FILES['lite_cirfile']['name']);
if(isset($_FILES['lite_cirfile']['name']) && $_FILES['lite_cirfile']['name']!=""){
if(in_array($mime, $allowed_mime_type_arr)){
return true;
}else{
$this->form_validation->set_message('file_check', 'Please select only a pdf file.');
return false;
}
}else{
$this->form_validation->set_message('file_check', 'Please choose a file to upload.');
return false;
}
}
public function delete($id)
{
$delete = $this->lite_cir_model->delete($id);
if($delete){
$this->session->set_flashdata('message', 'Lite CIR report deleted successfully.');
redirect('admin/lite-cir-publish');
}else{
redirect('admin/lite-cir-publish');
}
}
} |