Initial commit
This commit is contained in:
110
app/Http/Livewire/Welcome/ImageCardFull.php
Normal file
110
app/Http/Livewire/Welcome/ImageCardFull.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Welcome;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Livewire\Component;
|
||||
|
||||
class ImageCardFull extends Component
|
||||
{
|
||||
public $author;
|
||||
public $post = [];
|
||||
public $posts;
|
||||
public $media;
|
||||
public $postNr;
|
||||
public bool $random = false;
|
||||
|
||||
public function mount($postNr = 0, $random = null, Request $request)
|
||||
{
|
||||
$this->postNr = $postNr;
|
||||
|
||||
if ($random) {
|
||||
$this->random = true;
|
||||
}
|
||||
|
||||
// Get image posts from all locations for welcome page
|
||||
$locale = App::getLocale();
|
||||
$categoryTypes = ['App\Models\ImagePost', 'App\Models\ImagePost\\' . $locale];
|
||||
|
||||
$post = Post::with([
|
||||
'postable' => function ($query) {
|
||||
$query->select(['id', 'name']);
|
||||
},
|
||||
'category' => function ($query) use ($categoryTypes) {
|
||||
$query->whereIn('type', $categoryTypes);
|
||||
},
|
||||
'translations' => function ($query) {
|
||||
$query->where('locale', App::getLocale());
|
||||
},
|
||||
'author',
|
||||
'media',
|
||||
])
|
||||
->whereHas('category', function ($query) use ($categoryTypes) {
|
||||
$query->whereIn('type', $categoryTypes);
|
||||
})
|
||||
->whereHas('translations', function ($query) {
|
||||
$query
|
||||
->where('locale', App::getLocale())
|
||||
->whereDate('from', '<=', now())
|
||||
->where(function ($query) {
|
||||
$query->whereDate('till', '>', now())->orWhereNull('till');
|
||||
})
|
||||
->orderBy('updated_at', 'desc');
|
||||
});
|
||||
|
||||
// Apply random or sorted ordering
|
||||
if ($this->random) {
|
||||
$post = $post->inRandomOrder()->get();
|
||||
} else {
|
||||
$post = $post->get()->sortByDesc(function ($query) {
|
||||
if (isset($query->translations)) {
|
||||
return $query->translations->first()->updated_at;
|
||||
};
|
||||
})->values(); // Use values() method to reset the collection keys after sortBy
|
||||
}
|
||||
|
||||
$lastNr = $post->count() - 1;
|
||||
|
||||
if ($postNr > $lastNr || $post->isEmpty()) {
|
||||
$post = null;
|
||||
$this->post = null;
|
||||
} else {
|
||||
$post = $post[$postNr];
|
||||
}
|
||||
|
||||
if ($post && isset($post->translations) && $post->translations->isNotEmpty()) {
|
||||
$translation = $post->translations->first();
|
||||
$this->post = $translation;
|
||||
$this->post['slug'] = $translation->slug;
|
||||
$this->post['from'] = $translation->from;
|
||||
|
||||
$category = Category::find($post->category_id);
|
||||
$categoryTranslation = $category ? $category->translations->where('locale', App::getLocale())->first() : null;
|
||||
$this->post['category'] = $categoryTranslation ? $categoryTranslation->name : '';
|
||||
|
||||
$this->post['author'] = $post->author ? $post->author->name : timebank_config('posts.site-content-writer');
|
||||
$this->post['author_id'] = $post->author ? $post->author->id : null;
|
||||
$this->post['author_profile_photo_path'] = $post->author && $post->author->profile_photo_path ? $post->author->profile_photo_path : null;
|
||||
$this->post['id'] = $post->id;
|
||||
$this->post['model'] = get_class($post);
|
||||
$this->post['like_count'] = $post->like_count ?? 0;
|
||||
|
||||
if ($post->media && $post->media->isNotEmpty()) {
|
||||
$this->media = Post::find($post->id)->getFirstMedia('posts');
|
||||
|
||||
// Get media owner and caption
|
||||
$this->post['media_owner'] = $this->media->getCustomProperty('owner', '');
|
||||
$captionKey = 'caption-' . App::getLocale();
|
||||
$this->post['media_caption'] = $this->media->getCustomProperty($captionKey, '');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.welcome.image-card-full');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user