Browse Source

Merge pull request 'image & slider company' (#37) from dev-wahyun into staging

Reviewed-on: ibnu/generic-ospro-backend#37
pull/1/head
farhantock 11 months ago
parent
commit
7b4ddeb636
  1. 358
      app/Http/Controllers/ImageController.php
  2. 2
      routes/web.php

358
app/Http/Controllers/ImageController.php

@ -1,121 +1,237 @@
<?php <?php
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Models\Company;
use App\Models\Image; use Illuminate\Http\Request;
use App\Models\Image;
class ImageController extends Controller
{ class ImageController extends Controller
public function uploadImage(Request $request){ {
public function uploadImage(Request $request){
if($request->hasFile('files')){
if($request->hasFile('files')){
$this->validate($request, [
'ref_id' => 'required', $this->validate($request, [
'category' => 'required' 'ref_id' => 'required',
]); 'category' => 'required'
]);
$ref_id = $request->ref_id;
$dokumen = $request->file('files'); $ref_id = $request->ref_id;
$extension = $dokumen->extension(); $dokumen = $request->file('files');
$name = $ref_id.date("YmdHis").".".$extension; $extension = $dokumen->extension();
$category = $request->category;
$resultMove = $dokumen->move($this->pathImage, $name); $name = $ref_id.date("YmdHis").".".$extension;
if($resultMove){ $resultMove = $dokumen->move($this->pathImage, $name);
$data = [
'ref_id' => (int)$ref_id, if($resultMove){
'image' => $name, $data = [
'category' => $request->category 'ref_id' => (int)$ref_id,
]; 'image' => $name,
'category' => $category
$result = Image::create($data); ];
if($result){ $result = Image::create($data);
return response()->json(['status'=>'success', "name_image"=>$name,'message'=>'image upload is successfully!','code'=>200], 200);
}else{ $company = Company::find((int)$ref_id);
unlink($this->pathImage.$name); if($company && $category === 'company_logo_login') {
return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); $dataArray = json_decode($company['logo_login'], true);
} if(isset($dataArray['slider'])) {
$jsonImageSlider['login'] = $name;
}else{ $jsonImageSlider['slider'] = $dataArray['slider'];
return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); } else {
} $jsonImageSlider['login'] = $name;
}else{ }
return response()->json(['status'=>'failed','message'=>'image is required!','code'=>400], 400); $company->update([
} 'logo_login' => $jsonImageSlider
} ]);
}
public function delete($id)
{ if($company && $category === 'company_logo_header') {
$data = Image::find($id); $company->update([
'logo_header' => array('content'=>$name)
if($data){ ]);
unlink($this->pathImage.$data->image); }
$delete = $data->delete();
}else{ if($company && $category === 'company_favicon') {
return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400); $company->update([
die(); 'favicon_image' => array('content'=>$name)
} ]);
}
if($delete){ if($result){
return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200); return response()->json(['status'=>'success', "name_image"=>$name,'message'=>'image upload is successfully!','code'=>200], 200);
}else{ }else{
return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400); unlink($this->pathImage.$name);
} return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400);
} }
public function search(Request $request) }else{
{ return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400);
$payload = $request->all(); }
$dataBuilder = $this->setUpPayload($payload, 'm_image'); }else{
$builder = $dataBuilder['builder']; return response()->json(['status'=>'failed','message'=>'image is required!','code'=>400], 400);
$countBuilder = $dataBuilder['count']; }
$dataGet = $builder->get(); }
$totalRecord = $countBuilder->count();
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); public function uploadMultipleImage(Request $request) {
} $this->validate($request, [
'ref_id' => 'required',
public function deleteByRef($id, $category) 'category' => 'required',
{ 'files.*' => 'required|file|mimes:jpeg,png,jpg,gif|max:2048',
$data = Image::where("ref_id", $id)->where("category", $category)->first(); ]);
if($data){ if ($request->hasFile('files')) {
unlink($this->pathImage.$data->image); $ref_id = $request->ref_id;
$delete = $data->delete(); $category = $request->category;
}else{ $dokumen = $request->file('files');
return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400); $successCount = 0;
die(); $imageSlider = [];
} foreach ($dokumen as $file) {
$extension = $file->extension();
$name = $ref_id.date("YmdHis").$successCount.".".$extension;
if($delete){
return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200); $resultMove = $file->move($this->pathImage, $name);
}else{
return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400); if ($resultMove) {
} $data = [
} 'ref_id' => (int) $ref_id,
'image' => $name,
public function getByRefId($id, $category) 'category' => $category
{ ];
if(!$id || (int) $id < 0 || $id==""){ $imageSlider[] = [
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); 'content' => $name
die(); ];
} $result = Image::create($data);
if ($result) {
if(!$category || $category==""){ $successCount++;
return response()->json(['status'=>'failed','message'=>'category is required!','code'=>400], 400); } else {
die(); unlink($this->pathImage . $name);
} }
}
$result = Image::where("ref_id", $id)->where("category", $category)->first(); }
$company = Company::find((int)$ref_id);
if($result){ if($company && $category === 'company_slider_login') {
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); $dataArray = json_decode($company['logo_login'], true);
}else{ if(isset($dataArray['login'])) {
return response()->json(['status'=>'failed','message'=>'failed get data image, please try again later!','code'=>400], 400); $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);
}
public function deleteByRef($id, $category)
{
$data = Image::where("ref_id", intval($id))->where("category", $category)->first();
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 deleteByRefMultiple($id, $category)
{
$successCount = 0;
$data = Image::where([
["ref_id", intval($id)],
["category", $category]
])->get();
if($data->isNotEmpty()) {
foreach($data as $img) {
unlink($this->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);
}
}
}

2
routes/web.php

@ -314,7 +314,9 @@ $router->group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($ro
$router->post('/image/search', 'ImageController@search'); $router->post('/image/search', 'ImageController@search');
$router->delete('/image/delete/{id}', 'ImageController@delete'); $router->delete('/image/delete/{id}', 'ImageController@delete');
$router->delete('/image/delete/{id}/{category}', 'ImageController@deleteByRef'); $router->delete('/image/delete/{id}/{category}', 'ImageController@deleteByRef');
$router->delete('/image/multiple-delete/{id}/{category}', 'ImageController@deleteByRefMultiple');
$router->post('/image/upload', 'ImageController@uploadImage'); $router->post('/image/upload', 'ImageController@uploadImage');
$router->post('/image/multiple-upload', 'ImageController@uploadMultipleImage');
$router->get('/image/{id}/{category}', 'ImageController@getByRefId'); $router->get('/image/{id}/{category}', 'ImageController@getByRefId');
$router->post('/panic-button/add', 'PanicButtonController@add'); $router->post('/panic-button/add', 'PanicButtonController@add');

Loading…
Cancel
Save