Ejemplo n.º 1
0
 /**
  *
  */
 public function within(Request $request)
 {
     $start = microtime(true);
     if (str_contains($request->path(), 'mongo')) {
         $collection = $this->model->within($request->json('geometry.coordinates'));
         $elapsed = microtime(true) - $start;
     } else {
         $geometry = \geoPHP::load($request->input('geometry'), 'wkt');
         $collection = $this->model->within($geometry->asText('wkt'));
         $elapsed = microtime(true) - $start;
     }
     $logMessage = 'ms to get %s data: %f in %s';
     \Log::debug(sprintf($logMessage, str_contains($request->path(), 'mongo') ? 'Mongo' : 'PostGIS', $elapsed, 'within()'));
     return Response::json(['points' => $collection, 'area' => 0]);
 }
Ejemplo n.º 2
0
 /**
  * @param string $in
  * @param bool 	 $success
  * @param int    $code
  * @param array  $extraHeaders
  *
  * @return mixed
  */
 public function response($in = "", $code = 200, $extraHeaders = [])
 {
     $meta = ['RequestTimestamp' => (string) round($_SERVER["REQUEST_TIME_FLOAT"], 4), 'Duration' => (string) $this->executiontime(), 'ResponseTimestamp' => (string) round(microtime(true), 4)];
     $output = ['Success' => true, 'Meta' => $meta, 'Payload' => $in];
     $outp = json_encode($output, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
     $in = json_encode($output, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
     if (config('app.debug') === true) {
         \Log::debug("Output: \r\n" . $outp);
     }
     trim($outp);
     $response = new Response($output, $code);
     $response->header('Content-Type', 'application/json; charset=UTF-8');
     $response->header('Access-Control-Allow-Origin', '*');
     if (is_array($extraHeaders) && count($extraHeaders) > 0) {
         foreach ($extraHeaders as $key => $value) {
             $response->header($key, $value);
         }
     }
     return $response;
 }
Ejemplo n.º 3
0
 /**
  * Store a newly created resource in storage.
  *
  * @return \Illuminate\Http\Response
  */
 public function store(HandleVisitRequest $request)
 {
     // Make sure this is a valid stage
     $stage = Stage::where('id', '=', $request->stage);
     if ($stage->count() > 0) {
         $stage = $stage->first();
     } else {
         return response()->json(['status' => 'failure', 'message' => sprintf('Stage with ID %s does not exist', $request->stage)], 422);
     }
     // Make sure the destination is valid.
     if ($request->destination !== "__checkout__") {
         $destination = Stage::where('id', '=', $request->destination);
         if ($destination->count() == 0) {
             return response()->json(['status' => 'failure', 'message' => sprintf('Destination stage [ID: %s] does not exist', $request->destination)]);
         }
     }
     // Check if the visit has been created yet
     $visit;
     if (is_null($request->visit) || strlen($request->visit) == 0) {
         // Create a new visit
         $visit = new Visit();
         $visit->patients = array_keys($request->patients);
         $visit->stage = $request->stage;
         $saved = $visit->save();
         if (!$saved) {
             return response()->json(['status' => 'failure', 'message' => 'Failed to save new visit record.'], 422);
         }
     } else {
         // Check if the visit exists
         $visit = Visit::where('id', '=', $request->visit);
         if ($visit->count() > 0) {
             $visit = $visit->first();
         } else {
             return response()->json(['status' => 'failure', 'message' => sprintf('Visit with ID %s does not exist.', $request->visit)]);
         }
     }
     $errors = [];
     // Loop through patients, add a new row to the respective Stage for each patient
     foreach ($request->patients as $patientID => $patientData) {
         // Setup data array
         $data = array();
         // Make sure all the values in the patientData array are valid stage columns
         foreach ($patientData as $key => $value) {
             if (array_key_exists($key, $stage->fields)) {
                 // Check if we should mutate the data (i.e. multiselect)
                 switch ($stage->fields[$key]["type"]) {
                     case "file":
                     case "pharmacy":
                     case "multiselect":
                         // Value is an array, convert to string
                         $data[$key] = json_encode($value);
                         break;
                     default:
                         $data[$key] = $value;
                         break;
                 }
             } else {
                 \Log::debug(sprintf('Attemped to update stage [%s], column [%s], but this column is not valid', $stage->id, $key));
             }
         }
         // Make sure this patient record exists
         $patient = Patient::where('id', '=', $patientID);
         // If the patient exists...
         if ($patient->count() > 0) {
             // Grab patient record
             $patient = $patient->first();
             // Update patient record as necessary
             if ($stage->root) {
                 // All data is relative to Patient record
                 $data['concrete'] = true;
                 $data['current_visit'] = $visit->id;
                 // Add this visit ID to patient all-time visits array
                 // (we're at the root stage so this hasnt been done yet)
                 $thisPatientVisits = $patient->visits;
                 $thisPatientVisits[] = $visit->id;
                 $data['visits'] = $thisPatientVisits;
                 \Log::debug("Updating patient record at root stage with data:");
                 \Log::debug($data);
                 // Update patient record.
                 $patient->update($data);
             } else {
                 // Set visit_id and patient_id for reference in this stage
                 $data["visit_id"] = $visit->id;
                 $data["patient_id"] = $patientID;
                 // Insert data into this stage's table
                 DB::table($stage->tableName)->insert($data);
             }
             // If the destination is checkout, remove "current visit" from patient
             if ($request->destination == "__checkout__") {
                 \Log::debug("Destination for patient " . $patient->id . " in visit " . $visit->id . " is " . $request->destination);
                 $patient->current_visit = null;
                 $patient->save();
             }
         } else {
             $error = sprintf("Could not locate patient record [id: %s]", $patientID);
             $errors[] = $error;
             \Log::debug($error);
         }
     }
     // Patient data has been stored, update Visit record
     $visit->stage = $request->destination;
     if ($visit->save()) {
         return response()->json(['status' => 'success', 'message' => 'Visit moved successfully.', 'toStage' => $request->destination, 'visitID' => $visit->id, 'errors' => $errors]);
     } else {
         return response()->json(['status' => 'failure', 'message' => 'Failed to save visit record.', 'errors' => $errors]);
     }
 }
Ejemplo n.º 4
0
 /**
  * Handles interaction with file resources
  *
  * @return \Illuminate\Http\Response
  */
 public function resources(Request $request, $method)
 {
     switch ($method) {
         case 'upload':
             $i = 0;
             $files = array();
             $message = array();
             while ($request->has('file-' . $i)) {
                 $key = time() . "-" . mt_rand();
                 $inputName = 'file-' . $i;
                 $files[$key] = $request->input($inputName);
                 $i++;
             }
             foreach ($files as $fileKey => $fileData) {
                 $splitTypeAndBase64 = explode(";", $fileData);
                 \Log::debug($splitTypeAndBase64);
                 $dataType = explode(":", $splitTypeAndBase64[0]);
                 $splitType = explode("/", $dataType[1]);
                 $base64 = explode(",", $splitTypeAndBase64[1]);
                 $base64 = $base64[1];
                 $resource = new Resource();
                 $resource->type = $dataType[1];
                 $resource->base64 = $base64;
                 $resource->save();
                 $message[$resource->id] = array("type" => $resource->type, "data" => $splitType[0] == "image" ? $this->constructBase64File($resource) : null);
             }
             return response()->json(["status" => "success", "message" => $message]);
             break;
         case "fetch":
             if ($request->has('id')) {
                 $resource = Resource::where('id', $request->id);
                 if ($resource->count() > 0) {
                     $resource = $resource->first(["id", "type", "base64"]);
                     return response()->json(["status" => "success", "type" => $resource->type, "base64" => $this->constructBase64File($resource)]);
                 }
             } else {
                 return response()->json(["status" => "failure", "message" => "Missing ID parameter."]);
             }
             break;
         default:
             return response()->json(["status" => "failure", "message" => "Unknown method"]);
             break;
     }
 }
Ejemplo n.º 5
0
 /**
  * Allow user to leave team
  *
  * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  */
 public function leave()
 {
     $user = \Auth::user()->with(['team', 'team.members'])->first();
     $team_id = $user->team->id;
     // Remove user's team
     $user->team_id = null;
     $user->save();
     \Log::debug("User removed from team {$team_id}");
     // Clean up team if no more users
     if (count($user->team->members) - 1 == 0) {
         Team::destroy($team_id);
         \Log::info("Team {$team_id} deleted.");
     }
     return redirect('/');
 }
Ejemplo n.º 6
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  int $id
  *
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $this->validate($request, ['name' => 'required|min:3', 'category_id' => 'required']);
     $venue = Venue::where('id', $id)->first();
     \DB::transaction(function () use($request, $venue) {
         $venue->name = $request->get('name');
         $venue->category_id = $request->get('category_id');
         $venue->city = $request->get('city');
         $venue->latitude = $request->get('latitude');
         $venue->longitude = $request->get('longitude');
         $venue->address = $request->get('address');
         $venue->country = $request->get('country');
         $venue->zip = $request->get('zip');
         $venue->number = $request->get('number');
         $venue->email = $request->get('email');
         $venue->website = $request->get('website');
         if ($request->hasFile('logo') && $request->file('logo')->isValid()) {
             $venueLogo = $request->file('appIcon');
             $uri = config('services.uploads')['venues-logos'];
             $path = public_path() . $uri;
             $this->createFolderIfNotExists($path);
             $destinationPath = $path;
             $fileName = "i" . $venue->id . "." . $venueLogo->getClientOriginalExtension();
             $venueLogo->move($destinationPath, $fileName);
             \Log::debug(sprintf("New icon image has been uploaded to %s, under the name %s", $destinationPath, $fileName));
             $venue->logo = $uri . $fileName;
             // I might not use it, since I can predict what the name is going to be! id.xx
         }
         $venue->save();
     });
     session()->flash('message', ['success', 'Successfully Updated!']);
     return redirect(action('VenueController@index'));
 }