120 lines
4.3 KiB
PHP
120 lines
4.3 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Api;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\DocumentHistory;
|
||
use Illuminate\Http\Request;
|
||
|
||
/**
|
||
* @group Document Management
|
||
*
|
||
* APIs for managing documents and their history.
|
||
*/
|
||
class DocumentHistoryController extends Controller
|
||
{
|
||
/**
|
||
* Get Document History
|
||
*
|
||
* Retrieve the history of a document based on file name or table reference.
|
||
*
|
||
* @bodyParam table string The name of the table (e.g., document_revisions). Example: document_revisions
|
||
* @bodyParam column string The name of the column (e.g., pdf_spool_iso). Example: pdf_spool_iso
|
||
* @bodyParam row_id integer The ID of the row. Example: 3021
|
||
* @bodyParam file_name string The name of the file to search for. Example: MST-80-AH01PR-FE31.803.pdf
|
||
*
|
||
* @response {
|
||
* "id": 345,
|
||
* "user_name": "Linga Samiii",
|
||
* "action": "Archive",
|
||
* "file_name": "MST-80-AH01PR-FE31.803.pdf",
|
||
* "file_size": "474.4 KB",
|
||
* "moved_from": "001_Drawings",
|
||
* "moved_to": "Archive/001_Drawings",
|
||
* "action_date": "2025-07-19 16:11:00",
|
||
* "file_path": "storage/documents/...",
|
||
* "notes": "File replaced with new version..."
|
||
* }
|
||
*/
|
||
public function index(Request $request)
|
||
{
|
||
$table = $request->input('table');
|
||
$column = $request->input('column');
|
||
$row_id = $request->input('row_id');
|
||
$file_name = $request->input('file_name');
|
||
|
||
$query = DocumentHistory::query();
|
||
|
||
|
||
|
||
if ($file_name) {
|
||
// File name'e göre arama
|
||
$base_name = str_replace(".pdf", "", $file_name);
|
||
// Sadece tam dosya adlarını veya '_' ile ayrılmış arşiv adlarını (örn: _2025_08_30...) eşleştir
|
||
$query->where(function ($q) use ($base_name, $file_name) {
|
||
$q->where('file_name', $file_name)
|
||
->orWhere('file_name', 'LIKE', $base_name . '_%.pdf');
|
||
});
|
||
} elseif ($table && $row_id) {
|
||
// Table ve row_id'ye göre dosyayı bulup arama yap
|
||
try {
|
||
$row = \Illuminate\Support\Facades\DB::table($table)->find($row_id);
|
||
|
||
if ($row) {
|
||
$filenames = [];
|
||
|
||
if ($column && isset($row->$column)) {
|
||
// Belirli bir kolon varsa
|
||
$value = $row->$column;
|
||
if ($value && is_string($value)) {
|
||
$filenames[] = basename($value);
|
||
}
|
||
} else {
|
||
// Kolon belirtilmemişse, olası dosya kolonlarını tara
|
||
foreach ((array) $row as $key => $value) {
|
||
if ($value && is_string($value) && preg_match('/\.(pdf|xls|xlsx|doc|docx|jpg|png|jpeg)$/i', $value)) {
|
||
$filenames[] = basename($value);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!empty($filenames)) {
|
||
$query->whereIn('file_name', $filenames);
|
||
} else {
|
||
// Dosya bulunamadıysa boş dön
|
||
return response()->json([]);
|
||
}
|
||
} else {
|
||
return response()->json([]);
|
||
}
|
||
} catch (\Exception $e) {
|
||
// Tablo hatası vs.
|
||
return response()->json([]);
|
||
}
|
||
} else {
|
||
// Hiçbir geçerli filtre yoksa boş döndür
|
||
return response()->json([]);
|
||
}
|
||
|
||
|
||
$documentHistory = $query->orderBy('action_date', 'DESC')
|
||
->get()
|
||
->map(function ($item) {
|
||
return [
|
||
'id' => $item->id,
|
||
'user_name' => $item->user_name,
|
||
'action' => $item->action,
|
||
'file_name' => $item->file_name,
|
||
'file_size' => $item->file_size,
|
||
'moved_from' => $item->moved_from,
|
||
'moved_to' => $item->moved_to,
|
||
'action_date' => $item->action_date,
|
||
'file_path' => $item->file_path,
|
||
'notes' => $item->notes
|
||
];
|
||
});
|
||
|
||
return response()->json($documentHistory);
|
||
}
|
||
}
|