where('api_token',$request->api_token)->first();
if(empty($staff)){
return $this->sendError('Gagal', 'Kakitangan tidak wujud');
}else {
$dataLatLong = [
'latitude' => $request->latitude,
'longitude' => $request->longitude,
'status' => 'normal',
];
$staff->tracklocation()->create($dataLatLong);
if(!empty($staff->CurrentLocation)){
$staff->CurrentLocation->latitude = $request->latitude;
$staff->CurrentLocation->longitude = $request->longitude;
$staff->CurrentLocation->save();
}else{
$staff->currentlocation()->create($dataLatLong);
}
return $this->sendResponse('Berjaya', 'Koordinat berjaya direkod');
}
}
public function getStaffList(){
$data = array();
$location = CurrentLocation::all();
foreach ($location as $key => $l) {
$staff = Staff::with('StaffDetail')->where('_id',$l->staff_id)->first();
if(!empty($staff)){
array_push($data, array(
'id' => $l->staff_id,
'nama' => $staff->StaffDetail->full_name,
'no_badan' => $staff->StaffDetail->no_badan,
));
}
}
return $this->sendResponse($data, 'Berjaya dapatkan senarai penguatkuasa');
}
public function getNearestLocation($id){
$data = array();
$latitude = 1.5371973; $longitude = 103.6602123;
$location = CurrentLocation::all();
foreach ($location as $key => $l) {
$last_update = $l->updated_at;
$now = now();
$diff = $now->diffInMinutes($last_update);
$status = '';
if($diff <= 5){
$status = 'Di Atas Talian';
}else{
$status = 'Di Luar Talian';
}
$staff = Staff::with('StaffDetail')->where('_id',$l->staff_id)->first();
if($l->staff_id == ''){
array_push($data, array(
'_id' => $l->_id,
'latitude' => '1.5371973',
'longitude' => '103.6602123',
'title' => 'Majlis Daerah Cameron Highlands',
'content' => 'Ibu Pejabat',
'icon' => url('/uploads/map_marker/mdch_map.png'),
'status' => 'main',
'zindex' => $key+1
));
}else{
if(!empty($staff)){
array_push($data, array(
'_id' => $staff->_id,
'latitude' => $l->latitude,
'longitude' => $l->longitude,
'title' => $staff->StaffDetail->full_name,
'content' => $status,
'icon' => url('/uploads/map_marker/officer.png'),
'status' => $l->status,
'zindex' => $key+1
));
}
}
}
return $this->sendResponse($data, 'Berjaya dapatkan senarai Koordinat');
}
/**
* Create Emergency controller.
*
* @return json
*/
public function postEmergency(Request $request){
$staff = Staff::with('StaffDetail','TrackLocation','CurrentLocation')->where('api_token', $request->api_token)->first();
if(!empty($staff)){
$dataLatLong = [
'latitude' => $request->latitude,
'longitude' => $request->longitude,
'status' => 'emergency',
];
$staff->tracklocation()->create($dataLatLong);
$staff->CurrentLocation->latitude = $request->latitude;
$staff->CurrentLocation->longitude = $request->longitude;
$staff->CurrentLocation->status = 'emergency';
$staff->CurrentLocation->save();
return $this->sendResponse($staff->CurrentLocation, 'Berjaya hantar kecemasan');
}else{
return $this->sendError('', 'Tidak dapat hantar kecemasan');
}
}
public function returnNormal(Request $request){
$current = CurrentLocation::where('status','emergency')->get();
if(count($current) > 0){
foreach($current as $c){
$c->status = 'normal';
$c->save();
}
return $this->sendResponse('', 'Berjaya kembali kepada asal');
}else{
return $this->sendError('', 'Tidak dapat kembali keadaan asal');
}
}
}