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.

293 lines
12 KiB

<?php
namespace App\Http\Controllers;
10 months ago
use App\Models\Image;
use App\Models\Company;
use Illuminate\Http\Request;
10 months ago
use App\Models\ProductTransaction;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class ImageController extends Controller
{
public function uploadImage(Request $request){
DB::beginTransaction();
if($request->hasFile('files')){
$this->validate($request, [
'ref_id' => 'required',
'category' => 'required'
]);
$ref_id = $request->ref_id;
$dokumen = $request->file('files');
$extension = $dokumen->extension();
$category = $request->category;
$name = $ref_id.date("YmdHis").".".$extension;
10 months ago
// Limited Storage
$company = Company::where('company_name', $request->company_name)->first();
if($company) {
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$getLimitStorage = $this->setLimitsStorage($company, $dokumen, $destinationPath['pathImage'], $destinationPath);
10 months ago
}
if(isset($getLimitStorage)) {
if($getLimitStorage === false) {
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'Limited storage maximum!', 'code' => 500], 500);
}
$data = [
'ref_id' => (int)$ref_id,
'image' => $name,
'category' => $category
];
$result = Image::create($data);
$company = Company::find((int)$ref_id);
if($company && $category === 'company_logo_login') {
$dataArray = json_decode($company['logo_login'], true);
if(isset($dataArray['slider'])) {
$jsonImageSlider['login'] = $name;
$jsonImageSlider['slider'] = $dataArray['slider'];
} else {
$jsonImageSlider['login'] = $name;
}
$company->update([
'logo_login' => $jsonImageSlider
]);
}
if($company && $category === 'company_logo_header') {
$company->update([
'logo_header' => array('content' => $name)
]);
}
if($company && $category === 'company_favicon') {
$company->update([
'favicon_image' => array('content' => $name)
]);
}
if($dokumen->move($destinationPath['pathImage'], $name) && $result) {
DB::commit();
return response()->json(['status' => 'success', "name_image" => $name,'message' => 'image upload is successfully!','code' => 200], 200);
} else {
DB::rollBack();
10 months ago
unlink($destinationPath['pathImage'].$name);
return response()->json(['status' => 'failed','message' => 'image upload is failed!' ,'code' => 400], 400);
}
} else {
DB::rollBack();
return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400);
}
} else {
DB::rollBack();
return response()->json(['status'=>'failed','message'=>'image is required!','code'=>400], 400);
}
}
public function uploadMultipleImage(Request $request) {
$this->validate($request, [
'ref_id' => 'required',
'category' => 'required',
'files.*' => 'required|file|mimes:jpeg,png,jpg,gif|max:2048',
]);
if ($request->hasFile('files')) {
$ref_id = $request->ref_id;
$category = $request->category;
$dokumen = $request->file('files');
$successCount = 0;
$imageSlider = [];
10 months ago
// Limited Storage
$company = Company::where('company_name', $request->company_name)->first();
if($company) {
$totalSize = 0;
$sizeFile = 0;
foreach ($dokumen as $file) {
$sizeFile += $file->getSize();
}
10 months ago
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
$folderPath = $destinationPath['pathImage'];
if (!file_exists($folderPath)) {
mkdir($folderPath, 0777, true);
}
$files = scandir($folderPath);
foreach ($files as $file) {
if (is_file($folderPath . '/' . $file)) {
$totalSize += filesize($folderPath . '/' . $file);
}
}
$totalSize += $sizeFile;
$transaction = ProductTransaction::query()
->where('company_id', $company['id']);
$cloneQueryTransaction = clone $transaction;
$countCreate = false;
if($transaction->where([['type_paket','Basic'], ['amount','!=',null]])->exists()) {
$maximumSize = 500 * 1024 * 1024;
$countCreate = true;
} elseif ($cloneQueryTransaction->where([['type_paket','Free'], ['amount', 0]])->exists()) {
$maximumSize = 50 * 1024 * 1024;
$countCreate = true;
}
if($countCreate) {
if ($totalSize > $maximumSize) {
return response()->json(['status'=>'failed','message'=>'Limited storage maximum!','code'=> 500], 500);
}
}
}
foreach ($dokumen as $file) {
$extension = $file->extension();
$name = $ref_id.date("YmdHis").$successCount.".".$extension;
10 months ago
$resultMove = $file->move($folderPath, $name);
if ($resultMove) {
$data = [
'ref_id' => (int) $ref_id,
'image' => $name,
'category' => $category
];
$imageSlider[] = [
'content' => $name
];
$result = Image::create($data);
if ($result) {
$successCount++;
} else {
10 months ago
unlink($folderPath . $name);
}
}
}
$company = Company::find((int)$ref_id);
if($company && $category === 'company_slider_login') {
$dataArray = json_decode($company['logo_login'], true);
if(isset($dataArray['login'])) {
$jsonImageSlider['login'] = $dataArray['login'];
$jsonImageSlider['slider'] = $imageSlider;
} else {
$jsonImageSlider['slider'] = $imageSlider;
}
$company->update([
'logo_login' => $jsonImageSlider
]);
}
if ($successCount > 0 && $company) {
return response()->json(['status' => 'success', 'message' => "$successCount images uploaded successfully!", 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'No images uploaded successfully!', 'code' => 400], 400);
}
} else {
return response()->json(['status' => 'failed', 'message' => 'Image is required!', 'code' => 400], 400);
}
}
public function delete($id)
{
$data = Image::find($id);
if($data){
unlink($this->pathImage.$data->image);
$delete = $data->delete();
}else{
return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400);
die();
}
if($delete){
return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400);
}
}
public function search(Request $request)
{
$payload = $request->all();
$dataBuilder = $this->setUpPayload($payload, 'm_image');
$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 deleteByRef($id, $category, $company_id)
{
$data = Image::where("ref_id", (int)$id)->where("category", $category)->first();
10 months ago
$company = Company::find($company_id);
if($data && $company){
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
unlink($destinationPath['pathImage'].$data->image);
$delete = $data->delete();
}else{
return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400);
die();
}
if($delete){
return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400);
}
}
10 months ago
public function deleteByRefMultiple($id, $category, $company_id)
{
$successCount = 0;
$data = Image::where([
["ref_id", intval($id)],
["category", $category]
])->get();
10 months ago
$company = Company::find($company_id);
if($data->isNotEmpty() && $company) {
$destinationPath = $this->setCustomeDirectoryUpload($company['company_name']);
foreach($data as $img) {
10 months ago
unlink($destinationPath['pathImage'].$img->image);
$delete = $img->delete();
if($delete){
$successCount++;
}
}
} else {
return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400);
die();
}
if($successCount > 0) {
return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200);
} else {
return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400);
}
}
public function getByRefId($id, $category)
{
if(!$id || (int) $id < 0 || $id==""){
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
die();
}
if(!$category || $category==""){
return response()->json(['status'=>'failed','message'=>'category is required!','code'=>400], 400);
die();
}
$result = Image::where("ref_id", $id)->where("category", $category)->first();
if($result){
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get data image, please try again later!','code'=>400], 400);
}
}
}