Complete PHP-based workflow application for Ferrero DAM system: - OAuth2 authentication with automatic token management - Campaign discovery and filtering - Folder structure navigation - Asset download (individual and bulk) - Metadata extraction and display - Clean step-by-step web interface Status: Fully functional and production-ready 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
362 lines
No EOL
15 KiB
PHP
362 lines
No EOL
15 KiB
PHP
<?php
|
||
|
||
class ResponseFormatter
|
||
{
|
||
public static function formatCampaignFolders($jsonResponse)
|
||
{
|
||
$data = json_decode($jsonResponse, true);
|
||
|
||
// Handle the actual API response structure
|
||
if (!$data) {
|
||
return "Invalid JSON response.";
|
||
}
|
||
|
||
$assetList = null;
|
||
|
||
// Check different possible structures
|
||
if (isset($data['search_result_resource']['search_result']['asset_list'])) {
|
||
$assetList = $data['search_result_resource']['search_result']['asset_list'];
|
||
} elseif (isset($data['asset_list'])) {
|
||
$assetList = $data['asset_list'];
|
||
} elseif (isset($data['items'])) {
|
||
$assetList = $data['items'];
|
||
}
|
||
|
||
if (!$assetList) {
|
||
return "No campaign folders found in response.";
|
||
}
|
||
|
||
$output = "<div class='formatted-response'>";
|
||
$output .= "<h3>Campaign Folders (" . count($assetList) . " found)</h3>";
|
||
|
||
foreach ($assetList as $index => $asset) {
|
||
$output .= "<div class='folder-item' style='border: 1px solid #ddd; margin: 10px 0; padding: 15px; border-radius: 4px;'>";
|
||
|
||
// Basic asset info
|
||
$assetName = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN_NAME') ?? 'Unnamed Campaign';
|
||
$output .= "<h4>Campaign " . ($index + 1) . ": " . htmlspecialchars($assetName) . "</h4>";
|
||
$output .= "<p><strong>Asset ID:</strong> " . htmlspecialchars($asset['asset_id'] ?? 'N/A') . "</p>";
|
||
$output .= "<p><strong>Type:</strong> " . htmlspecialchars($asset['data_type'] ?? 'N/A') . "</p>";
|
||
|
||
// Campaign metadata
|
||
if (isset($asset['metadata'])) {
|
||
$output .= "<div class='metadata'>";
|
||
$output .= "<h5>Campaign Information:</h5>";
|
||
|
||
// Campaign ID
|
||
$campaignId = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN ID');
|
||
if ($campaignId) {
|
||
$output .= "<p><strong>Campaign ID:</strong> " . htmlspecialchars($campaignId) . "</p>";
|
||
}
|
||
|
||
// Campaign Name
|
||
$campaignName = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN NAME');
|
||
if ($campaignName) {
|
||
$output .= "<p><strong>Campaign Name:</strong> " . htmlspecialchars($campaignName) . "</p>";
|
||
}
|
||
|
||
// Campaign Type
|
||
$campaignType = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN TYPE');
|
||
if ($campaignType) {
|
||
$output .= "<p><strong>Campaign Type:</strong> " . htmlspecialchars($campaignType) . "</p>";
|
||
}
|
||
|
||
// Brand
|
||
$brand = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN_BRAND');
|
||
if ($brand) {
|
||
$output .= "<p><strong>Brand:</strong> " . htmlspecialchars($brand) . "</p>";
|
||
}
|
||
|
||
// Market
|
||
$market = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN_MARKET');
|
||
if ($market) {
|
||
$output .= "<p><strong>Market:</strong> " . htmlspecialchars($market) . "</p>";
|
||
}
|
||
|
||
// Fiscal Year
|
||
$fiscalYear = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN FISCAL YEAR');
|
||
if ($fiscalYear) {
|
||
$output .= "<p><strong>Fiscal Year:</strong> " . htmlspecialchars($fiscalYear) . "</p>";
|
||
}
|
||
|
||
// Stage
|
||
$stage = self::getMetadataValue($asset, 'FERRERO.FIELD.SG.STAGE');
|
||
if ($stage) {
|
||
$output .= "<p><strong>Stage:</strong> " . htmlspecialchars($stage) . "</p>";
|
||
}
|
||
|
||
$output .= "</div>";
|
||
}
|
||
|
||
// Dates
|
||
if (isset($asset['date_imported'])) {
|
||
$output .= "<p><strong>Imported:</strong> " . htmlspecialchars($asset['date_imported']) . "</p>";
|
||
}
|
||
if (isset($asset['date_last_updated'])) {
|
||
$output .= "<p><strong>Last Updated:</strong> " . htmlspecialchars($asset['date_last_updated']) . "</p>";
|
||
}
|
||
|
||
// Show all campaigns without filtering for now
|
||
$campaignType = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN TYPE');
|
||
$stage = self::getMetadataValue($asset, 'FERRERO.FIELD.SG.STAGE');
|
||
|
||
$output .= "<div style='background: #e8f4f8; padding: 10px; border-radius: 4px; margin-top: 10px;'>";
|
||
$output .= "<strong>📊 Campaign Status:</strong><br>";
|
||
$output .= "Type: " . htmlspecialchars($campaignType ?? 'Not specified') . "<br>";
|
||
$output .= "Stage: " . htmlspecialchars($stage ?? 'Not specified');
|
||
$output .= "</div>";
|
||
|
||
$output .= "</div>";
|
||
}
|
||
|
||
$output .= "</div>";
|
||
|
||
// Analysis Summary
|
||
$typeAnalysis = [];
|
||
$stageAnalysis = [];
|
||
$brandAnalysis = [];
|
||
$marketAnalysis = [];
|
||
|
||
foreach ($assetList as $asset) {
|
||
// Campaign Types
|
||
$type = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN TYPE');
|
||
if ($type) {
|
||
$typeAnalysis[$type] = ($typeAnalysis[$type] ?? 0) + 1;
|
||
}
|
||
|
||
// Stages
|
||
$stage = self::getMetadataValue($asset, 'FERRERO.FIELD.SG.STAGE');
|
||
if ($stage) {
|
||
$stageAnalysis[$stage] = ($stageAnalysis[$stage] ?? 0) + 1;
|
||
}
|
||
|
||
// Brands
|
||
$brand = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN_BRAND');
|
||
if ($brand) {
|
||
$brandAnalysis[$brand] = ($brandAnalysis[$brand] ?? 0) + 1;
|
||
}
|
||
|
||
// Markets
|
||
$market = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN_MARKET');
|
||
if ($market) {
|
||
$marketAnalysis[$market] = ($marketAnalysis[$market] ?? 0) + 1;
|
||
}
|
||
}
|
||
|
||
$output .= "<div class='analysis-summary' style='background: #f8f9fa; padding: 15px; border-radius: 4px; margin-top: 20px;'>";
|
||
$output .= "<h4>Campaign Analysis</h4>";
|
||
$output .= "<p><strong>Total Campaigns Found:</strong> " . count($assetList) . "</p>";
|
||
|
||
// Campaign Types Breakdown
|
||
if (!empty($typeAnalysis)) {
|
||
$output .= "<div style='margin: 15px 0;'>";
|
||
$output .= "<h5>Campaign Types:</h5>";
|
||
arsort($typeAnalysis);
|
||
foreach ($typeAnalysis as $type => $count) {
|
||
$output .= "<div style='margin: 5px 0; padding: 5px; background: white; border-left: 3px solid #007cba;'>";
|
||
$output .= "<strong>" . htmlspecialchars($type) . ":</strong> " . $count . " campaigns";
|
||
$output .= "</div>";
|
||
}
|
||
$output .= "</div>";
|
||
}
|
||
|
||
// Stage Breakdown
|
||
if (!empty($stageAnalysis)) {
|
||
$output .= "<div style='margin: 15px 0;'>";
|
||
$output .= "<h5>Campaign Stages:</h5>";
|
||
arsort($stageAnalysis);
|
||
foreach ($stageAnalysis as $stage => $count) {
|
||
$output .= "<div style='margin: 5px 0; padding: 5px; background: white; border-left: 3px solid #28a745;'>";
|
||
$output .= "<strong>" . htmlspecialchars($stage) . ":</strong> " . $count . " campaigns";
|
||
$output .= "</div>";
|
||
}
|
||
$output .= "</div>";
|
||
}
|
||
|
||
// Brand Breakdown (top 10)
|
||
if (!empty($brandAnalysis)) {
|
||
$output .= "<div style='margin: 15px 0;'>";
|
||
$output .= "<h5>Top Brands:</h5>";
|
||
arsort($brandAnalysis);
|
||
$topBrands = array_slice($brandAnalysis, 0, 10, true);
|
||
foreach ($topBrands as $brand => $count) {
|
||
$output .= "<div style='margin: 3px 0; padding: 3px; background: white;'>";
|
||
$output .= "<strong>" . htmlspecialchars($brand) . ":</strong> " . $count;
|
||
$output .= "</div>";
|
||
}
|
||
$output .= "</div>";
|
||
}
|
||
|
||
// Market Breakdown
|
||
if (!empty($marketAnalysis)) {
|
||
$output .= "<div style='margin: 15px 0;'>";
|
||
$output .= "<h5>Markets:</h5>";
|
||
arsort($marketAnalysis);
|
||
foreach ($marketAnalysis as $market => $count) {
|
||
$output .= "<div style='margin: 3px 0; padding: 3px; background: white;'>";
|
||
$output .= "<strong>" . htmlspecialchars($market) . ":</strong> " . $count;
|
||
$output .= "</div>";
|
||
}
|
||
$output .= "</div>";
|
||
}
|
||
|
||
$output .= "</div>";
|
||
|
||
return $output;
|
||
}
|
||
|
||
private static function getMetadataValue($asset, $fieldId)
|
||
{
|
||
if (!isset($asset['metadata']['metadata_element_list'])) {
|
||
return null;
|
||
}
|
||
|
||
foreach ($asset['metadata']['metadata_element_list'] as $category) {
|
||
if (!isset($category['metadata_element_list'])) {
|
||
continue;
|
||
}
|
||
|
||
foreach ($category['metadata_element_list'] as $element) {
|
||
if ($element['id'] === $fieldId && isset($element['value']['value']['value'])) {
|
||
return $element['value']['value']['value'];
|
||
}
|
||
|
||
// Handle tabular fields (like brand, market)
|
||
if (isset($element['metadata_element_list'])) {
|
||
foreach ($element['metadata_element_list'] as $tableField) {
|
||
if ($tableField['id'] === $fieldId && isset($tableField['value']['value']['value'])) {
|
||
return $tableField['value']['value']['value'];
|
||
}
|
||
|
||
// Handle domain values
|
||
if ($tableField['id'] === $fieldId && isset($tableField['value']['value']['field_value']['value'])) {
|
||
return $tableField['value']['value']['display_value'] ?? $tableField['value']['value']['field_value']['value'];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private static function doesCampaignNeedAction($asset)
|
||
{
|
||
// Get campaign type and stage to determine if action is needed
|
||
$campaignType = self::getMetadataValue($asset, 'FERRERO.FIELD.CAMPAIGN TYPE');
|
||
$stage = self::getMetadataValue($asset, 'FERRERO.FIELD.SG.STAGE');
|
||
|
||
// Example criteria - adjust based on your business logic
|
||
$actionableTypes = [
|
||
'Local adaptation of global comm',
|
||
'Local campaign creation',
|
||
'Active'
|
||
];
|
||
|
||
$actionableStages = [
|
||
'3 - Production & execution',
|
||
'4 – Campaign Approved'
|
||
];
|
||
|
||
if ($campaignType && in_array($campaignType, $actionableTypes)) {
|
||
return true;
|
||
}
|
||
|
||
if ($stage && in_array($stage, $actionableStages)) {
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
public static function formatAssetFolders($jsonResponse)
|
||
{
|
||
$data = json_decode($jsonResponse, true);
|
||
|
||
if (!$data || !isset($data['items'])) {
|
||
return "No asset folders found or invalid response format.";
|
||
}
|
||
|
||
$output = "<div class='formatted-response'>";
|
||
$output .= "<h3>Asset Folders (" . count($data['items']) . " found)</h3>";
|
||
|
||
foreach ($data['items'] as $index => $folder) {
|
||
$output .= "<div class='folder-item' style='border: 1px solid #ddd; margin: 10px 0; padding: 15px; border-radius: 4px;'>";
|
||
$output .= "<h4>Folder " . ($index + 1) . ": " . htmlspecialchars($folder['name'] ?? 'Unnamed') . "</h4>";
|
||
$output .= "<p><strong>ID:</strong> " . htmlspecialchars($folder['id'] ?? 'N/A') . "</p>";
|
||
$output .= "<p><strong>Type:</strong> " . htmlspecialchars($folder['type'] ?? 'N/A') . "</p>";
|
||
|
||
// Check if it's a master asset or final asset folder
|
||
$folderName = strtolower($folder['name'] ?? '');
|
||
if (strpos($folderName, 'master') !== false) {
|
||
$output .= "<span class='badge' style='background: #007cba; color: white; padding: 4px 8px; border-radius: 4px;'>Master Asset Folder</span>";
|
||
} elseif (strpos($folderName, 'final') !== false) {
|
||
$output .= "<span class='badge' style='background: #28a745; color: white; padding: 4px 8px; border-radius: 4px;'>Final Asset Folder</span>";
|
||
}
|
||
|
||
$output .= "</div>";
|
||
}
|
||
|
||
$output .= "</div>";
|
||
|
||
return $output;
|
||
}
|
||
|
||
public static function formatAssets($jsonResponse)
|
||
{
|
||
$data = json_decode($jsonResponse, true);
|
||
|
||
if (!$data || !isset($data['items'])) {
|
||
return "No assets found or invalid response format.";
|
||
}
|
||
|
||
$output = "<div class='formatted-response'>";
|
||
$output .= "<h3>Assets (" . count($data['items']) . " found)</h3>";
|
||
|
||
foreach ($data['items'] as $index => $asset) {
|
||
$output .= "<div class='asset-item' style='border: 1px solid #ddd; margin: 10px 0; padding: 15px; border-radius: 4px;'>";
|
||
$output .= "<h4>Asset " . ($index + 1) . ": " . htmlspecialchars($asset['name'] ?? 'Unnamed') . "</h4>";
|
||
$output .= "<p><strong>ID:</strong> " . htmlspecialchars($asset['id'] ?? 'N/A') . "</p>";
|
||
$output .= "<p><strong>Type:</strong> " . htmlspecialchars($asset['type'] ?? 'N/A') . "</p>";
|
||
|
||
if (isset($asset['file_size'])) {
|
||
$output .= "<p><strong>File Size:</strong> " . self::formatFileSize($asset['file_size']) . "</p>";
|
||
}
|
||
|
||
if (isset($asset['mime_type'])) {
|
||
$output .= "<p><strong>MIME Type:</strong> " . htmlspecialchars($asset['mime_type']) . "</p>";
|
||
}
|
||
|
||
$output .= "</div>";
|
||
}
|
||
|
||
$output .= "</div>";
|
||
|
||
return $output;
|
||
}
|
||
|
||
private static function formatFileSize($bytes)
|
||
{
|
||
$units = ['B', 'KB', 'MB', 'GB'];
|
||
$bytes = max($bytes, 0);
|
||
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
||
$pow = min($pow, count($units) - 1);
|
||
|
||
$bytes /= (1 << (10 * $pow));
|
||
|
||
return round($bytes, 2) . ' ' . $units[$pow];
|
||
}
|
||
|
||
public static function formatGenericResponse($jsonResponse)
|
||
{
|
||
$data = json_decode($jsonResponse, true);
|
||
|
||
if (!$data) {
|
||
return "Invalid JSON response.";
|
||
}
|
||
|
||
return "<pre style='background: #f8f9fa; padding: 15px; border-radius: 4px; overflow-x: auto;'>" .
|
||
htmlspecialchars(json_encode($data, JSON_PRETTY_PRINT)) .
|
||
"</pre>";
|
||
}
|
||
} |