73 lines
3.4 KiB
PHP
73 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Services\InvestmentGeography;
|
|
use App\Services\OfferPublicCatalogue;
|
|
use App\Services\OpportunityManagement;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
final class OfferPublicCatalogueController extends Controller
|
|
{
|
|
public function index(Request $request, OfferPublicCatalogue $catalogue): Response
|
|
{
|
|
$filters = $catalogue->filters($request);
|
|
|
|
return $this->uncached(Inertia::render('offers/catalogue', [...$catalogue->page($request, $filters), 'filters' => $filters,
|
|
'mapData' => in_array($filters['view'] ?? '', ['map', 'combined'], true) ? $catalogue->map($filters) : null,
|
|
'territorySelection' => app(InvestmentGeography::class)->selection($filters),
|
|
'references' => ['categories' => OpportunityManagement::CATEGORIES, 'options' => [], 'clusters' => [], 'corridors' => [], 'projects' => [], 'locations' => [], 'sectors' => []]])->toResponse($request));
|
|
}
|
|
|
|
public function show(Request $request, int $id, OfferPublicCatalogue $catalogue): Response
|
|
{
|
|
return $this->uncached(Inertia::render('offers/public-detail', ['offer' => $catalogue->detail($id)])->toResponse($request));
|
|
}
|
|
|
|
public function search(Request $request, OfferPublicCatalogue $catalogue): JsonResponse
|
|
{
|
|
return response()->json($catalogue->page($request, $catalogue->filters($request)))->header('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function detail(int $id, OfferPublicCatalogue $catalogue): JsonResponse
|
|
{
|
|
return response()->json(['data' => $catalogue->detail($id)])->header('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function map(Request $request, OfferPublicCatalogue $catalogue): JsonResponse
|
|
{
|
|
return response()->json($catalogue->map($catalogue->filters($request)))->header('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function export(Request $request, OfferPublicCatalogue $catalogue): StreamedResponse
|
|
{
|
|
$result = $catalogue->records($catalogue->filters($request));
|
|
|
|
return response()->streamDownload(function () use ($result) {
|
|
$stream = fopen('php://output', 'w');
|
|
if ($stream === false) {
|
|
return;
|
|
}
|
|
fwrite($stream, "\xEF\xBB\xBF");
|
|
fputcsv($stream, ['Título', 'Categoria', 'Localização', 'Preço', 'Moeda', 'Publicação', 'URL'], ';', '"', '');
|
|
foreach ($result['records'] as $record) {
|
|
$fields = [$record['title'], $record['category'], implode(' / ', array_column($record['location'], 'name')),
|
|
$record['price']['mode'] === 'negotiable' ? 'A negociar' : $record['price']['amount'], $record['price']['currency'], $record['published_at'], url($record['url'])];
|
|
fputcsv($stream, array_map(fn ($value) => preg_match('/^[\s]*[=+@-]/u', (string) $value) ? "'".$value : $value, $fields), ';', '"', '');
|
|
}
|
|
fclose($stream);
|
|
}, 'ofertas.csv', ['Content-Type' => 'text/csv; charset=UTF-8', 'Cache-Control' => 'private, no-store', 'X-Export-Limit' => '1000', 'X-Results-Truncated' => $result['truncated'] ? 'true' : 'false']);
|
|
}
|
|
|
|
private function uncached(Response $response): Response
|
|
{
|
|
$response->headers->set('Cache-Control', 'private, no-store');
|
|
|
|
return $response;
|
|
}
|
|
}
|