Files
crm.clientright.ru/crm_extensions/file_storage/api/edit_document.php

75 lines
2.0 KiB
PHP

<?php
/**
* Document Editor API Endpoint
* API для подготовки документов к редактированию
*/
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
// Обработка preflight запросов
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
require_once __DIR__ . '/../FileStorageManager.php';
require_once __DIR__ . '/../../shared/Logger.php';
try {
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
throw new Exception('Invalid JSON input');
}
$action = $input['action'] ?? '';
$recordId = $input['record_id'] ?? '';
$fileName = $input['file_name'] ?? '';
if (empty($recordId)) {
throw new Exception('Record ID is required');
}
CRM_Logger::info("Document editor API called", [
'action' => $action,
'record_id' => $recordId,
'file_name' => $fileName
]);
$fileManager = new FileStorageManager();
switch ($action) {
case 'prepare_edit':
$result = $fileManager->prepareForEditing($recordId, $fileName);
break;
case 'sync_changes':
$result = $fileManager->syncAfterEditing($recordId);
break;
case 'get_edit_status':
$result = $fileManager->getEditStatus($recordId);
break;
default:
throw new Exception('Unknown action: ' . $action);
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
} catch (Exception $e) {
CRM_Logger::error("Document editor API error", [
'error' => $e->getMessage(),
'input' => $input ?? null
]);
http_response_code(400);
echo json_encode([
'success' => false,
'error' => $e->getMessage()
], JSON_UNESCAPED_UNICODE);
}