new Entry(), 'section' => $section, 'words' => FormHelpers::getEntryFormWords($section), 'isEdit' => false, 'oldModifications' => old( 'modifications', [] ), 'oldLanguages' => old( 'languages', [] ), 'oldFilesArray' => $this->services->prepareOldFiles( null ) ]; if( $data['words'] === [] ) abort(500); if( section_must_be( 'romhacks', $section ) ){ $data['modifications'] = Modification::orderBy('name')->get(); } if( section_must_be( [ 'romhacks', 'translations' ], $section ) ){ $data['statuses'] = Status::orderBy('id')->get(); } 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() ?? [] ), 'oldLanguages' => old('languages', $entry->languages->pluck('id')->toArray() ?? [] ), 'oldFilesArray' => $this->services->prepareOldFiles( $entry ) ]; if( $data['words'] === [] ) abort(500); if( section_must_be( 'romhacks', $section ) ){ $data['modifications'] = Modification::orderBy('name')->get(); } if( section_must_be( [ 'romhacks', 'translations' ], $section ) ){ $data['statuses'] = Status::orderBy('id')->get(); } return view('submissions.edit', $data); } public function store(StoreEntryRequest $request, string $section){ 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()]); } } public function update(StoreEntryRequest $request, string $section, Entry $entry) { if( $entry->type !== $section ) { abort(404); } $gameId = null; if( !$request->input('game_id') ){ if( $request->input('new-game-title') && $request->input('new-game-platform') && $request->input('new-game-genre') ){ $platform = Platform::find($request->input('new-game-platform')); $genre = Genre::find($request->input('new-game-genre')); $game = Game::create([ 'name' => $request->input('new-game-title'), 'slug' => Str::slug($request->input('new-game-title')), 'platform_id' => $platform->id, 'genre_id' => $genre->id, ]); $gameId = $game->id; } } else { $gameId = $request->input('game_id'); } $mainImage = $entry->main_image; if ( $request->hasFile('main-image') ) { if ( $mainImage ) { Storage::disk('public')->delete($mainImage); } $mainImage = $request->file('main-image')->store('entries/main_images', 'public'); } elseif ( $request->input('remove_main_image') === '1' ) { if ( $mainImage ) { Storage::disk('public')->delete($mainImage); } $mainImage = null; } $staffCredits = collect($request->input('credits', [])) ->filter(fn($item) => isset($item['name']) || isset($item['description'])) ->map(function ($item) { $name = trim($item['name'] ?? ''); $description = trim($item['description'] ?? ''); if ($name === '' && $description === '') { return null; } return trim($name . ($name !== '' && $description !== '' ? ' — ' : '') . $description); }) ->filter() ->implode("\n"); $fields = [ 'type' => $section, 'title' => $request->input('entry_title'), 'slug' => $request->input('slug') ?? Str::slug($request->input('entry_title', '')), 'description' => $request->input('description'), 'main_image' => $mainImage, 'state' => $request->input('submit-state', 'draft'), 'game_id' => $gameId, 'status_id' => $request->input('status'), 'version' => $request->input('version'), 'release_date' => $request->input('release-date'), 'staff_credits' => $staffCredits ?: null, 'relevant_link' => $request->input('release_site'), 'youtube_link' => $request->input('youtube_video'), ]; $entry->update($fields); $entry->hashes()->delete(); foreach ( $request->input('hashes', []) as $hash ) { if( !isset($hash['filename'], $hash['crc32'], $hash['sha1'], $hash['verified']) ) { continue; } EntryHash::create([ 'entry_id' => $entry->id, 'filename' => $hash['filename'], 'hash_crc32' => $hash['crc32'], 'hash_sha1' => $hash['sha1'], 'verified' => $hash['verified'], ]); } $authorIds = []; foreach ( $request->input('authors', []) as $authorId ) { $author = Author::find($authorId); if( $author ) { $authorIds[] = $author->id; } } foreach( $request->input('new-authors', []) as $authorName ) { $authorName = trim($authorName); if ($authorName === '') continue; $author = Author::firstOrCreate( ['slug' => Str::slug($authorName)], ['name' => $authorName], ); $authorIds[] = $author->id; } $entry->authors()->sync(array_values(array_unique($authorIds))); if( section_must_be( 'romhacks', $section ) ){ $entry->modifications()->sync($request->input('modifications', [])); } else { $entry->modifications()->sync([]); } $entry->languages()->sync($request->input('languages', [])); $existingFileUuids = $request->input('existing_file_ids', []); if (!is_array($existingFileUuids)) { $existingFileUuids = []; } $entry->files()->whereNotIn('file_uuid', $existingFileUuids)->delete(); foreach ( $request->input('file_ids', []) as $file_uuid ) { $fileData = Cache::pull("uploaded_file_{$file_uuid}"); if( ! $fileData ) { continue; } EntryFile::create([ 'entry_id' => $entry->id, 'file_uuid' => $fileData['uuid'], 'filename' => $fileData['filename'], 'filepath' => $fileData['filepath'], 'favorite_server' => $fileData['favorite_server'], 'favorite_at' => \DateTimeImmutable::createFromTimestamp( $fileData['favorite_at'] ), 'filesize' => $fileData['filesize'], 'state' => 'public' ]); } $existingGalleryIds = $request->input('existing_gallery_ids', []); if (!is_array($existingGalleryIds)) { $existingGalleryIds = []; } $entry->gallery()->whereNotIn('id', $existingGalleryIds)->get()->each(function ($gallery) { if ($gallery->image) { Storage::disk('public')->delete($gallery->image); } $gallery->delete(); }); foreach ( $request->file('gallery', [] ) as $galleryFile ){ if( !$galleryFile->isValid() ){ continue; } $path = $galleryFile->store('entries/gallery/' . $entry->id, 'public'); EntryGallery::create([ 'entry_id' => $entry->id, 'image' => $path ]); } 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.") }; } }