Skip to content
Merged

Dev #3326

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
31 changes: 31 additions & 0 deletions app/GirlsInDigitalButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class GirlsInDigitalButton extends Model
{
protected $table = 'girls_in_digital_buttons';

protected $fillable = [
'page_id',
'key',
'label',
'url',
'open_new_tab',
'enabled',
'position',
];

protected $casts = [
'open_new_tab' => 'boolean',
'enabled' => 'boolean',
'position' => 'integer',
];

public function page()
{
return $this->belongsTo(GirlsInDigitalPage::class, 'page_id');
}
}
115 changes: 88 additions & 27 deletions app/GirlsInDigitalPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,67 @@ class GirlsInDigitalPage extends Model
{
protected $table = 'girls_in_digital_page';

/** Content key => DB column (when different). */
protected static $contentKeyToColumn = [
'landing_header' => 'hero_intro',
'about_girls_title' => 'about_title',
'about_girls_description_1' => 'about_description_1',
'about_girls_description_2' => 'about_description_2',
];

protected $fillable = [
'use_dynamic_content',
'hero_dynamic',
'about_dynamic',
'resources_dynamic',
'matters_dynamic',
'faq_dynamic',
'hero_intro',
'hero_video_url',
'hero_image',
'about_title',
'about_description_1',
'about_description_2',
'about_image',
'resource_title',
'resource_person_title',
'resource_person_description_1',
'resource_person_description_2',
'resource_educator_title',
'resource_educator_description',
'buttons',
'resource_young_image',
'resource_educator_image',
'matters_title',
'matters_graph1_image',
'matters_graph1_link',
'matters_graph1_caption',
'matters_graph2_image',
'matters_graph2_link',
'matters_graph2_caption',
'matters_graph3_image',
'matters_graph3_link',
'matters_graph3_caption',
'matters_paragraph_1',
'matters_paragraph_2',
'faq_title',
'faq_items',
'locale_overrides',
];

protected $casts = [
'use_dynamic_content' => 'boolean',
'buttons' => 'array',
'hero_dynamic' => 'boolean',
'about_dynamic' => 'boolean',
'resources_dynamic' => 'boolean',
'matters_dynamic' => 'boolean',
'faq_dynamic' => 'boolean',
'faq_items' => 'array',
'locale_overrides' => 'array',
];

public function buttons()
{
return $this->hasMany(GirlsInDigitalButton::class, 'page_id')->orderBy('position');
}

/**
* Get the singleton page config (id = 1). Create if missing.
*/
Expand Down Expand Up @@ -60,9 +98,24 @@ public static function contentKeys(): array
'resource_person_description_2',
'resource_educator_title',
'resource_educator_description',
'matters_title',
'matters_graph1_caption',
'matters_graph2_caption',
'matters_graph3_caption',
'matters_paragraph_1',
'matters_paragraph_2',
'faq_title',
];
}

/**
* Resolve content key to DB column name.
*/
public static function contentKeyToColumn(string $key): string
{
return self::$contentKeyToColumn[$key] ?? $key;
}

/**
* Get content for a key in the current locale: locale override, then main (English) column, then lang file.
*/
Expand All @@ -73,8 +126,9 @@ public function contentForLocale(string $key, ?string $locale = null): string
if (! empty($overrides[$locale][$key])) {
return (string) $overrides[$locale][$key];
}
if (in_array($key, self::contentKeys(), true)) {
$value = $this->getAttribute($key);
$column = self::contentKeyToColumn($key);
if (in_array($column, self::contentKeys(), true)) {
$value = $this->getAttribute($column);
if ($value !== null && $value !== '') {
return (string) $value;
}
Expand All @@ -83,43 +137,50 @@ public function contentForLocale(string $key, ?string $locale = null): string
}

/**
* Get a button by key. Returns stdClass with label, url, open_new_tab, enabled or null.
* Get a button by key from the buttons relationship. Returns stdClass with label, url, open_new_tab, enabled or null.
*/
public function getButton(string $key, ?string $locale = null): ?object
{
$buttons = $this->getButtonsForLocale($locale);
$btn = $buttons->firstWhere('key', $key);
if (! $btn || empty($btn['enabled'])) {
$button = $this->buttons()->where('key', $key)->first();
if (! $button || ! $button->enabled) {
return null;
}
$o = new \stdClass;
$o->key = $btn['key'];
$o->label = $btn['label'] ?? '';
$o->url = $btn['url'] ?? '#';
$o->open_new_tab = ! empty($btn['open_new_tab']);
$o->key = $button->key;
$o->label = $button->label;
$o->url = $button->url ?: '#';
$o->open_new_tab = $button->open_new_tab;
$o->enabled = true;
return $o;
}

/**
* Buttons for locale: merge locale_overrides[locale].buttons onto main buttons.
* All buttons for this page (for Nova / admin). Keys preserved from relationship.
*/
public function getButtonsForLocale(?string $locale = null): Collection
{
return $this->buttons()->get()->map(fn ($b) => [
'key' => $b->key,
'label' => $b->label,
'url' => $b->url,
'open_new_tab' => $b->open_new_tab,
'enabled' => $b->enabled,
'position' => $b->position,
]);
}

/**
* FAQ items for locale: locale_overrides[locale].faq_items or main faq_items.
* Each item: ['question' => string, 'answer' => string].
*/
public function faqItemsForLocale(?string $locale = null): array
{
$locale = $locale ?? app()->getLocale();
$main = collect($this->buttons ?? []);
$overrides = $this->locale_overrides ?? [];
$localeButtons = $overrides[$locale]['buttons'] ?? null;
if (! is_array($localeButtons)) {
return $main;
}
$byKey = $main->keyBy('key');
foreach ($localeButtons as $b) {
$k = $b['key'] ?? null;
if ($k !== null) {
$byKey->put($k, array_merge($byKey->get($k) ?? [], $b));
}
if (! empty($overrides[$locale]['faq_items']) && is_array($overrides[$locale]['faq_items'])) {
return $overrides[$locale]['faq_items'];
}
return $byKey->values()->sortBy('position')->values();
$items = $this->faq_items ?? [];
return is_array($items) ? $items : [];
}
}
Loading
Loading