Files
RomhackPlaza/app/Http/Controllers/EntryController.php

89 lines
2.5 KiB
PHP
Raw Normal View History

2026-05-20 18:25:15 +02:00
<?php
namespace App\Http\Controllers;
2026-05-27 21:24:38 +02:00
use App\Helpers\EntryHelpers;
2026-05-20 18:25:15 +02:00
use App\Models\Entry;
2026-06-16 16:21:43 +02:00
use App\Models\News;
2026-06-24 19:20:33 +02:00
use Artesaos\SEOTools\Facades\SEOTools;
2026-06-02 20:54:10 +02:00
use Illuminate\Support\Facades\Gate;
2026-05-20 18:25:15 +02:00
use Illuminate\Http\Request;
2026-06-24 19:20:33 +02:00
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
2026-05-20 18:25:15 +02:00
use Illuminate\View\View;
class EntryController extends Controller
{
2026-06-16 16:21:43 +02:00
private const SECTION_TYPES = ['translations', 'romhacks', 'homebrew', 'utilities', 'documents', 'lua-scripts'];
2026-05-20 18:25:15 +02:00
public function index(): View
{
2026-06-24 19:20:33 +02:00
SEOTools::setTitle('Database');
return view('entries.index');
2026-05-20 18:25:15 +02:00
}
2026-06-08 16:25:52 +02:00
public function section_redirect(string $section)
{
return redirect( databaseRoute( ['types' => [ $section ] ] ) );
}
2026-05-20 18:25:15 +02:00
public function show(string $section, Entry $entry): View
{
2026-06-24 19:20:33 +02:00
2026-06-02 20:54:10 +02:00
if (!in_array($section, self::SECTION_TYPES))
2026-05-20 18:25:15 +02:00
abort(404);
2026-06-02 20:54:10 +02:00
if ($entry->type !== $section)
2026-05-20 18:25:15 +02:00
abort(404);
2026-06-16 16:21:43 +02:00
if( !\Auth::guest() )
Gate::authorize('viewAny', $entry);
2026-06-02 20:54:10 +02:00
// Permissions.
$entryPolicy = match ($entry->state) {
'pending' => 'viewPending',
'draft' => 'viewDraft',
'rejected' => 'viewRejected',
'hidden' => 'viewHidden',
'locked' => 'viewLocked',
'published' => null,
'default' => null
};
if ($entryPolicy)
Gate::authorize($entryPolicy, $entry);
$comments = EntryHelpers::getLatestComments($entry);
2026-06-23 19:24:38 +02:00
$reviews = $entry->reviews()->orderBy('created_at', 'desc')->limit(10)->get();
2026-05-27 21:24:38 +02:00
2026-06-24 19:20:33 +02:00
SEOTools::setTitle($entry->complete_title ?? $entry->title);
SEOTools::setDescription( Str::limit( $entry->description, 150) );
SEOTools::opengraph()->addProperty('type', 'article');
if( $entry->main_image ){
SEOTools::opengraph()->addImage( url( Storage::url( $entry->main_image ) ) );
}
2026-06-23 19:24:38 +02:00
return view('entries.show', compact('entry', 'section', 'comments', 'reviews'));
2026-05-20 18:25:15 +02:00
}
2026-06-08 16:25:52 +02:00
public function drafts(): View
{
$drafts = Entry::where('user_id', \Auth::user()->user_id )
->where('state', 'draft')
->with('game.platform', 'status')
->orderBy('updated_at', 'desc')
->paginate(20);
2026-06-16 16:21:43 +02:00
$newsDrafts = News::where('user_id', \Auth::user()->user_id )
->where('state', 'draft')
->orderBy('updated_at', 'desc')
->paginate(20);
2026-06-24 19:20:33 +02:00
SEOTools::setTitle('My Drafts');
2026-06-16 16:21:43 +02:00
return view('entries.drafts', compact('drafts', 'newsDrafts'));
2026-06-08 16:25:52 +02:00
}
2026-05-20 18:25:15 +02:00
}