ldc-groupe/app/Helpers/DocumentHelper.php
2026-02-11 09:15:43 +01:00

102 lines
3.3 KiB
PHP

<?php
namespace App\Helpers;
use App\Enums\Document\DocumentStatusEnum;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
class DocumentHelper
{
/**
* Extract the Issuu embed URL from an iframe or raw Issuu input.
*
* This helper is used when storing a document.
* It allows users to paste either:
* - a full Issuu iframe embed code, or
* - a direct Issuu embed URL
*
* The method normalizes the input by extracting and returning
* the usable Issuu embed URL (`https://e.issuu.com/embed.html?...`)
* that can safely be used on front-end pages.
*
* HTML entities (e.g. `&amp;`) are automatically decoded.
*
* @param string|null $issuIframe Raw Issuu iframe HTML or embed URL.
* @return string|null The normalized Issuu embed URL, or null if none is found.
*/
public static function extractDocumentFromIssuIframe(?string $issuIframe): ?string
{
$content = trim((string) $issuIframe);
if ($content === '') {
return null;
}
if (str_starts_with($content, 'https://e.issuu.com/embed.html')) {
return html_entity_decode($content, ENT_QUOTES);
}
preg_match(
'/src="(https:\/\/e\.issuu\.com\/embed\.html\?[^"]+)"/',
$content,
$matches
);
return isset($matches[1])
? html_entity_decode($matches[1], ENT_QUOTES)
: null;
}
/**
* Return a document query with all the request filters applied
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder $query
*/
public static function getQueryWithFilters(Request $request, Builder $query): \Illuminate\Database\Eloquent\Builder
{
$statusFilter = $request->status;
if ($statusFilter) {
$query->where('status', DocumentStatusEnum::make($statusFilter, 'name')->value);
}
$hasNoLinkFilter = request()->boolean('hasNoLink');
$query->when($hasNoLinkFilter, function ($query) {
$locale = app()->getLocale();
$query->where(function ($q) use ($locale) {
$q->whereNull("issuu_path->$locale")
->whereNull("path->$locale");
});
});
$categoriesFilter = $request->input('categories');
$query->when($categoriesFilter, function ($query, $categoriesFilter) {
$categoryIds = collect(explode(',', $categoriesFilter))
->map(fn($id) => (int) $id)
->filter()
->values();
$query->whereHas('categories', function ($q) use ($categoryIds) {
$q->whereIn('document_categories.id', $categoryIds);
});
});
$tagsFilter = $request->input('tags');
$query->when($tagsFilter, function ($query, $tagsFilter) {
$tagIds = collect(explode(',', $tagsFilter))
->map(fn($id) => (int) $id)
->filter()
->values();
$query->whereHas('tags', function ($q) use ($tagIds) {
$q->whereIn('document_tags.id', $tagIds);
});
});
return $query;
}
}