loreal-global-kickoff/submit.php
DJP dbf7090d09 Initial commit: L'Oréal Box Asset Submission Form
- Set up PHP application with Composer and JWT library
- Implemented SSO authentication with local dev mode
- Created Box API service for folder validation
- Built two-column form interface (form + preview)
- Added real-time Box ID validation with AJAX
- Integrated webhook submission with status response
- Auto-populate Master Campaign Number from Box folder hierarchy
- Responsive design with Montserrat font and black/yellow theme

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 14:43:36 -05:00

128 lines
3.9 KiB
PHP

<?php
/**
* Form Submission Handler
* Processes the form and sends data to webhook
*/
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 0);
// Set JSON header
header('Content-Type: application/json');
try {
// Load dependencies
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/AuthMiddleware.php';
require_once __DIR__ . '/BoxService.php';
// Check authentication
$auth = new AuthMiddleware();
$user = $auth->requireAuth();
// Check if this is a POST request
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode([
'success' => false,
'error' => 'Method not allowed'
]);
exit;
}
// Get request body
$input = json_decode(file_get_contents('php://input'), true);
// Validate required fields
$requiredFields = ['boxId', 'supplyDate', 'liveDate', 'endDate', 'boxData'];
foreach ($requiredFields as $field) {
if (!isset($input[$field]) || empty($input[$field])) {
http_response_code(400);
echo json_encode([
'success' => false,
'error' => "Missing required field: $field"
]);
exit;
}
}
// Load configuration
$config = require __DIR__ . '/config.php';
$webhookConfig = $config['webhook'];
// Prepare webhook payload
$payload = [
'userEmail' => $user['email'],
'userName' => $user['name'],
'boxId' => $input['boxId'],
'masterCampaignNumber' => $input['boxData']['masterCampaignNumber'] ?? 'N/A',
'masterCampaignId' => $input['boxData']['masterCampaignId'] ?? null,
'supplyDate' => $input['supplyDate'],
'liveDate' => $input['liveDate'],
'endDate' => $input['endDate'],
'boxContents' => [
'folderName' => $input['boxData']['folderName'] ?? '',
'totalItems' => $input['boxData']['contents']['total'] ?? 0,
'folders' => $input['boxData']['contents']['folders'] ?? [],
'files' => $input['boxData']['contents']['files'] ?? []
],
'submittedAt' => date('Y-m-d H:i:s')
];
// Send to webhook
$ch = curl_init($webhookConfig['url']);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $webhookConfig['timeout'],
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-API-Key: ' . $webhookConfig['api_key']
]
]);
$webhookResponse = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($curlError) {
throw new Exception('Webhook request failed: ' . $curlError);
}
// Parse webhook response
$webhookData = json_decode($webhookResponse, true);
// Check if webhook returned success
if ($httpCode >= 200 && $httpCode < 300) {
// Webhook accepted the data
echo json_encode([
'success' => true,
'message' => 'Submission successful',
'webhookResponse' => $webhookData,
'webhookStatus' => $httpCode
]);
} else {
// Webhook rejected or failed
http_response_code($httpCode);
echo json_encode([
'success' => false,
'error' => 'Webhook processing failed',
'webhookResponse' => $webhookData ?? $webhookResponse,
'webhookStatus' => $httpCode
]);
}
} catch (Exception $e) {
// Log error
error_log('Submission error: ' . $e->getMessage());
http_response_code(500);
echo json_encode([
'success' => false,
'error' => 'Server error occurred during submission',
'debug' => $e->getMessage() // Remove in production
]);
}