Add export to CSV button. Add method to controller that uploads a csv file and outputs a csv report of the results. #249

This commit is contained in:
Korina Cordero 2019-08-15 05:52:29 +00:00
parent 74e1e329a7
commit 498b61be48
3 changed files with 54 additions and 4 deletions

View file

@ -37,3 +37,8 @@ rep_popapp_comp_submit:
path: /report/popapp_comparison
controller: App\Controller\ReportController::popappComparisonSubmit
methods: [POST]
rep_popapp_export_csv:
path: /report/popapp_export
controller: App\Controller\ReportController::popappExportCSV
methods: [POST]

View file

@ -439,6 +439,49 @@ class ReportController extends Controller
return $this->render('report/popapp/form.html.twig', $params);
}
/**
* @Menu(selected="outlet_list")
*/
public function popappExportCSV(Request $req)
{
// retrieve temporary info for file
$file = $req->files->get('csv_file');
// process the csv file
$data = $this->processPopappFile($file);
$resp = new StreamedResponse();
$resp->setCallback(function() use ($data) {
// csv output
$csv_handle = fopen('php://output', 'w+');
fputcsv($csv_handle, [
'Customer ID',
'Last Name',
'First Name',
'Plate Number',
'Serial',
'Warranty Create Date',
'Activation Status',
'Has Mobile App?',
]);
foreach ($data as $row)
{
fputcsv($csv_handle, $row);
}
fclose($csv_handle);
});
$filename = 'popapp_comparison_report' . '.csv';
$resp->setStatusCode(200);
$resp->headers->set('Content-Type', 'text/csv; charset=utf-8');
$resp->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
return $resp;
}
protected function processPopappFile(UploadedFile $csv_file)
{
// attempt to open file
@ -507,8 +550,8 @@ class ReportController extends Controller
'plate_num' => $cv->getPlateNumber(),
'serial' => $warranty->getSerial(),
'warr_date_create' => $warranty->getDateCreate()->format("d M Y"),
'has_mobile' => $has_mobile,
'warr_activation_status' => $warranty->isActivated(),
'warr_activation_status' => ($warranty->isActivated() ? 'Active' : 'Inactive'),
'has_mobile' => ($has_mobile ? 'Yes' : 'No'),
];
}
@ -517,4 +560,5 @@ class ReportController extends Controller
return $results;
}
}

View file

@ -39,6 +39,7 @@
<div class="row">
<div class="col-lg-12">
<button type="submit" class="btn btn-success">Submit</button>
<button type="submit" formaction="{{ url('rep_popapp_export_csv')}}" class="btn btn-success">Export to CSV</button>
</div>
</div>
</div>
@ -96,8 +97,8 @@
<td>{{ result.plate_num|default('') }}</td>
<td>{{ result.serial|default('') }}</td>
<td>{{ result.warr_date_create|default('') }}</td>
<td>{{ result.warr_activation_status ? 'Active' : 'Inactive' }}</td>
<td>{{ result.has_mobile ? 'Yes' : 'No' }} </td>
<td>{{ result.warr_activation_status }}</td>
<td>{{ result.has_mobile }} </td>
</tr>
{% endfor %}
</tbody>