From 6e2753e89af7fe00801bdfd57f5fcb9a1c7f8b9a Mon Sep 17 00:00:00 2001 From: 1sonhador <1sonhador@gmail.com> Date: Wed, 15 Oct 2025 00:38:25 +0100 Subject: [PATCH] Restore campaign logic adapted for redirects using cuid as slug --- .../Controllers/PublicCampaignsController.php | 137 ++++++++++++------ 1 file changed, 94 insertions(+), 43 deletions(-) diff --git a/crm/Modules/Controllers/PublicCampaignsController.php b/crm/Modules/Controllers/PublicCampaignsController.php index f32883f..dee22b0 100644 --- a/crm/Modules/Controllers/PublicCampaignsController.php +++ b/crm/Modules/Controllers/PublicCampaignsController.php @@ -69,22 +69,30 @@ class PublicCampaignsController extends BaseController // Log para debug error_log("API called with slug: " . $slug); - // Por agora, sempre retornar dados de teste - $data = $this->getRealRedirectData(1, $startDate, $endDate, $domain); + // Buscar campanha por cuid (slug) - adaptado para redirects + $campaign = $this->getCampaignByCuid($slug); + if (!$campaign) { + http_response_code(404); + echo json_encode(['success' => false, 'message' => 'Campaign not found']); + return; + } + + // Buscar dados reais da campanha (adaptado para redirects) + $data = $this->getRealCampaignData($campaign['campaign_id'], $startDate, $endDate, $domain); echo json_encode(['success' => true, 'data' => $data]); } catch (\Exception $e) { error_log("API error: " . $e->getMessage()); http_response_code(500); - echo json_encode(['success' => false, 'message' => 'Error fetching redirect data']); + echo json_encode(['success' => false, 'message' => 'Error fetching campaign data']); } } /** - * Get redirect by cuid (slug) + * Get campaign by cuid (slug) - adapted for redirects */ - private function getRedirectByCuid(string $cuid): ?array + private function getCampaignByCuid(string $cuid): ?array { // First, let's check if the table exists and what redirects are available try { @@ -98,37 +106,21 @@ class PublicCampaignsController extends BaseController $allRedirects = $this->db->fetchAll("SELECT cuid, name FROM redirects LIMIT 5"); error_log("Available redirects: " . json_encode($allRedirects)); - // If no redirects found, return a test redirect - if (empty($allRedirects)) { - return [ - 'redirect_id' => 1, - 'redirect_name' => 'Test Redirect', - 'source_url' => 'https://example.com/source', - 'target_url' => 'https://example.com/target', - 'redirect_cuid' => $cuid, - 'redirect_status' => 1, - 'redirect_type' => '302', - 'tracking_enabled' => 1, - 'created_date' => date('Y-m-d H:i:s') - ]; - } - } catch (\Exception $e) { error_log("Error checking redirects: " . $e->getMessage()); } $sql = "SELECT - r.id AS redirect_id, - r.name AS redirect_name, - r.source_url AS source_url, + r.id AS campaign_id, + r.name AS campaign_name, + r.source_url AS campaign_description, r.target_url AS target_url, - r.fallback_url AS fallback_url, - r.cuid AS redirect_cuid, - r.status AS redirect_status, + r.cuid AS campaign_cuid, + r.status AS campaign_status, r.redirect_type AS redirect_type, r.tracking_enabled AS tracking_enabled, r.max_clicks AS max_clicks, - r.expires_at AS expires_at, + r.expires_at AS end_date, r.created_at AS created_date, r.updated_at AS updated_date FROM @@ -139,23 +131,29 @@ class PublicCampaignsController extends BaseController } /** - * Get real redirect data from database + * Get real campaign data from database - adapted for redirects */ - private function getRealRedirectData(int $redirectId, string $startDate, string $endDate, string $domain): array + private function getRealCampaignData(int $campaignId, string $startDate, string $endDate, string $domain): array { - // Buscar estatísticas básicas do redirect - $stats = $this->getRedirectStatsData($redirectId, $startDate, $endDate); + // Buscar formatos da campanha (adaptado para redirects) + $formats = $this->getCampaignFormats($campaignId); + + // Buscar estatísticas reais + $stats = $this->getCampaignStatsData($campaignId, $startDate, $endDate); // Buscar dados de gráficos - $charts = $this->getRedirectCharts($redirectId, $startDate, $endDate); + $charts = $this->getCampaignCharts($campaignId, $startDate, $endDate); // Buscar dados de tabelas - $tables = $this->getRedirectTables($redirectId, $startDate, $endDate); + $tables = $this->getCampaignTables($campaignId, $startDate, $endDate); + + // Buscar domínios disponíveis + $domains = $this->getAvailableDomains($campaignId); return [ - 'slug' => $redirectId, + 'slug' => $campaignId, 'filters' => [ - 'domains' => ['all'] + 'domains' => $domains ], 'scriptInfo' => [ 'estimatedRevenue' => $stats['total_revenue'] ?? 0, @@ -164,7 +162,7 @@ class PublicCampaignsController extends BaseController 'totalViewsAllTime' => $stats['total_views'] ?? 0, 'firstHit' => $stats['first_hit'] ?? date('Y-m-d'), 'dealType' => 'CPM', - 'scriptId' => 'script_' . $redirectId + 'scriptId' => 'script_' . $campaignId ], 'kpis' => [ 'impressions' => ['value' => $stats['total_views'] ?? 0], @@ -177,9 +175,28 @@ class PublicCampaignsController extends BaseController } /** - * Get redirect stats data + * Get campaign formats - adapted for redirects */ - private function getRedirectStatsData(int $redirectId, string $startDate, string $endDate): array + private function getCampaignFormats(int $campaignId): array + { + // Por agora, retornar dados mockados para redirects + return [ + [ + 'format_id' => 1, + 'format_name' => 'Redirect Format', + 'format_type' => 'redirect', + 'format_width' => 0, + 'format_height' => 0, + 'format_weight' => 100, + 'is_active' => true + ] + ]; + } + + /** + * Get campaign stats data - adapted for redirects + */ + private function getCampaignStatsData(int $campaignId, string $startDate, string $endDate): array { // Por agora, retornar dados mockados para testar return [ @@ -192,7 +209,10 @@ class PublicCampaignsController extends BaseController ]; } - private function getRedirectCharts(int $redirectId, string $startDate, string $endDate): array + /** + * Get campaign charts - adapted for redirects + */ + private function getCampaignCharts(int $campaignId, string $startDate, string $endDate): array { // Dados mockados para gráficos return [ @@ -206,7 +226,10 @@ class PublicCampaignsController extends BaseController ]; } - private function getRedirectTables(int $redirectId, string $startDate, string $endDate): array + /** + * Get campaign tables - adapted for redirects + */ + private function getCampaignTables(int $campaignId, string $startDate, string $endDate): array { // Dados mockados para tabelas return [ @@ -218,10 +241,38 @@ class PublicCampaignsController extends BaseController ]; } - private function getRedirectById(int $redirectId): ?array + /** + * Get available domains - adapted for redirects + */ + private function getAvailableDomains(int $campaignId): array { - $sql = "SELECT * FROM redirects WHERE id = :id"; - return $this->db->fetch($sql, [':id' => $redirectId]); + return ['all']; + } + + /** + * Get status class + */ + private function getStatusClass(string $status): string + { + return match($status) { + 'active' => 'success', + 'paused' => 'warning', + 'archived' => 'secondary', + default => 'secondary' + }; + } + + /** + * Get status display + */ + private function getStatusDisplay(string $status): string + { + return match($status) { + 'active' => 'Active', + 'paused' => 'Paused', + 'archived' => 'Archived', + default => 'Unknown' + }; } private function getCampaignFormats(int $campaignId): array