$_SESSION['baseUrl'] ?? '', 'apiKey' => $_SESSION['apiKey'] ?? '', 'timeout' => 180, // Increase to 3 minutes 'headers' => [ 'Content-Type' => 'application/json', 'Accept' => 'application/json' ] ]; $collectionPath = __DIR__ . '/Content Scaling Flow.postman_collection_Oliver(New).json'; if (!file_exists($collectionPath)) { $collectionPath = __DIR__ . '/Content Scaling Flow_Oliver.Postman_Collection.json'; } $testRunner = null; $error = null; $actionableCampaigns = []; $selectedCampaign = null; $workflowStep = $_GET['step'] ?? 1; $workflowResults = []; $campaignFilters = []; $availableFilters = []; $allCampaignsData = null; try { if (file_exists($collectionPath)) { $testRunner = new TestRunner($collectionPath, $config); } else { $error = "Postman collection file not found"; } } catch (Exception $e) { $error = "Error initializing test runner: " . $e->getMessage(); } // Handle form submissions if ($_POST && $testRunner) { if ($_POST['action'] === 'update_config') { $_SESSION['baseUrl'] = $_POST['baseUrl'] ?? ''; $_SESSION['apiKey'] = $_POST['apiKey'] ?? ''; $config['baseUrl'] = $_SESSION['baseUrl']; $config['apiKey'] = $_SESSION['apiKey']; $testRunner->updateConfig($config); $message = "Configuration updated successfully"; } if ($_POST['action'] === 'load_campaigns') { // Step 1: Load campaigns $requests = $testRunner->getAvailableRequests(); foreach ($requests as $index => $request) { if (strpos($request['name'], 'Retrieve Localized Campaign Folders') !== false) { $modifiedRequest = $request; $url = is_array($request['request']['url']) ? $request['request']['url']['raw'] : $request['request']['url']; // Use the original "Local Adaptation" search from Postman collection // This gives us the reliable 14 campaigns we know work // Replace {{baseUrl}} $baseUrl = 'https://ppr.dam.ferrero.com/otmmapi'; $url = str_replace('{{baseUrl}}', $baseUrl, $url); if (is_array($modifiedRequest['request']['url'])) { $modifiedRequest['request']['url']['raw'] = $url; } else { $modifiedRequest['request']['url'] = $url; } $result = $testRunner->runSingleTest($modifiedRequest, $index); if ($result['status'] === 'PASS') { $allCampaignsData = $result['response']['body']; $actionableCampaigns = CampaignFormatter::getActionableCampaigns($allCampaignsData); $workflowStep = 1; } else { // Try to recover partial response if we got data $responseBody = $result['response']['body'] ?? ''; if (!empty($responseBody) && strlen($responseBody) > 1000) { $dataSize = number_format(strlen($responseBody)); $message = "⚠️ Timeout occurred but received {$dataSize} bytes of data. Attempting recovery..."; // Save the partial response for analysis file_put_contents(__DIR__ . '/partial_response.json', $responseBody); // Advanced JSON recovery $fixedJson = $responseBody; // Try to find where the JSON breaks and truncate to last complete object $lastCompletePos = strrpos($fixedJson, '"}},{"type":"com.artesia'); if ($lastCompletePos !== false) { $fixedJson = substr($fixedJson, 0, $lastCompletePos + 3); // Keep the "} } // Add missing closing brackets $openBraces = substr_count($fixedJson, '{') - substr_count($fixedJson, '}'); $openBrackets = substr_count($fixedJson, '[') - substr_count($fixedJson, ']'); for ($i = 0; $i < $openBrackets; $i++) { $fixedJson .= ']'; } for ($i = 0; $i < $openBraces; $i++) { $fixedJson .= '}'; } $partialData = json_decode($fixedJson, true); if ($partialData && isset($partialData['search_result_resource'])) { $allCampaignsData = $fixedJson; $_SESSION['all_campaigns_data'] = $allCampaignsData; $availableFilters = CampaignFormatter::getAvailableFilterOptions($allCampaignsData); $actionableCampaigns = CampaignFormatter::getActionableCampaigns($allCampaignsData); $workflowStep = 1; $assetCount = count($partialData['search_result_resource']['asset_list'] ?? []); $message .= " ✅ Successfully recovered {$assetCount} campaigns from partial data! (Saved to partial_response.json)"; } else { $error = "Timeout: Received {$dataSize} bytes but couldn't parse as valid JSON. Try 'Load Filtered Campaigns' for smaller dataset."; } } else { $error = "Failed to load campaigns: " . ($result['response']['error'] ?? 'Unknown error'); } } break; } } } if ($_POST['action'] === 'select_campaign' && !empty($_POST['selected_campaign_id'])) { // Store selected campaign and move to step 2 $_SESSION['selected_campaign_id'] = $_POST['selected_campaign_id']; $_SESSION['actionable_campaigns'] = $_POST['campaigns_data'] ?? ''; $workflowStep = 2; } if ($_POST['action'] === 'get_folders') { // Step 2: Get Master Asset and Final Asset folders $campaignId = $_SESSION['selected_campaign_id'] ?? ''; if ($campaignId) { $workflowResults['folders'] = getAssetFolders($testRunner, $campaignId); $workflowStep = 2; } } if ($_POST['action'] === 'test_token') { // Test OAuth2 token and connection $tokenStatus = $testRunner->getOAuth2Status(); if ($tokenStatus['enabled'] && ($tokenStatus['has_token'] ?? false)) { // Test with a simple API call $testResult = $testRunner->testConnection('https://ppr.dam.ferrero.com/otmmapi/v6/users/current'); if ($testResult['success']) { $message = "✅ OAuth2 token is working. Connection successful."; } else { $error = "❌ OAuth2 token may be expired or invalid. Error: " . ($testResult['error'] ?? 'Connection failed'); } } else { $error = "❌ OAuth2 token not available or expired"; } } if ($_POST['action'] === 'back_to_step1') { // Clear session and go back to step 1 unset($_SESSION['selected_campaign_id']); unset($_SESSION['actionable_campaigns']); unset($_SESSION['master_folder_id']); $workflowStep = 1; $actionableCampaigns = []; $selectedCampaign = null; $workflowResults = []; } if ($_POST['action'] === 'get_assets') { // Step 3: Get assets from Master Asset folder $folderId = $_POST['master_folder_id'] ?? ''; if ($folderId) { $workflowResults['assets'] = getAssetsFromFolder($testRunner, $folderId); $_SESSION['master_folder_id'] = $folderId; $workflowStep = 3; } } if ($_POST['action'] === 'peek_folder') { // Peek inside folder to see what's there $folderId = $_POST['peek_folder_id'] ?? ''; if ($folderId) { $workflowResults['peek'] = getAssetsFromFolder($testRunner, $folderId); $workflowStep = 2; // Stay on step 2 but show peek results } } } // Load saved data from session if (isset($_SESSION['selected_campaign_id']) && !empty($_SESSION['actionable_campaigns'])) { $actionableCampaigns = json_decode($_SESSION['actionable_campaigns'], true) ?: []; $selectedCampaignId = $_SESSION['selected_campaign_id']; foreach ($actionableCampaigns as $campaign) { if ($campaign['asset_id'] === $selectedCampaignId) { $selectedCampaign = $campaign; break; } } } function getAssetFolders($testRunner, $campaignId) { $requests = $testRunner->getAvailableRequests(); foreach ($requests as $index => $request) { $name = strtolower($request['name']); if (strpos($name, 'master asset folder') !== false && strpos($name, 'final asset') !== false) { // Modify the request URL to use the specific campaign ID $modifiedRequest = $request; $url = is_array($request['request']['url']) ? $request['request']['url']['raw'] : $request['request']['url']; // Replace placeholder with actual campaign ID $url = str_replace('6930c59abea5bd4259b67f7647f65cd01d36278d', $campaignId, $url); // Replace {{baseUrl}} with the actual base URL $baseUrl = 'https://ppr.dam.ferrero.com/otmmapi'; $url = str_replace('{{baseUrl}}', $baseUrl, $url); // If still relative, prepend base URL if (!preg_match('/^https?:\/\//', $url)) { $url = rtrim($baseUrl, '/') . '/' . ltrim($url, '/'); } if (is_array($modifiedRequest['request']['url'])) { $modifiedRequest['request']['url']['raw'] = $url; } else { $modifiedRequest['request']['url'] = $url; } return $testRunner->runSingleTest($modifiedRequest, $index); } } return ['status' => 'ERROR', 'message' => 'Asset folders request not found']; } function getAssetsFromFolder($testRunner, $folderId) { $requests = $testRunner->getAvailableRequests(); foreach ($requests as $index => $request) { $name = strtolower($request['name']); if (strpos($name, 'all assets from') !== false) { // Modify the request URL to use the specific folder ID $modifiedRequest = $request; $url = is_array($request['request']['url']) ? $request['request']['url']['raw'] : $request['request']['url']; // Replace placeholder with actual folder ID - need to find the right pattern $url = preg_replace('/folders\/[^\/]+\//', "folders/{$folderId}/", $url); // Replace {{baseUrl}} with the actual base URL $baseUrl = 'https://ppr.dam.ferrero.com/otmmapi'; $url = str_replace('{{baseUrl}}', $baseUrl, $url); // If still relative, prepend base URL if (!preg_match('/^https?:\/\//', $url)) { $url = rtrim($baseUrl, '/') . '/' . ltrim($url, '/'); } if (is_array($modifiedRequest['request']['url'])) { $modifiedRequest['request']['url']['raw'] = $url; } else { $modifiedRequest['request']['url'] = $url; } return $testRunner->runSingleTest($modifiedRequest, $index); } } return ['status' => 'ERROR', 'message' => 'Assets request not found']; } $oauth2Status = $testRunner ? $testRunner->getOAuth2Status() : null; $credentials = $testRunner ? $testRunner->getCredentials() : []; ?>
Multi-step process to retrieve and download campaign assets
Load all campaigns and filter by stage, type, brand, or market
= htmlspecialchars($selectedCampaign['asset_id']) ?>
Status: = $foldersResult['status'] ?>
HTTP Code: = $foldersResult['response']['http_code'] ?? 'N/A' ?>
URL: = htmlspecialchars($foldersResult['response']['url'] ?? 'N/A') ?>
Debug - Response Structure:"; echo "Only seeing Final Assets folder? The Master Assets folder might be:
"; echo "Suggestions:
"; echo "= htmlspecialchars(substr($foldersResult['response']['body'], 0, 2000)) ?>...
= htmlspecialchars($folderId) ?>Error: = htmlspecialchars($foldersResult['response']['error'] ?? $foldersResult['message'] ?? 'Unknown error') ?>
Status: = $peekResult['status'] ?>
HTTP Code: = $peekResult['response']['http_code'] ?? 'N/A' ?>
Total Items Found: = count($peekAssets) ?>
📁 Subfolders: = $folderCount ?>
📄 Assets: = $assetCount ?>
File Types Found:
No assets or subfolders found in this folder.
Error: = htmlspecialchars($peekResult['response']['error'] ?? $peekResult['message'] ?? 'Unknown error') ?>
Status: = $assetsResult['status'] ?>
HTTP Code: = $assetsResult['response']['http_code'] ?? 'N/A' ?>
URL: = htmlspecialchars($assetsResult['response']['url'] ?? 'N/A') ?>
= htmlspecialchars($asset['id'] ?? 'N/A') ?>Error: = htmlspecialchars($assetsResult['response']['error'] ?? $assetsResult['message'] ?? 'Unknown error') ?>