123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
-
- namespace App\Http\Controllers\api;
-
- use Illuminate\Http\Request;
- use App\Http\Controllers\Api\BaseController;
-
- use Config;
- use File;
- use Carbon\Carbon;
-
- use App\Model\Staff;
- use App\Model\StaffDetail;
- use App\Model\Module\TrackLocation;
- use App\Model\Module\CurrentLocation;
- use App\Model\Module\EmergencyMarker;
-
- class TrackMapController extends BaseController
- {
-
- /**
- * Create Location controller.
- *
- * @return json
- */
- public function save_updateLocation(Request $request)
- {
- $staff = Staff::with('StaffDetail','TrackLocation','CurrentLocation')->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 = '<span style="color:green"><b>Di Atas Talian</b></span>';
- }else{
- $status = '<span style="color:red"><b>Di Luar Talian</b></span>';
- }
-
- $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' => '<b>Ibu Pejabat</b>',
- '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');
- }
- }
- }
|