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:
parent
74e1e329a7
commit
498b61be48
3 changed files with 54 additions and 4 deletions
|
|
@ -37,3 +37,8 @@ rep_popapp_comp_submit:
|
||||||
path: /report/popapp_comparison
|
path: /report/popapp_comparison
|
||||||
controller: App\Controller\ReportController::popappComparisonSubmit
|
controller: App\Controller\ReportController::popappComparisonSubmit
|
||||||
methods: [POST]
|
methods: [POST]
|
||||||
|
|
||||||
|
rep_popapp_export_csv:
|
||||||
|
path: /report/popapp_export
|
||||||
|
controller: App\Controller\ReportController::popappExportCSV
|
||||||
|
methods: [POST]
|
||||||
|
|
|
||||||
|
|
@ -439,6 +439,49 @@ class ReportController extends Controller
|
||||||
return $this->render('report/popapp/form.html.twig', $params);
|
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)
|
protected function processPopappFile(UploadedFile $csv_file)
|
||||||
{
|
{
|
||||||
// attempt to open file
|
// attempt to open file
|
||||||
|
|
@ -507,8 +550,8 @@ class ReportController extends Controller
|
||||||
'plate_num' => $cv->getPlateNumber(),
|
'plate_num' => $cv->getPlateNumber(),
|
||||||
'serial' => $warranty->getSerial(),
|
'serial' => $warranty->getSerial(),
|
||||||
'warr_date_create' => $warranty->getDateCreate()->format("d M Y"),
|
'warr_date_create' => $warranty->getDateCreate()->format("d M Y"),
|
||||||
'has_mobile' => $has_mobile,
|
'warr_activation_status' => ($warranty->isActivated() ? 'Active' : 'Inactive'),
|
||||||
'warr_activation_status' => $warranty->isActivated(),
|
'has_mobile' => ($has_mobile ? 'Yes' : 'No'),
|
||||||
];
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -517,4 +560,5 @@ class ReportController extends Controller
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-12">
|
<div class="col-lg-12">
|
||||||
<button type="submit" class="btn btn-success">Submit</button>
|
<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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -96,8 +97,8 @@
|
||||||
<td>{{ result.plate_num|default('') }}</td>
|
<td>{{ result.plate_num|default('') }}</td>
|
||||||
<td>{{ result.serial|default('') }}</td>
|
<td>{{ result.serial|default('') }}</td>
|
||||||
<td>{{ result.warr_date_create|default('') }}</td>
|
<td>{{ result.warr_date_create|default('') }}</td>
|
||||||
<td>{{ result.warr_activation_status ? 'Active' : 'Inactive' }}</td>
|
<td>{{ result.warr_activation_status }}</td>
|
||||||
<td>{{ result.has_mobile ? 'Yes' : 'No' }} </td>
|
<td>{{ result.has_mobile }} </td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue