Skip to content

Commit 9c2b2ae

Browse files
author
Cameron Kelley
committedJan 17, 2016
Working on FieldData map system
1 parent 48c1b1b commit 9c2b2ae

File tree

12 files changed

+1134
-20
lines changed

12 files changed

+1134
-20
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
composer.phar
44

5+
storage/imports/*
56
.vagrant/
67
.bundle/
78

‎app/Http/Controllers/Console/FieldDataController.php

+96-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
use Illuminate\Http\Request;
66

77
use App\Http\Requests;
8+
use App\Http\Requests\ImportFieldDataRequest;
9+
use App\Http\Requests\MapFieldDataRequest;
810
use App\Http\Controllers\Controller;
911

12+
use Carbon\Carbon;
13+
use Excel;
14+
use App\Stage;
15+
1016
class FieldDataController extends Controller
1117
{
1218
/**
@@ -37,9 +43,97 @@ public function create()
3743
* @param \Illuminate\Http\Request $request
3844
* @return \Illuminate\Http\Response
3945
*/
40-
public function store(Request $request)
46+
public function store(ImportFieldDataRequest $request)
4147
{
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);
43137
}
44138

45139
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use App\Http\Requests\Request;
6+
use Auth;
7+
8+
class ImportFieldDataRequest extends Request
9+
{
10+
/**
11+
* Determine if the user is authorized to make this request.
12+
*
13+
* @return bool
14+
*/
15+
public function authorize()
16+
{
17+
return Auth::check();
18+
}
19+
20+
/**
21+
* Get the validation rules that apply to the request.
22+
*
23+
* @return array
24+
*/
25+
public function rules()
26+
{
27+
return [
28+
//
29+
'upload' => 'required',
30+
'mode' => 'required'
31+
];
32+
}
33+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use App\Http\Requests\Request;
6+
use Auth;
7+
8+
class MapFieldDataRequest extends Request
9+
{
10+
/**
11+
* Determine if the user is authorized to make this request.
12+
*
13+
* @return bool
14+
*/
15+
public function authorize()
16+
{
17+
return Auth::check();
18+
}
19+
20+
/**
21+
* Get the validation rules that apply to the request.
22+
*
23+
* @return array
24+
*/
25+
public function rules()
26+
{
27+
return [
28+
//
29+
'filename' => 'required',
30+
'mode' => 'required'
31+
];
32+
}
33+
}

‎app/Http/routes.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function() {
262262

263263
// Create user
264264
Route::get('import', [
265-
'as' => 'create',
265+
'as' => 'import',
266266
'uses' => 'FieldDataController@create'
267267
]);
268268

@@ -272,6 +272,11 @@ function() {
272272
'uses' => 'FieldDataController@store'
273273
]);
274274

275+
Route::post('map', [
276+
'as' => 'map',
277+
'uses' => 'FieldDataController@map'
278+
]);
279+
275280
});
276281

277282

‎composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"php": ">=5.5.9",
99
"laravel/framework": "5.1.*",
1010
"doctrine/dbal": "2.5.*",
11-
"jenssegers/agent": "*"
11+
"jenssegers/agent": "*",
12+
"maatwebsite/excel": "~2.0.0"
1213
},
1314
"require-dev": {
1415
"fzaninotto/faker": "~1.4",

‎composer.lock

+173-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎config/app.php

+2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
*/
151151
App\Providers\HelperServiceProvider::class,
152152
Jenssegers\Agent\AgentServiceProvider::class,
153+
Maatwebsite\Excel\ExcelServiceProvider::class,
153154

154155
],
155156

@@ -200,6 +201,7 @@
200201
'View' => Illuminate\Support\Facades\View::class,
201202

202203
'Agent' => Jenssegers\Agent\Facades\Agent::class,
204+
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
203205

204206
],
205207

‎config/excel.php

+683
Large diffs are not rendered by default.

‎resources/views/console/field-data/import.blade.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@
66

77
<h1 class="p-t">Import new field data</h1>
88
<hr/>
9-
<form action="{{ route('console::flow::store') }}" method="POST">
9+
<form action="{{ route('console::field-data::store') }}" method="POST" enctype="multipart/form-data">
1010
{!! csrf_field() !!}
1111

1212
<div class="form-group row">
13-
<label for="stageType" class="col-sm-2 form-control-label">Stage type</label>
14-
<div class="col-sm-10">
15-
<select name="type" class="form-control" id="stageType">
16-
<option value="basic">Basic</option>
17-
<option value="pharmacy">Pharmacy</option>
18-
</select>
13+
<label class="col-sm-3 form-control-label">Upload CSV file</label>
14+
<div class="col-sm-9">
15+
<input type="file" name="upload" id="file" accept=".csv" required>
1916
</div>
2017
</div>
2118
<div class="form-group row">
22-
<label for="name" class="col-sm-2 form-control-label">Stage name</label>
23-
<div class="col-sm-10">
24-
<input type="text" name="name" class="form-control" id="name" placeholder="Enter stage name">
19+
<label for="stageType" class="col-sm-3 form-control-label">Upload mode</label>
20+
<div class="col-sm-9">
21+
<select name="mode" class="form-control" id="stageType" required>
22+
<option value="fresh">Delete already-existing field numbers and start fresh</option>
23+
<option value="append">Keep old values, skip if field number already exists</option>
24+
<option value="overwrite">Keep old values, overwrite old data if field number already exists</option>
25+
</select>
2526
</div>
2627
</div>
2728
<div class="form-group row">
28-
<div class="col-sm-offset-2 col-sm-10">
29-
<button type="submit" class="btn btn-primary">Create stage</button>
29+
<div class="col-sm-offset-3 col-sm-9">
30+
<button type="submit" class="btn btn-primary">Upload</button>
3031
</div>
3132
</div>
3233
</form>
3334

34-
3535
@endsection

‎resources/views/console/field-data/index.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<hr/>
99
<ul class="nav nav-pills">
1010
<div class="btn-group btn-group-lg">
11-
<a href="{{ route('console::field-data::create') }}" class="btn btn-primary-outline">Import new field data</a>
11+
<a href="{{ route('console::field-data::import') }}" class="btn btn-primary-outline">Import new field data</a>
1212
</div>
1313
</ul>
1414

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
@extends('templates/console')
2+
3+
@section('page-title', 'Map imported values / Field data - Forcept Console')
4+
5+
@section('console-content')
6+
7+
<h1 class="p-t">Map imported field data</h1>
8+
<form action="{{ route('console::field-data::map') }}" method="POST">
9+
{!! csrf_field() !!}
10+
<input type="hidden" name="filename" value="{{ $name }}" />
11+
<input type="hidden" name="mode" value="{{ $mode }}" />
12+
<table class="table">
13+
<thead class="thead-inverse">
14+
<tr>
15+
<th>Heading</th>
16+
<th>Map to field...</th>
17+
</tr>
18+
</thead>
19+
<tbody>
20+
@foreach($headings as $index => $heading)
21+
<tr>
22+
<td>
23+
<h4>Column heading: <strong>{{ $heading }}</strong></h4>
24+
<table class="table table-sm">
25+
<thead class="thead-default">
26+
<tr>
27+
@if($index !== 0)
28+
<th>...</th>
29+
@endif
30+
<th>{{ $heading }}</th>
31+
@if($index !== count($headings) - 1)
32+
<th>...</th>
33+
@endif
34+
</tr>
35+
</thead>
36+
<tbody>
37+
@foreach($sheet->take(3)->get([$heading])->toArray() as $column)
38+
<tr>
39+
@if($index !== 0)
40+
<td>...</td>
41+
@endif
42+
<td>{{ $column[$heading] }}</td>
43+
@if($index !== count($headings) - 1)
44+
<td>...</td>
45+
@endif
46+
</tr>
47+
@endforeach
48+
<tr>
49+
@if($index !== 0)
50+
<td></td>
51+
@endif
52+
<td><strong>&plus;{{ $countRows - 3 }} more...</strong></td>
53+
@if($index !== count($headings) - 1)
54+
<td></td>
55+
@endif
56+
</tr>
57+
</tbody>
58+
</table>
59+
</td>
60+
<td>
61+
<input type="hidden" name="heading-{{ $index }}" value="{{ $heading }}" />
62+
<select class="form-control" name="map-{{ $index }}" required>
63+
<?php
64+
echo sprintf("<option value='__nullify__' selected>Don't use '%s'</option>", $heading);
65+
echo "<optgroup label='Actions'>";
66+
echo sprintf("<option value='field_number'>Use '%s' as the searchable field number</option>", $heading);
67+
echo "</optgroup>";
68+
echo "<optgroup label='Map to field:'>";
69+
foreach($fields as $fieldKey => $fieldData) {
70+
echo sprintf("<option value='%s'%s>[%s, %s] %s</option>",
71+
$fieldKey,
72+
$heading === $fieldKey ? " selected" : "",
73+
$fieldKey,
74+
$fieldData['type'],
75+
$fieldData['name']
76+
);
77+
}
78+
echo "</optgroup>";
79+
?>
80+
</select>
81+
</td>
82+
</tr>
83+
@endforeach
84+
</tbody>
85+
</table>
86+
<div class="form-group row">
87+
<button type="submit" class="btn btn-lg btn-primary">Continue &rarr;</button>
88+
</div>
89+
</form>
90+
91+
@endsection

0 commit comments

Comments
 (0)
Please sign in to comment.