db = DatabaseManager::getInstance(); } /** * Get all campaigns with filters */ public function getAll(array $filters = [], int $limit = 50, int $offset = 0): array { $sql = "SELECT * FROM advertising_campaigns WHERE status >= 0"; $params = []; if (!empty($filters['search'])) { $sql .= " AND (name LIKE ? OR description LIKE ?)"; $searchTerm = '%' . $filters['search'] . '%'; $params[] = $searchTerm; $params[] = $searchTerm; } if (!empty($filters['status'])) { $sql .= " AND status = ?"; $params[] = $filters['status']; } if (!empty($filters['region'])) { $sql .= " AND region = ?"; $params[] = $filters['region']; } $sql .= " ORDER BY created_at DESC LIMIT " . (int)$limit . " OFFSET " . (int)$offset; return $this->db->fetchAll($sql, $params) ?: []; } /** * Get campaign count with filters */ public function getCount(array $filters = []): int { $sql = "SELECT COUNT(*) as count FROM advertising_campaigns WHERE status >= 0"; $params = []; if (!empty($filters['search'])) { $sql .= " AND (name LIKE ? OR description LIKE ?)"; $searchTerm = '%' . $filters['search'] . '%'; $params[] = $searchTerm; $params[] = $searchTerm; } if (!empty($filters['status'])) { $sql .= " AND status = ?"; $params[] = $filters['status']; } if (!empty($filters['region'])) { $sql .= " AND region = ?"; $params[] = $filters['region']; } $result = $this->db->fetch($sql, $params); return (int)($result['count'] ?? 0); } /** * Get campaign statistics */ public function getStats(): array { $sql = "SELECT COUNT(*) as total, SUM(IF(status = 'active', 1, 0)) as active, SUM(IF(status = 'paused', 1, 0)) as paused, SUM(IF(status = 'draft', 1, 0)) as draft, SUM(IF(status = 'ended', 1, 0)) as ended, SUM(IF(end_date IS NULL, 1, 0)) as no_end_date FROM advertising_campaigns WHERE status >= 0"; $result = $this->db->fetch($sql); return [ 'total' => (int)($result['total'] ?? 0), 'active' => (int)($result['active'] ?? 0), 'paused' => (int)($result['paused'] ?? 0), 'draft' => (int)($result['draft'] ?? 0), 'ended' => (int)($result['ended'] ?? 0), 'no_end_date' => (int)($result['no_end_date'] ?? 0) ]; } /** * Find campaign by ID */ public function find(int $id): ?array { $result = $this->db->fetch(" SELECT c.id AS campaign_id, c.name AS campaign_name, c.description AS campaign_description, c.status AS campaign_status, c.budget AS campaign_budget, c.start_date AS start_date, c.end_date AS end_date, c.site_group_id AS site_group_id, c.cuid AS campaign_cuid, c.created_at AS created_date, c.updated_at AS updated_date FROM advertising_campaigns c WHERE c.id = ? AND c.status >= 0 ", [$id]); return $result ?: null; } /** * Find campaign by CUID */ public function findByCuid(string $cuid): ?array { $result = $this->db->fetch("SELECT * FROM advertising_campaigns WHERE cuid = ? AND status >= 0", [$cuid]); return $result ?: null; } /** * Get campaign statistics */ public function getCampaignStats(int $campaignId): array { // Get campaign info $campaign = $this->find($campaignId); // Get trackers count through campaign_trackers relationship $trackersCount = $this->db->fetch("SELECT COUNT(*) as count FROM advertising_campaign_trackers WHERE campaign_id = ?", [$campaignId])['count'] ?? 0; return [ 'id' => $campaignId, 'name' => $campaign['name'] ?? 'Unknown Campaign', 'status' => $campaign['status'] ?? 'inactive', 'formats_count' => 0, // Formats are not directly linked to campaigns 'creatives_count' => 0, // Creatives are not directly linked to campaigns 'trackers_count' => $trackersCount, 'total_views' => 0, 'total_clicks' => 0, 'total_registers' => 0, 'total_revenue' => 0, 'avg_ctr' => 0, 'avg_cpm' => 0, 'avg_epc' => 0, 'first_hit' => date('Y-m-d') ]; } /** * Get campaign spend data */ public function getCampaignSpend(int $campaignId): array { return [ 'total_spend' => 0, 'daily_spend' => 0, 'budget_remaining' => 0 ]; } /** * Update campaign */ public function update(int $id, array $data): bool { $fields = []; $params = []; foreach ($data as $key => $value) { if ($key !== 'id' && $key !== 'cuid') { $fields[] = "$key = ?"; $params[] = $value; } } if (empty($fields)) { return false; } $params[] = $id; $sql = "UPDATE advertising_campaigns SET " . implode(', ', $fields) . ", updated_at = NOW() WHERE id = ?"; return $this->db->query($sql, $params) > 0; } /** * Create campaign */ public function create(array $data): int { $fields = array_keys($data); $placeholders = str_repeat('?,', count($fields) - 1) . '?'; $sql = "INSERT INTO advertising_campaigns (" . implode(', ', $fields) . ", created_at, updated_at) VALUES ($placeholders, NOW(), NOW())"; $this->db->query($sql, array_values($data)); return $this->db->lastInsertId(); } }