You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.3 KiB
141 lines
4.3 KiB
<?php |
|
|
|
namespace App\Http\Controllers\system; |
|
|
|
use App\Http\Controllers\Controller; |
|
use App\Models\ExportFiles; |
|
use App\Models\User; |
|
use Carbon\Carbon; |
|
use Illuminate\Http\Request; |
|
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Exp; |
|
|
|
class FileIndexController extends Controller |
|
{ |
|
/** |
|
* Display a listing of the resource. |
|
*/ |
|
public function index() |
|
{ |
|
return view('filesystem.index'); |
|
} |
|
|
|
//api_index |
|
public function api_index(Request $request) |
|
{ |
|
// 從資料庫取得檔案索引表 ExportFiles |
|
$user_id = auth('api')->user()->id; |
|
$files = ExportFiles::where('user_id', $user_id)->get(); |
|
|
|
[$draw, $totalRecords, $totalRecordswithFilter, $data_arr] = $this->getData($request); |
|
|
|
$response = array( |
|
"draw" => intval($draw), |
|
"iTotalRecords" => $totalRecords, |
|
"iTotalDisplayRecords" => $totalRecordswithFilter, |
|
"aaData" => $data_arr |
|
); |
|
|
|
|
|
// dd($response); |
|
echo json_encode($response); |
|
exit; |
|
|
|
} |
|
// get data |
|
#region 取得資料 |
|
function getData($request) |
|
{ |
|
$user_id = auth('api')->user()->id; |
|
|
|
#region Request 搜尋值 |
|
if (isset($request->searchByFromdate)) |
|
$searchByFromdate = $request->searchByFromdate; |
|
if (isset($request->searchByTodate)) |
|
$searchByTodate = $request->searchByTodate; |
|
#endregion |
|
|
|
|
|
|
|
#region DataTable 搜尋屬性 |
|
if (!isset($request->export)) { |
|
$draw = $request->get('draw'); |
|
$start = $request->get("start"); |
|
$rowperpage = $request->get("length"); // Rows display per page |
|
|
|
$columnIndex_arr = $request->get('order'); |
|
$columnName_arr = $request->get('columns'); |
|
$order_arr = $request->get('order'); |
|
$search_arr = $request->get('search'); |
|
|
|
$columnIndex = $columnIndex_arr[0]['column']; // Column index |
|
$columnName = $columnName_arr[$columnIndex]['data']; // Column name |
|
$columnSortOrder = $order_arr[0]['dir']; // asc or desc |
|
$searchValue = $search_arr['value']; // Search value |
|
} else { |
|
$draw = 0; |
|
} |
|
#endregion |
|
|
|
// Fetch records |
|
$records = ExportFiles::query(); |
|
$records->where('user_id', $user_id); |
|
|
|
$totalRecords = $records->count(); |
|
|
|
if (isset($columnName)) |
|
$records->orderBy($columnName, $columnSortOrder); |
|
if (isset($searchValue)) { |
|
$records->where(function ($query) use ($searchValue) { |
|
$query->where('remark', 'like', '%' . $searchValue . '%'); |
|
}); |
|
} |
|
|
|
//時間限制 |
|
if (isset($searchByFromdate)) |
|
$records->where('created_at', ">", $searchByFromdate . ' 00:00:00'); |
|
if (isset($searchByTodate)) |
|
$records->where('created_at', "<", $searchByTodate . ' 23:59:59'); |
|
|
|
|
|
$totalRecordswithFilter = $records->count(); |
|
|
|
#region DataTable 分頁(報表不分頁) |
|
if (!isset($request->export)) { |
|
if (isset($start)) |
|
$records->skip($start); |
|
if (isset($rowperpage)) |
|
$records->take($rowperpage); |
|
} |
|
#endregion |
|
|
|
$records = $records->get(); |
|
|
|
#region 資料處理 |
|
$data_arr = array(); |
|
$sno = 1 + ($request->get('start') ?? 0); |
|
foreach ($records as $record) { |
|
$data_arr[] = array( |
|
"id" => $sno++, // $record->id, |
|
"display_name" => $record->remark, |
|
"user" => User::find($record->user_id)->name ?? '', |
|
"created_at" => Carbon::parse($record->created_at)->format('Y-m-d H:i:s'), |
|
"name" => "$record->name", |
|
); |
|
} |
|
#endregion |
|
return [$draw, $totalRecords, $totalRecordswithFilter, $data_arr]; |
|
} |
|
#endregion |
|
|
|
// 下載檔案 透過檔案名稱 |
|
public function downloadFile(Request $request, $path) |
|
{ |
|
$file = ExportFiles::where('name', $path)->first(); |
|
$path = storage_path('app/' . $file->path . '/' . $file->name); |
|
// 檔案名稱 |
|
$name = $file->remark; |
|
// 副檔名 |
|
$ext = pathinfo($path, PATHINFO_EXTENSION); |
|
return response()->download($path, $name . '.' . $ext); |
|
} |
|
}
|
|
|