diff --git a/crm/Modules/Controllers/PublicCampaignsController.php b/crm/Modules/Controllers/PublicCampaignsController.php index dee22b0..eecc64c 100644 --- a/crm/Modules/Controllers/PublicCampaignsController.php +++ b/crm/Modules/Controllers/PublicCampaignsController.php @@ -274,290 +274,4 @@ class PublicCampaignsController extends BaseController default => 'Unknown' }; } - - private function getCampaignFormats(int $campaignId): array - { - $sql = "SELECT - f.id AS format_id, - f.name AS format_name, - f.type AS format_type, - f.width AS format_width, - f.height AS format_height, - f.weight AS format_weight, - f.is_active AS is_active, - f.campaign_id AS campaign_id, - f.settings AS format_settings, - f.created_at AS created_date, - f.updated_at AS updated_date - FROM - advertising_formats f - WHERE - f.campaign_id = :campaign_id AND f.is_active = 1"; - return $this->db->fetchAll($sql, [':campaign_id' => $campaignId]) ?: []; - } - - /** - * Get campaign statistics data - */ - private function getCampaignStatsData(int $campaignId, string $startDate, string $endDate): array - { - $sql = "SELECT - c.id, - c.name, - c.status, - COUNT(DISTINCT f.id) as formats_count, - COUNT(DISTINCT cr.id) as creatives_count, - SUM(r.views) as total_views, - SUM(r.clicks) as total_clicks, - SUM(r.registers) as total_registers, - SUM(r.revenue) as total_revenue, - AVG(r.ctr) as avg_ctr, - AVG(r.cpm) as avg_cpm, - AVG(r.epc) as avg_epc, - MIN(r.date) as first_hit - FROM advertising_campaigns c - LEFT JOIN advertising_formats f ON c.id = f.campaign_id - LEFT JOIN advertising_creatives cr ON f.id = cr.format_id - LEFT JOIN advertising_reports_daily r ON c.id = r.campaign_id - WHERE c.id = :campaign_id AND c.status >= 0"; - - $params = [':campaign_id' => $campaignId]; - - if ($startDate) { - $sql .= " AND r.date >= :start_date"; - $params[':start_date'] = $startDate; - } - - if ($endDate) { - $sql .= " AND r.date <= :end_date"; - $params[':end_date'] = $endDate; - } - - $sql .= " GROUP BY c.id"; - - return $this->db->fetch($sql, $params) ?: []; - } - - /** - * Get campaign charts data - */ - private function getCampaignCharts(int $campaignId, string $startDate, string $endDate): array - { - // Dados de impressions e clicks por dia - $impressionsClicks = $this->getImpressionsClicksData($campaignId, $startDate, $endDate); - - // Dados geográficos - $geos = $this->getGeosData($campaignId, $startDate, $endDate); - - // Dados de dispositivos - $devices = $this->getDevicesData($campaignId, $startDate, $endDate); - - return [ - 'impressionsAndClicks' => $impressionsClicks, - 'geos' => $geos, - 'devices' => $devices - ]; - } - - /** - * Get impressions and clicks data for charts - */ - private function getImpressionsClicksData(int $campaignId, string $startDate, string $endDate): array - { - $sql = "SELECT - DATE(r.date) as date, - SUM(r.views) as impressions, - SUM(r.clicks) as clicks - FROM advertising_reports_daily r - WHERE r.campaign_id = :campaign_id - AND r.date BETWEEN :start_date AND :end_date - GROUP BY DATE(r.date) - ORDER BY r.date"; - - $result = $this->db->fetchAll($sql, [ - ':campaign_id' => $campaignId, - ':start_date' => $startDate, - ':end_date' => $endDate - ]) ?: []; - - $categories = []; - $impressionsData = []; - $clicksData = []; - - foreach ($result as $row) { - $categories[] = $row['date']; - $impressionsData[] = (int)$row['impressions']; - $clicksData[] = (int)$row['clicks']; - } - - return [ - 'series' => [ - ['name' => 'Impressions', 'data' => $impressionsData], - ['name' => 'Clicks', 'data' => $clicksData] - ], - 'categories' => $categories - ]; - } - - /** - * Get geographical data for charts - */ - private function getGeosData(int $campaignId, string $startDate, string $endDate): array - { - $sql = "SELECT - r.country_code, - SUM(r.views) as impressions - FROM advertising_reports_daily r - WHERE r.campaign_id = :campaign_id - AND r.date BETWEEN :start_date AND :end_date - AND r.country_code IS NOT NULL - GROUP BY r.country_code - ORDER BY impressions DESC - LIMIT 10"; - - $result = $this->db->fetchAll($sql, [ - ':campaign_id' => $campaignId, - ':start_date' => $startDate, - ':end_date' => $endDate - ]) ?: []; - - $series = []; - $labels = []; - - foreach ($result as $row) { - $labels[] = $row['country_code']; - $series[] = (int)$row['impressions']; - } - - return [ - 'series' => $series, - 'labels' => $labels - ]; - } - - /** - * Get devices data for charts - */ - private function getDevicesData(int $campaignId, string $startDate, string $endDate): array - { - $sql = "SELECT - r.device_type, - SUM(r.views) as impressions - FROM advertising_reports_daily r - WHERE r.campaign_id = :campaign_id - AND r.date BETWEEN :start_date AND :end_date - AND r.device_type IS NOT NULL - GROUP BY r.device_type - ORDER BY impressions DESC"; - - $result = $this->db->fetchAll($sql, [ - ':campaign_id' => $campaignId, - ':start_date' => $startDate, - ':end_date' => $endDate - ]) ?: []; - - $series = []; - $labels = []; - - foreach ($result as $row) { - $labels[] = ucfirst($row['device_type']); - $series[] = (int)$row['impressions']; - } - - return [ - 'series' => $series, - 'labels' => $labels - ]; - } - - /** - * Get campaign tables data - */ - private function getCampaignTables(int $campaignId, string $startDate, string $endDate): array - { - $sql = "SELECT - r.date, - r.country_code, - r.views, - r.clicks, - r.ctr - FROM advertising_reports_daily r - WHERE r.campaign_id = :campaign_id - AND r.date BETWEEN :start_date AND :end_date - ORDER BY r.date DESC, r.views DESC - LIMIT 20"; - - $result = $this->db->fetchAll($sql, [ - ':campaign_id' => $campaignId, - ':start_date' => $startDate, - ':end_date' => $endDate - ]) ?: []; - - $dailyStats = []; - foreach ($result as $row) { - $dailyStats[] = [ - $row['date'], - $row['country_code'] ?? 'Unknown', - number_format($row['views']), - number_format($row['clicks']), - number_format($row['ctr'], 2) . '%' - ]; - } - - return [ - 'dailyStats' => $dailyStats - ]; - } - - /** - * Get available domains for the campaign - */ - private function getAvailableDomains(int $campaignId): array - { - try { - // Buscar domínios reais das campanhas - $domains = $this->db->fetchAll("SELECT DISTINCT - domain - FROM advertising_campaigns - WHERE domain IS NOT NULL AND domain != '' AND deleted_at IS NULL - ORDER BY domain"); - - if (empty($domains)) { - return ['performix.pt', 'bitreport.pt', 'crm.local']; - } - - return array_column($domains, 'domain'); - - } catch (\Exception $e) { - return ['performix.pt', 'bitreport.pt', 'crm.local']; - } - } - - /** - * Get status class for display - */ - private function getStatusClass(string $status): string - { - return match($status) { - 'active' => 'badge bg-success-subtle text-success', - 'paused' => 'badge bg-warning-subtle text-warning', - 'completed' => 'badge bg-info-subtle text-info', - 'archived' => 'badge bg-secondary-subtle text-secondary', - default => 'badge bg-secondary-subtle text-secondary' - }; - } - - /** - * Get status display text - */ - private function getStatusDisplay(string $status): string - { - return match($status) { - 'active' => 'Ativo', - 'paused' => 'Pausado', - 'completed' => 'Concluído', - 'archived' => 'Arquivado', - default => 'Desconhecido' - }; - } }