1: <?php
2:
3: namespace App\Http\Controllers;
4:
5:
6: use App\AdClick;
7: use App\Taxibase;
8: use Carbon\Carbon;
9: use Illuminate\Database\Eloquent\ModelNotFoundException;
10: use Illuminate\Http\Request;
11: use App\Http\Controllers\Controller;
12:
13: use App\Http\Requests;
14: use App\AdLocation;
15: use App\User;
16: use App\Tablet;
17: use App\Driver;
18: use App\Ad;
19: use App\Route;
20: use App\Newspaper;
21: use App\Emergency;
22: use App\Taxi;
23: use App\Comment;
24: use App\Route as RouteR;
25: use Illuminate\Http\Response as Response;
26:
27: use Illuminate\Support\Facades\Input;
28: class ApiController extends Controller
29: {
30: 31: 32: 33: 34: 35: 36: 37: 38:
39: private static $apikey = "alpha";
40:
41: 42: 43: 44: 45: 46: 47: 48: 49:
50: private static $error = array(
51: 'success' => false,
52: 'error' => 'Wrong API key used, access denied.',
53: 'api-version' => '1.0',
54: 'status' => '401'
55: );
56:
57: 58: 59: 60: 61: 62: 63: 64:
65: private static $none = array(
66: 'success' => false,
67: 'error' => 'Whoops, we could not find any.',
68: 'status' => '404'
69: );
70:
71: 72: 73: 74: 75: 76: 77: 78:
79: public function adsPerLocation()
80: {
81: $location = Input::get('location');
82: $key = Input::get('key');
83:
84: if(!empty($location)){
85: if ($key == self::$apikey) {
86: $results = AdLocation::with('ad')->where('location', '=', $location)->get();
87: if($results->isEmpty()){
88: return response()->json(self::$none, 404);
89: }else{
90: return response()->json(array(
91: 'advertisements' => $results,
92: 'success' => true,
93: 'action' => 'get_ads_for_location',
94: 'status' => '200'
95: ),200);
96: }
97: }
98: return response()->json(self::$error, 401);
99: }
100: return response()->json(array(
101: 'success' => false,
102: 'info' => 'Check if all parameters are filled in',
103: 'status' => '400'
104: ),400);
105: }
106:
107:
108: 109: 110: 111: 112: 113: 114: 115:
116: public function adsByType()
117: {
118: $type = Input::get('type');
119: $location = Input::get('location');
120: $key = Input::get('key');
121:
122: if(!empty($type) && !empty($location)){
123: if ($key == self::$apikey) {
124: $results = AdLocation::with('ad')->where('location', '=', $location)->get();
125:
126: $response = array();
127: foreach($results as $key => $value){
128: if($value->ad->type == $type){
129: $response[] = $value;
130: }
131: }
132:
133: if($results->isEmpty()){
134: return response()->json(self::$none, 404);
135: }else{
136: return response()->json(array(
137: 'advertisements' => $response,
138: 'success' => true,
139: 'action' => 'get_ads_for_location_and_type',
140: 'status' => '200'
141: ),200);
142: }
143: }
144: return response()->json(self::$error, 401);
145: }
146: return response()->json(array(
147: 'success' => false,
148: 'info' => 'Check if all parameters are filled in',
149: 'status' => '400'
150: ),400);
151: }
152:
153: 154: 155: 156: 157: 158: 159:
160: public function increaseClickOfAd()
161: {
162: $id = Input::get('id');
163: $key = Input::get('key');
164: $ad = Ad::with('adClicks')->where('id',$id)->first();
165: $today = date('Y-m-d');
166:
167: if(!empty($id)){
168: if ($key == self::$apikey) {
169: $adclicks = AdClick::where('ad_id',$ad->id)
170: ->whereDate('created_at','=',$today)->first();
171: if(count($adclicks) > 0){
172: $adclicks->update([
173: 'clicks' => $adclicks->clicks + 1
174: ]);
175: }else{
176: AdClick::create([
177: 'ad_id' => $ad->id,
178: 'clicks' => 1
179: ]);
180: }
181: return response()->json(array(
182: 'success' => true,
183: 'action' => 'increase_ad_click_count',
184: 'status' => '200'
185: ));
186: }
187: return response()->json(self::$error, 401);
188: }else{
189: return response()->json(array(
190: 'success' => false,
191: 'info' => 'Check if all parameters are filled in',
192: 'status' => '400'
193: ),400);
194: }
195: }
196:
197: 198: 199: 200: 201: 202: 203: 204:
205: public function getDriverOffTablet()
206: {
207: $tablet = Input::get('tablet_name');
208: $key = Input::get('key');
209: if(!empty($tablet)){
210: if ($key == self::$apikey) {
211: $user = User::where('tablet_name', '=', $tablet)->first();
212: $tablet = Tablet::with('taxi')->where('user_id', '=', $user->id)->first();
213: $driver = Driver::where('id', '=', $tablet->taxi->driver_id)->first();
214: $user = User::where('id',$driver->user_id)->first();
215:
216: $rowCount = 0;
217: $stars = 0;
218: $allComments = Driver::with('comment')->where('id',$driver->id)->get();
219: foreach($allComments as $comm){
220: foreach($comm->comment as $comment){
221: if($comment->approved > 0){
222: $stars += $comment->star_rating;
223: $rowCount+= count($comment->star_rating);
224: }
225: }
226: }
227: if($rowCount > 0){
228: $driverStars = $stars / $rowCount;
229: $user->stars = $driverStars;
230: }
231:
232: return response()->json(array(
233: 'driver' => $driver,
234: 'user' => $user,
235: 'success' => true,
236: 'action' => 'get_driver_off_tablet',
237: 'status' => '200'
238: ),200);
239: }
240: return response()->json(self::$error, 401);
241: }else{
242: return response()->json(array(
243: 'success' => false,
244: 'info' => 'Check if all parameters are filled in',
245: 'status' => '400'
246: ),400);
247: }
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257:
258: public function getRoutes()
259: {
260: $key = Input::get('key');
261: $today = date('Y-m-d');
262: $all_routes = RouteR::all();
263: $routeArray = array();
264:
265: if ($key == self::$apikey) {
266: foreach($all_routes as $route){
267: if(date('Y-m-d',strtotime($route->pickup_time)) == $today){
268: $routeArray[] = $route;
269: }
270: }
271: if(count($routeArray) < 1){
272: return response()->json(self::$none, 404);
273: }else{
274: return response()->json(array(
275: 'routes' => $routeArray,
276: 'success' => true,
277: 'action' => 'get_all_routes',
278: 'status' => '200'
279: ),200);
280: }
281: }
282: return response()->json(self::$error, 401);
283: }
284:
285: 286: 287: 288: 289: 290: 291: 292:
293: public function getRoutesForTaxi()
294: {
295: $taxiId = Input::get('taxi_id');
296: $key = Input::get('key');
297: $today = date('Y-m-d');
298: $all_routes = RouteR::where('taxi_id',$taxiId)->get();
299: $routeArray = array();
300:
301: if(!empty($taxiId)){
302: if ($key == self::$apikey) {
303: foreach($all_routes as $route){
304: if(date('Y-m-d',strtotime($route->pickup_time)) == $today){
305: $routeArray[] = $route;
306: }
307: }
308: if(count($routeArray) < 1){
309: return response()->json(self::$none, 404);
310: }else {
311: return response()->json(array(
312: 'routes' => $routeArray,
313: 'success' => true,
314: 'action' => 'get_routes_for_current_taxi',
315: 'status' => '200'
316: ), 200);
317: }
318: }
319: return response()->json(self::$error, 401);
320: }else{
321: return response()->json(array(
322: 'success' => false,
323: 'info' => 'Check if all parameters are filled in',
324: 'status' => '400'
325: ),400);
326: }
327: }
328:
329: 330: 331: 332: 333: 334: 335: 336:
337: public function getNewsfeeds()
338: {
339: $key = Input::get('key');
340: if ($key == self::$apikey) {
341: $news = Newspaper::all();
342: if($news->isEmpty()){
343: return response()->json(self::$none, 404);
344: }else{
345: return response()->json(array(
346: 'news' => $news,
347: 'success' => true,
348: 'action' => 'get_news_feeds',
349: 'status' => '200'
350: ),200);
351: }
352: }
353: return response()->json(self::$error, 401);
354: }
355:
356: 357: 358: 359: 360: 361: 362: 363:
364: public function sendSOS()
365: {
366: $id = Input::get('taxi_id');
367: $last_lat = Input::get('lat');
368: $last_long = Input::get('lng');
369: $key = Input::get('key');
370:
371: if(!empty($id) && !empty($last_lat) && !empty($last_long)){
372: if ($key == self::$apikey) {
373: Emergency::where('taxi_id',$id)->update(array('taxi_id' => $id, 'seen' => '0'));
374:
375: Taxi::where('id', '=', $id)->update(
376: array('last_latitude' => $last_lat,
377: 'last_longtitude' => $last_long
378: ));
379: return response()->json(array(
380: 'success' => true,
381: 'action' => 'emergency_signal_send',
382: 'status' => '200'
383: ), 200);
384: }
385: return response()->json(self::$error, 401);
386: }else{
387: return response()->json(array(
388: 'success' => false,
389: 'info' => 'Check if all parameters are filled in',
390: 'status' => '400'
391: ),400);
392: }
393: }
394:
395:
396: 397: 398: 399: 400: 401: 402: 403: 404:
405: public function tabletLogin()
406: {
407: $tablet_name = Input::get('tablet_name');
408: $key = Input::get('key');
409: if(!empty($tablet_name)) {
410: if ($key == self::$apikey) {
411: $tablet = User::with('tablet')->where('tablet_name', '=', $tablet_name)->first();
412: $exists = count($tablet);
413:
414: if ($exists > 0) {
415: $taxi = Taxi::with('driver')->where('id', '=', $tablet->tablet->taxi_id)->first();
416: $user = User::where('id',$taxi->driver->user_id)->first();
417: $result = collect([
418: array('tablet' => $tablet),
419: array('taxi_with_driver' => $taxi),
420: array('taxi_user' => $user)
421: ]);
422: if(count($taxi) < 1 || count($user) < 1){
423: return response()->json(self::$none, 404);
424: }else{
425: return response()->json(array(
426: 'result' => $result,
427: 'success' => true,
428: 'action' => 'tablet_login',
429: 'status' => '200'
430: ),200);
431: }
432: }
433: return response()->json(self::$none, 404);
434: }
435: return response()->json(self::$error, 401);
436: }else{
437: return response()->json(array(
438: 'success' => false,
439: 'info' => 'Check if all parameters are filled in',
440: 'status' => '400'
441: ),400);
442: }
443: }
444:
445: 446: 447: 448: 449: 450: 451: 452:
453: public function postComment()
454: {
455: $driver = Input::get('driver_id');
456: $message = Input::get('message');
457: $stars = Input::get('stars');
458: $key = Input::get('key');
459:
460: if(!empty($driver) && !empty($message) && !empty($stars)) {
461: if ($key == self::$apikey) {
462: $data = array(
463: 'driver_id' => $driver,
464: 'comment' => $message,
465: 'approved' => 0,
466: 'star_rating' => $stars);
467: Comment::create($data);
468:
469: return response()->json(array(
470: 'success' => true,
471: 'action' => 'comment_posted',
472: 'status' => '200'
473: ), 200);
474: }
475: return response()->json(self::$error, 401);
476: }else{
477: return response()->json(array(
478: 'success' => false,
479: 'info' => 'Check if all parameters are filled in',
480: 'status' => '400'
481: ),400);
482: }
483: }
484:
485: 486: 487: 488: 489: 490: 491: 492: 493:
494: public function signalCheck()
495: {
496: $taxis = Taxi::where('in_shift', '=', 1)->get();
497:
498: foreach ($taxis as $taxi) {
499: $last = Carbon::createFromFormat('Y-m-d H:i:s', $taxi->last_seen);
500: $diff = $last->diffInMinutes(Carbon::now()->addMinutes(7));
501: if ($diff >= 20){
502: if(!Emergency::where('taxi_id',$taxi->id)->where('seen',0)->exists()){
503: Emergency::create(array('taxi_id' => $taxi->id, 'seen' => 0));
504: }
505: }
506: }
507: }
508:
509: 510: 511: 512: 513: 514: 515: 516:
517: public function getSOS()
518: {
519: $emergencies = Emergency::with('taxi')->where('seen', 0)->get();
520: if (count($emergencies) > 0) {
521: foreach ($emergencies as $sos) {
522: if ($sos->taxi) {
523: $sosArray[] = array('sos' => 'true', 'taxi_license_plate' => $sos->taxi->license_plate,
524: 'last_seen' => $sos->taxi->last_seen);
525: }
526: }
527: return response()->json($sosArray);
528: }
529: return response()->json(['sos' => 'false']);
530: }
531:
532: 533: 534: 535: 536: 537: 538: 539:
540: public function getCommentsOffDriver(){
541: $driverID = Input::get('driver_id');
542: $key = Input::get('key');
543:
544: if(!empty($driverID)) {
545: if ($key == self::$apikey) {
546: $comment = Comment::where('driver_id', $driverID)->where('approved', 1)->get();
547: if($comment->isEmpty()){
548: return response()->json(self::$none, 404);
549: }else{
550: return response()->json(array(
551: 'comments' => $comment,
552: 'success' => true,
553: 'action' => 'comments_off_driver',
554: 'status' => '200'
555: ),200);
556: }
557: }
558: return response()->json(self::$error, 401);
559: }else{
560: return response()->json(array(
561: 'success' => false,
562: 'info' => 'Check if all parameters are filled in',
563: 'status' => '400'
564: ),400);
565: }
566: }
567:
568: 569: 570: 571: 572: 573: 574: 575:
576: public function sendLocation(){
577: $driverID = Input::get('driver_id');
578: $latitude = Input::get('lat');
579: $longtitude = Input::get('lng');
580: $key = Input::get('key');
581: $datetime = date('Y-m-d H:i:s');
582: if(!empty($driverID) && !empty($latitude) && !empty($longtitude)) {
583: if ($key == self::$apikey) {
584: $find = Taxi::where('driver_id', $driverID)->first();
585: if(count($find) < 1){
586: return response()->json(self::$none, 404);
587: }else{
588: $find->update([
589: 'last_latitude' => $latitude,
590: 'last_longtitude' => $longtitude,
591: 'last_seen' => $datetime
592: ]);
593: }
594: return response()->json(array(
595: 'success' => true,
596: 'action' => 'current_location_coords_send',
597: 'status' => '200'
598: ),200);
599: }
600: return response()->json(self::$error, 401);
601: }else{
602: return response()->json(array(
603: 'success' => false,
604: 'info' => 'Check if all parameters are filled in',
605: 'status' => '400'
606: ),400);
607: }
608: }
609:
610: 611: 612: 613: 614: 615: 616: 617:
618: public function inShift(){
619: $taxiID = Input::get('taxi_id');
620: $key = Input::get('key');
621:
622: if(!empty($taxiID)) {
623: if ($key == self::$apikey) {
624: $car = Taxi::where('id', $taxiID)->first();
625: if(count($car) < 1){
626: return response()->json(self::$none, 404);
627: }else{
628: $car->update(['in_shift' => 1]);
629:
630: return response()->json(array(
631: 'success' => true,
632: 'action' => 'shift_value_changed',
633: 'value' => $car->in_shift,
634: 'status' => '200'
635: ),200);
636: }
637: }
638: return response()->json(self::$error, 401);
639: }else{
640: return response()->json(array(
641: 'success' => false,
642: 'info' => 'Check if all parameters are filled in',
643: 'status' => '400'
644: ),400);
645: }
646: }
647:
648: 649: 650: 651: 652: 653: 654: 655:
656: public function offShift(){
657: $taxiID = Input::get('taxi_id');
658: $key = Input::get('key');
659:
660: if(!empty($taxiID)) {
661: if ($key == self::$apikey) {
662: $car = Taxi::where('id', $taxiID)->first();
663: if(count($car) < 1){
664: return response()->json(self::$none, 404);
665: }else{
666: $car->update(['in_shift' => 0]);
667:
668: return response()->json(array(
669: 'success' => true,
670: 'action' => 'shift_value_changed',
671: 'value' => $car->in_shift,
672: 'status' => '200'
673: ),200);
674: }
675: }
676: return response()->json(self::$error, 401);
677: }else{
678: return response()->json(array(
679: 'success' => false,
680: 'info' => 'Check if all parameters are filled in',
681: 'status' => '400'
682: ),400);
683: }
684: }
685:
686: 687: 688: 689: 690: 691: 692: 693: 694: 695:
696: public function requestReturnRide()
697: {
698: $key = Input::get('key');
699: $start_city = Input::get('start_city');
700: $start_zip = Input::get('start_zip');
701: $start_numb = Input::get('start_number');
702: $start_street = Input::get('start_street');
703: $end_city = Input::get('end_city');
704: $end_zip = Input::get('end_zip');
705: $end_numb = Input::get('end_number');
706: $end_street = Input::get('end_street');
707: $pickup_time = Input::get('pickup_time');
708: $phone_cust = Input::get('phone_customer');
709: $email_cust = Input::get('email_customer');
710:
711: if( !empty($start_city) &&
712: !empty($start_zip) &&
713: !empty($start_numb) &&
714: !empty($start_street) &&
715: !empty($end_city) &&
716: !empty($end_zip) &&
717: !empty($end_numb) &&
718: !empty($end_street) &&
719: !empty($pickup_time) &&
720: !empty($phone_cust) &&
721: !empty($email_cust)
722: ) {
723: if ($key == self::$apikey) {
724: RouteR::create([
725: 'start_city' => $start_city,
726: 'start_zip' => $start_zip,
727: 'start_number' => $start_numb,
728: 'start_street' => $start_street,
729:
730: 'end_city' => $end_city,
731: 'end_zip' => $end_zip,
732: 'end_number' => $end_numb,
733: 'end_street' => $end_street,
734:
735: 'pickup_time' => $pickup_time,
736: 'phone_customer' => $phone_cust,
737: 'email_customer' => $email_cust
738: ]);
739: return response()->json(array(
740: 'success' => true,
741: 'action' => 'return_ride_requested',
742: 'status' => '200'
743: ), 200);
744: }
745: return response()->json(self::$error, 401);
746: }else{
747: return response()->json(array(
748: 'success' => false,
749: 'info' => 'Check if all parameters are filled in',
750: 'status' => '400'
751: ),400);
752: }
753: }
754:
755: 756: 757: 758: 759: 760: 761: 762:
763: public function postBase(){
764: $key = Input::get('key');
765: $lat = Input::get('lat');
766: $long = Input::get('lng');
767: $name = Input::get('base_name');
768:
769: if(!empty($lat) && !empty($long) && !empty($name)){
770: if($key == self::$apikey){
771: Taxibase::create([
772: 'latitude' => $lat,
773: 'longtitude' => $long,
774: 'base_name' => $name
775: ]);
776: return response()->json(array(
777: 'success' => true,
778: 'action' => 'base_added',
779: 'status' => '200'
780: ),200);
781: }
782: return response()->json(self::$error,401);
783: }else{
784: return response()->json(array(
785: 'success' => false,
786: 'info' => 'Check if all parameters are filled in',
787: 'status' => '400'
788: ),400);
789: }
790:
791: }
792:
793: 794: 795: 796: 797: 798: 799: 800:
801: public function getLocations(){
802: $key = Input::get('key');
803:
804: if($key == self::$apikey){
805: $cars = Taxi::with('driver','emergency')->where('in_shift',1)->where('last_latitude','!=','')->where('last_longtitude','!=','')->get();
806:
807: return response()->json(array(
808: 'cars' => $cars
809: ),200);
810: }
811: return response()->json(self::$error,401);
812: }
813:
814: 815: 816: 817: 818: 819: 820: 821:
822: public function getBaseLocations(){
823: $key = Input::get('key');
824:
825: if($key == self::$apikey){
826: $bases = Taxibase::all();
827:
828: return response()->json(array(
829: 'bases' => $bases
830: ),200);
831: }
832: return response()->json(self::$error,401);
833: }
834:
835: 836: 837: 838: 839: 840: 841: 842:
843: public function getShiftState(){
844: $key = Input::get('key');
845: $taxi_id = Input::get('taxi_id');
846:
847: if($key == self::$apikey){
848: $current = Taxi::where('id',$taxi_id)->first();
849: if(is_null($current)){
850: return response()->json([
851: 'error' => 'not_found'
852: ],404);
853: }else{
854: return response()->json(array(
855: 'shift_val' => $current->in_shift
856: ),200);
857: }
858: }
859: return response()->json(self::$error,401);
860: }
861:
862: 863: 864: 865: 866: 867:
868: public function getLocationsInRadius(){
869: $radius = Input::get('radius');
870: $lat = Input::get('lat');
871: $lng = Input::get('lng');
872:
873: $radius = $radius * 0.62137;
874: $url = 'http://gd.geobytes.com/GetNearbyCities?radius='.$radius.'&Latitude='.$lat.'&Longitude='.$lng.'&limit=999';
875:
876: $response_json = file_get_contents($url);
877:
878: $response = json_decode($response_json, true);
879:
880: return $response;
881: }
882:
883: 884: 885: 886: 887: 888:
889: public function statisticsDataYear(){
890: $year = Input::get('year');
891: $clicks = AdClick::whereYear('created_at','=',$year)->orderBy('created_at','asc')->get();
892:
893: return response()->json(array(
894: 'result' => $clicks
895: ));
896: }
897:
898: 899: 900: 901: 902: 903:
904: public function statisticsDataMonth(){
905: $month = Input::get('month');
906: $year = Input::get('year');
907: $clicks = AdClick::whereMonth('created_at','=',$month)->whereYear('created_at','=',$year)->orderBy('created_at', 'asc')->get();
908:
909: return response()->json(array(
910: 'result' => $clicks
911: ));
912:
913: }
914: 915: 916: 917: 918: 919:
920: public function statisticsDataWeek(){
921: $week = Input::get('week');
922: $year = Input::get('year');
923:
924: $response = array();
925:
926: $clicks = AdClick::whereYear('created_at','=',$year)
927: ->orderBy('created_at', 'asc')
928: ->get();
929:
930: foreach ($clicks as $key => $value) {
931:
932: if($value->created_at->format('W') == $week){
933: $response[] = $value;
934: }
935: }
936:
937: return response()->json(array(
938: 'result' => $response
939: ));
940: }
941:
942: }
943:
944: