97 lines
3.0 KiB
PHP
97 lines
3.0 KiB
PHP
<?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;
|
|
}
|
|
|
|
}
|