wahyuun
1 year ago
1 changed files with 177 additions and 171 deletions
@ -1,171 +1,177 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
namespace App\Http\Controllers; |
namespace App\Http\Controllers; |
||||||
|
|
||||||
use Illuminate\Http\Request; |
use Illuminate\Http\Request; |
||||||
use App\Models\Broadcast; |
use App\Models\Broadcast; |
||||||
use App\Models\User; |
use App\Models\User; |
||||||
use App\Services\FCMService; |
use App\Services\FCMService; |
||||||
|
|
||||||
class BroadcastController extends Controller |
class BroadcastController extends Controller |
||||||
{ |
{ |
||||||
public function add(Request $request) |
public function add(Request $request) |
||||||
{ |
{ |
||||||
|
|
||||||
$data = $request->all(); |
$data = $request->all(); |
||||||
$data['status_send'] = true; |
$data['created_by'] = $this->currentName; |
||||||
$data['created_by'] = $this->currentName; |
if (isset($data['send_to_id'])) { |
||||||
if (isset($data['send_to_id'])) { |
$idArray = $data['send_to_id']; |
||||||
$idArray = $data['send_to_id']; |
if (is_array($data['send_to_id'])) { |
||||||
if (is_array($data['send_to_id'])) { |
$data['send_to_id'] = implode(", ", $idArray); |
||||||
$data['send_to_id'] = implode(", ", $idArray); |
} |
||||||
} |
} |
||||||
} |
$result = Broadcast::create($data); |
||||||
// dd($data); |
if($result){ |
||||||
$result = Broadcast::create($data); |
$data['status_send'] !== "registered" ? $this->sendNotification($data) : ''; |
||||||
if($result){ |
return response()->json(['status'=>'success','message'=>'add broadcast successfully!','code'=>200], 200); |
||||||
$this->sendNotification($data); |
}else{ |
||||||
return response()->json(['status'=>'success','message'=>'add broadcast successfully!','code'=>200], 200); |
return response()->json(['status'=>'failed','message'=>'add broadcast failed!','code'=>400], 400); |
||||||
}else{ |
} |
||||||
return response()->json(['status'=>'failed','message'=>'add broadcast failed!','code'=>400], 400); |
} |
||||||
} |
|
||||||
} |
public function edit($id){ |
||||||
|
if(!$id || (int) $id < 0 || $id==""){ |
||||||
public function edit($id){ |
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
||||||
if(!$id || (int) $id < 0 || $id==""){ |
die(); |
||||||
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
} |
||||||
die(); |
|
||||||
} |
$result = Broadcast::find($id); |
||||||
|
|
||||||
$result = Broadcast::find($id); |
if($result){ |
||||||
|
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); |
||||||
if($result){ |
}else{ |
||||||
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); |
return response()->json(['status'=>'failed','message'=>'failed get data broadcast, please try again later!','code'=>400], 400); |
||||||
}else{ |
} |
||||||
return response()->json(['status'=>'failed','message'=>'failed get data broadcast, please try again later!','code'=>400], 400); |
} |
||||||
} |
|
||||||
} |
public function update(Request $request, $id) |
||||||
|
{ |
||||||
public function update(Request $request, $id) |
if(!$id || (int) $id < 0 || $id==""){ |
||||||
{ |
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
||||||
if(!$id || (int) $id < 0 || $id==""){ |
} |
||||||
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
|
||||||
} |
$data = Broadcast::find($id); |
||||||
|
|
||||||
$data = Broadcast::find($id); |
$status_send = $request->status_send; |
||||||
|
|
||||||
if($data){ |
if($status_send === 'resend' || $status_send === 'send' && !empty($data)) |
||||||
$result = $data->update($request->all()); |
{ |
||||||
}else{ |
$data['status_send'] = $status_send; |
||||||
return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); |
$this->sendNotification($data); |
||||||
die(); |
} |
||||||
} |
|
||||||
|
if($data){ |
||||||
|
$result = $data->update($request->all()); |
||||||
if($result){ |
}else{ |
||||||
return response()->json(['status'=>'success','message'=>'data broadcast successfully updated!','code'=>200], 200); |
return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); |
||||||
}else{ |
die(); |
||||||
return response()->json(['status'=>'failed','message'=>'data broadcast failed updated!','code'=>400], 400); |
} |
||||||
} |
|
||||||
} |
|
||||||
|
if($result){ |
||||||
public function delete($id) |
return response()->json(['status'=>'success','message'=>'data broadcast successfully updated!','code'=>200], 200); |
||||||
{ |
}else{ |
||||||
$data = Broadcast::find($id); |
return response()->json(['status'=>'failed','message'=>'data broadcast failed updated!','code'=>400], 400); |
||||||
|
} |
||||||
if($data){ |
} |
||||||
$delete = $data->delete(); |
|
||||||
}else{ |
public function delete($id) |
||||||
return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); |
{ |
||||||
die(); |
$data = Broadcast::find($id); |
||||||
} |
|
||||||
|
if($data){ |
||||||
|
$delete = $data->delete(); |
||||||
if($delete){ |
}else{ |
||||||
return response()->json(['status'=>'success','message'=>'data broadcast successfully deleted!','code'=>200], 200); |
return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); |
||||||
}else{ |
die(); |
||||||
return response()->json(['status'=>'failed','message'=>'data broadcast failed deleted!','code'=>400], 400); |
} |
||||||
} |
|
||||||
} |
|
||||||
|
if($delete){ |
||||||
public function search(Request $request) |
return response()->json(['status'=>'success','message'=>'data broadcast successfully deleted!','code'=>200], 200); |
||||||
{ |
}else{ |
||||||
$payload = $request->all(); |
return response()->json(['status'=>'failed','message'=>'data broadcast failed deleted!','code'=>400], 400); |
||||||
$dataBuilder = $this->setUpPayload($payload, 'm_broadcast'); |
} |
||||||
$builder = $dataBuilder['builder']; |
} |
||||||
$countBuilder = $dataBuilder['count']; |
|
||||||
$dataGet = $builder->get(); |
public function search(Request $request) |
||||||
$totalRecord = $countBuilder->count(); |
{ |
||||||
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); |
$payload = $request->all(); |
||||||
} |
$dataBuilder = $this->setUpPayload($payload, 'm_broadcast'); |
||||||
|
$builder = $dataBuilder['builder']; |
||||||
public function list() |
$countBuilder = $dataBuilder['count']; |
||||||
{ |
$dataGet = $builder->get(); |
||||||
$data = Broadcast::all(); |
$totalRecord = $countBuilder->count(); |
||||||
$countData = $data->count(); |
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); |
||||||
|
} |
||||||
if($data){ |
|
||||||
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); |
public function list() |
||||||
}else{ |
{ |
||||||
return response()->json(['status'=>'failed','message'=>'failed get list broadcast, please try again later!','code'=>400], 400); |
$data = Broadcast::all(); |
||||||
} |
$countData = $data->count(); |
||||||
} |
|
||||||
|
if($data){ |
||||||
public function sendNotification($data) |
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); |
||||||
{ |
}else{ |
||||||
// send_to_type (all, roles, user) |
return response()->json(['status'=>'failed','message'=>'failed get list broadcast, please try again later!','code'=>400], 400); |
||||||
if (isset($data['send_to_type'])) { |
} |
||||||
switch ($data['send_to_type']) { |
} |
||||||
case 'all': |
|
||||||
$users = User::whereNotNull('fcm_token')->get(); |
public function sendNotification($data) |
||||||
if (isset($users)) { |
{ |
||||||
foreach ($users as $user) { |
// send_to_type (all, roles, user) |
||||||
FCMService::send( |
if (isset($data['send_to_type'])) { |
||||||
$user->fcm_token, |
switch ($data['send_to_type']) { |
||||||
[ |
case 'all': |
||||||
'title' => $data['title_notif'], |
$users = User::whereNotNull('fcm_token')->get(); |
||||||
'body' => $data['message_notif'], |
if (isset($users)) { |
||||||
] |
foreach ($users as $user) { |
||||||
); |
FCMService::send( |
||||||
} |
$user->fcm_token, |
||||||
} |
[ |
||||||
break; |
'title' => $data['title_notif'], |
||||||
|
'body' => $data['message_notif'], |
||||||
case 'roles': |
] |
||||||
$users = User::where("role_id", $data['send_to_id'])->whereNotNull('fcm_token')->get(); |
); |
||||||
if (isset($users)) { |
} |
||||||
foreach ($users as $user) { |
} |
||||||
FCMService::send( |
break; |
||||||
$user->fcm_token, |
|
||||||
[ |
case 'roles': |
||||||
'title' => $data['title_notif'], |
$users = User::where("role_id", $data['send_to_id'])->whereNotNull('fcm_token')->get(); |
||||||
'body' => $data['message_notif'], |
if (isset($users)) { |
||||||
] |
foreach ($users as $user) { |
||||||
); |
FCMService::send( |
||||||
} |
$user->fcm_token, |
||||||
} |
[ |
||||||
break; |
'title' => $data['title_notif'], |
||||||
|
'body' => $data['message_notif'], |
||||||
case 'users': |
] |
||||||
$idArray = explode(", ", $data['send_to_id']); |
); |
||||||
foreach ($idArray as $key => $value) { |
} |
||||||
$user = User::where("id", $value)->whereNotNull('fcm_token')->first(); |
} |
||||||
if (isset($user)) { |
break; |
||||||
FCMService::send( |
|
||||||
$user->fcm_token, |
case 'users': |
||||||
[ |
$idArray = explode(", ", $data['send_to_id']); |
||||||
'title' => $data['title_notif'], |
foreach ($idArray as $key => $value) { |
||||||
'body' => $data['message_notif'], |
$user = User::where("id", $value)->whereNotNull('fcm_token')->first(); |
||||||
] |
if (isset($user)) { |
||||||
); |
FCMService::send( |
||||||
} |
$user->fcm_token, |
||||||
} |
[ |
||||||
break; |
'title' => $data['title_notif'], |
||||||
|
'body' => $data['message_notif'], |
||||||
default: |
] |
||||||
# code... |
); |
||||||
break; |
} |
||||||
} |
} |
||||||
} |
break; |
||||||
} |
|
||||||
} |
default: |
||||||
|
# code... |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
Loading…
Reference in new issue