Custom Backend OSPRO Surveyor Indonesia
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.

116 lines
3.9 KiB

10 months ago
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\{DokumenProject, Company};
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
10 months ago
class ProjectDokumenController extends Controller
{
public function dokumenByProyekId($id)
{
if (empty($id) || !is_int((int)$id)) {
return response()->json(['status' => 'failed', 'message' => 'project id is required!', 'code' => 400], 400);
10 months ago
}
$document = DokumenProject::where("ref_id", $id)->get();
if (count($document) == 0)
return response()->json(['status' => '', 'message' => 'Data not found!', 'code' => 404], 404);
10 months ago
return response()->json(['status' => 'success', 'data' => $document, 'code' => 200], 200);
10 months ago
}
public function delete($id)
10 months ago
{
if (empty($id) || !is_int((int)$id)) {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
10 months ago
}
$document = DokumenProject::find($id);
if (!$document) {
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 400], 400);
}
$destinationPath = $this->setCustomeDirectoryUpload();
if (file_exists($destinationPath['pathDocument'] . $document['file'])) {
unlink($destinationPath['pathDocument'] . $document['file']);
10 months ago
}
$document->delete();
return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200);
10 months ago
}
public function uploadProjectDokumen(Request $request)
{
DB::beginTransaction();
$timeNow = Carbon::now();
if ($request->hasFile('dokumen')) {
10 months ago
$document = $request->file('dokumen');
$ref_id = $request->ref_id;
$originalFilename = $document->getClientOriginalName();
$extension = pathinfo($originalFilename, PATHINFO_EXTENSION);
$filename = pathinfo($originalFilename, PATHINFO_FILENAME);
$name = $filename . '_' . $timeNow->format('d-m-y-His') . '.' . $extension;
$destinationPath = $this->setCustomeDirectoryUpload();
$data = [
'ref_id' => (int)$ref_id,
'file' => $name,
'type_dokumen' => isset($request->type_dokumen) ? $request->type_dokumen : 'project-document'
];
$result = DokumenProject::create($data);
if (!$document->move($destinationPath['pathDocument'], $name) && $result) {
unlink($destinationPath['pathDocument'] . $name);
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'Dokumen project gagal diupload!', 'code' => 500], 500);
10 months ago
}
DB::commit();
return response()->json(['status' => 'success', 'message' => 'Dokumen project berhasil diupload!', 'code' => 200], 200);
10 months ago
}
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'File is required!', 'code' => 400], 400);
10 months ago
}
public function searchDocProject(Request $request)
{
10 months ago
$payload = $request->all();
$dataBuilder = $this->setUpPayload($payload, 'document_project');
$builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count'];
$dataGet = $builder->get();
$totalRecord = $countBuilder->count();
return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200);
10 months ago
}
public function downloadDokumen($id)
10 months ago
{
if (empty($id) || !is_int((int)$id)) {
return response()->json(['status' => 'failed', 'message' => 'Id is required!', 'code' => 400], 400);
}
10 months ago
$document = DokumenProject::find($id);
if (!$document) {
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
}
$destinationPath = $this->setCustomeDirectoryUpload();
$pathToFile = $destinationPath['pathDocument'] . $document['file'];
if (!file_exists($pathToFile)) {
return response()->json(['status' => 'failed', 'message' => 'Directory not found!', 'code' => 404], 404);
}
$name = pathinfo($pathToFile, PATHINFO_FILENAME) . "." . pathinfo($pathToFile, PATHINFO_EXTENSION);
$headers = [
'Content-Disposition' => 'attachment; filename="' . $name . '"'
];
return response()->download($pathToFile, $name, $headers);
10 months ago
}
}