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

199 lines
7.5 KiB
PHP

<?php
// resources/views/admin-ajax/test-dynamic-document-item.blade.php
use Illuminate\Support\Facades\DB;
$sqlQuery = post('sql_query');
$searchFolder = post('search_folder');
$identifierField = post('identifier_field');
$dateField = post('date_field');
$fileSearchPattern = post('file_search_pattern');
$title4Pattern = post('title4_pattern');
// Debug: Log the received values
error_log("DEBUG - identifierField: " . ($identifierField ?? 'NULL'));
error_log("DEBUG - dateField: " . ($dateField ?? 'NULL'));
error_log("DEBUG - fileSearchPattern: " . ($fileSearchPattern ?? 'NULL'));
error_log("DEBUG - title4Pattern: " . ($title4Pattern ?? 'NULL'));
// Set default values if null or empty
if (empty($identifierField)) {
$identifierField = 'identifier';
}
if (empty($dateField)) {
$dateField = 'document_date';
}
if (empty($fileSearchPattern)) {
$fileSearchPattern = '*{identifier}*.pdf';
}
if (empty($title4Pattern)) {
$title4Pattern = 'Отчет №{identifier}';
}
// Get test values from test_ prefixed inputs
$testValues = [];
$emptyTestFields = [];
foreach ($_POST as $key => $value) {
if (strpos($key, 'test_') === 0) {
// Remove 'test_' prefix to get the actual field name
$fieldName = substr($key, 5); // Remove 'test_' (5 characters)
if (!empty($value)) {
$testValues[$fieldName] = $value;
} else {
$emptyTestFields[] = $fieldName;
}
}
}
try {
// Replace placeholders
$executedQuery = $sqlQuery;
// Find all placeholders in the query
preg_match_all('/:(\w+)/', $sqlQuery, $matches);
$placeholdersFound = $matches[1];
$replacedPlaceholders = [];
foreach ($testValues as $key => $value) {
if (!empty($value)) {
$quotedValue = DB::getPdo()->quote($value);
$executedQuery = str_replace(":{$key}", $quotedValue, $executedQuery);
$replacedPlaceholders[] = $key;
}
}
// Check for unreplaced placeholders
$unreplacedPlaceholders = array_diff($placeholdersFound, $replacedPlaceholders);
if (!empty($unreplacedPlaceholders)) {
$errorMsg = "Missing test values for placeholders: " . implode(', ', array_map(function($p) { return ":{$p}"; }, $unreplacedPlaceholders));
$errorMsg .= "\n\nPlease fill in the test input fields above before running the test.";
$errorMsg .= "\n\nDetected placeholders in SQL: " . implode(', ', array_map(function($p) { return ":{$p}"; }, $placeholdersFound));
$errorMsg .= "\nProvided test values: " . (empty($testValues) ? 'NONE' : implode(', ', array_keys($testValues)));
if (!empty($emptyTestFields)) {
$errorMsg .= "\nEmpty test fields: " . implode(', ', $emptyTestFields);
}
throw new Exception($errorMsg);
}
// Execute query
$start = microtime(true);
$results = DB::select($executedQuery);
$executionTime = round((microtime(true) - $start) * 1000, 2);
// Preview results
$preview = [];
foreach (array_slice($results, 0, 5) as $row) {
$rowArray = (array) $row;
// Debug: Log available fields
$availableFields = array_keys($rowArray);
// Debug: Show what fields are available
$availableFields = array_keys($rowArray);
// Try multiple field name variations
$identifier = $rowArray[$identifierField] ??
$rowArray['vt_request_no'] ??
$rowArray['identifier'] ??
'N/A';
$date = $rowArray[$dateField] ??
$rowArray['vt_test_date'] ??
$rowArray['document_date'] ??
'';
// Add debug info to preview
$debugInfo = [
'looking_for_identifier' => $identifierField,
'looking_for_date' => $dateField,
'available_fields' => $availableFields,
'found_identifier' => $identifier,
'found_date' => $date
];
// Apply title4 pattern - try both {field} and {{field}} syntax
$rowTitle = $title4Pattern;
foreach ($rowArray as $field => $value) {
$rowTitle = str_replace("{{$field}}", $value, $rowTitle);
$rowTitle = str_replace("{$field}", $value, $rowTitle);
}
// Apply file search pattern - try both {field} and {{field}} syntax
$searchPattern = $fileSearchPattern;
foreach ($rowArray as $field => $value) {
$searchPattern = str_replace("{{$field}}", $value, $searchPattern);
$searchPattern = str_replace("{$field}", $value, $searchPattern);
}
// Special handling for identifier field - map actual field names to pattern placeholders
// Since identifierField might be null, we'll use the actual field name from SQL result
$identifierValue = $rowArray[$identifierField] ?? $rowArray['vt_request_no'] ?? '';
// Apply pattern replacements
if (!empty($identifierValue)) {
$rowTitle = str_replace("{identifier}", $identifierValue, $rowTitle);
$searchPattern = str_replace("{identifier}", $identifierValue, $searchPattern);
}
// Additional pattern replacements for common field mappings
if (isset($rowArray['vt_request_no'])) {
$rowTitle = str_replace("{vt_request_no}", $rowArray['vt_request_no'], $rowTitle);
$searchPattern = str_replace("{vt_request_no}", $rowArray['vt_request_no'], $searchPattern);
}
// Debug: Log pattern replacement
error_log("DEBUG - Pattern replacement: identifierValue='$identifierValue', rowTitle='$rowTitle', searchPattern='$searchPattern'");
// Debug: Add pattern info
$debugInfo['title4_pattern'] = $title4Pattern;
$debugInfo['file_search_pattern'] = $fileSearchPattern;
$debugInfo['final_row_title'] = $rowTitle;
$debugInfo['final_search_pattern'] = $searchPattern;
$debugInfo['pattern_replacements'] = [];
$debugInfo['identifier_field_value'] = $rowArray[$identifierField] ?? 'NOT_FOUND';
$debugInfo['pattern_replacement_steps'] = [
'step1_title' => $title4Pattern,
'step1_pattern' => $fileSearchPattern,
'step2_title' => $rowTitle,
'step2_pattern' => $searchPattern,
'identifier_value_used' => $identifierValue
];
// Log each replacement attempt
foreach ($rowArray as $field => $value) {
$debugInfo['pattern_replacements'][] = "Field: {$field}, Value: {$value}";
}
// Search files
$fullPath = "storage/documents/{$searchFolder}/{$searchPattern}";
$files = glob($fullPath);
$preview[] = [
'identifier' => $identifier,
'date' => $date,
'row_title' => $rowTitle,
'search_pattern' => $searchPattern,
'files_found' => count($files),
'sample_files' => array_slice($files, 0, 2),
'debug' => $debugInfo
];
}
echo json_encode([
'success' => true,
'record_count' => count($results),
'execution_time' => $executionTime,
'executed_query' => $executedQuery,
'placeholders_replaced' => $replacedPlaceholders,
'test_values' => $testValues,
'preview' => $preview
]);
} catch (\Throwable $th) {
echo json_encode([
'success' => false,
'message' => $th->getMessage(),
'error' => $th->getFile() . ':' . $th->getLine()
]);
}