loreal-global-kickoff/validate-box.php
DJP 47b0615c3a Fix grandparent logic: properly get folder two levels up
Structure: Campaign Number → CAMPAIGN_ASSETS → SUPPLIED_ASSETS
- Changed to fetch parent, then parent's parent (proper two levels up)
- Previous logic was using path_collection which showed CAMPAIGN_ASSETS
- Now correctly retrieves the campaign number folder
- Uses two separate API calls to traverse up the hierarchy

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 15:22:06 -05:00

96 lines
2.7 KiB
PHP

<?php
/**
* AJAX Endpoint for Box ID Validation
*/
// Enable error reporting for debugging (disable in production)
error_reporting(E_ALL);
ini_set('display_errors', 0); // Don't display errors in output
// 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);
if (!isset($input['boxId']) || empty($input['boxId'])) {
http_response_code(400);
echo json_encode([
'success' => false,
'error' => 'Box ID is required'
]);
exit;
}
$boxId = trim($input['boxId']);
// Validate Box ID format (should be numeric)
if (!is_numeric($boxId)) {
http_response_code(400);
echo json_encode([
'success' => false,
'error' => 'Invalid Box ID format. Box ID must be numeric.'
]);
exit;
}
// Initialize Box service and validate
$boxService = new BoxService();
$result = $boxService->validateBoxId($boxId);
if (!$result['valid']) {
http_response_code(404);
echo json_encode([
'success' => false,
'error' => $result['error'] ?? 'Box ID not found or inaccessible'
]);
exit;
}
// Return successful validation with data
echo json_encode([
'success' => true,
'data' => [
'boxId' => $boxId,
'folderName' => $result['folderInfo']['name'],
'masterCampaignNumber' => $result['grandparent']['name'] ?? 'N/A',
'masterCampaignId' => $result['grandparent']['id'] ?? null,
'contents' => [
'total' => $result['contents']['total'],
'folders' => $result['contents']['folders'],
'files' => $result['contents']['files']
]
]
]);
} catch (Exception $e) {
// Log error (in production, log to file instead of returning details)
error_log('Box validation error: ' . $e->getMessage());
http_response_code(500);
echo json_encode([
'success' => false,
'error' => 'Server error occurred while validating Box ID',
'debug' => $e->getMessage() // Remove in production
]);
}