googleSheetsService = new GoogleSheetsService(); $this->integrationService = new ScriptBuilderIntegrationService(); } /** * Display Google Sheets synchronization dashboard */ public function index(): void { try { // Get sync statistics $stats = $this->integrationService->getSyncStats(); // Check Google Sheets availability $google_sheets_available = $this->googleSheetsService->isAvailable(); } catch (\Exception $e) { // Set default values if services fail $stats = [ 'campaigns' => ['total' => 0, 'synced' => 0], 'creatives' => ['total' => 0, 'synced' => 0] ]; $google_sheets_available = false; } View::render('layouts/main.php', [ 'title' => 'Google Sheets Synchronization', 'page_name' => 'apps/advertising/google-sheets-sync', 'page_content' => function() use ($stats, $google_sheets_available) { View::render('layouts/page-title.php', [ 'pagetitle' => 'Advertising', 'title' => 'Google Sheets Synchronization' ]); View::render('@advertising/google-sheets-sync', [ 'stats' => $stats, 'google_sheets_available' => $google_sheets_available ]); } ]); } /** * Perform manual synchronization from Google Sheets */ public function syncFromSheets(): void { try { // Perform full synchronization from Google Sheets $results = $this->integrationService->syncFromGoogleSheets(); if ($results['success']) { $this->successResponse($results, 'Synchronization completed successfully'); } else { $this->errorResponse($results['error'] ?? 'Synchronization failed', 500, $results); } } catch (\Exception $e) { $this->errorResponse('Synchronization error: ' . $e->getMessage(), 500); } } /** * Get synchronization status and statistics */ public function getStats(): void { try { $stats = $this->integrationService->getSyncStats(); $this->successResponse($stats); } catch (\Exception $e) { $this->errorResponse('Failed to get sync stats: ' . $e->getMessage(), 500); } } /** * Preview Google Sheets data without importing */ public function previewSheetsData(): void { try { // Preview real Google Sheets data $campaigns = $this->googleSheetsService->readCampaignsFromSheets(); $creatives = $this->googleSheetsService->readCreativesFromSheets(); $this->successResponse([ 'campaigns' => array_slice($campaigns, 0, 10), // Limit to first 10 for preview 'creatives' => array_slice($creatives, 0, 10), // Limit to first 10 for preview 'total_campaigns' => count($campaigns), 'total_creatives' => count($creatives), ]); } catch (\Exception $e) { $this->errorResponse('Failed to preview sheets data: ' . $e->getMessage(), 500); } } /** * Test Google Sheets connection */ public function testConnection(): void { try { $available = $this->googleSheetsService->isAvailable(); if ($available) { // Try to read version to test actual connection $version = $this->googleSheetsService->getSheetsVersion(); $this->successResponse([ 'connected' => true, 'sheets_version' => $version, 'message' => 'Google Sheets connection is working' ]); } else { $this->errorResponse('Google Sheets service is not available', 503); } } catch (\Exception $e) { $this->errorResponse('Connection test failed: ' . $e->getMessage(), 500); } } /** * Get sync history/log */ public function getSyncHistory(): void { try { // Get recent sync operations from database $history = $this->db->fetchAll( "SELECT type, version, updated_at, created_at FROM advertising_sync_versions ORDER BY updated_at DESC LIMIT 50" ); $this->successResponse([ 'history' => $history, 'total_records' => count($history) ]); } catch (\Exception $e) { $this->errorResponse('Failed to get sync history: ' . $e->getMessage(), 500); } } /** * Force regenerate trackers.json */ public function regenerateTrackers(): void { try { // This will be handled by the integration service $this->integrationService->syncFromGoogleSheets(); $this->successResponse([ 'message' => 'trackers.json regenerated successfully', 'timestamp' => date('Y-m-d H:i:s') ]); } catch (\Exception $e) { $this->errorResponse('Failed to regenerate trackers.json: ' . $e->getMessage(), 500); } } /** * Create missing trackers for campaigns */ public function createMissingTrackers(): void { try { $results = $this->integrationService->createMissingTrackersForCampaigns(); $this->successResponse($results, 'Missing trackers created successfully'); } catch (\Exception $e) { $this->errorResponse('Failed to create missing trackers: ' . $e->getMessage(), 500); } } /** * Show Google Sheets configuration page */ public function configure(): void { View::render('layouts/main.php', [ 'title' => 'Configurar Google Sheets', 'page_name' => 'apps/advertising/google-sheets-configure', 'page_content' => function() { View::render('layouts/page-title.php', [ 'pagetitle' => 'Publicidade', 'title' => 'Configurar Google Sheets' ]); View::render('@advertising/google-sheets-configure'); } ]); } }