From 2889314a6c82e6ea664a670af3ecdda3c920901f Mon Sep 17 00:00:00 2001 From: sackey Date: Mon, 13 Jan 2025 19:14:18 +0000 Subject: [PATCH] Initial commit --- assets/js/hubtel_sms.js | 140 +++++++++ config.php | 0 controllers/Hubtel_sms.php | 221 ++++++++++++++ helpers/hubtel_sms_helper.php | 21 ++ hubtel_sms.php | 72 +++++ install.php | 63 ++++ language/english/hubtel_sms_lang.php | 109 +++++++ libraries/Hubtel_api.php | 172 +++++++++++ models/Hubtel_sms_model.php | 287 ++++++++++++++++++ uninstall.php | 10 + views/hubtel_sms/modals/bulk_sms.php | 76 +++++ views/hubtel_sms/modals/preview_template.php | 42 +++ views/hubtel_sms/modals/send_sms.php | 55 ++++ views/hubtel_sms/modals/view_message.php | 18 ++ views/manage.php | 301 +++++++++++++++++++ views/message_details.php | 103 +++++++ views/recipients_preview.php | 26 ++ views/settings.php | 87 ++++++ views/template.php | 75 +++++ views/template_preview.php | 36 +++ 20 files changed, 1914 insertions(+) create mode 100644 assets/js/hubtel_sms.js create mode 100644 config.php create mode 100644 controllers/Hubtel_sms.php create mode 100644 helpers/hubtel_sms_helper.php create mode 100644 hubtel_sms.php create mode 100644 install.php create mode 100644 language/english/hubtel_sms_lang.php create mode 100644 libraries/Hubtel_api.php create mode 100644 models/Hubtel_sms_model.php create mode 100644 uninstall.php create mode 100644 views/hubtel_sms/modals/bulk_sms.php create mode 100644 views/hubtel_sms/modals/preview_template.php create mode 100644 views/hubtel_sms/modals/send_sms.php create mode 100644 views/hubtel_sms/modals/view_message.php create mode 100644 views/manage.php create mode 100644 views/message_details.php create mode 100644 views/recipients_preview.php create mode 100644 views/settings.php create mode 100644 views/template.php create mode 100644 views/template_preview.php diff --git a/assets/js/hubtel_sms.js b/assets/js/hubtel_sms.js new file mode 100644 index 0000000..dee9ac3 --- /dev/null +++ b/assets/js/hubtel_sms.js @@ -0,0 +1,140 @@ +// Initialize components on document ready +$(function() { + // Initialize tooltips + $('[data-toggle="tooltip"]').tooltip(); + + // Initialize select2 for dropdowns + $('.select2').select2(); + + // Initialize client groups select2 + $('#client_groups').select2({ + placeholder: app.lang.select_client_groups, + allowClear: true + }); + + // Handle message character count + function updateCharCount(textarea, charCount, msgCount) { + var chars = $(textarea).val().length; + $(charCount).text(chars); + $(msgCount).text(Math.ceil(chars / 160)); + } + + $('#message').on('keyup', function() { + updateCharCount(this, '#char_count', '#messages_count'); + }); + + $('#message_bulk').on('keyup', function() { + updateCharCount(this, '#char_count_bulk', '#messages_count_bulk'); + }); + + // Handle client groups selection + $('#client_groups').on('change', function() { + var selectedGroups = $(this).val(); + if (selectedGroups) { + $.get(admin_url + 'hubtel_sms/get_recipients_preview', { + groups: selectedGroups + }, function(response) { + $('#recipients_preview').html(response.html); + $('#recipients_count').text(response.count); + }, 'json'); + } else { + $('#recipients_preview').html(''); + $('#recipients_count').text('0'); + } + }); + + // Handle template selection for single SMS + $('#template_id').on('change', function() { + var templateId = $(this).val(); + if (templateId) { + $.get(admin_url + 'hubtel_sms/get_template/' + templateId, function(response) { + if (response.success) { + $('#message').val(response.template.template); + updateCharCount('#message', '#char_count', '#messages_count'); + } + }, 'json'); + } + }); + + // Handle template selection for bulk SMS + $('#template_id_bulk').on('change', function() { + var templateId = $(this).val(); + if (templateId) { + $.get(admin_url + 'hubtel_sms/get_template/' + templateId, function(response) { + if (response.success) { + $('#message_bulk').val(response.template.template); + updateCharCount('#message_bulk', '#char_count_bulk', '#messages_count_bulk'); + } + }, 'json'); + } + }); + + // Handle form submissions + $('#sms-form').on('submit', function() { + var $btn = $('#sendSmsBtn').prop('disabled', true); + $btn.html(' ' + app.lang.sending); + }); + + $('#bulk-sms-form').on('submit', function() { + if (!confirm(app.lang.bulk_sms_confirm)) { + return false; + } + var $btn = $('#sendBulkSmsBtn').prop('disabled', true); + $btn.html(' ' + app.lang.sending); + }); +}); + +// Function to show send SMS modal +function send_sms_modal() { + $('#send_sms_modal').modal('show'); + $('#send_sms_modal').find('form')[0].reset(); + $('#template_id').val('').trigger('change'); + $('#char_count').text('0'); + $('#messages_count').text('1'); +} + +// Function to show bulk SMS modal +function bulk_sms_modal() { + $('#bulk_sms_modal').modal('show'); + $('#bulk_sms_modal').find('form')[0].reset(); + $('#client_groups').val('').trigger('change'); + $('#template_id_bulk').val('').trigger('change'); + $('#char_count_bulk').text('0'); + $('#messages_count_bulk').text('1'); + $('#recipients_preview').html(''); + $('#recipients_count').text('0'); +} + +// Function to view message details +function view_message(id) { + $('#message_details').html('
'); + $('#view_message_modal').modal('show'); + + $.get(admin_url + 'hubtel_sms/view_message/' + id, function(response) { + $('#message_details').html(response); + }); +} + +// Function to preview template +function preview_template(id) { + $('#template_preview').html('
'); + $('#preview_template_modal').modal('show'); + + $.get(admin_url + 'hubtel_sms/preview_template/' + id, function(response) { + $('#template_preview').html(response); + }); +} + +// Function to resend SMS +function resend_sms(id) { + if (confirm(app.lang.confirm_action_prompt)) { + $.get(admin_url + 'hubtel_sms/resend/' + id, function(response) { + if (response.success) { + alert_float('success', response.message); + location.reload(); + } else { + alert_float('danger', response.message); + } + }, 'json'); + } +} \ No newline at end of file diff --git a/config.php b/config.php new file mode 100644 index 0000000..e69de29 diff --git a/controllers/Hubtel_sms.php b/controllers/Hubtel_sms.php new file mode 100644 index 0000000..bf3d79f --- /dev/null +++ b/controllers/Hubtel_sms.php @@ -0,0 +1,221 @@ +load->helper('hubtel_sms/hubtel_sms'); + $this->load->model('hubtel_sms_model'); + $this->load->library('hubtel_sms/hubtel_api'); + } + + public function index() + { + if (!has_permission('hubtel_sms', '', 'view')) { + access_denied('Hubtel SMS'); + } + + if (!is_hubtel_sms_enabled()) { + set_alert('warning', _l('hubtel_sms_module_disabled')); + redirect(admin_url()); + } + + $data['title'] = _l('hubtel_sms'); + + // Get statistics + $data['total_messages'] = $this->hubtel_sms_model->get_total_messages(); + $data['sent_messages'] = $this->hubtel_sms_model->get_total_messages('sent'); + $data['failed_messages'] = $this->hubtel_sms_model->get_total_messages('failed'); + $data['total_cost'] = $this->hubtel_sms_model->get_total_cost(); + + // Get messages and templates + $data['messages'] = $this->hubtel_sms_model->get_messages(); + $data['templates'] = $this->hubtel_sms_model->get_templates(); + + // Load client groups for bulk SMS + $this->load->model('client_groups_model'); + $data['client_groups'] = $this->client_groups_model->get_groups(); + + $this->load->view('hubtel_sms/manage', $data); + } + + public function send_sms() + { + if (!has_permission('hubtel_sms', '', 'create')) { + access_denied('Send SMS'); + } + + if ($this->input->post()) { + $response = $this->hubtel_sms_model->send_sms( + $this->input->post('to'), + $this->input->post('message'), + $this->input->post('template_id') + ); + + if ($response['success']) { + set_alert('success', _l('sms_sent_successfully')); + } else { + set_alert('danger', $response['message']); + } + } + + redirect(admin_url('hubtel_sms')); + } + + public function send_bulk_sms() + { + if (!has_permission('hubtel_sms', '', 'create')) { + access_denied('Send Bulk SMS'); + } + + if ($this->input->post()) { + $client_groups = $this->input->post('client_groups'); + $message = $this->input->post('message'); + $template_id = $this->input->post('template_id'); + + $success = 0; + $failed = 0; + $total_recipients = 0; + + if (!empty($client_groups)) { + $this->load->model('clients_model'); + foreach ($client_groups as $group_id) { + $clients = $this->clients_model->get_clients_by_group($group_id); + foreach ($clients as $client) { + if (!empty($client['phonenumber'])) { + $total_recipients++; + $response = $this->hubtel_sms_model->send_sms( + $client['phonenumber'], + $message, + $template_id + ); + if ($response['success']) { + $success++; + } else { + $failed++; + } + } + } + } + } + + set_alert('info', sprintf( + _l('bulk_sms_sent_info'), + $total_recipients, + $success, + $failed + )); + } + + redirect(admin_url('hubtel_sms')); + } + + public function view_message($id) + { + if (!has_permission('hubtel_sms', '', 'view')) { + access_denied('View Message'); + } + + $message = $this->hubtel_sms_model->get_messages($id); + $logs = $this->hubtel_sms_model->get_message_logs($message->message_id); + + echo $this->load->view('hubtel_sms/message_details', [ + 'message' => $message, + 'logs' => $logs + ], true); + } + + public function preview_template($id) + { + if (!has_permission('hubtel_sms', '', 'view')) { + access_denied('Preview Template'); + } + + $template = $this->hubtel_sms_model->get_template($id); + echo $this->load->view('hubtel_sms/template_preview', [ + 'template' => $template + ], true); + } + + public function get_template($id) + { + if (!has_permission('hubtel_sms', '', 'view')) { + access_denied('Get Template'); + } + + $template = $this->hubtel_sms_model->get_template($id); + + if ($template) { + echo json_encode([ + 'success' => true, + 'template' => $template + ]); + } else { + echo json_encode([ + 'success' => false, + 'message' => _l('template_not_found') + ]); + } + } + + public function resend($id) + { + if (!has_permission('hubtel_sms', '', 'create')) { + access_denied('Resend SMS'); + } + + $message = $this->hubtel_sms_model->get_messages($id); + if ($message) { + $response = $this->hubtel_sms_model->send_sms( + $message->to, + $message->message + ); + + echo json_encode($response); + } else { + echo json_encode([ + 'success' => false, + 'message' => _l('message_not_found') + ]); + } + } + + public function get_recipients_preview() +{ + if (!has_permission('hubtel_sms', '', 'view')) { + access_denied('Get Recipients Preview'); + } + + $groups = $this->input->get('groups'); + $recipients = []; + + if (!empty($groups)) { + $this->load->model('clients_model'); + foreach ($groups as $group_id) { + $clients = $this->clients_model->get_clients_by_group($group_id); + $group_name = $this->client_groups_model->get_group($group_id)->name; + + foreach ($clients as $client) { + if (!empty($client['phonenumber'])) { + $recipients[] = [ + 'company' => $client['company'], + 'phonenumber' => $client['phonenumber'], + 'group_name' => $group_name + ]; + } + } + } + } + + echo json_encode([ + 'success' => true, + 'html' => $this->load->view('hubtel_sms/recipients_preview', [ + 'recipients' => $recipients + ], true), + 'count' => count($recipients) + ]); +} +} \ No newline at end of file diff --git a/helpers/hubtel_sms_helper.php b/helpers/hubtel_sms_helper.php new file mode 100644 index 0000000..c65b8f0 --- /dev/null +++ b/helpers/hubtel_sms_helper.php @@ -0,0 +1,21 @@ +add_action('admin_init', 'hubtel_sms_module_init_menu_items'); +hooks()->add_action('admin_init', 'hubtel_sms_permissions'); +hooks()->add_action('admin_navbar_start', 'hubtel_sms_load_helper'); + +function hubtel_sms_module_init_menu_items() +{ + $CI = &get_instance(); + + if (has_permission('hubtel_sms', '', 'view')) { + $CI->app_menu->add_sidebar_menu_item('hubtel-sms', [ + 'name' => _l('hubtel_sms'), + 'href' => admin_url('hubtel_sms'), + 'icon' => 'fa fa-envelope', + 'position' => 30, + ]); + } +} + +function hubtel_sms_permissions() +{ + $capabilities = []; + + $capabilities['capabilities'] = [ + 'view' => _l('permission_hubtel_sms_view'), + 'create' => _l('permission_hubtel_sms_create'), + 'edit' => _l('permission_hubtel_sms_edit'), + 'delete' => _l('permission_hubtel_sms_delete') + ]; + + register_staff_capabilities('hubtel_sms', $capabilities, _l('hubtel_sms')); +} + +/** +* Register language files, must be registered if the module is using languages +*/ +register_language_files(HUBTEL_SMS_MODULE_NAME, [HUBTEL_SMS_MODULE_NAME]); + + +function hubtel_sms_activation_hook() +{ + require_once(__DIR__ . '/install.php'); +} + +// Load helper using hook instead of direct loading +function hubtel_sms_load_helper() +{ + $CI = &get_instance(); + $CI->load->helper('hubtel_sms/hubtel_sms'); +} \ No newline at end of file diff --git a/install.php b/install.php new file mode 100644 index 0000000..af927cb --- /dev/null +++ b/install.php @@ -0,0 +1,63 @@ +db->table_exists(db_prefix() . 'hubtel_sms_messages')) { + $CI->db->query('CREATE TABLE `' . db_prefix() . "hubtel_sms_messages` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `to` varchar(15) NOT NULL, + `message` text NOT NULL, + `rate` DECIMAL(10,2) NOT NULL DEFAULT 0.00, + `status` varchar(20) DEFAULT NULL, + `date_sent` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `sent_by` int(11) NOT NULL, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';'); +} + +// Create templates table +if (!$CI->db->table_exists(db_prefix() . 'hubtel_sms_templates')) { + $CI->db->query('CREATE TABLE `' . db_prefix() . "hubtel_sms_templates` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(100) NOT NULL, + `template` text NOT NULL, + `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_at` datetime DEFAULT NULL, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';'); +} + +// Create logs table +if (!$CI->db->table_exists(db_prefix() . 'hubtel_sms_logs')) { + $CI->db->query('CREATE TABLE `' . db_prefix() . "hubtel_sms_logs` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `message_id` int(11) NOT NULL, + `response` text, + `status` varchar(20) DEFAULT NULL, + `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';'); +} + + +// Check if new columns exist and add them if they don't +$CI->db->query("SHOW COLUMNS FROM `" . db_prefix() . "hubtel_sms_messages` LIKE 'response_code'"); +if ($CI->db->affected_rows() == 0) { + $CI->db->query("ALTER TABLE `" . db_prefix() . "hubtel_sms_messages` + ADD COLUMN `response_code` varchar(10) DEFAULT NULL, + ADD COLUMN `rate` decimal(10,2) DEFAULT NULL, + ADD COLUMN `network_id` varchar(50) DEFAULT NULL, + ADD COLUMN `response_message` text DEFAULT NULL, + ADD COLUMN `message_id` varchar(50) DEFAULT NULL"); +} \ No newline at end of file diff --git a/language/english/hubtel_sms_lang.php b/language/english/hubtel_sms_lang.php new file mode 100644 index 0000000..be036f5 --- /dev/null +++ b/language/english/hubtel_sms_lang.php @@ -0,0 +1,109 @@ +CI = &get_instance(); + + // Load Hubtel credentials from options + $this->client_id = get_option('hubtel_sms_client_id'); + $this->client_secret = get_option('hubtel_sms_client_secret'); + } + + /** + * Send SMS using Hubtel API + * + * @param string $to Recipient phone number (E164 format) + * @param string $message Message content (max 160 characters) + * @param string $from Sender ID (max 11 alpha-numeric characters) + * @return array Response with success status and message + */ + public function send_sms($to, $message, $from) + { + // Validate credentials + if (empty($this->client_id) || empty($this->client_secret)) { + return [ + 'success' => false, + 'message' => 'API credentials not configured' + ]; + } + + // Validate sender ID (must be 11 alpha-numeric characters or less) + if (strlen($from) > 11 || !preg_match('/^[a-zA-Z0-9]+$/', $from)) { + return [ + 'success' => false, + 'message' => 'Invalid Sender ID. Must be 11 alpha-numeric characters or less.' + ]; + } + + // Check message length + if (strlen($message) > 160) { + return [ + 'success' => false, + 'message' => 'Message content exceeds 160 characters' + ]; + } + + // Prepare the request URL with parameters + $params = [ + 'clientid' => $this->client_id, + 'clientsecret' => $this->client_secret, + 'from' => $from, + 'to' => $to, + 'content' => $message + ]; + + $url = $this->api_endpoint . '?' . http_build_query($params); + + // Set up cURL request + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_HTTPGET, true); // Using GET method as per API spec + + // Execute the request + $response = curl_exec($ch); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $curl_error = curl_error($ch); + + curl_close($ch); + + // Handle cURL errors + if ($curl_error) { + return [ + 'success' => false, + 'message' => 'cURL Error: ' . $curl_error + ]; + } + + // Parse response + $result = json_decode($response, true); + + // Handle API response + if ($http_code === 200) { + return [ + 'success' => true, + 'message' => $result['message'] ?? 'Message sent', + 'responseCode' => $result['responseCode'] ?? '', + 'data' => [ + 'rate' => $result['data']['rate'] ?? 0, + 'messageId' => $result['data']['messageId'] ?? '', + 'status' => $result['data']['status'] ?? 0, + 'networkId' => $result['data']['networkId'] ?? '' + ] + ]; + } + + return [ + 'success' => false, + 'message' => $result['message'] ?? 'Failed to send SMS', + 'responseCode' => $result['responseCode'] ?? '', + 'data' => $result['data'] ?? null + ]; + } + + /** + * Get account balance + * + * @return array Response with account balance + */ + public function get_balance() + { + if (empty($this->client_id) || empty($this->client_secret)) { + return [ + 'success' => false, + 'message' => 'API credentials not configured' + ]; + } + + $params = [ + 'clientid' => $this->client_id, + 'clientsecret' => $this->client_secret + ]; + + $url = 'https://smsc.hubtel.com/v1/accounts/balance?' . http_build_query($params); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_HTTPGET, true); + + $response = curl_exec($ch); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $curl_error = curl_error($ch); + + curl_close($ch); + + if ($curl_error) { + return [ + 'success' => false, + 'message' => 'cURL Error: ' . $curl_error + ]; + } + + $result = json_decode($response, true); + + if ($http_code === 200) { + return [ + 'success' => true, + 'balance' => $result['data']['balance'] ?? 0, + 'currency' => $result['data']['currency'] ?? 'GHS', + 'responseCode' => $result['responseCode'] ?? '', + 'response' => $result + ]; + } + + return [ + 'success' => false, + 'message' => $result['message'] ?? 'Failed to get balance', + 'responseCode' => $result['responseCode'] ?? '', + 'response' => $result + ]; + } +} \ No newline at end of file diff --git a/models/Hubtel_sms_model.php b/models/Hubtel_sms_model.php new file mode 100644 index 0000000..cca259b --- /dev/null +++ b/models/Hubtel_sms_model.php @@ -0,0 +1,287 @@ +db->where('id', $id); + return $this->db->get(db_prefix() . 'hubtel_sms_messages')->row(); + } + + return $this->db->get(db_prefix() . 'hubtel_sms_messages')->result_array(); + } + + public function get_template($id) + { + $this->db->where('id', $id); + return $this->db->get(db_prefix() . 'hubtel_sms_templates')->row(); + } + + public function get_templates() + { + return $this->db->get(db_prefix() . 'hubtel_sms_templates')->result_array(); + } + + public function add_template($data) + { + $this->db->insert(db_prefix() . 'hubtel_sms_templates', [ + 'name' => $data['name'], + 'template' => $data['template'] + ]); + + return $this->db->insert_id(); + } + + public function update_template($id, $data) + { + $this->db->where('id', $id); + $this->db->update(db_prefix() . 'hubtel_sms_templates', [ + 'name' => $data['name'], + 'template' => $data['template'], + 'updated_at' => date('Y-m-d H:i:s') + ]); + + return $this->db->affected_rows() > 0; + } + + public function delete_template($id) + { + $this->db->where('id', $id); + $this->db->delete(db_prefix() . 'hubtel_sms_templates'); + + return $this->db->affected_rows() > 0; + } + + public function send_sms($to, $message, $template_id = null) + { + // Get Hubtel API credentials + $client_id = get_option('hubtel_sms_client_id'); + $client_secret = get_option('hubtel_sms_client_secret'); + $sender_id = get_option('hubtel_sms_sender_id'); + + if (empty($client_id) || empty($client_secret) || empty($sender_id)) { + return [ + 'success' => false, + 'message' => 'Hubtel API credentials not configured' + ]; + } + + // If template is provided, get the message from template + if ($template_id) { + $template = $this->get_template($template_id); + if ($template) { + $message = $template->template; + } + } + + // Record the message in database + $message_data = [ + 'to' => $to, + 'message' => $message, + 'status' => 'pending', + 'sent_by' => get_staff_user_id() + ]; + + $this->db->insert(db_prefix() . 'hubtel_sms_messages', $message_data); + $message_id = $this->db->insert_id(); + + // Send SMS using Hubtel API + $response = $this->hubtel_api->send_sms($to, $message, $sender_id); + + // Update message status and log the response + $this->db->where('id', $message_id); + $this->db->update(db_prefix() . 'hubtel_sms_messages', [ + 'status' => $response['success'] ? 'sent' : 'failed' + ]); + + // Log the response + $this->db->insert(db_prefix() . 'hubtel_sms_logs', [ + 'message_id' => $message_id, + 'response' => json_encode($response), + 'status' => $response['success'] ? 'success' : 'failed' + ]); + + return $response; + } + + public function get_total_messages($status = '') + { + if ($status !== '') { + $this->db->where('status', $status); + } + return $this->db->count_all_results(db_prefix() . 'hubtel_sms_messages'); + } + + public function get_total_cost() + { + $this->db->select_sum('rate'); + $this->db->where('status', 'sent'); + $query = $this->db->get(db_prefix() . 'hubtel_sms_messages'); + return $query->row()->rate ?? 0; + } + + // public function get_messages($id = '') + // { + // if (is_numeric($id)) { + // $this->db->where('id', $id); + // return $this->db->get(db_prefix() . 'hubtel_sms_messages')->row(); + // } + + // $this->db->order_by('date_sent', 'desc'); + // return $this->db->get(db_prefix() . 'hubtel_sms_messages')->result_array(); + // } + + // public function get_template($id) + // { + // $this->db->where('id', $id); + // return $this->db->get(db_prefix() . 'hubtel_sms_templates')->row(); + // } + + // public function get_templates() + // { + // $this->db->order_by('name', 'asc'); + // return $this->db->get(db_prefix() . 'hubtel_sms_templates')->result_array(); + // } + + // public function add_template($data) + // { + // $this->db->insert(db_prefix() . 'hubtel_sms_templates', [ + // 'name' => $data['name'], + // 'template' => $data['template'] + // ]); + // return $this->db->insert_id(); + // } + + // public function update_template($id, $data) + // { + // $this->db->where('id', $id); + // $this->db->update(db_prefix() . 'hubtel_sms_templates', [ + // 'name' => $data['name'], + // 'template' => $data['template'], + // 'updated_at' => date('Y-m-d H:i:s') + // ]); + // return $this->db->affected_rows() > 0; + // } + + // public function delete_template($id) + // { + // $this->db->where('id', $id); + // $this->db->delete(db_prefix() . 'hubtel_sms_templates'); + // return $this->db->affected_rows() > 0; + // } + + public function get_message_logs($message_id) + { + $this->db->where('message_id', $message_id); + $this->db->order_by('created_at', 'desc'); + return $this->db->get(db_prefix() . 'hubtel_sms_logs')->result_array(); + } + + // public function send_sms($to, $message, $template_id = null) + // { + // // Get Hubtel API credentials + // $sender_id = get_option('hubtel_sms_sender_id'); + + // // Validate credentials + // if (!$this->validate_credentials()) { + // return [ + // 'success' => false, + // 'message' => 'Hubtel API credentials not configured' + // ]; + // } + + // // Process template if provided + // if ($template_id) { + // $template = $this->get_template($template_id); + // if ($template) { + // $message = $this->process_template_merge_fields($template->template); + // } + // } + + // // Record initial message in database + // $message_data = [ + // 'to' => $to, + // 'message' => $message, + // 'status' => 'pending', + // 'sent_by' => get_staff_user_id(), + // 'date_sent' => date('Y-m-d H:i:s') + // ]; + + // $this->db->insert(db_prefix() . 'hubtel_sms_messages', $message_data); + // $message_id = $this->db->insert_id(); + + // // Send SMS using Hubtel API + // $response = $this->hubtel_api->send_sms($to, $message, $sender_id); + + // // Update message with API response + // $update_data = [ + // 'status' => $response['success'] ? 'sent' : 'failed', + // 'message_id' => $response['data']['messageId'] ?? null, + // 'rate' => $response['data']['rate'] ?? null, + // 'network_id' => $response['data']['networkId'] ?? null, + // 'response_code' => $response['responseCode'] ?? null, + // 'response_message' => $response['message'] ?? null + // ]; + + // $this->db->where('id', $message_id); + // $this->db->update(db_prefix() . 'hubtel_sms_messages', $update_data); + + // // Log the complete response + // $this->db->insert(db_prefix() . 'hubtel_sms_logs', [ + // 'message_id' => $response['data']['messageId'] ?? null, + // 'response_code' => $response['responseCode'] ?? null, + // 'response_message' => $response['message'] ?? null, + // 'rate' => $response['data']['rate'] ?? null, + // 'network_id' => $response['data']['networkId'] ?? null, + // 'status' => $response['success'] ? 'success' : 'failed', + // 'created_at' => date('Y-m-d H:i:s') + // ]); + + // return [ + // 'success' => $response['success'], + // 'message' => $response['message'], + // 'messageId' => $response['data']['messageId'] ?? null, + // 'rate' => $response['data']['rate'] ?? null, + // 'responseCode' => $response['responseCode'] ?? null + // ]; + // } + + private function validate_credentials() + { + $client_id = get_option('hubtel_sms_client_id'); + $client_secret = get_option('hubtel_sms_client_secret'); + $sender_id = get_option('hubtel_sms_sender_id'); + + return !empty($client_id) && !empty($client_secret) && !empty($sender_id); + } + + private function process_template_merge_fields($template) + { + // Add merge field processing here + // Example: Replace {contact_firstname} with actual value + $CI = &get_instance(); + + // Load necessary models + $CI->load->model('clients_model'); + $CI->load->model('invoices_model'); + + // Process client fields + $client_id = get_client_user_id(); + if ($client_id) { + $client = $CI->clients_model->get($client_id); + $template = str_replace('{contact_firstname}', $client->firstname ?? '', $template); + $template = str_replace('{contact_lastname}', $client->lastname ?? '', $template); + $template = str_replace('{client_company}', $client->company ?? '', $template); + $template = str_replace('{client_phonenumber}', $client->phonenumber ?? '', $template); + } + + return $template; + } +} \ No newline at end of file diff --git a/uninstall.php b/uninstall.php new file mode 100644 index 0000000..5d4c0f1 --- /dev/null +++ b/uninstall.php @@ -0,0 +1,10 @@ +db->query("DELETE FROM " . db_prefix() . "options WHERE name LIKE 'hubtel_sms_%'"); + +// Drop module tables +$CI->db->query("DROP TABLE IF EXISTS " . db_prefix() . "hubtel_sms_messages"); +$CI->db->query("DROP TABLE IF EXISTS " . db_prefix() . "hubtel_sms_templates"); +$CI->db->query("DROP TABLE IF EXISTS " . db_prefix() . "hubtel_sms_logs"); \ No newline at end of file diff --git a/views/hubtel_sms/modals/bulk_sms.php b/views/hubtel_sms/modals/bulk_sms.php new file mode 100644 index 0000000..977e0be --- /dev/null +++ b/views/hubtel_sms/modals/bulk_sms.php @@ -0,0 +1,76 @@ + \ No newline at end of file diff --git a/views/hubtel_sms/modals/preview_template.php b/views/hubtel_sms/modals/preview_template.php new file mode 100644 index 0000000..1798ae1 --- /dev/null +++ b/views/hubtel_sms/modals/preview_template.php @@ -0,0 +1,42 @@ + \ No newline at end of file diff --git a/views/hubtel_sms/modals/send_sms.php b/views/hubtel_sms/modals/send_sms.php new file mode 100644 index 0000000..2412a34 --- /dev/null +++ b/views/hubtel_sms/modals/send_sms.php @@ -0,0 +1,55 @@ + \ No newline at end of file diff --git a/views/hubtel_sms/modals/view_message.php b/views/hubtel_sms/modals/view_message.php new file mode 100644 index 0000000..ca8e4d3 --- /dev/null +++ b/views/hubtel_sms/modals/view_message.php @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/views/manage.php b/views/manage.php new file mode 100644 index 0000000..5f8207e --- /dev/null +++ b/views/manage.php @@ -0,0 +1,301 @@ + + +
+
+
+
+ +
+
+
+
+

+ +
+
+
+
+
+
+

+ +
+
+
+
+
+
+

+ +
+
+
+
+
+
+

+ +
+
+
+
+ +
+
+
+ + + + + + + + + + + + + + + + + +
+
+
+ + + + + +
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + +
+ + + + + + + + +
+
+
+
+
+ + +
+
+
+ + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+ + + + + + + + + + + + + +
+
+
+
+
+
+
+
+
+
+
+
+ + +load->view('hubtel_sms/modals/send_sms'); ?> +load->view('hubtel_sms/modals/bulk_sms'); ?> +load->view('hubtel_sms/modals/view_message'); ?> +load->view('hubtel_sms/modals/preview_template'); ?> + + + + + \ No newline at end of file diff --git a/views/message_details.php b/views/message_details.php new file mode 100644 index 0000000..55c4e5f --- /dev/null +++ b/views/message_details.php @@ -0,0 +1,103 @@ + + +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
:to; ?>
:message); ?>
: + status == 'sent') { + $status_badge = 'success'; + } elseif ($message->status == 'failed') { + $status_badge = 'danger'; + } + ?> + + status); ?> + +
:message_id; ?>
:response_code; ?>
:rate, get_base_currency()); ?>
:network_id; ?>
:sent_by); ?>
:date_sent); ?>
+
+
+
+ + +
+
+

+
+
+ + + + + + + + + + + + + + + + + + + +
+ + + + +
+
+
+
+ \ No newline at end of file diff --git a/views/recipients_preview.php b/views/recipients_preview.php new file mode 100644 index 0000000..8896948 --- /dev/null +++ b/views/recipients_preview.php @@ -0,0 +1,26 @@ + + + +
+ + + + + + + + + + + + + + + + + +
+
+ +

+ \ No newline at end of file diff --git a/views/settings.php b/views/settings.php new file mode 100644 index 0000000..c0e77d2 --- /dev/null +++ b/views/settings.php @@ -0,0 +1,87 @@ + + +
+
+
+
+
+
+

+
+ +
+
+
+ + +
+
+ + +
+
+ + + +
+
+ +
+ +
+ > + +
+
+
+
+
+ +
+
+ +
+
+ +
+
+ Loading... +
+
+
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/views/template.php b/views/template.php new file mode 100644 index 0000000..203fa9a --- /dev/null +++ b/views/template.php @@ -0,0 +1,75 @@ + + +
+
+
+
+
+
+

+ + + + +

+
+ id : ''))); ?> +
+
+
+ + +
+
+ + +
+ +
+
+
+
+
+

:

+

{contact_firstname}

+

{contact_lastname}

+

{client_company}

+

{client_phonenumber}

+
+
+

:

+

{invoice_number}

+

{invoice_duedate}

+

{invoice_total}

+

{invoice_status}

+
+
+
+
+
+
+
+
+ +
+
+ +
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/views/template_preview.php b/views/template_preview.php new file mode 100644 index 0000000..a1d2f33 --- /dev/null +++ b/views/template_preview.php @@ -0,0 +1,36 @@ + + +
+
+

name; ?>

+
+
+ template); ?> +
+
+
+ +
+
+
+
+

+
+
+ template; + $preview = str_replace('{contact_firstname}', 'John', $preview); + $preview = str_replace('{contact_lastname}', 'Doe', $preview); + $preview = str_replace('{client_company}', 'Sample Company Ltd', $preview); + $preview = str_replace('{client_phonenumber}', '+233123456789', $preview); + $preview = str_replace('{invoice_number}', 'INV-2025-001', $preview); + $preview = str_replace('{invoice_duedate}', date('Y-m-d', strtotime('+7 days')), $preview); + $preview = str_replace('{invoice_total}', app_format_money(1000, get_base_currency()), $preview); + $preview = str_replace('{invoice_status}', _l('invoice_status_unpaid'), $preview); + echo htmlspecialchars($preview); + ?> +
+
+
+
\ No newline at end of file