306 lines
9.8 KiB
PHP
306 lines
9.8 KiB
PHP
<?php
|
||
namespace App\Http\Controllers;
|
||
|
||
use Illuminate\Support\Facades\Storage;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use Carbon\Carbon;
|
||
|
||
use Illuminate\Http\Request;
|
||
use App\Contents;
|
||
use Illuminate\Support\Facades\DB;
|
||
use App\Models\DocumentRevision;
|
||
use App\Models\WeldLog;
|
||
use App\Models\DocumentHistory;
|
||
|
||
// Permission control for NDT report uploads and Incoming Control
|
||
$tableName = $request->input('table_name', null);
|
||
$permissionMapping = [
|
||
'v_t_logs' => 'vt_log_upload_permission',
|
||
'radiographic_tests' => 'rt_log_upload_permission',
|
||
'p_t_logs' => 'pt_log_upload_permission',
|
||
'magnetic_tests' => 'mt_log_upload_permission',
|
||
'p_w_h_t_s' => 'pwht_log_upload_permission',
|
||
'hardness_tests' => 'ht_log_upload_permission',
|
||
'p_m_i_tests' => 'pmi_log_upload_permission',
|
||
'ultrasonic_tests' => 'ut_log_upload_permission',
|
||
'ferrits' => 'ferrite_log_upload_permission',
|
||
'incoming_controls' => 'incoming_control_upload_permission',
|
||
];
|
||
|
||
if ($tableName && isset($permissionMapping[$tableName])) {
|
||
$permissionKey = $permissionMapping[$tableName];
|
||
$allowedLevels = j(setting($permissionKey));
|
||
$userLevel = Auth::user()->level ?? null;
|
||
|
||
if (is_array($allowedLevels) && !in_array($userLevel, $allowedLevels)) {
|
||
http_response_code(403);
|
||
die(json_encode([
|
||
'status' => 'error',
|
||
'message' => 'You do not have permission to upload files for this module.'
|
||
]));
|
||
}
|
||
}
|
||
|
||
|
||
/*
|
||
$post = $request->all();
|
||
$folder = post("folder");
|
||
$ext = strtolower($request->file->getClientOriginalExtension());
|
||
$name = $request->file->getClientOriginalName();
|
||
$name = str_replace(".PDF", ".pdf", $name);
|
||
$path = $request->file->storeAs($folder, $name);
|
||
$path = $request->file->storeAs($folder, $name);
|
||
$onlyFileName = str_replace('.' . $ext, "", $name);
|
||
$onlyFileName = strtoupper($onlyFileName);
|
||
*/
|
||
//$onlyFileName = explode(" ", $onlyFileName)[0];
|
||
|
||
$post = $request->all();
|
||
$folder = $request->input("folder");
|
||
$ext = strtolower($request->file('file')->getClientOriginalExtension());
|
||
$name = $request->file('file')->getClientOriginalName();
|
||
$name = str_replace(".PDF", ".pdf", $name);
|
||
$onlyFileName = str_replace('.' . $ext, "", $name);
|
||
$onlyFileName = strtoupper($onlyFileName);
|
||
|
||
// Define the path
|
||
$cleanFolder = trim($folder, '/');
|
||
$fullPath = $cleanFolder . '/' . $name;
|
||
|
||
|
||
// Store the file using Storage::put
|
||
|
||
// Check for existing files that match the base name (ignoring Rev suffixes)
|
||
$uploadedBaseName = pathinfo($name, PATHINFO_FILENAME);
|
||
// Remove " Rev" suffix to get the root name (e.g. "DocName Rev4" -> "DocName")
|
||
$uploadedRootName = preg_replace('/\sRev.*$/i', '', $uploadedBaseName);
|
||
|
||
// Get all files in the directory
|
||
$allFiles = Storage::files($folder);
|
||
|
||
foreach ($allFiles as $existingFile) {
|
||
$existingFileName = basename($existingFile);
|
||
$existingBaseName = pathinfo($existingFileName, PATHINFO_FILENAME);
|
||
$existingRootName = preg_replace('/\sRev.*$/i', '', $existingBaseName);
|
||
|
||
// If the root names match, archive the existing file
|
||
if ($existingRootName === $uploadedRootName) {
|
||
// Eski dosyayı arşivle
|
||
$currentDateTime = Carbon::now()->format('Y_m_d_H_i_s');
|
||
$fileExtension = pathinfo($existingFileName, PATHINFO_EXTENSION);
|
||
|
||
// Archive klasör yapısı: Archive/{original_folder}/{file_name_folder}/{file_name}_{datetime}.{ext}
|
||
$archiveFolder = 'Archive/' . $folder . '/' . $existingRootName;
|
||
$archivedFileName = $existingBaseName . '_' . $currentDateTime . '.' . $fileExtension;
|
||
$archivedPath = $archiveFolder . '/' . $archivedFileName;
|
||
|
||
// Archive klasörünü oluştur
|
||
if (!Storage::exists($archiveFolder)) {
|
||
Storage::makeDirectory($archiveFolder);
|
||
}
|
||
|
||
// Eski dosyayı arşive taşı
|
||
Storage::move($existingFile, $archivedPath);
|
||
|
||
// Document history tablosuna kayıt ekle
|
||
DocumentHistory::create([
|
||
'file_name' => $existingFileName,
|
||
'file_path' => $archivedPath,
|
||
'user_name' => Auth::user()->name ?? 'System',
|
||
'action' => 'Archive',
|
||
'file_size' => Storage::exists($archivedPath) ? round(Storage::size($archivedPath) / 1024, 2) . ' KB' : 'Unknown',
|
||
'moved_from' => $existingFile,
|
||
'moved_to' => $archivedPath,
|
||
'action_date' => Carbon::now(),
|
||
'table_name' => $request->input('table_name', null),
|
||
'column_name' => $request->input('column_name', null),
|
||
'row_id' => $request->input('row_id', null),
|
||
'notes' => 'File replaced with new version and archived'
|
||
]);
|
||
}
|
||
}
|
||
Storage::put($folder . '/' . $name, file_get_contents($request->file('file')->getRealPath()));
|
||
|
||
// Yeni dosya upload edildiğinde history kaydı ekle
|
||
DocumentHistory::create([
|
||
'file_name' => $name,
|
||
'file_path' => $folder . '/' . $name,
|
||
'user_name' => Auth::user()->name ?? 'System',
|
||
'action' => 'Upload',
|
||
'file_size' => $request->file('file')->getSize() ? round($request->file('file')->getSize() / 1024, 2) . ' KB' : 'Unknown',
|
||
'moved_from' => 'Local',
|
||
'moved_to' => $folder,
|
||
'action_date' => Carbon::now(),
|
||
'table_name' => $request->input('table_name', null),
|
||
'column_name' => $request->input('column_name', null),
|
||
'row_id' => $request->input('row_id', null),
|
||
'notes' => 'New file uploaded to server'
|
||
]);
|
||
|
||
|
||
if(postisset("table_name")) {
|
||
$tableName = post("table_name");
|
||
if($tableName == "weld_logs") {
|
||
$lineNumber = $name;
|
||
$lineNumber = str_replace(".pdf", "", $lineNumber);
|
||
dump($lineNumber);
|
||
?>
|
||
@include("cron.pdf-db-sync", ["lineNumber" => $lineNumber])
|
||
@include("cron.document_revisions-pdf-db-document-revisions-sync", [
|
||
"lineNumber" => $lineNumber,
|
||
])
|
||
<?php
|
||
}
|
||
|
||
// document_revisions için sync
|
||
if ($tableName == "document_revisions") {
|
||
$lineNumber = $name;
|
||
$lineNumber = str_replace(".pdf", "", $lineNumber);
|
||
dump($lineNumber);
|
||
?>
|
||
@include("cron.document_revisions-pdf-db-document-revisions-sync", [
|
||
"lineNumber" => $lineNumber,
|
||
])
|
||
<?php
|
||
}
|
||
|
||
if ($tableName == "incoming_control_paints") {
|
||
$lineNumber = $name;
|
||
$lineNumber = str_replace(".pdf", "", $lineNumber);
|
||
// dump($lineNumber);
|
||
?>
|
||
@include("cron.incoming_control_paints-pdf-db-incoming-control-sync", [
|
||
"lineNumber" => $lineNumber,
|
||
])
|
||
<?php
|
||
}
|
||
|
||
$logTables = array_values(log_test_types());
|
||
|
||
if (in_array($tableName, $logTables)) {
|
||
// tableName logTables içinde mevcut
|
||
// Burada gerekli işlemleri yapabilirsiniz
|
||
$logTable = $tableName;
|
||
$logName = get_key_by_value($tableName);
|
||
$fileName = $name;
|
||
$fileName = str_replace(".pdf", "", $fileName);
|
||
?>
|
||
@include("cron.pdf-db-ndt-sync", [
|
||
"logTable" => $logTable,
|
||
"logName" => $logName,
|
||
"fileName" => $fileName,
|
||
])
|
||
<?php
|
||
}
|
||
// incoming_controls için sync
|
||
if ($tableName == "incoming_controls") {
|
||
?>
|
||
@include("cron.pdf-db-incoming-control-sync", [
|
||
"fileName" => $onlyFileName,
|
||
])
|
||
<?php
|
||
}
|
||
if ($tableName == "naks_certificates") {
|
||
?>
|
||
@include("cron.pdf-db-naks-technology-sync", [
|
||
"fileName" => $onlyFileName,
|
||
])
|
||
<?php
|
||
}
|
||
|
||
if ($tableName == "naks_consumables") {
|
||
?>
|
||
@include("cron.pdf-db-naks-consumables-sync", [
|
||
"fileName" => $onlyFileName,
|
||
])
|
||
<?php
|
||
}
|
||
|
||
if ($tableName == "supports") {
|
||
?>
|
||
@include("cron.pdf-db-support-sync", [
|
||
"fileName" => $onlyFileName
|
||
])
|
||
<?php
|
||
}
|
||
|
||
if ($tableName == "i_t_p_s") {
|
||
?>
|
||
@include("cron.pdf-db-itp-sync", [
|
||
"fileName" => $onlyFileName
|
||
])
|
||
<?php
|
||
}
|
||
|
||
if ($tableName == "document_procedures") {
|
||
?>
|
||
@include("cron.pdf-db-document-procedure-sync", [
|
||
"fileName" => $onlyFileName
|
||
])
|
||
<?php
|
||
}
|
||
|
||
// Calibration Log - sync uploaded calibration certificates with calibration_logs table
|
||
if ($tableName == "calibration_logs") {
|
||
?>
|
||
@include("cron.pdf-db-calibration-logs-sync", [
|
||
"fileName" => $onlyFileName,
|
||
])
|
||
<?php
|
||
}
|
||
|
||
if ($tableName == "r_f_i_s") {
|
||
?>
|
||
@include("cron.pdf-db-rfi-sync", [
|
||
"folder" => $folder,
|
||
"fileName" => $name,
|
||
])
|
||
<?php
|
||
}
|
||
}
|
||
|
||
|
||
if(getesit("module","document-revision")) {
|
||
$revNoRegexPattern = "/(?i)(Rev|Rev.|Рев|Рев.)(.\d{2}.\d{2}|00.\d{2}|\d{2}_.\d{2}|\d{2})/";
|
||
$area = explode("-", $onlyFileName)[0];
|
||
preg_match($revNoRegexPattern, $onlyFileName, $revNoMatches);
|
||
dump($onlyFileName);
|
||
dump($revNoRegexPattern);
|
||
dump($revNoMatches);
|
||
|
||
$revisionNo = $revNoMatches[2] ?? null;
|
||
$allRevisionNo = $revNoMatches[0] ?? null;
|
||
|
||
if($allRevisionNo != "") {
|
||
$drawingNo = str_replace("-" . $allRevisionNo, "", $onlyFileName);
|
||
} else {
|
||
$drawingNo = $onlyFileName;
|
||
}
|
||
$drawingNo = str_replace($allRevisionNo, "", $drawingNo);
|
||
|
||
|
||
if($folder == "001_Drawings/001_Engineering") {
|
||
$documentRevisionData = [
|
||
'area' => $area,
|
||
'transmittal_document' => post("transmittal_document"),
|
||
'publish_date' => post("publish_date"),
|
||
'zone' => post("zone"),
|
||
'subcontructer' => post("subcontractor"),
|
||
'drawing_no' => $drawingNo,
|
||
'revision_no' => $revisionNo
|
||
];
|
||
|
||
dump($documentRevisionData);
|
||
DocumentRevision::firstOrCreate($documentRevisionData);
|
||
dump($documentRevisionData);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
echo "storage/documents/" . $fullPath;
|
||
|
||
|
||
$return = null;
|