Initial baseline WACloud CI3 production
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Auth_model extends CI_Model
|
||||
{
|
||||
|
||||
public function get_user($username)
|
||||
{
|
||||
$this->db->where('username', $username);
|
||||
$query = $this->db->get('users');
|
||||
return $query->row_array();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class Chat_model extends CI_Model {
|
||||
|
||||
// 🔹 Ambil semua session aktif beserta pesan terakhir
|
||||
public function get_all_sessions()
|
||||
{
|
||||
$sql = "
|
||||
SELECT
|
||||
s.id,
|
||||
s.customer_name,
|
||||
s.customer_number,
|
||||
s.last_message_at,
|
||||
s.status,
|
||||
(
|
||||
SELECT m.message_body
|
||||
FROM messages m
|
||||
WHERE m.session_id = s.id
|
||||
ORDER BY m.id DESC
|
||||
LIMIT 1
|
||||
) AS last_message
|
||||
FROM sessions s
|
||||
ORDER BY s.last_message_at DESC LIMIT 200
|
||||
";
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
// 🔹 Ambil semua semua chat per session
|
||||
public function get_messages_by_session($session_id)
|
||||
{
|
||||
$this->db->select('
|
||||
m.id,
|
||||
m.session_id,
|
||||
m.direction,
|
||||
m.message_type,
|
||||
m.message_body,
|
||||
m.content,
|
||||
m.status,
|
||||
m.timestamp,
|
||||
f.file_name,
|
||||
f.file_url,
|
||||
f.mime_type,
|
||||
f.file_size,
|
||||
g.name
|
||||
');
|
||||
$this->db->from('messages m');
|
||||
$this->db->join('message_files f', 'f.message_id = m.id', 'left');
|
||||
$this->db->join('users g', 'g.id = m.user_id', 'left');
|
||||
$this->db->where('m.session_id', $session_id);
|
||||
$this->db->order_by('m.timestamp', 'ASC');
|
||||
|
||||
$query = $this->db->get();
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
// 🔹 Ambil semua semua chat per session
|
||||
public function get_messages_by_id($messages_id)
|
||||
{
|
||||
$this->db->select('
|
||||
m.id,
|
||||
m.session_id,
|
||||
m.direction,
|
||||
m.message_type,
|
||||
m.message_body,
|
||||
m.content,
|
||||
m.status,
|
||||
m.timestamp,
|
||||
f.file_name,
|
||||
f.file_url,
|
||||
f.mime_type,
|
||||
f.file_size,
|
||||
g.name
|
||||
');
|
||||
$this->db->from('messages m');
|
||||
$this->db->join('message_files f', 'f.message_id = m.id', 'left');
|
||||
$this->db->join('users g', 'g.id = m.user_id', 'left');
|
||||
$this->db->where('m.id', $messages_id);
|
||||
|
||||
$query = $this->db->get();
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
// 🔹 Update status session jadi 'closed'
|
||||
public function updateStatusSession($session_id)
|
||||
{
|
||||
$this->db->where('id', $session_id);
|
||||
return $this->db->update('sessions', ['status' => 'closed']);
|
||||
}
|
||||
|
||||
// 🔹 Update user_id message white session active
|
||||
public function updateUserMessage($session_id, $user_id)
|
||||
{
|
||||
// 🔹 Gunakan array tunggal atau beberapa where terpisah
|
||||
$this->db->where([
|
||||
'session_id' => $session_id,
|
||||
'user_id' => 0,
|
||||
'direction' => 'in',
|
||||
]);
|
||||
|
||||
return $this->db->update('messages', ['user_id' => $user_id]);
|
||||
}
|
||||
|
||||
|
||||
public function get_template_chat($whatsapp_number_id, $template_name)
|
||||
{
|
||||
return $this->db
|
||||
->where('whatsapp_number_id', $whatsapp_number_id)
|
||||
->where('template_name', $template_name)
|
||||
->get('whatsapp_templates')
|
||||
->row();
|
||||
}
|
||||
|
||||
public function get_whatsapp_number_by_phone($phoneNumber)
|
||||
{
|
||||
return $this->db
|
||||
->where('customer_number', $phoneNumber)
|
||||
->get('sessions')
|
||||
->row();
|
||||
}
|
||||
|
||||
public function get_session_by_id($session_id)
|
||||
{
|
||||
return $this->db
|
||||
->where('id', $session_id)
|
||||
->get('sessions')
|
||||
->row();
|
||||
}
|
||||
|
||||
public function get_session_detail($session_id)
|
||||
{
|
||||
$this->db->select('
|
||||
m.customer_number,
|
||||
f.phone_number_id,
|
||||
f.access_token
|
||||
');
|
||||
$this->db->from('sessions m');
|
||||
$this->db->join('whatsapp_numbers f', 'f.id = m.whatsapp_number_id', 'left');
|
||||
$this->db->where('m.id', $session_id);
|
||||
|
||||
$query = $this->db->get();
|
||||
return $query->row_array();
|
||||
}
|
||||
|
||||
public function get_template($whatsapp_number_id, $template_name, $language = 'id')
|
||||
{
|
||||
$query = $this->db
|
||||
->select('*')
|
||||
->from('whatsapp_templates')
|
||||
->where('whatsapp_number_id', $whatsapp_number_id)
|
||||
->where('template_name', $template_name)
|
||||
->where('language', $language)
|
||||
->limit(1)
|
||||
->get();
|
||||
|
||||
$result = $query->row();
|
||||
|
||||
if ($result && isset($result->components)) {
|
||||
// Karena kolom JSON, biarkan seperti apa adanya
|
||||
$result->components = $result->components;
|
||||
}
|
||||
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function get_sessions_paginated($whatsapp_number_id, $limit = 15, $offset = 0, $search = null, $filter = 'all')
|
||||
{
|
||||
if (empty($whatsapp_number_id)) {
|
||||
// ⚠️ Kalau tidak ada ID, jangan ambil data apa pun
|
||||
return [];
|
||||
}
|
||||
|
||||
$this->db->select('*');
|
||||
$this->db->from('sessions');
|
||||
$this->db->where('whatsapp_number_id', $whatsapp_number_id);
|
||||
|
||||
// ✅ Filter unread session
|
||||
if ($filter === 'unread') {
|
||||
$this->db->where('status', 'open');
|
||||
}
|
||||
|
||||
// ✅ Pencarian nama / nomor pelanggan
|
||||
if (!empty($search)) {
|
||||
$this->db->group_start();
|
||||
$this->db->like('customer_name', $search);
|
||||
$this->db->or_like('customer_number', $search);
|
||||
$this->db->group_end();
|
||||
}
|
||||
|
||||
$this->db->order_by('last_message_at', 'DESC');
|
||||
$this->db->limit($limit, $offset);
|
||||
|
||||
return $this->db->get()->result_array();
|
||||
}
|
||||
|
||||
|
||||
public function count_sessions($search = null, $filter = 'all')
|
||||
{
|
||||
$this->db->select('COUNT(*) AS total');
|
||||
$this->db->from('sessions');
|
||||
|
||||
if ($filter === 'unread') {
|
||||
$this->db->where('status', 'open');
|
||||
}
|
||||
|
||||
if (!empty($search)) {
|
||||
$this->db->group_start();
|
||||
$this->db->like('customer_name', $search);
|
||||
$this->db->or_like('customer_number', $search);
|
||||
$this->db->group_end();
|
||||
}
|
||||
|
||||
return (int) $this->db->get()->row()->total;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class Dashboard_model extends CI_Model {
|
||||
|
||||
private $version = 'v20.0'; // versi Graph API statis
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ambil semua analytics untuk satu nomor WhatsApp
|
||||
*
|
||||
* @param string $number Nomor WhatsApp
|
||||
* @param int $start_time Epoch waktu mulai
|
||||
* @param int $end_time Epoch waktu akhir
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function get_phone_number($user_id)
|
||||
{
|
||||
$this->db->select('phone_number, number_name')->from('whatsapp_numbers');
|
||||
|
||||
// Jika bukan user_id 1, tambahkan filter
|
||||
if ($user_id != 1) {
|
||||
$this->db->where('user_id', $user_id);
|
||||
}
|
||||
|
||||
return $this->db->get()->result();
|
||||
}
|
||||
|
||||
|
||||
public function get_whatsapp_data_by_phone($phoneNumber)
|
||||
{
|
||||
return $this->db
|
||||
->where('phone_number', $phoneNumber)
|
||||
->get('whatsapp_numbers')
|
||||
->row();
|
||||
}
|
||||
|
||||
public function getAnalyticsByNumber($waba_id, $access_token, $number, $start_time, $end_time)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
// Hanya ambil 2 jenis analytics (analytics dan pricing_analytics)
|
||||
$analytics_types = [
|
||||
'analytics' => 'Analytics',
|
||||
'pricing_analytics' => 'Pricing Analytics'
|
||||
];
|
||||
|
||||
foreach ($analytics_types as $key => $title) {
|
||||
|
||||
// Gunakan cara asli pengambilan data
|
||||
if ($key === 'analytics') {
|
||||
$url = "https://graph.facebook.com/{$this->version}/{$waba_id}?" . http_build_query([
|
||||
'fields' => sprintf(
|
||||
'analytics.start(%d).end(%d).granularity(DAY).phone_numbers(["%s"]).country_codes(["ID"])',
|
||||
$start_time, $end_time, $number
|
||||
),
|
||||
'access_token' => $access_token
|
||||
]);
|
||||
} elseif ($key === 'pricing_analytics') {
|
||||
|
||||
$url = "https://graph.facebook.com/{$this->version}/{$waba_id}?" . http_build_query([
|
||||
'fields' => sprintf(
|
||||
'pricing_analytics.start(%d).end(%d).granularity(DAILY).phone_numbers(["%s"]).country_codes(["ID"])',
|
||||
$start_time, $end_time, $number
|
||||
),
|
||||
'access_token' => $access_token
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
// Ambil data dari API
|
||||
$data = @json_decode(file_get_contents($url), true);
|
||||
|
||||
if ($data && !isset($data['error'])) {
|
||||
$result[$key] = [
|
||||
'title' => $title,
|
||||
'data' => $data
|
||||
];
|
||||
} else {
|
||||
$result[$key] = [
|
||||
'title' => $title,
|
||||
'error' => true,
|
||||
'http_code' => http_response_code() ?: 400,
|
||||
'message' => $data['error']['message'] ?? 'Gagal mengambil data dari API'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class User_management_model extends CI_Model
|
||||
{
|
||||
|
||||
public function get_all_users()
|
||||
{
|
||||
$query = $this->db->get('users');
|
||||
return $query->result_array();
|
||||
}
|
||||
|
||||
public function get_users_by_session_id($id)
|
||||
{
|
||||
$this->db->from('users');
|
||||
$this->db->where('master_id', $id);
|
||||
return $this->db->get()->result_array();
|
||||
// $query = $this->db->get('users');
|
||||
// return $query->result_array();
|
||||
}
|
||||
|
||||
public function get_user_by_id($id)
|
||||
{
|
||||
$this->db->from('users');
|
||||
$this->db->where('id', $id);
|
||||
return $this->db->get()->row();
|
||||
}
|
||||
|
||||
public function get_phone_number_id($id)
|
||||
{
|
||||
return $this->db->select('id, phone_number')
|
||||
->from('whatsapp_numbers')
|
||||
->where('user_id', $id)
|
||||
->get()
|
||||
->result();
|
||||
}
|
||||
|
||||
// Fungsi baru untuk insert user
|
||||
public function insert_user($data)
|
||||
{
|
||||
return $this->db->insert('users', $data);
|
||||
}
|
||||
|
||||
// 🔹 Insert user baru
|
||||
public function insert($data) {
|
||||
$this->db->insert('users', $data);
|
||||
return $this->db->affected_rows() > 0;
|
||||
}
|
||||
|
||||
// 🔹 Update user
|
||||
public function update($id, $data) {
|
||||
$this->db->where('id', $id);
|
||||
$this->db->update('users', $data);
|
||||
return $this->db->affected_rows() > 0;
|
||||
}
|
||||
|
||||
// 🔹 Delete User
|
||||
public function delete($id)
|
||||
{
|
||||
$this->db->delete('users', ['id' => $id]);
|
||||
return $this->db->affected_rows() > 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class Model_cron_file extends CI_Model
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ambil file yang statusnya 'pending'
|
||||
* @param int $limit
|
||||
* @return array
|
||||
*/
|
||||
public function get_pending_files($limit = 10)
|
||||
{
|
||||
return $this->db
|
||||
->where('status', 'pending')
|
||||
->order_by('created_at', 'ASC')
|
||||
->limit($limit)
|
||||
->get('message_files')
|
||||
->result_array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update status file
|
||||
*/
|
||||
public function update_file_status($id, $status, $file_url = null, $file_name = null, $file_size = null)
|
||||
{
|
||||
$data = [
|
||||
'status' => $status,
|
||||
'updated_at' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
if ($file_url !== null) {
|
||||
$data['file_url'] = $file_url;
|
||||
}
|
||||
|
||||
if ($file_name !== null) {
|
||||
$data['file_name'] = $file_name;
|
||||
}
|
||||
|
||||
if ($file_size !== null) {
|
||||
$data['file_size'] = $file_size;
|
||||
}
|
||||
|
||||
$this->db->where('id', $id)->update('message_files', $data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Simpan file ke folder uploads/{customer_number}/
|
||||
*/
|
||||
public function save_file_to_local($customer_number, $filename, $content)
|
||||
{
|
||||
$folder = FCPATH . "uploads/{$customer_number}/";
|
||||
if (!is_dir($folder)) mkdir($folder, 0777, true);
|
||||
$path = $folder . $filename;
|
||||
file_put_contents($path, $content);
|
||||
return "uploads/{$customer_number}/{$filename}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Ambil file binary dari Meta Graph API
|
||||
*/
|
||||
public function download_file_from_meta($meta_url, $access_token)
|
||||
{
|
||||
// --- Jika URL dari DB tidak punya 'fields=url', tambahkan otomatis
|
||||
if (preg_match('/^https:\/\/graph\.facebook\.com\/v\d+\.\d+\/\d+$/', $meta_url)) {
|
||||
$meta_url .= '?fields=url';
|
||||
}
|
||||
|
||||
// --- Step 1: Ambil URL file asli dari Meta Graph API
|
||||
$ch = curl_init();
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $meta_url,
|
||||
CURLOPT_HTTPHEADER => ["Authorization: Bearer $access_token"],
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_TIMEOUT => 15,
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
]);
|
||||
$response = curl_exec($ch);
|
||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($http_code !== 200 || !$response) {
|
||||
log_message('error', "Failed to fetch meta URL (HTTP $http_code): $error | $response");
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = json_decode($response, true);
|
||||
if (!isset($result['url'])) {
|
||||
log_message('error', 'Meta Graph response missing "url": ' . $response);
|
||||
return false;
|
||||
}
|
||||
|
||||
$download_url = $result['url'];
|
||||
|
||||
// --- Step 2: Download file binary dari URL file asli
|
||||
$ch = curl_init();
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $download_url,
|
||||
CURLOPT_HTTPHEADER => ["Authorization: Bearer $access_token"],
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_TIMEOUT => 30,
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
]);
|
||||
$file_content = curl_exec($ch);
|
||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($http_code !== 200 || !$file_content) {
|
||||
log_message('error', "Failed to download file (HTTP $http_code): $error");
|
||||
return false;
|
||||
}
|
||||
|
||||
return $file_content;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class WhatsappTemplate_model extends CI_Model
|
||||
{
|
||||
// private $access_token = 'EAAN0PjgeNRsBPqGqvuv48h90PQ6ZCo1StyQ5vSFBZB7UiyjFLxneRvz4xE0a66HbZBmY0rLo7cpsQV0Ecey3Ggy9UK36MJiSxQXebMexHLyKlFSTAbf6AUHO7EVQEaJvINxA51j4TMiq3InlIy7Yn9muxITYGvjB403uXcuD71MieL7Q1KEm8PXopiRqgZDZD';
|
||||
|
||||
// Ambil data whatsapp_number berdasar phone_number
|
||||
public function get_whatsapp_number_by_phone($phone_number)
|
||||
{
|
||||
return $this->db->get_where('whatsapp_numbers', ['phone_number' => $phone_number])->row();
|
||||
}
|
||||
|
||||
// Ambil template dari Meta API
|
||||
public function get_templates_from_meta($phone_number_id,$access_token)
|
||||
{
|
||||
$url = "https://graph.facebook.com/v16.0/{$phone_number_id}/message_templates";
|
||||
|
||||
$curl = curl_init();
|
||||
|
||||
curl_setopt_array($curl, [
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
"Authorization: Bearer {$access_token}"
|
||||
],
|
||||
]);
|
||||
|
||||
$response = curl_exec($curl);
|
||||
curl_close($curl);
|
||||
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
// Ambil template dari DB berdasarkan whatsapp_number_id
|
||||
public function get_templates_by_whatsapp_number_id($whatsapp_number_id)
|
||||
{
|
||||
return $this->db->get_where('whatsapp_templates', ['whatsapp_number_id' => $whatsapp_number_id])->result();
|
||||
}
|
||||
|
||||
// Insert/update template ke DB (upsert)
|
||||
public function upsert_template($data)
|
||||
{
|
||||
$this->db->where('template_name', $data['template_name']);
|
||||
$this->db->where('language', $data['language']);
|
||||
$this->db->where('whatsapp_number_id', $data['whatsapp_number_id']);
|
||||
$existing = $this->db->get('whatsapp_templates')->row();
|
||||
|
||||
if ($existing) {
|
||||
$this->db->where('id', $existing->id);
|
||||
return $this->db->update('whatsapp_templates', $data);
|
||||
} else {
|
||||
return $this->db->insert('whatsapp_templates', $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>403 Forbidden</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p>Directory access is forbidden.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user