Update Submissions and add fields
This commit is contained in:
@@ -8,15 +8,17 @@ use App\Helpers\XenForoHelpers;
|
||||
use App\Http\Requests\StoreEntryRequest;
|
||||
use App\Jobs\CreateXenForoCommentsThread;
|
||||
use App\Models\Author;
|
||||
use App\Models\Category;
|
||||
use App\Models\Entry;
|
||||
use App\Models\EntryFile;
|
||||
use App\Models\EntryGallery;
|
||||
use App\Models\Gallery;
|
||||
use App\Models\EntryHash;
|
||||
use App\Models\Game;
|
||||
use App\Models\Genre;
|
||||
use App\Models\Language;
|
||||
use App\Models\Modification;
|
||||
use App\Models\Platform;
|
||||
use App\Models\System;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -118,11 +120,7 @@ class SubmissionsService {
|
||||
$entry = DB::transaction(function () use ( $user_id ) {
|
||||
|
||||
// STEP 2 : Create game.
|
||||
|
||||
$gameId = null;
|
||||
if( section_must_be( ['romhacks', 'translations'], $this->section ) ){
|
||||
$gameId = $this->Step2_CreateAndReturnGameId();
|
||||
}
|
||||
$gameId = $this->Step2_CreateAndReturnGameId();
|
||||
|
||||
// STEP 3 : Create Complete title.
|
||||
$completeTitle = $this->Step3_BuildCompleteTitle( $gameId );
|
||||
@@ -132,7 +130,7 @@ class SubmissionsService {
|
||||
|
||||
if( section_must_be( 'translations', $this->section ) &&
|
||||
!$this->request->input('entry_title') ){
|
||||
$entryTitle = Game::find($gameId)->name;
|
||||
$entryTitle = Game::find($gameId)->name ?? "";
|
||||
} else {
|
||||
$entryTitle = $this->request->input('entry_title');
|
||||
}
|
||||
@@ -149,6 +147,7 @@ class SubmissionsService {
|
||||
'main_image' => $this->request->input('main-image'),
|
||||
'state' => $this->request->input('submit-state'),
|
||||
'game_id' => $gameId,
|
||||
'platform_id' => $this->request->input('platform_only_id'),
|
||||
'status_id' => $this->request->input('status'),
|
||||
'version' => $this->request->input('version'),
|
||||
'release_date' => $this->request->input('release-date'),
|
||||
@@ -157,6 +156,7 @@ class SubmissionsService {
|
||||
'youtube_link' => $this->request->input('youtube_video'),
|
||||
'user_id' => $user_id,
|
||||
'complete_title' => $completeTitle,
|
||||
'level_id' => $this->request->input('level')
|
||||
];
|
||||
|
||||
$entry = Entry::create( $fields );
|
||||
@@ -165,19 +165,28 @@ class SubmissionsService {
|
||||
$this->Step7_SaveEntryFiles( $entry );
|
||||
|
||||
// STEP 8 : Save hashes.
|
||||
$this->Step8_SaveHashes( $entry->id );
|
||||
if( section_must_be( ['translations', 'romhacks' ], $this->section ) )
|
||||
$this->Step8_SaveHashes( $entry->id );
|
||||
|
||||
// STEP 9 : Save Authors.
|
||||
$this->Step9_SaveAuthors( $entry );
|
||||
|
||||
// STEP 10 : Save Modifications.
|
||||
if( section_must_be( 'romhacks', $this->section ) ){
|
||||
if( section_must_be( ['romhacks','lua-scripts'], $this->section ) ){
|
||||
$this->Step10_SaveRomhacksModifications( $entry );
|
||||
}
|
||||
if( section_must_be( 'utilities', $this->section ) ){
|
||||
$this->Step10_SaveUtilitiesSystems( $entry );
|
||||
}
|
||||
|
||||
// STEP 11 : Save Languages
|
||||
$this->Step11_SaveLanguages( $entry );
|
||||
|
||||
// STEP 11.5 : Save Categories
|
||||
if( section_must_be( 'utilities', $this->section ) ) {
|
||||
$this->Step11_5_SaveCategories($entry);
|
||||
}
|
||||
|
||||
// STEP 12 : Prepare Gallery images.
|
||||
$this->Step12a_PrepareGalleryImages( $entry );
|
||||
|
||||
@@ -207,6 +216,10 @@ class SubmissionsService {
|
||||
*/
|
||||
private function Step2_CreateAndReturnGameId(): ?int {
|
||||
|
||||
$mode = $this->request->input('game_selection_mode', 'game');
|
||||
if( $mode !== 'game' )
|
||||
return null;
|
||||
|
||||
// Already existing game.
|
||||
if( $this->request->input('game_id') )
|
||||
return $this->request->input('game_id');
|
||||
@@ -261,9 +274,9 @@ class SubmissionsService {
|
||||
if( section_must_be( 'translations', $this->section ) ) {
|
||||
$fields['languages_string'] = Language::whereIn('id', $this->request->input('languages', []))->pluck('name')->implode(', ');
|
||||
}
|
||||
if( section_must_be(['romhacks', 'translations', 'homebrew', 'lua-scripts', 'tutorials'], $this->section ) ) {
|
||||
if( section_must_be(['romhacks', 'translations', 'homebrew', 'lua-scripts'], $this->section ) ) {
|
||||
// TODO: Add single platform ID compatibility.
|
||||
$fields['platform_name'] = $gameId ? Game::find( $gameId )->platform->name : null;
|
||||
$fields['platform_name'] = $gameId ? Game::find( $gameId )->platform->name : Platform::find( $this->request->input('platform_only_id') )?->name ?? null;
|
||||
}
|
||||
|
||||
return EntryHelpers::buildCompleteTitle( $this->section, $fields );
|
||||
@@ -381,6 +394,24 @@ class SubmissionsService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Entry $entry
|
||||
*
|
||||
* @return void
|
||||
* @throws SubmissionException
|
||||
*/
|
||||
private function Step10_SaveUtilitiesSystems( Entry $entry ): void
|
||||
{
|
||||
// TODO: Replace by edit version
|
||||
|
||||
foreach ( $this->request->input('systems', [] ) ?? [] as $systemId ) {
|
||||
$system = System::find( $systemId );
|
||||
if( !$system )
|
||||
throw new SubmissionException( "System {$systemId} does not exist." );
|
||||
$entry->systems()->attach( $system->id );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Entry $entry
|
||||
*
|
||||
@@ -400,11 +431,29 @@ class SubmissionsService {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Entry $entry
|
||||
*
|
||||
* @return void
|
||||
* @throws SubmissionException
|
||||
*/
|
||||
private function Step11_5_SaveCategories( Entry $entry ): void
|
||||
{
|
||||
// TODO: Replace by edit version.
|
||||
|
||||
foreach ( $this->request->input('categories', [] ) ?? [] as $categoryId ) {
|
||||
$category = Category::find( $categoryId );
|
||||
if( !$category )
|
||||
throw new SubmissionException( "Category {$categoryId} does not exist." );
|
||||
$entry->categories()->attach( $category->id );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function Step12a_PrepareGalleryImages( Entry $entry ): void
|
||||
{
|
||||
foreach ( $this->request->input('gallery', [] ) ?? [] as $imagePath ) {
|
||||
EntryGallery::create([
|
||||
'entry_id' => $entry->id,
|
||||
$entry->gallery()->create([
|
||||
'image' => $imagePath,
|
||||
]);
|
||||
}
|
||||
@@ -464,9 +513,7 @@ class SubmissionsService {
|
||||
|
||||
// STEP 2: Create game if different.
|
||||
$gameId = null;
|
||||
if( section_must_be( ['romhacks', 'translations' ], $this->section ) ){
|
||||
$gameId = $this->eStep2_VerifyCreateAndEditGameId();
|
||||
}
|
||||
$gameId = $this->eStep2_VerifyCreateAndEditGameId();
|
||||
|
||||
// STEP 3: Recreate complete title and refresh slug if needed.
|
||||
$completeTitle = $this->Step3_BuildCompleteTitle( $gameId );
|
||||
@@ -494,6 +541,7 @@ class SubmissionsService {
|
||||
'main_image' => $this->request->input('main-image'),
|
||||
'state' => $this->request->input('submit-state'),
|
||||
'game_id' => $gameId,
|
||||
'platform_id' => $this->request->input('platform_only_id'),
|
||||
'status_id' => $this->request->input('status'),
|
||||
'version' => $this->request->input('version'),
|
||||
'release_date' => $this->request->input('release-date'),
|
||||
@@ -502,12 +550,13 @@ class SubmissionsService {
|
||||
'youtube_link' => $this->request->input('youtube_video'),
|
||||
'user_id' => $user_id,
|
||||
'complete_title' => $completeTitle,
|
||||
'comments_thread_id' => $this->request->input('comments_thread_id'),
|
||||
'featured' => $this->request->input('featured') ?? false,
|
||||
'level_id' => $this->request->input('level'),
|
||||
];
|
||||
|
||||
if( \Auth::user()->can('moderate', $this->entry) ){
|
||||
$fields['staff_comment'] = $this->request->input('staff_comment');
|
||||
$fields['featured'] = $this->request->input('featured') ?? false;
|
||||
$fields['comments_thread_id'] = $this->request->input('comments_thread_id');
|
||||
}
|
||||
|
||||
$this->entry->update( $fields );
|
||||
@@ -516,19 +565,27 @@ class SubmissionsService {
|
||||
$this->eStep6_UpdateEntryFiles( $this->entry->id );
|
||||
|
||||
// STEP 7: Update hashes.
|
||||
$this->eStep7_UpdateHashes( $this->entry->id );
|
||||
if( section_must_be( ['translations', 'romhacks' ], $this->section ) )
|
||||
$this->eStep7_UpdateHashes( $this->entry->id );
|
||||
|
||||
// STEP 8: Update Authors.
|
||||
$this->eStep8_UpdateAuthors();
|
||||
|
||||
// STEP 9: Update romhacks modifications.
|
||||
if( section_must_be( 'romhacks', $this->section ) ) {
|
||||
if( section_must_be( ['romhacks', 'lua-scripts'], $this->section ) ) {
|
||||
$this->eStep9_UpdateRomhacksModifications();
|
||||
}
|
||||
if( section_must_be( 'utilities', $this->section ) ) {
|
||||
$this->eStep9_UpdateUtilitiesSystems();
|
||||
}
|
||||
|
||||
// STEP 10: Update Languages.
|
||||
$this->eStep10_UpdateLanguages();
|
||||
|
||||
// STEP 10.5 : Update categories
|
||||
if( section_must_be( 'utilities', $this->section ) )
|
||||
$this->eStep10_5_UpdateCategories();
|
||||
|
||||
// STEP 11: Prepare new gallery images and prepare deletion of others ones.
|
||||
$galleryPaths = $this->eStep11a_UpdateGalleryImages();
|
||||
|
||||
@@ -558,8 +615,14 @@ class SubmissionsService {
|
||||
/**
|
||||
* @throws SubmissionException
|
||||
*/
|
||||
private function eStep2_VerifyCreateAndEditGameId(): int
|
||||
private function eStep2_VerifyCreateAndEditGameId(): ?int
|
||||
{
|
||||
|
||||
$mode = $this->request->input('game_selection_mode', 'game');
|
||||
if ($mode !== 'game') {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Already existing game.
|
||||
if( $this->request->input('game_id') ){
|
||||
|
||||
@@ -721,6 +784,27 @@ class SubmissionsService {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws SubmissionException
|
||||
*/
|
||||
private function eStep9_UpdateUtilitiesSystems(): void
|
||||
{
|
||||
$requestSystems = $this->request->input('systems', [] ) ?? [];
|
||||
if( !empty( $requestSystems ) ){
|
||||
$valid = System::whereIn( 'id', $requestSystems )->pluck('id')->toArray();
|
||||
|
||||
if( count( $valid ) !== count( $requestSystems ) ){
|
||||
throw new SubmissionException( "One of the systems doesn't exist." );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
$this->entry->systems()->sync( $requestSystems );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws SubmissionException
|
||||
@@ -739,6 +823,24 @@ class SubmissionsService {
|
||||
$this->entry->languages()->sync( $requestLanguages );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws SubmissionException
|
||||
*/
|
||||
private function eStep10_5_UpdateCategories(): void
|
||||
{
|
||||
$requestCategories = $this->request->input('categories', [] ) ?? [];
|
||||
if( !empty( $requestCategories ) ){
|
||||
$valid = Category::whereIn( 'id', $requestCategories )->pluck('id')->toArray();
|
||||
if( count( $valid ) !== count( $requestCategories ) ){
|
||||
throw new SubmissionException( "One of the categories doesn't exist." );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$this->entry->categories()->sync( $requestCategories );
|
||||
}
|
||||
|
||||
private function eStep11a_UpdateGalleryImages(): array
|
||||
{
|
||||
$requestGallery = $this->request->input('gallery', [] ) ?? [];
|
||||
@@ -747,14 +849,13 @@ class SubmissionsService {
|
||||
$needDeletion = array_diff( $existingGalleryPaths, $requestGallery );
|
||||
|
||||
if( !empty( $needDeletion ) ){
|
||||
EntryGallery::where('entry_id', $this->entry->id)->whereIn('image', $needDeletion )->delete();
|
||||
$this->entry->gallery()->whereIn('image', $needDeletion )->delete();
|
||||
}
|
||||
|
||||
$needAddition = array_diff( $requestGallery, $existingGalleryPaths );
|
||||
$images = [];
|
||||
foreach( $needAddition as $imagePath ){
|
||||
$images[] = EntryGallery::create([
|
||||
'entry_id' => $this->entry->id,
|
||||
$images[] = $this->entry->gallery()->create([
|
||||
'image' => $imagePath,
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user