/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $data = $request->json(); // $data = json_decode($request->input('data'), true); $ride = Ride::find($data->get('ride_id')); if ($ride) { $route = new Route(); // $route->startTime = $request->input('start_time'); if ($ride->initiator === Auth::user()->id) { $route->state = 'accepted'; } else { $route->state = 'requested'; } $route->endTime = !empty($data->get('share_details')) && isset($data->get('share_details')['arrival_time']) ? $data->get('share_details')['arrival_time'] : ''; $route->user_id = Auth::user()->id; $route->ride_id = $ride->id; $route->note = !empty($data->get('share_details')) && isset($data->get('share_details')['notes']) ? $data->get('share_details')['notes'] : ''; if ($ride->initiator === Auth::user()->id) { $route->extends = $ride->head; $route->save(); $ride->head = $route->id; $ride->save(); } else { $route->save(); } foreach ($data->get('origins') as $point) { RoutePoint::create(['route_id' => $route->id, 'placeId' => $point['google_place_id'], 'address' => $point['formatted_address'], 'location' => $point['longitude'] . ',' . $point['latitude'], 'name' => $point['name'], 'type' => 'start']); } foreach ($data->get('destinations') as $point) { RoutePoint::create(['route_id' => $route->id, 'placeId' => $point['google_place_id'], 'address' => $point['formatted_address'], 'location' => $point['longitude'] . ',' . $point['latitude'], 'name' => $point['name'], 'type' => 'end']); } return Response::json(['status' => 'success', 'data' => DbUtil::serializeRoute($route)]); } else { return Response::json(['status' => 'failure', 'message' => 'record not found']); } }
public function getFriendRides() { $friends = User::getFriends(User::find(Auth::user()->id)); $ids = []; foreach ($friends as $friend) { $ids[] = $friend->id; } $rides = []; if (!empty($ids)) { $rides = Ride::whereIn('initiator', $ids)->get(); } return Response::json(DbUtil::serializeRides($rides)); }
public function actionRides() { $_currUser = Yii::$app->user->isGuest ? null : User::findOne(['id' => Yii::$app->user->id]); $_request = Yii::$app->request; $isAdminMode = false; if ($_currUser && $_currUser->is_admin && $_request->get('id')) { $isAdminMode = true; $_currUser = User::findOne(['id' => $_request->get('id')]); } if (!$_currUser) { return $this->goHome(); } $context = ['user_id' => $_currUser->id, 'user_email' => $_currUser->email, 'user_type' => $_currUser->user_type, 'is_admin' => $isAdminMode]; $ridesPending = null; $ridesActive = null; $ridesHistory = null; if (User::$_TYPE_CUSTOMER === $_currUser->user_type) { if ($_request->get('ride_id') && $_request->get('action')) { $ride = Ride::find()->where('(id=:ride_id) AND (client_id = :client_id)', ['ride_id' => intval($_request->get('ride_id')), 'client_id' => $_currUser->id])->one(); if ($ride && in_array($_request->get('action'), ['markComplete', 'rateDriver'])) { if ('markComplete' === $_request->get('action') && in_array($ride->status, [Ride::$_STATUS_ACCEPTED])) { $ride->status = Ride::$_STATUS_COMPLETE; $ride->save(); } elseif ('rateDriver' === $_request->get('action') && in_array($ride->status, [Ride::$_STATUS_COMPLETE])) { $rate_score = $_request->get('rate') ? floatval($_request->get('rate')) : null; if ($rate_score) { $_driver = User::findOne(['id' => $ride->driver_id, 'user_type' => User::$_TYPE_DRIVER]); if ($_driver && !$_driver->isRatedBy($_currUser->id)) { $_driver->rateByUser($_currUser->id, $rate_score); } } } } } $ridesPending = Ride::find()->where('(client_id=:client_id) AND (status IN (:stat_invitation, :stat_pending))', ['client_id' => $_currUser->id, 'stat_invitation' => Ride::$_STATUS_INVITATION, 'stat_pending' => Ride::$_STATUS_PENDING])->orderBy('created_at ASC')->all(); $ridesActive = Ride::find()->where('(client_id=:client_id) AND (status=:stat_accepted)', ['client_id' => $_currUser->id, 'stat_accepted' => Ride::$_STATUS_ACCEPTED])->orderBy('created_at ASC')->all(); $ridesHistory = Ride::find()->where('(client_id=:client_id) AND (status IN (:stat_declined, :stat_completed))', ['client_id' => $_currUser->id, 'stat_declined' => Ride::$_STATUS_DECLINED, 'stat_completed' => Ride::$_STATUS_COMPLETE])->limit(10)->orderBy('created_at ASC')->all(); } else { if ($_request->get('ride_id') && $_request->get('action')) { $ride = Ride::find()->where('(id=:ride_id) AND (driver_id = :driver_id)', ['ride_id' => intval($_request->get('ride_id')), 'driver_id' => $_currUser->id])->one(); if ($ride && in_array($_request->get('action'), ['accept', 'decline', 'rateClient'])) { if ('accept' === $_request->get('action') && in_array($ride->status, [Ride::$_STATUS_INVITATION, Ride::$_STATUS_PENDING])) { $ride->status = Ride::$_STATUS_ACCEPTED; $ride->save(); } elseif ('decline' === $_request->get('action') && in_array($ride->status, [Ride::$_STATUS_INVITATION, Ride::$_STATUS_PENDING])) { $ride->status = Ride::$_STATUS_DECLINED; $ride->save(); } elseif ('rateClient' === $_request->get('action') && in_array($ride->status, [Ride::$_STATUS_COMPLETE])) { $rate_score = $_request->get('rate') ? floatval($_request->get('rate')) : null; if ($rate_score) { $_client = User::findOne(['id' => $ride->client_id, 'user_type' => User::$_TYPE_CUSTOMER]); if ($_client && !$_client->isRatedBy($_currUser->id)) { $_client->rateByUser($_currUser->id, $rate_score); } } } } } $ridesPending = Ride::find()->where('(driver_id=:driver_id) AND (status IN (:stat_invitation, :stat_pending))', ['driver_id' => $_currUser->id, 'stat_invitation' => Ride::$_STATUS_INVITATION, 'stat_pending' => Ride::$_STATUS_PENDING])->orderBy('created_at ASC')->all(); $ridesActive = Ride::find()->where('(driver_id=:driver_id) AND (status=:stat_accepted)', ['driver_id' => $_currUser->id, 'stat_accepted' => Ride::$_STATUS_ACCEPTED])->orderBy('created_at ASC')->all(); $ridesHistory = Ride::find()->where('(driver_id=:driver_id) AND (status IN (:stat_declined, :stat_completed))', ['driver_id' => $_currUser->id, 'stat_declined' => Ride::$_STATUS_DECLINED, 'stat_completed' => Ride::$_STATUS_COMPLETE])->orderBy('created_at ASC')->limit(10)->all(); } return $this->render('rides', ['ridesPending' => $ridesPending, 'ridesActive' => $ridesActive, 'ridesHistory' => $ridesHistory, 'context' => $context]); }
js/moment-with-locales.min.js" type="text/javascript"></script> <script src="<?php echo Yii::$app->homeUrl; ?> js/bootstrap-datetimepicker.js" type="text/javascript"></script> </head> <body> <?php $items = [['label' => 'Home', 'url' => ['site/home']]]; $_currUser = Yii::$app->user->isGuest ? null : User::findOne(['id' => Yii::$app->user->id]); $pendingRides = null; if ($_currUser) { if (User::$_TYPE_CUSTOMER === $_currUser->user_type) { $pendingRides = Ride::find()->where('(client_id = :client_id) AND (status IN (:status_invitation, :status_pending))', [':client_id' => $_currUser->id, ':status_invitation' => Ride::$_STATUS_INVITATION, ':status_pending' => Ride::$_STATUS_PENDING])->count(); } else { $pendingRides = Ride::find()->where('(driver_id = :driver_id) AND (status IN (:status_invitation, :status_pending))', [':driver_id' => $_currUser->id, ':status_invitation' => Ride::$_STATUS_INVITATION, ':status_pending' => Ride::$_STATUS_PENDING])->count(); } } if (!$_currUser || User::$_TYPE_CUSTOMER === $_currUser->user_type) { $items[] = ['label' => 'Drivers' . ($_currUser && $_currUser->is_admin ? ' (admin mode)' : ''), 'url' => ['site/drivers']]; } if ($_currUser && $_currUser->is_admin) { $items[] = ['label' => 'Users (admin mode)', 'url' => ['site/users']]; } if (!$_currUser) { $items[] = ['label' => 'Sign In', 'url' => ['/site/login']]; $items[] = ['label' => 'Sign Up', 'url' => ['/site/signup']]; } else { if (User::$_TYPE_CUSTOMER === $_currUser->user_type) { $items[] = ['label' => 'Create Ride Invitation', 'url' => ['/site/invitation']]; }