Dashboard sipadu mbip
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TrackMapController.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Api\BaseController;
  5. use Config;
  6. use File;
  7. use Carbon\Carbon;
  8. use App\Model\Staff;
  9. use App\Model\StaffDetail;
  10. use App\Model\Module\TrackLocation;
  11. use App\Model\Module\CurrentLocation;
  12. use App\Model\Module\EmergencyMarker;
  13. class TrackMapController extends BaseController
  14. {
  15. /**
  16. * Create Location controller.
  17. *
  18. * @return json
  19. */
  20. public function save_updateLocation(Request $request)
  21. {
  22. $staff = Staff::with('StaffDetail','TrackLocation','CurrentLocation')->where('api_token',$request->api_token)->first();
  23. if(empty($staff)){
  24. return $this->sendError('Gagal', 'Kakitangan tidak wujud');
  25. }else {
  26. $dataLatLong = [
  27. 'latitude' => $request->latitude,
  28. 'longitude' => $request->longitude,
  29. 'status' => 'normal',
  30. ];
  31. $staff->tracklocation()->create($dataLatLong);
  32. if(!empty($staff->CurrentLocation)){
  33. $staff->CurrentLocation->latitude = $request->latitude;
  34. $staff->CurrentLocation->longitude = $request->longitude;
  35. $staff->CurrentLocation->save();
  36. }else{
  37. $staff->currentlocation()->create($dataLatLong);
  38. }
  39. return $this->sendResponse('Berjaya', 'Koordinat berjaya direkod');
  40. }
  41. }
  42. public function getStaffList(){
  43. $data = array();
  44. $location = CurrentLocation::all();
  45. foreach ($location as $key => $l) {
  46. $staff = Staff::with('StaffDetail')->where('_id',$l->staff_id)->first();
  47. if(!empty($staff)){
  48. array_push($data, array(
  49. 'id' => $l->staff_id,
  50. 'nama' => $staff->StaffDetail->full_name,
  51. 'no_badan' => $staff->StaffDetail->no_badan,
  52. ));
  53. }
  54. }
  55. return $this->sendResponse($data, 'Berjaya dapatkan senarai penguatkuasa');
  56. }
  57. public function getNearestLocation($id){
  58. $data = array();
  59. $latitude = 4.4695721; $longitude = 101.376383;
  60. $location = CurrentLocation::all();
  61. foreach ($location as $key => $l) {
  62. $last_update = $l->updated_at;
  63. $now = now();
  64. $diff = $now->diffInMinutes($last_update);
  65. $status = '';
  66. if($diff <= 5){
  67. $status = '<span style="color:green"><b>Di Atas Talian</b></span>';
  68. }else{
  69. $status = '<span style="color:red"><b>Di Luar Talian</b></span>';
  70. }
  71. $staff = Staff::with('StaffDetail')->where('_id',$l->staff_id)->first();
  72. if($l->staff_id == ''){
  73. array_push($data, array(
  74. '_id' => $l->_id,
  75. 'latitude' => '4.4695721',
  76. 'longitude' => '101.376383',
  77. 'title' => 'Majlis Daerah Cameron Highlands',
  78. 'content' => '<b>Ibu Pejabat</b>',
  79. 'icon' => url('/uploads/map_marker/mdch_map.png'),
  80. 'status' => 'main',
  81. 'zindex' => $key+1
  82. ));
  83. }else{
  84. if(!empty($staff)){
  85. array_push($data, array(
  86. '_id' => $staff->_id,
  87. 'latitude' => $l->latitude,
  88. 'longitude' => $l->longitude,
  89. 'title' => $staff->StaffDetail->full_name,
  90. 'content' => $status,
  91. 'icon' => url('/uploads/map_marker/officer.png'),
  92. 'status' => $l->status,
  93. 'zindex' => $key+1
  94. ));
  95. }
  96. }
  97. }
  98. return $this->sendResponse($data, 'Berjaya dapatkan senarai Koordinat');
  99. }
  100. /**
  101. * Create Emergency controller.
  102. *
  103. * @return json
  104. */
  105. public function postEmergency(Request $request){
  106. $staff = Staff::with('StaffDetail','TrackLocation','CurrentLocation')->where('api_token', $request->api_token)->first();
  107. if(!empty($staff)){
  108. $dataLatLong = [
  109. 'latitude' => $request->latitude,
  110. 'longitude' => $request->longitude,
  111. 'status' => 'emergency',
  112. ];
  113. $staff->tracklocation()->create($dataLatLong);
  114. $staff->CurrentLocation->latitude = $request->latitude;
  115. $staff->CurrentLocation->longitude = $request->longitude;
  116. $staff->CurrentLocation->status = 'emergency';
  117. $staff->CurrentLocation->save();
  118. return $this->sendResponse($staff->CurrentLocation, 'Berjaya hantar kecemasan');
  119. }else{
  120. return $this->sendError('', 'Tidak dapat hantar kecemasan');
  121. }
  122. }
  123. public function returnNormal(Request $request){
  124. $current = CurrentLocation::where('status','emergency')->get();
  125. if(count($current) > 0){
  126. foreach($current as $c){
  127. $c->status = 'normal';
  128. $c->save();
  129. }
  130. return $this->sendResponse('', 'Berjaya kembali kepada asal');
  131. }else{
  132. return $this->sendError('', 'Tidak dapat kembali keadaan asal');
  133. }
  134. }
  135. }