Skip to content
Merged

Dev #3352

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
39 changes: 20 additions & 19 deletions app/GirlsInDigitalPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,43 +63,44 @@ class GirlsInDigitalPage extends Model
'locale_overrides' => 'array',
];

/**
* Button updates from Nova form; never persisted to DB.
*
* @var array<string, array<string, mixed>>|null
*/
public $nonPersistedButtonUpdates;

/**
* Prevent relationship / virtual attributes from being persisted (e.g. from Nova form).
* Store _button_updates only in nonPersistedButtonUpdates so it never hits the DB.
*/
public function setAttribute($key, $value)
{
if (in_array($key, ['faqItems'], true)) {
return $this;
}
if ($key === '_button_updates') {
$this->nonPersistedButtonUpdates = $value;

return $this;
}
if (str_starts_with($key, 'button_') || str_starts_with($key, 'locale_')) {
return $this;
}

return parent::setAttribute($key, $value);
}

/**
* Used by Nova afterSave; not persisted to DB.
*
* @var array<string, array<string, mixed>>|null
*/
public $nonPersistedButtonUpdates;

/**
* Keep _button_updates out of the SQL UPDATE (used by Nova to pass button data to afterSave).
* So that $model->_button_updates reads back the non-persisted value.
*/
protected static function booted(): void
public function getAttribute($key)
{
static::saving(function (self $model): void {
$model->preserveButtonUpdatesBeforeSave();
});
}

private function preserveButtonUpdatesBeforeSave(): void
{
if (array_key_exists('_button_updates', $this->attributes)) {
$this->nonPersistedButtonUpdates = $this->attributes['_button_updates'];
unset($this->attributes['_button_updates']);
if ($key === '_button_updates') {
return $this->nonPersistedButtonUpdates;
}

return parent::getAttribute($key);
}

public function buttons()
Expand Down
52 changes: 35 additions & 17 deletions app/Nova/GirlsInDigitalPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,22 @@ private function localeField(string $label, string $locale, string $key, bool $t
return $overrides[$locale][$key] ?? '';
});
$field->fillUsing(function ($request, $model, $attribute, $requestAttribute) use ($locale, $key) {
$value = $request->get($requestAttribute);
$overrides = $model->locale_overrides ?? [];
if (! isset($overrides[$locale])) {
$overrides[$locale] = [];
try {
$value = $request->get($requestAttribute);
$overrides = $model->locale_overrides ?? [];
if (! isset($overrides[$locale])) {
$overrides[$locale] = [];
}
$overrides[$locale][$key] = $value;
$model->locale_overrides = $overrides;
} catch (\Throwable $e) {
\Illuminate\Support\Facades\Log::error('GirlsInDigital locale field fill failed: ' . $attribute, [
'exception' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]);
throw $e;
}
$overrides[$locale][$key] = $value;
$model->locale_overrides = $overrides;
});
return $field;
}
Expand Down Expand Up @@ -335,16 +344,25 @@ private function getButtonField(string $key, string $field)

private function fillButtonField(\App\GirlsInDigitalPage $model, string $key, string $field, $value): void
{
if (! isset($model->_button_updates)) {
$model->_button_updates = [];
}
if (! isset($model->_button_updates[$key])) {
$model->_button_updates[$key] = [];
}
if ($field === 'enabled' || $field === 'open_new_tab') {
$value = (bool) $value;
try {
if (! isset($model->_button_updates)) {
$model->_button_updates = [];
}
if (! isset($model->_button_updates[$key])) {
$model->_button_updates[$key] = [];
}
if ($field === 'enabled' || $field === 'open_new_tab') {
$value = (bool) $value;
}
$model->_button_updates[$key][$field] = $value;
} catch (\Throwable $e) {
\Illuminate\Support\Facades\Log::error('GirlsInDigital button field fill failed: ' . $key . '.' . $field, [
'exception' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]);
throw $e;
}
$model->_button_updates[$key][$field] = $value;
}

public static function afterSave(NovaRequest $request, $model): void
Expand All @@ -356,7 +374,7 @@ public static function afterSave(NovaRequest $request, $model): void
if (! Schema::hasTable('girls_in_digital_buttons')) {
return;
}
$updates = $model->nonPersistedButtonUpdates ?? $model->_button_updates ?? null;
$updates = $model->nonPersistedButtonUpdates ?? null;
if (! is_array($updates) || empty($updates)) {
return;
}
Expand All @@ -380,7 +398,7 @@ public static function afterSave(NovaRequest $request, $model): void
report($e);
}
}
unset($model->_button_updates, $model->nonPersistedButtonUpdates);
unset($model->nonPersistedButtonUpdates);
} catch (\Throwable $e) {
report($e);
}
Expand Down
Loading