Files
MoneyFlix/crm/Modules/Controllers/PublicCampaignsController.php
T

154 lines
5.0 KiB
PHP

<?php
namespace App\Modules\Controllers;
use App\Core\BaseController;
use App\Core\View;
use App\Core\DatabaseManager;
use App\Core\Branding;
use App\Modules\Redirects\Models\RedirectStatsModel;
class PublicCampaignsController extends BaseController
{
private DatabaseManager $db;
private RedirectStatsModel $statsModel;
public function __construct()
{
parent::__construct();
$this->db = DatabaseManager::getInstance();
$this->statsModel = new RedirectStatsModel();
}
/**
* Renders the public campaign analytics page - simplified to use existing redirects stats
*/
public function analytics(string $slug): void
{
if (empty($slug)) {
http_response_code(404);
echo "Campaign slug is required.";
return;
}
// Get redirect by slug
$redirect = $this->getRedirectBySlug($slug);
if (!$redirect) {
http_response_code(404);
echo "Redirect not found.";
return;
}
$branding = Branding::get();
$page_title = ($branding['company_name'] ?? 'Analytics') . ' - ' . $redirect['name'];
// Get date range from query params
$startDate = $_GET['startDate'] ?? date('Y-m-d', strtotime('-30 days'));
$endDate = $_GET['endDate'] ?? date('Y-m-d');
// Get real stats data for this specific redirect
$stats = $this->getRedirectStats($redirect['id'], $startDate, $endDate);
$filterOptions = $this->getFilterOptions($redirect['id']);
View::render('layouts/public-blank.php', [
'title' => $page_title,
'page_name' => 'public-campaign-analytics',
'branding' => $branding,
'slug' => $slug,
'page_content' => function() use ($branding, $slug, $stats, $filterOptions, $startDate, $endDate) {
View::render('layouts/page-title.php', [
'pagetitle' => 'Campaign Analytics',
'title' => 'Campaign Performance'
]);
View::render('@pages/public-campaign-analytics', [
'branding' => $branding,
'slug' => $slug,
'stats' => $stats,
'filterOptions' => $filterOptions,
'startDate' => $startDate,
'endDate' => $endDate
]);
}
]);
}
/**
* API endpoint: Get redirect stats by slug
*/
public function getCampaignStats(string $slug): void
{
header('Content-Type: application/json');
if (empty($slug)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Missing campaign slug']);
return;
}
$startDate = $_GET['startDate'] ?? date('Y-m-d', strtotime('-30 days'));
$endDate = $_GET['endDate'] ?? date('Y-m-d');
try {
$redirect = $this->getRedirectBySlug($slug);
if (!$redirect) {
http_response_code(404);
echo json_encode(['success' => false, 'message' => 'Redirect not found']);
return;
}
$stats = $this->getRedirectStats($redirect['id'], $startDate, $endDate);
echo json_encode(['success' => true, 'data' => $stats]);
} catch (\Exception $e) {
error_log("API error: " . $e->getMessage());
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Error fetching redirect data']);
}
}
/**
* Get redirect by slug (cuid)
*/
private function getRedirectBySlug(string $slug): ?array
{
$sql = "SELECT id, name, cuid, target_url, status FROM redirects WHERE cuid = :slug AND status >= 0";
return $this->db->fetch($sql, [':slug' => $slug]);
}
/**
* Get real redirect stats data
*/
private function getRedirectStats(int $redirectId, string $startDate, string $endDate): array
{
// Get overview stats
$overview = $this->statsModel->getStatsByRedirect($redirectId, $startDate, $endDate);
// Get stats by country
$byCountry = $this->statsModel->getStatsByCountry($redirectId, $startDate, $endDate);
// Get stats by device
$byDevice = $this->statsModel->getStatsByDevice($redirectId, $startDate, $endDate);
// Get stats by browser
$byBrowser = $this->statsModel->getStatsByBrowser($redirectId, $startDate, $endDate);
return [
'overview' => $overview,
'by_country' => $byCountry,
'by_device' => $byDevice,
'by_browser' => $byBrowser
];
}
/**
* Get filter options for the specific redirect
*/
private function getFilterOptions(int $redirectId): array
{
return [
'redirects' => [$redirectId], // Only this redirect
'countries' => [],
'browsers' => [],
'operatingSystems' => []
];
}
}