2021-09-10 15:43:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2021-09-14 10:58:00 +02:00
|
|
|
use App\Http\Requests\QuestionExport;
|
|
|
|
|
use App\Models\Brand;
|
2021-09-10 15:43:27 +02:00
|
|
|
use App\Models\Question;
|
2021-09-14 10:58:00 +02:00
|
|
|
use Carbon\Carbon;
|
2021-09-10 15:43:27 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2021-09-14 10:58:00 +02:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Str;
|
2021-09-10 15:43:27 +02:00
|
|
|
use Kwaadpepper\CrudPolicies\Http\Requests\CrudIndexRequest;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Traits\CrudController;
|
2021-09-14 10:58:00 +02:00
|
|
|
use League\Csv\Writer;
|
|
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
2021-09-10 15:43:27 +02:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
*/
|
2021-09-10 15:43:27 +02:00
|
|
|
class QuestionController extends Controller
|
|
|
|
|
{
|
|
|
|
|
use CrudController {
|
|
|
|
|
index as crudIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var Question */
|
|
|
|
|
public static $modelClass = Question::class;
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* The Bo app layout
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-09-10 15:43:27 +02:00
|
|
|
public static $layout = 'layouts.app';
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Event index crud-policies
|
|
|
|
|
*
|
|
|
|
|
* @param Builder $query
|
|
|
|
|
* @return Builder
|
|
|
|
|
*/
|
|
|
|
|
public function indexQuery(Builder $query): Builder
|
|
|
|
|
{
|
|
|
|
|
if (request()->brand) {
|
|
|
|
|
$query = $query->where('brand_id', '=', request()->brand);
|
|
|
|
|
}
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Overload crud-policies index
|
|
|
|
|
*
|
|
|
|
|
* @param CrudIndexRequest $request
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2021-09-10 15:43:27 +02:00
|
|
|
public function index(CrudIndexRequest $request)
|
|
|
|
|
{
|
|
|
|
|
/** @var \Illuminate\View\View */
|
|
|
|
|
$view = $this->crudIndex($request);
|
|
|
|
|
if ($request->ajax()) {
|
|
|
|
|
return $view;
|
|
|
|
|
}
|
|
|
|
|
return view('question.index', $view->getData());
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Shows the question export form
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2021-09-14 10:58:00 +02:00
|
|
|
public function export()
|
|
|
|
|
{
|
|
|
|
|
$brands = Brand::all();
|
|
|
|
|
return view('question.export', compact('brands'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Exports questions as csv the requested export type
|
|
|
|
|
*
|
|
|
|
|
* @param QuestionExport $request
|
|
|
|
|
* @return \Symfony\Component\HttpFoundation\StreamedResponse
|
|
|
|
|
*/
|
2021-09-14 10:58:00 +02:00
|
|
|
public function exportSubmit(QuestionExport $request)
|
|
|
|
|
{
|
|
|
|
|
$results = [];
|
|
|
|
|
$brand = Brand::findOrFail($request->brand);
|
|
|
|
|
$questions = $brand->questions->load('answers');
|
|
|
|
|
|
|
|
|
|
foreach ($questions as $question) {
|
|
|
|
|
$answersCounts = DB::query()
|
|
|
|
|
->selectRaw('count(*) as total, answer_id')
|
|
|
|
|
->from('customer_answer')
|
|
|
|
|
->whereIn('answer_id', $question->answers->pluck('id'))
|
|
|
|
|
->groupBy('answer_id')
|
|
|
|
|
->get()->keyBy('answer_id');
|
|
|
|
|
|
|
|
|
|
$results[$question->id] = [
|
|
|
|
|
'question' => $question->question,
|
|
|
|
|
'answers' => []
|
|
|
|
|
];
|
|
|
|
|
foreach ($question->answers as $answer) {
|
|
|
|
|
$results[$question->id]['answers'][] =
|
|
|
|
|
[
|
|
|
|
|
"$answer->answer ",
|
|
|
|
|
$answersCounts[$answer->id]->total ?? 0
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$w = Writer::createFromString('');
|
|
|
|
|
$w->setDelimiter(';');
|
|
|
|
|
$w->setEnclosure('"');
|
|
|
|
|
$w->setOutputBOM(Writer::BOM_UTF8);
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// * We send the CSV using Symfony StreamedResponse.
|
2021-09-14 10:58:00 +02:00
|
|
|
$response = new StreamedResponse();
|
|
|
|
|
$response->headers->set('Content-Encoding', 'none');
|
|
|
|
|
$response->headers->set('Content-Type', 'text/csv; charset=UTF-8');
|
|
|
|
|
$response->headers->set('Description', 'File Transfer');
|
|
|
|
|
$response->headers->set(
|
|
|
|
|
'Content-Disposition',
|
|
|
|
|
sprintf(
|
|
|
|
|
'attachment; filename="Export pour newsletter %s %s.csv"',
|
|
|
|
|
Carbon::now()->format('d-m-Y_H-i'),
|
|
|
|
|
Str::slug($brand->name)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach ($results as $result) {
|
|
|
|
|
$w->insertOne([
|
|
|
|
|
'question' => $result['question'],
|
|
|
|
|
]);
|
|
|
|
|
foreach ($result['answers'] as $answer) {
|
|
|
|
|
$w->insertOne($answer);
|
|
|
|
|
}
|
|
|
|
|
$w->insertOne([]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/*
|
|
|
|
|
We create a callable to output the CSV in chunk
|
|
|
|
|
with Symfony StreamResponse you can flush the body content if necessary
|
|
|
|
|
see Symfony documentation for more information
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// * The flush value should depend on your CSV size.
|
|
|
|
|
$flush_threshold = 1000;
|
|
|
|
|
|
2021-09-14 10:58:00 +02:00
|
|
|
$response->setCallback(function () use ($w, $flush_threshold) {
|
|
|
|
|
foreach ($w->chunk(1024) as $offset => $chunk) {
|
|
|
|
|
echo $chunk;
|
|
|
|
|
if ($offset % $flush_threshold === 0) {
|
|
|
|
|
flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
2021-09-10 15:43:27 +02:00
|
|
|
}
|