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.
177 lines
6.0 KiB
177 lines
6.0 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use Illuminate\Http\Request; |
|
use App\Models\Broadcast; |
|
use App\Models\User; |
|
use App\Services\FCMService; |
|
|
|
class BroadcastController extends Controller |
|
{ |
|
public function add(Request $request) |
|
{ |
|
|
|
$data = $request->all(); |
|
$data['created_by'] = $this->currentName; |
|
if (isset($data['send_to_id'])) { |
|
$idArray = $data['send_to_id']; |
|
if (is_array($data['send_to_id'])) { |
|
$data['send_to_id'] = implode(", ", $idArray); |
|
} |
|
} |
|
$result = Broadcast::create($data); |
|
if($result){ |
|
$data['status_send'] !== "registered" ? $this->sendNotification($data) : ''; |
|
return response()->json(['status'=>'success','message'=>'add broadcast successfully!','code'=>200], 200); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'add broadcast failed!','code'=>400], 400); |
|
} |
|
} |
|
|
|
public function edit($id){ |
|
if(!$id || (int) $id < 0 || $id==""){ |
|
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
|
die(); |
|
} |
|
|
|
$result = Broadcast::find($id); |
|
|
|
if($result){ |
|
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'failed get data broadcast, please try again later!','code'=>400], 400); |
|
} |
|
} |
|
|
|
public function update(Request $request, $id) |
|
{ |
|
if(!$id || (int) $id < 0 || $id==""){ |
|
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
|
} |
|
|
|
$data = Broadcast::find($id); |
|
|
|
$status_send = $request->status_send; |
|
|
|
if($status_send === 'resend' || $status_send === 'send' && !empty($data)) |
|
{ |
|
$data['status_send'] = $status_send; |
|
$this->sendNotification($data); |
|
} |
|
|
|
if($data){ |
|
$result = $data->update($request->all()); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); |
|
die(); |
|
} |
|
|
|
|
|
if($result){ |
|
return response()->json(['status'=>'success','message'=>'data broadcast successfully updated!','code'=>200], 200); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'data broadcast failed updated!','code'=>400], 400); |
|
} |
|
} |
|
|
|
public function delete($id) |
|
{ |
|
$data = Broadcast::find($id); |
|
|
|
if($data){ |
|
$delete = $data->delete(); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); |
|
die(); |
|
} |
|
|
|
|
|
if($delete){ |
|
return response()->json(['status'=>'success','message'=>'data broadcast successfully deleted!','code'=>200], 200); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'data broadcast failed deleted!','code'=>400], 400); |
|
} |
|
} |
|
|
|
public function search(Request $request) |
|
{ |
|
$payload = $request->all(); |
|
$dataBuilder = $this->setUpPayload($payload, 'm_broadcast'); |
|
$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 list() |
|
{ |
|
$data = Broadcast::all(); |
|
$countData = $data->count(); |
|
|
|
if($data){ |
|
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); |
|
}else{ |
|
return response()->json(['status'=>'failed','message'=>'failed get list broadcast, please try again later!','code'=>400], 400); |
|
} |
|
} |
|
|
|
public function sendNotification($data) |
|
{ |
|
// send_to_type (all, roles, user) |
|
if (isset($data['send_to_type'])) { |
|
switch ($data['send_to_type']) { |
|
case 'all': |
|
$users = User::whereNotNull('fcm_token')->get(); |
|
if (isset($users)) { |
|
foreach ($users as $user) { |
|
FCMService::send( |
|
$user->fcm_token, |
|
[ |
|
'title' => $data['title_notif'], |
|
'body' => $data['message_notif'], |
|
] |
|
); |
|
} |
|
} |
|
break; |
|
|
|
case 'roles': |
|
$users = User::where("role_id", $data['send_to_id'])->whereNotNull('fcm_token')->get(); |
|
if (isset($users)) { |
|
foreach ($users as $user) { |
|
FCMService::send( |
|
$user->fcm_token, |
|
[ |
|
'title' => $data['title_notif'], |
|
'body' => $data['message_notif'], |
|
] |
|
); |
|
} |
|
} |
|
break; |
|
|
|
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)) { |
|
FCMService::send( |
|
$user->fcm_token, |
|
[ |
|
'title' => $data['title_notif'], |
|
'body' => $data['message_notif'], |
|
] |
|
); |
|
} |
|
} |
|
break; |
|
|
|
default: |
|
# code... |
|
break; |
|
} |
|
} |
|
} |
|
}
|
|
|