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

160 lines
6.1 KiB
PHP
Raw Normal View History

2026-05-20 18:25:15 +02:00
<?php
namespace App\Http\Controllers;
use App\Exceptions\SubmissionException;
use App\Helpers\FormHelpers;
2026-06-08 16:25:52 +02:00
use App\Http\Requests\StoreDraftRequest;
2026-05-20 18:25:15 +02:00
use App\Http\Requests\StoreEntryRequest;
2026-06-08 16:25:52 +02:00
use App\Jobs\DeleteXenForoCommentsThread;
2026-05-20 18:25:15 +02:00
use App\Models\Author;
use App\Models\Entry;
use App\Models\EntryFile;
2026-06-10 11:04:26 +02:00
use App\Models\Gallery;
2026-05-20 18:25:15 +02:00
use App\Models\EntryHash;
use App\Models\Game;
use App\Models\Genre;
use App\Models\Language;
2026-06-10 11:04:26 +02:00
use App\Models\Level;
2026-05-20 18:25:15 +02:00
use App\Models\Modification;
use App\Models\Platform;
use App\Models\Status;
2026-06-10 11:04:26 +02:00
use App\Models\System;
2026-05-20 18:25:15 +02:00
use App\Services\SubmissionsService;
2026-06-08 16:25:52 +02:00
use App\Services\XenforoApiService;
2026-05-20 18:25:15 +02:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\View\View;
class SubmissionController extends Controller
{
public function __construct(private SubmissionsService $services){}
public function create(Request $request, string $section)
{
$data = [
'entry' => new Entry(),
'section' => $section,
'words' => FormHelpers::getEntryFormWords($section),
'isEdit' => false,
'oldModifications' => old( 'modifications', [] ),
2026-06-10 11:04:26 +02:00
'oldSystems' => old( 'systems', [] ),
2026-05-20 18:25:15 +02:00
'oldLanguages' => old( 'languages', [] ),
2026-06-10 11:04:26 +02:00
'oldCategories' => old( 'categories', [] ),
2026-05-20 18:25:15 +02:00
'oldFilesArray' => $this->services->prepareOldFiles( null )
];
if( $data['words'] === [] )
abort(500);
2026-06-10 11:04:26 +02:00
if( section_must_be( ['romhacks', 'lua-scripts'], $section ) ){
2026-05-20 18:25:15 +02:00
$data['modifications'] = Modification::orderBy('name')->get();
}
2026-06-10 11:04:26 +02:00
if( section_must_be( [ 'romhacks', 'translations', 'homebrew', 'lua-scripts' ], $section ) ){
2026-05-20 18:25:15 +02:00
$data['statuses'] = Status::orderBy('id')->get();
}
2026-06-10 11:04:26 +02:00
if( section_must_be( 'utilities' , $section ) ){
$data['systems'] = System::orderBy('name')->get();
}
if( section_must_be( [ 'utilities', 'documents' ], $section ) ) {
$data['levels'] = Level::orderBy('id')->get();
}
2026-05-20 18:25:15 +02:00
return view('submissions.create', $data);
}
public function edit(Request $request, string $section, Entry $entry){
if( $entry->type !== $section )
abort(404);
$data = [
'entry' => $entry,
'section' => $section,
'words' => FormHelpers::getEntryFormWords($section),
'isEdit' => true,
'oldModifications' => old('modifications', $entry->modifications->pluck('id')->toArray() ?? [] ),
2026-06-10 11:04:26 +02:00
'oldSystems' => old( 'systems', $entry->systems->pluck('id')->toArray() ?? [] ),
2026-05-20 18:25:15 +02:00
'oldLanguages' => old('languages', $entry->languages->pluck('id')->toArray() ?? [] ),
2026-06-10 11:04:26 +02:00
'oldCategories' => old('categories', $entry->categories->pluck('id')->toArray() ?? [] ),
2026-05-20 18:25:15 +02:00
'oldFilesArray' => $this->services->prepareOldFiles( $entry )
];
if( $data['words'] === [] )
abort(500);
2026-06-10 11:04:26 +02:00
if( section_must_be( [ 'romhacks', 'lua-scripts' ], $section ) ){
2026-05-20 18:25:15 +02:00
$data['modifications'] = Modification::orderBy('name')->get();
}
2026-06-10 11:04:26 +02:00
if( section_must_be( [ 'romhacks', 'translations', 'homebrew', 'lua-scripts' ], $section ) ){
2026-05-20 18:25:15 +02:00
$data['statuses'] = Status::orderBy('id')->get();
}
2026-06-10 11:04:26 +02:00
if( section_must_be( 'utilities' , $section ) ){
$data['systems'] = System::orderBy('name')->get();
}
if( section_must_be( [ 'utilities', 'documents' ], $section ) ) {
$data['levels'] = Level::orderBy('id')->get();
}
2026-05-20 18:25:15 +02:00
return view('submissions.edit', $data);
}
2026-06-08 16:25:52 +02:00
public function destroy(Request $request, string $section, Entry $entry)
{
if( $entry->type !== $section )
abort(404);
if( $entry->comments_thread_id)
DeleteXenForoCommentsThread::dispatch( $entry->comments_thread_id );
$entry->delete();
return redirect( route('entries.index') )->with('success', "Entry successfully deleted.");
}
public function store(Request $request, string $section){
$request = $request->input('submit-state') === 'draft' ? app(StoreDraftRequest::class) : app(StoreEntryRequest::class);
$request->validateResolved();
2026-05-20 18:25:15 +02:00
try {
$entry = $this->services->storeEntry($request, $section);
return match ($entry->state) {
'published' => redirect()->route('entries.show', ['section' => $section, 'entry' => $entry->slug])->with('success', "Your entry has been published."),
'pending' => redirect()->route('home')->with('success', "Your entry has been submitted and is pending review."),
default => redirect()->route('home')->with('success', "Your entry has been saved as a draft.")
};
} catch ( SubmissionException $e ) {
return back()->withInput()->withErrors(['error' => $e->getMessage()]);
} catch ( \Exception $e ) {
return back()->withInput()->withErrors(['error' => 'Unknown error: '.$e->getMessage()]);
}
}
2026-06-08 16:25:52 +02:00
public function update(Request $request, string $section, Entry $entry)
2026-05-20 18:25:15 +02:00
{
2026-06-08 16:25:52 +02:00
$request = $request->input('submit-state') === 'draft' ? app(StoreDraftRequest::class) : app(StoreEntryRequest::class);
$request->validateResolved();
2026-05-27 21:24:38 +02:00
try {
$entry = $this->services->editEntry($request, $section, $entry);
2026-05-20 18:25:15 +02:00
2026-05-27 21:24:38 +02:00
return match ($entry->state) {
'published' => redirect()->route('entries.show', ['section' => $section, 'entry' => $entry->slug])->with('success', "Your entry has been published."),
'pending' => redirect()->route('home')->with('success', "Your entry has been submitted and is pending review."),
default => redirect()->route('home')->with('success', "Your entry has been saved as a draft.")
};
} catch ( SubmissionException $e ) {
return back()->withInput()->withErrors(['error' => $e->getMessage()]);
} catch ( \Exception $e ) {
return back()->withInput()->withErrors(['error' => 'Unknown error: '.$e->getMessage()]);
2026-05-20 18:25:15 +02:00
}
}
}