loreal-global-kickoff/validate-box.php
DJP ef7ffedc94 Fix Master Campaign Number to use parent folder (one level up)
Changed from grandparent (two levels up) to parent (one level up).
- Renamed getGrandparentFolder() to getParentFolder()
- Uses folder's direct parent instead of path_collection traversal
- Master Campaign Number should now show the campaign number folder
- Updated validate-box.php to use 'parent' instead of 'grandparent'

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 15:20:39 -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['parent']['name'] ?? 'N/A',
'masterCampaignId' => $result['parent']['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
]);
}