/** * Run the database seeds. * * @return void */ public function run() { for ($i = 1; $i <= 3; $i++) { $partner = Partner::find($i); $event = Event::find(1); $distance = LocationController::getMapsMatrixDistance($event->location, $partner->location); $event->partners()->save($partner, ['distance' => $distance]); } }
/** * Adds the suggested partner to the database using the details * returned from the Google Places API * @param $encryptedPlaceID The encrypted place ID, of the place to be added to the database */ public function addSuggestedPartner($encryptedPlaceID) { //decrypt place id $placeID = Crypt::decrypt($encryptedPlaceID); //Find from google places $result = file_get_contents('https://maps.googleapis.com/maps/api/place/details/json?placeid=' . $placeID . '&key=' . env('GOOGLE_API_KEY')); //store the location (call location.createLocationForPlace( $longitude, $latitude, $title )) $result = json_decode($result, true); $longitude = $result['result']['geometry']['location']['lng']; $latitude = $result['result']['geometry']['location']['lat']; $title = $result['result']['name']; $locationID = LocationController::createLocationForPlace($longitude, $latitude, $title); //Create new partner in db $price = 0; if (isset($result['result']['price_level'])) { $price = 5 * $result['result']['price_level']; } $url = isset($result['result']['website']) ? $result['result']['website'] : ""; $type = isset($result['result']['types'][0]) ? $result['result']['types'][0] : ""; $newData = array("name" => $title, "description" => $title, "type" => $type, "price" => $price, "location_id" => $locationID, "email" => "", "featured_image" => "/images/red-geometric-background.png", "url" => $url, "logo" => "/images/default_profile_image.png"); // Create the new partner $newPartner = Partner::create($newData); //associate partner with event //TODO }
/** * Calculates the distance to travel from origin to destination using thier respective co-ordinates. * This function makes use of the Google Maps Distance Matrix API and requires a valid Server API key. * * @param App\Location $origin The origin, for this project it should be the co-ordinates of the event * @param App\Location $destination The destination, as above * @return int The distance returned by the Google Maps Distance Matrix API */ public static function getMapsMatrixDistance($origin, $destination) { //Try to get Distance from Google Distance Matrix API $response = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins=' . $origin->longitude . ',' . $origin->latitude . '&destinations=' . $destination->longitude . ',' . $destination->latitude . '&key=' . env('GOOGLE_API_KEY')); $response = json_decode($response, true, 512); //WOO Debug code! Documenting my approach tbh //If we're all cool with the functional code, I'll get rid of these comments // // var_dump($response); // var_dump($response["rows"][0]["elements"][0]["distance"]["value"]); //echo ($response["rows"][0]["elements"][0]["distance"]["value"] == '' ? 'empty' : 'not empty'); //if( isset($response["rows"][0]["elements"][0]["distance"]["value"]) ){echo 'not empty';} else {echo 'empty';} //If response contains zero results call getDistance instead //This will only happen (I think) when Google can't find a route between the two points //Or on an invalid request if (isset($response["rows"][0]["elements"][0]["distance"]["value"])) { return $response["rows"][0]["elements"][0]["distance"]["value"]; } else { return LocationController::getDistance($origin, $destination); } }
public function update($id, Request $request) { // Ensure the user is logged in as an admin if (!Auth::check() || !Auth::user()->is_admin) { return response(view('errors.403', ['error' => $this->errorMessages['incorrect_permissions']]), 403); } // Try to retrieve the model for updating from the database try { $event = Event::findOrFail($id); } catch (ModelNotFoundException $e) { return Redirect::back()->withErrors(['message' => $this->errorMessages['event_not_found']]); } // Get the required fields from the form $data = $request->only(['title', 'tagline', 'description', 'partner_id', 'featured_image', 'start_date', 'end_date', 'start_time', 'end_time', 'location_id', 'price']); // Validate the data $validator = Validator::make($data, ['title' => 'required', 'tagline' => 'required', 'description' => 'required', 'start_date' => 'required', 'end_date' => 'required', 'start_time' => 'required', 'end_time' => 'required', 'location_id' => 'required', 'price' => 'required|numeric|min:0', 'featured_image' => 'image|sometimes']); // If validation fails; if ($validator->fails()) { // Redirect back to registration form with errors return Redirect::back()->withErrors($validator)->withInput(); } // Format the start and end datetime properly // for storage in the database $start_datetime = $data['start_date'] . ' ' . $data['start_time'] . ':' . '00'; $end_datetime = $data['end_date'] . ' ' . $data['end_time'] . ':' . '00'; $newData = array("title" => $data["title"], "tagline" => $data["tagline"], "description" => $data["description"], "start_datetime" => $start_datetime, "end_datetime" => $end_datetime, "location_id" => $data["location_id"], "price" => $data["price"]); // Only if the user updated the photo; if ($request->hasFile('featured_image')) { // Upload the image $data['featured_image'] = MediaController::uploadImage($request->file('featured_image'), time(), $directory = "event_photos", $bestFit = true, $fitDimensions = [1920, 1080]); // Store the new image $event->featured_image = $data['featured_image']; } else { unset($data['featured_image']); } // Store the values from the form in event $event->update($newData); // Detach all partners and attach new partners to event $event->partners()->detach(); $distance; if (isset($data['partner_id']) && $data['partner_id'] != '') { foreach ($data['partner_id'] as $partner_id) { $distance = LocationController::getMapsMatrixDistance(Partner::find($partner_id)->location, $event->location); $event->partners()->attach($partner_id, ['distance' => $distance]); } } // Save the event to the database $event->save(); // Return to the events index return Redirect::route('events.show', $event->id); }