|
5 | 5 | use Illuminate\Http\Request;
|
6 | 6 |
|
7 | 7 | use App\Http\Requests;
|
| 8 | +use App\Http\Requests\ImportFieldDataRequest; |
| 9 | +use App\Http\Requests\MapFieldDataRequest; |
8 | 10 | use App\Http\Controllers\Controller;
|
9 | 11 |
|
| 12 | +use Carbon\Carbon; |
| 13 | +use Excel; |
| 14 | +use App\Stage; |
| 15 | + |
10 | 16 | class FieldDataController extends Controller
|
11 | 17 | {
|
12 | 18 | /**
|
@@ -37,9 +43,97 @@ public function create()
|
37 | 43 | * @param \Illuminate\Http\Request $request
|
38 | 44 | * @return \Illuminate\Http\Response
|
39 | 45 | */
|
40 |
| - public function store(Request $request) |
| 46 | + public function store(ImportFieldDataRequest $request) |
41 | 47 | {
|
42 |
| - // |
| 48 | + |
| 49 | + // If the upload is found in the request data |
| 50 | + if($request->hasFile('upload')) { |
| 51 | + // If the uploaded file is valid |
| 52 | + if($request->file('upload')->isValid()) { |
| 53 | + |
| 54 | + // Grabname, move to storage |
| 55 | + $name = "field-data-" . date('mdY-His') . ".csv"; |
| 56 | + $request->file('upload')->move(storage_path('imports'), $name); |
| 57 | + |
| 58 | + $sheet = Excel::load(storage_path('imports') . "/" . $name, function($reader) { |
| 59 | + |
| 60 | + }); |
| 61 | + |
| 62 | + return view('console/field-data/map', [ |
| 63 | + 'name' => $name, |
| 64 | + 'mode' => $request->mode, |
| 65 | + 'sheet' => $sheet, |
| 66 | + 'countRows' => $sheet->all()->count(), |
| 67 | + 'headings' => $sheet->first()->keys()->toArray(), |
| 68 | + 'fields' => Stage::where('root', true)->first(['fields'])->fields |
| 69 | + ]); |
| 70 | + |
| 71 | + } else return redirect()->route('console::field-data::import')->with('alert', array('type' => 'failure', 'message' => 'File not found')); |
| 72 | + } else return redirect()->route('console::field-data::import')->with('alert', array('type' => 'failure', 'message' => 'File not found')); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Map CSV columns to a patient column. |
| 77 | + * |
| 78 | + * @param \Illuminate\Http\Request $request |
| 79 | + * @return \Illuminate\Http\Response |
| 80 | + */ |
| 81 | + public function map(MapFieldDataRequest $request) { |
| 82 | + |
| 83 | + // Load the sheet from imports |
| 84 | + $sheet = Excel::load(storage_path('imports') . "/" . $request->filename, function($reader) { |
| 85 | + |
| 86 | + }); |
| 87 | + |
| 88 | + // Grab the headings for this sheet |
| 89 | + $fieldNumberHeading = null; |
| 90 | + $headings = $sheet->first()->keys()->toArray(); |
| 91 | + $map = array(); |
| 92 | + |
| 93 | + |
| 94 | + // Loop through headings and check if we have one at this index |
| 95 | + foreach($headings as $index => $heading) { |
| 96 | + // Make sure the heading matches the heading we just grabbed |
| 97 | + if($request->has('heading-' . $index) && $request->input('heading-' . $index) === $heading) { |
| 98 | + // Make sure we have a map value for this heading. |
| 99 | + if($request->has('map-' . $index)) { |
| 100 | + |
| 101 | + // Grab the destination column |
| 102 | + $destination = $request->input('map-' . $index); |
| 103 | + |
| 104 | + \Log::debug(sprintf("Heading: %s, destination: %s", $heading, $destination)); |
| 105 | + |
| 106 | + // As long as the destination doesn't equal nullify |
| 107 | + if($destination !== "__nullify__") { |
| 108 | + |
| 109 | + // At some point, a field_number is required |
| 110 | + if($destination === "field_number") { |
| 111 | + $fieldNumberHeading = $heading; |
| 112 | + } else { |
| 113 | + // Push heading to map |
| 114 | + $map[$heading] = $destination; |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + $now = Carbon::now()->toDateTimeString(); |
| 125 | + $insert = $sheet->all()->each(function($row) use ($map, $fieldNumberHeading) { |
| 126 | + $data = array(); |
| 127 | + foreach($map as $heading => $destination) { |
| 128 | + $data[$destination] = $row->{$heading}; |
| 129 | + } |
| 130 | + return array( |
| 131 | + "field_number" => $row->{$fieldNumberHeading}, |
| 132 | + "data" => $data |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + \Log::debug($insert); |
43 | 137 | }
|
44 | 138 |
|
45 | 139 | /**
|
|
0 commit comments