hubtel_sms/models/Hubtel_sms_model.php
2025-01-13 19:14:18 +00:00

287 lines
9.6 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Hubtel_sms_model extends App_Model
{
public function __construct()
{
parent::__construct();
}
public function get_messages($id = '')
{
if (is_numeric($id)) {
$this->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;
}
}