Files
timebank-cc-public/app/Http/Livewire/StaticPost.php
Ronald Huynen 2547717edb Initial commit
2026-03-23 21:37:59 +01:00

111 lines
4.1 KiB
PHP

<?php
namespace App\Http\Livewire;
use App\Models\Language;
use App\Models\Post;
use Illuminate\Support\Facades\App;
use Livewire\Component;
class StaticPost extends Component
{
public $type;
public ?int $limit = null;
public bool $hideAuthor = false;
public bool $showFallback = false;
public bool $fallbackExists = false;
public string $imageClass = 'w-full h-auto mb-4';
public ?string $fallbackText = null;
public ?array $versionInfo = null;
public function mount($type, $limit = 1, $hideAuthor = false, $imageClass = 'w-full h-auto mb-4', $fallbackText = null, $versionInfo = null)
{
$this->type = $type;
$this->limit = $limit;
$this->hideAuthor = $hideAuthor ?? false;
$this->imageClass = $imageClass;
$this->fallbackText = $fallbackText;
$this->versionInfo = $versionInfo;
}
public function loadFallback()
{
$this->showFallback = true;
}
public function getPosts($locale)
{
return Post::with([
'category',
'images' => function ($query) {
$query->select('images.id', 'caption', 'path');
},
// Eager load all active translations for the given locale, ordered by most recent
'translations' => function ($query) use ($locale) {
$query->where('locale', 'like', $locale . '%') // Use LIKE for locale flexibility
->whereDate('from', '<=', now())
->where(function ($query) {
$query->whereDate('till', '>', now())->orWhereNull('till');
})
->orderBy('updated_at', 'desc'); // No limit here for robustness
}
])
->whereHas('category', function ($query) {
$query->where('type', $this->type);
})
// Filter for posts that have at least one active translation for the given locale
->whereHas('translations', function ($query) use ($locale) {
$query->where('locale', 'like', $locale . '%') // Use LIKE for locale flexibility
->whereDate('from', '<=', now())
->where(function ($query) {
$query->whereDate('till', '>', now())->orWhereNull('till');
});
})
->orderBy('created_at', 'desc')
->limit($this->limit)
->get();
}
/**
* The render method prepares all data needed by the view.
*/
public function render()
{
$locale = $this->showFallback ? config('app.fallback_locale') : App::getLocale();
$posts = $this->getPosts($locale);
// If no posts are found and we're not already showing the fallback,
// perform an efficient check to see if any fallback content exists.
if ($posts->isEmpty() && !$this->showFallback) {
$fallbackLocale = config('app.fallback_locale');
if (trim(App::getLocale()) !== trim($fallbackLocale)) {
$this->fallbackExists = Post::whereHas('category', function ($query) {
$query->where('type', $this->type);
})
->whereHas('translations', function ($query) use ($fallbackLocale) {
$query->where('locale', 'like', $fallbackLocale . '%')
->whereDate('from', '<=', now())
->where(function ($query) {
$query->whereDate('till', '>', now())->orWhereNull('till');
});
})
->exists();
}
}
$photo = null;
if ($posts->isNotEmpty()) {
$firstPost = $posts->first();
if ($firstPost->hasMedia('*')) {
$photo = $firstPost->getFirstMediaUrl('*');
}
}
return view('livewire.static-post', [
'posts' => $posts,
'photo' => $photo,
'fallbackLocale' => __(Language::where('lang_code', config('app.fallback_locale'))->first()->name ?? 'Fallback Language'),
]);
}
}