Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync achievement title/description/badge to active event achievements #3181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/Filament/Resources/AchievementResource/Pages/Edit.php
Copy link
Member

@wescopeland wescopeland Feb 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionally the code works great. My only concern is everything within the conditionals is duplicative. Seems like it would be easy to change it in one place and forget to change it in the other.

I'm wondering if it'd be best to extract it to either a method on the model or an action, eg:

if ($record->isDirty(['BadgeName', 'Title', 'Description'])) {
    /**
     * reusable code
     * $record->syncMetadataWithEventAchievements()?
     * (new SyncMetadataWithEventAchievementsAction())->execute(...)?
     */
}
$shouldResyncEventAchievementMetadata = (
    in_array('badge', $fields)
    || in_array('title', $fields)
    || in_array('description', $fields)
);
if ($shouldResyncEventAchievementMetadata) {
    /**
     * reusable code
     * $record->syncMetadataWithEventAchievements()?
     * (new SyncMetadataWithEventAchievementsAction())->execute(...)?
     */
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use App\Filament\Enums\ImageUploadType;
use App\Filament\Resources\AchievementResource;
use App\Models\Achievement;
use App\Models\EventAchievement;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Database\Eloquent\Model;

class Edit extends EditRecord
{
Expand Down Expand Up @@ -56,4 +58,34 @@ protected function mutateFormDataBeforeSave(array $data): array

return $data;
}

protected function handleRecordUpdate(Model $record, array $data): Model
{
$record->fill($data);

// copy the updated badge/title/description to any active event achievements referencing it.
if ($record->isDirty('BadgeName')
|| $record->isDirty('Title')
|| $record->isDirty('Description')) {

/** @var Achievement $achievement */
$achievement = $record;

$eventAchievements = EventAchievement::with(['achievement'])
->where('source_achievement_id', $achievement->id)
->active()
->get();

foreach ($eventAchievements as $eventAchievement) {
$eventAchievement->achievement->BadgeName = $achievement->BadgeName;
$eventAchievement->achievement->Title = $achievement->Title;
$eventAchievement->achievement->Description = $achievement->Description;
$eventAchievement->achievement->save();
}
}

$record->save();

return $record;
}
}
19 changes: 19 additions & 0 deletions app/Helpers/database/achievement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use App\Community\Enums\ArticleType;
use App\Enums\Permissions;
use App\Models\Achievement;
use App\Models\EventAchievement;
use App\Models\User;
use App\Platform\Actions\SyncAchievementSetOrderColumnsFromDisplayOrdersAction;
use App\Platform\Actions\UpsertTriggerVersionAction;
Expand Down Expand Up @@ -448,6 +449,24 @@ function UploadNewAchievement(
);
}
}

// copy the updated badge/title/description to any active event achievements referencing it.
if (in_array('badge', $fields)
|| in_array('title', $fields)
|| in_array('description', $fields)) {

$eventAchievements = EventAchievement::with(['achievement'])
->where('source_achievement_id', $achievement->id)
->active()
->get();

foreach ($eventAchievements as $eventAchievement) {
$eventAchievement->achievement->BadgeName = $badge;
$eventAchievement->achievement->Title = $title;
$eventAchievement->achievement->Description = $desc;
$eventAchievement->achievement->save();
}
}
}

return true;
Expand Down