Files
citrus-cms/resources/views/admin-ajax/save-dynamic-document-item.blade.php
T
2026-04-28 21:15:09 +03:00

122 lines
4.1 KiB
PHP

<?php
// resources/views/admin-ajax/save-dynamic-document-item.blade.php
use Illuminate\Support\Facades\DB;
// Get form data using request() helper to ensure proper data retrieval
$name = request()->input('name');
$displayTitle = request()->input('display_title');
$description = request()->input('description');
$icon = request()->input('icon', '🔧');
$badgeText = request()->input('badge_text', 'Dynamic');
$badgeColor = request()->input('badge_color', 'warning');
$searchFolder = request()->input('search_folder');
$sqlQuery = request()->input('sql_query');
$identifierField = request()->input('identifier_field', 'identifier');
$dateField = request()->input('date_field', 'document_date');
$fileSearchPattern = request()->input('file_search_pattern', '*{identifier}*.pdf');
$title2Pattern = request()->input('title2_pattern', '{identifier}');
$title4Pattern = request()->input('title4_pattern', 'Отчет №{identifier}');
// Debug log
error_log("DEBUG - Form Data Received:");
error_log("Name: " . ($name ?? 'NULL'));
error_log("Display Title: " . ($displayTitle ?? 'NULL'));
error_log("Search Folder: " . ($searchFolder ?? 'NULL'));
error_log("Identifier Field: " . ($identifierField ?? 'NULL'));
error_log("Date Field: " . ($dateField ?? 'NULL'));
error_log("File Search Pattern: " . ($fileSearchPattern ?? 'NULL'));
try {
// Validate required fields
if (empty($name)) {
throw new Exception('Item Name is required');
}
if (empty($displayTitle)) {
throw new Exception('Display Title is required');
}
if (empty($searchFolder)) {
throw new Exception('Search Folder is required');
}
if (empty($sqlQuery)) {
throw new Exception('SQL Query is required');
}
// Ensure identifier_field and date_field have values
if (empty($identifierField)) {
$identifierField = 'identifier';
}
if (empty($dateField)) {
$dateField = 'document_date';
}
if (empty($fileSearchPattern)) {
$fileSearchPattern = '*{identifier}*.pdf';
}
// Check if name already exists
$existing = DB::table('register_document_mappings')
->where('name', $name)
->first();
if ($existing) {
throw new Exception('An item with this name already exists');
}
// Validate SQL query syntax (basic check)
// Allow multiline queries and flexible whitespace
$cleanedQuery = preg_replace('/\s+/', ' ', trim($sqlQuery));
if (!preg_match('/SELECT\s+.+\s+FROM\s+[\w`]+/i', $cleanedQuery)) {
throw new Exception('Invalid SQL query syntax. Must contain SELECT ... FROM ...');
}
// Insert new dynamic document mapping
$mappingId = DB::table('register_document_mappings')->insertGetId([
'name' => $name,
'description' => $description,
'is_active' => 1,
'order' => 0,
'search_folder' => $searchFolder,
'sql_query' => $sqlQuery,
'identifier_field' => $identifierField,
'date_field' => $dateField,
'file_search_pattern' => $fileSearchPattern,
'title_pattern' => null,
'title2_pattern' => $title2Pattern,
'title3_pattern' => null,
'title4_pattern' => $title4Pattern,
'icon' => $icon,
'badge_text' => $badgeText,
'color' => $badgeColor,
'created_at' => now(),
'updated_at' => now()
]);
echo json_encode([
'success' => true,
'message' => 'Dynamic document item created successfully',
'mapping_id' => $mappingId
]);
} catch (\Throwable $th) {
// Log detailed error
error_log("ERROR in save-dynamic-document-item: " . $th->getMessage());
error_log("File: " . $th->getFile() . " Line: " . $th->getLine());
error_log("Stack trace: " . $th->getTraceAsString());
echo json_encode([
'success' => false,
'message' => $th->getMessage(),
'error_details' => [
'file' => $th->getFile(),
'line' => $th->getLine(),
'type' => get_class($th)
],
'sql_error' => (strpos($th->getMessage(), 'SQLSTATE') !== false) ? $th->getMessage() : null
]);
}