diff --git a/src/DatabaseClient.php b/src/DatabaseClient.php index d417ada..4726798 100644 --- a/src/DatabaseClient.php +++ b/src/DatabaseClient.php @@ -34,15 +34,25 @@ class DatabaseClient } try { - // Insert using existing columns + // Extract metadata from DAM asset using CampaignFormatter helper + $metadata = $this->extractMetadataFields($assetData); + + // Insert using all available columns $stmt = $this->pdo->prepare(" INSERT INTO master_assets ( tracking_id, opentext_id, original_filename, file_extension, + brand_code, + brand_name, + country_code, + language_code, + asset_type, file_size_bytes, mime_type, + width_px, + height_px, upload_directory, description, status @@ -51,31 +61,50 @@ class DatabaseClient :opentext_id, :original_filename, :file_extension, + :brand_code, + :brand_name, + :country_code, + :language_code, + :asset_type, :file_size_bytes, :mime_type, + :width_px, + :height_px, :upload_directory, :description, :status ) ON CONFLICT (tracking_id) DO UPDATE SET upload_directory = EXCLUDED.upload_directory, + brand_code = EXCLUDED.brand_code, + country_code = EXCLUDED.country_code, + language_code = EXCLUDED.language_code, + width_px = EXCLUDED.width_px, + height_px = EXCLUDED.height_px, description = EXCLUDED.description "); $pathInfo = pathinfo($assetData['name'] ?? 'unknown'); - // Store Box info, DAM upload folder ID, and metadata JSON + // Store Box info and full metadata JSON $metadataJson = json_encode($assetData, JSON_PRETTY_PRINT); - $description = "Box File ID: {$boxFileId}\nBox URL: {$boxUrl}\nDAM Asset ID: " . ($assetData['asset_id'] ?? 'Unknown') . "\n\nDAM Metadata JSON:\n" . substr($metadataJson, 0, 5000); // Limit to 5KB + $description = "Box File ID: {$boxFileId}\nBox URL: {$boxUrl}\nDAM Asset ID: " . ($assetData['asset_id'] ?? 'Unknown') . "\n\nDAM Metadata JSON:\n" . substr($metadataJson, 0, 5000); $stmt->execute([ ':tracking_id' => $trackingId, ':opentext_id' => $assetData['asset_id'] ?? 'unknown', ':original_filename' => $pathInfo['filename'], ':file_extension' => '.' . ($pathInfo['extension'] ?? ''), + ':brand_code' => $metadata['brand_code'], + ':brand_name' => $metadata['brand_name'], + ':country_code' => $metadata['country_code'], + ':language_code' => $metadata['language_code'], + ':asset_type' => $metadata['asset_type'], ':file_size_bytes' => $assetData['file_size'] ?? null, ':mime_type' => $assetData['mime_type'] ?? null, - ':upload_directory' => $uploadFolderId, // DAM Final Assets folder ID + ':width_px' => $metadata['width_px'], + ':height_px' => $metadata['height_px'], + ':upload_directory' => $uploadFolderId, // Final Assets folder ID ':description' => $description, ':status' => 'active' ]); @@ -90,6 +119,101 @@ class DatabaseClient } } + /** + * Extract metadata fields from DAM asset + */ + private function extractMetadataFields($assetData) + { + $fields = [ + 'brand_code' => null, + 'brand_name' => null, + 'country_code' => null, + 'language_code' => null, + 'asset_type' => null, + 'width_px' => null, + 'height_px' => null + ]; + + // Extract from asset_content_info if available + if (isset($assetData['asset_content_info']['master_content'])) { + $content = $assetData['asset_content_info']['master_content']; + $fields['width_px'] = $content['width'] ?? null; + $fields['height_px'] = $content['height'] ?? null; + } + + // Extract from metadata fields + if (isset($assetData['metadata']['metadata_element_list'])) { + foreach ($assetData['metadata']['metadata_element_list'] as $category) { + if (!isset($category['metadata_element_list'])) { + continue; + } + + foreach ($category['metadata_element_list'] as $element) { + $fieldId = $element['id'] ?? ''; + + // Extract brand + if ($fieldId === 'FERRERO.FIELD.CAMPAIGN_BRAND') { + $fields['brand_name'] = $this->getFieldValue($element); + // Try to extract brand code from brand name (first 3 chars or full if shorter) + $brandName = $fields['brand_name']; + if ($brandName) { + $fields['brand_code'] = strtoupper(substr($brandName, 0, 3)); + } + } + + // Extract country/market + if ($fieldId === 'FERRERO.FIELD.CAMPAIGN_MARKET') { + $market = $this->getFieldValue($element); + if ($market && strlen($market) >= 2) { + $fields['country_code'] = strtoupper(substr($market, 0, 2)); + } + } + + // Check for tabular fields + if (isset($element['metadata_element_list'])) { + foreach ($element['metadata_element_list'] as $tableField) { + $tableFieldId = $tableField['id'] ?? ''; + + if ($tableFieldId === 'FERRERO.FIELD.CAMPAIGN_BRAND') { + $fields['brand_name'] = $this->getFieldValue($tableField); + if ($fields['brand_name']) { + $fields['brand_code'] = strtoupper(substr($fields['brand_name'], 0, 3)); + } + } + + if ($tableFieldId === 'FERRERO.FIELD.CAMPAIGN_MARKET') { + $market = $this->getFieldValue($tableField); + if ($market && strlen($market) >= 2) { + $fields['country_code'] = strtoupper(substr($market, 0, 2)); + } + } + } + } + } + } + } + + return $fields; + } + + /** + * Get field value from metadata element (handles both domain and regular values) + */ + private function getFieldValue($element) + { + // Handle domain values (with field_value) + if (isset($element['value']['value']['field_value']['value'])) { + return $element['value']['value']['field_value']['value']; + } + + // Handle regular string fields + if (isset($element['value']['value']['value'])) { + return $element['value']['value']['value']; + } + + return null; + } + /** * Check if database is available */