db = DatabaseManager::getInstance(); } /** * Criar template de landing */ public function create(array $data): int { try { $sql = "INSERT INTO advertising_landing_templates ( cuid, brand_id, country_code, template_type, size, image_path, mobile_image_path, desktop_image_path, is_active, created_by, created_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())"; $cuid = (string) new \Visus\Cuid2\Cuid2(); $this->db->query($sql, [ $cuid, $data['brand_id'], $data['country_code'], $data['template_type'] ?? 'banner', $data['size'] ?? '300x600', $data['image_path'], $data['mobile_image_path'] ?? null, $data['desktop_image_path'] ?? null, $data['is_active'] ?? 1, $data['created_by'] ?? 1 ]); return $this->db->lastInsertId(); } catch (\Exception $e) { ErrorReporter::report($e, [ 'context' => 'landing_template_create', 'data' => $data ]); throw $e; } } /** * Encontrar template por marca/país */ public function findByBrandAndCountry(int $brandId, string $countryCode): ?array { try { return $this->db->fetch(" SELECT lt.id AS template_id, lt.cuid AS template_cuid, lt.brand_id AS brand_id, lt.country_code AS country_code, lt.template_type AS template_type, lt.size AS size, lt.image_path AS image_path, lt.mobile_image_path AS mobile_image_path, lt.desktop_image_path AS desktop_image_path, lt.is_active AS is_active, lt.created_at AS created_date, b.name AS brand_name, b.slug AS brand_slug FROM advertising_landing_templates lt LEFT JOIN advertising_brands b ON lt.brand_id = b.id WHERE lt.brand_id = ? AND lt.country_code = ? AND lt.is_active = 1 AND lt.deleted_at IS NULL ", [$brandId, strtoupper($countryCode)]); } catch (\Exception $e) { ErrorReporter::report($e, [ 'context' => 'landing_template_find', 'brand_id' => $brandId, 'country_code' => $countryCode ]); return null; } } /** * Listar todos os templates */ public function getAll(array $filters = [], int $limit = 50, int $offset = 0): array { try { $sql = " SELECT lt.id AS template_id, lt.cuid AS template_cuid, lt.brand_id AS brand_id, lt.country_code AS country_code, lt.template_type AS template_type, lt.size AS size, lt.image_path AS image_path, lt.mobile_image_path AS mobile_image_path, lt.desktop_image_path AS desktop_image_path, lt.is_active AS is_active, lt.created_at AS created_date, b.name AS brand_name, b.slug AS brand_slug FROM advertising_landing_templates lt LEFT JOIN advertising_brands b ON lt.brand_id = b.id WHERE lt.deleted_at IS NULL "; $params = []; if (!empty($filters['brand_id'])) { $sql .= " AND lt.brand_id = ?"; $params[] = $filters['brand_id']; } if (!empty($filters['country_code'])) { $sql .= " AND lt.country_code = ?"; $params[] = strtoupper($filters['country_code']); } if (!empty($filters['template_type'])) { $sql .= " AND lt.template_type = ?"; $params[] = $filters['template_type']; } $sql .= " ORDER BY lt.created_at DESC LIMIT " . (int)$limit . " OFFSET " . (int)$offset; return $this->db->fetchAll($sql, $params) ?: []; } catch (\Exception $e) { ErrorReporter::report($e, [ 'context' => 'landing_template_get_all', 'filters' => $filters ]); return []; } } /** * Atualizar template */ public function update(int $id, array $data): bool { try { $sql = "UPDATE advertising_landing_templates SET template_type = ?, image_path = ?, mobile_image_path = ?, desktop_image_path = ?, is_active = ?, updated_at = NOW() WHERE id = ?"; return $this->db->query($sql, [ $data['template_type'] ?? 'banner', $data['image_path'], $data['mobile_image_path'] ?? null, $data['desktop_image_path'] ?? null, $data['is_active'] ?? 1, $id ]); } catch (\Exception $e) { ErrorReporter::report($e, [ 'context' => 'landing_template_update', 'id' => $id, 'data' => $data ]); return false; } } /** * Soft delete template */ public function delete(int $id): bool { try { return $this->db->query( "UPDATE advertising_landing_templates SET deleted_at = NOW() WHERE id = ?", [$id] ); } catch (\Exception $e) { ErrorReporter::report($e, [ 'context' => 'landing_template_delete', 'id' => $id ]); return false; } } /** * Gerar URL da imagem para Script Builder */ public function generateImageUrl(string $brandSlug, string $templateType, string $countryCode, ?string $device = null, ?string $size = null): string { $baseUrl = 'https://js.pmx.bet/images/'; // Mapeamento de tipos para tamanhos padrão baseado na Google Sheet $defaultSizes = [ 'Sticky Bottom Banner' => '728x90', 'Scrollable Top Banner' => '728x90', 'Interstitial' => '800x600', 'Cube' => '300x250', '320x50' => '320x50', '320x100' => '320x100', '800x600' => '800x600', '300x600' => '300x600', 'Multibook' => '300x600', 'redirect' => '0x0' // Sem dimensões - apenas redirect ]; $size = $size ?: ($defaultSizes[$templateType] ?? '300x600'); if ($device && in_array($device, ['mobile', 'desktop'])) { return $baseUrl . $brandSlug . '-' . $size . '-' . $device . '-' . strtolower($countryCode) . '.jpg'; } return $baseUrl . $brandSlug . '-' . $size . '-' . strtolower($countryCode) . '.jpg'; } /** * Obter tamanhos disponíveis por tipo */ public function getAvailableSizes(string $templateType): array { $sizes = [ 'Sticky Bottom Banner' => ['728x90', '320x50', '320x100'], 'Scrollable Top Banner' => ['728x90', '320x50', '320x100'], 'Interstitial' => ['800x600', '1024x768', '1200x800'], 'Cube' => ['300x250', '250x250', '200x200'], '320x50' => ['320x50'], '320x100' => ['320x100'], '800x600' => ['800x600'], '300x600' => ['300x600'], 'Multibook' => ['300x600', '728x90', '320x50'], 'redirect' => ['0x0'] // Sem dimensões - apenas redirect ]; return $sizes[$templateType] ?? ['300x600']; } /** * Verificar se template existe para marca/país */ public function exists(int $brandId, string $countryCode): bool { try { $result = $this->db->fetch(" SELECT COUNT(*) as count FROM advertising_landing_templates WHERE brand_id = ? AND country_code = ? AND is_active = 1 AND deleted_at IS NULL ", [$brandId, strtoupper($countryCode)]); return $result && ((int)$result['count'] > 0); } catch (\Exception $e) { ErrorReporter::report($e, [ 'context' => 'landing_template_exists', 'brand_id' => $brandId, 'country_code' => $countryCode ]); return false; } } }