Skip to content
Merged

Dev #3324

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
35 changes: 0 additions & 35 deletions app/GirlsInDigitalButton.php

This file was deleted.

92 changes: 89 additions & 3 deletions app/GirlsInDigitalPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,34 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class GirlsInDigitalPage extends Model
{
protected $table = 'girls_in_digital_page';

protected $fillable = ['locale_overrides'];
protected $fillable = [
'use_dynamic_content',
'hero_intro',
'hero_video_url',
'about_title',
'about_description_1',
'about_description_2',
'resource_title',
'resource_person_title',
'resource_person_description_1',
'resource_person_description_2',
'resource_educator_title',
'resource_educator_description',
'buttons',
'locale_overrides',
];

protected $casts = ['locale_overrides' => 'array'];
protected $casts = [
'use_dynamic_content' => 'boolean',
'buttons' => 'array',
'locale_overrides' => 'array',
];

/**
* Get the singleton page config (id = 1). Create if missing.
Expand All @@ -25,7 +45,26 @@ public static function config(): self
}

/**
* Get content for a key in the current locale: override or lang fallback.
* Content keys that have a main (default/English) column on the model.
*/
public static function contentKeys(): array
{
return [
'hero_intro',
'about_title',
'about_description_1',
'about_description_2',
'resource_title',
'resource_person_title',
'resource_person_description_1',
'resource_person_description_2',
'resource_educator_title',
'resource_educator_description',
];
}

/**
* Get content for a key in the current locale: locale override, then main (English) column, then lang file.
*/
public function contentForLocale(string $key, ?string $locale = null): string
{
Expand All @@ -34,6 +73,53 @@ 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);
if ($value !== null && $value !== '') {
return (string) $value;
}
}
return (string) __('girls-in-digital.' . $key);
}

/**
* Get a button by key. 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'])) {
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->enabled = true;
return $o;
}

/**
* Buttons for locale: merge locale_overrides[locale].buttons onto main buttons.
*/
public function getButtonsForLocale(?string $locale = null): Collection
{
$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));
}
}
return $byKey->values()->sortBy('position')->values();
}
}
7 changes: 1 addition & 6 deletions app/Http/View/Composers/GirlsInDigitalComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\View\Composers;

use App\GirlsInDigitalButton;
use App\GirlsInDigitalPage;
use Illuminate\Support\Facades\Schema;
use Illuminate\View\View;
Expand All @@ -11,11 +10,7 @@ class GirlsInDigitalComposer
{
public function compose(View $view): void
{
$buttons = collect();
if (Schema::hasTable('girls_in_digital_buttons')) {
$buttons = GirlsInDigitalButton::enabled()->ordered()->get();
}
$page = Schema::hasTable('girls_in_digital_page') ? GirlsInDigitalPage::config() : null;
$view->with(compact('buttons', 'page'));
$view->with(compact('page'));
}
}
57 changes: 0 additions & 57 deletions app/Nova/GirlsInDigitalButton.php

This file was deleted.

78 changes: 49 additions & 29 deletions app/Nova/GirlsInDigitalPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace App\Nova;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\Code;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;

Expand All @@ -18,7 +21,7 @@ class GirlsInDigitalPage extends Resource

public static function label()
{
return 'Girls in Digital – Page content';
return 'Girls in Digital';
}

public static function singularLabel()
Expand All @@ -36,6 +39,12 @@ public static function uriKey(): string
return 'girls-in-digital-page';
}

/** Hide from index – single resource, edit from detail. */
public static function indexQuery(NovaRequest $request, $query)
{
return $query->where('id', 1);
}

private static function localesSorted(): array
{
$locales = config('app.locales', ['en']);
Expand All @@ -50,43 +59,19 @@ private static function localesSorted(): array
return $locales;
}

/** Content keys that can be overridden per locale (from resources/lang/xx/girls-in-digital.php). */
public static function contentKeys(): array
{
return [
'landing_header',
'about_girls_title',
'about_girls_description_1',
'about_girls_description_2',
'resource_title',
'resource_person_title',
'resource_person_description_1',
'resource_person_description_2',
'resource_educator_title',
'resource_educator_description',
'resource_search_activity',
'resource_organise_activity',
'resource_digital_activity',
'resource_social_media',
];
}

public function fields(Request $request): array
{
$locales = self::localesSorted();
$keysHelp = 'Optional keys: ' . implode(', ', self::contentKeys());

$localePanels = [];
foreach ($locales as $locale) {
$localePanels[] = new Panel('Override: ' . strtoupper($locale), [
Code::make('[' . $locale . ']', 'locale_override_' . $locale)
->json()
->nullable()
->help($keysHelp)
->help('Optional. Override text/buttons for this locale. Keys: ' . implode(', ', \App\GirlsInDigitalPage::contentKeys()) . '. For buttons use "buttons": [{"key":"gcib_sprint_hero","label":"...","url":"...","open_new_tab":false,"enabled":true,"position":0}].')
->resolveUsing(function () use ($locale) {
$overrides = $this->resource->locale_overrides ?? [];
$data = $overrides[$locale] ?? [];
return $data;
return $overrides[$locale] ?? [];
})
->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
$locale = str_replace('locale_override_', '', $attribute);
Expand All @@ -100,8 +85,43 @@ public function fields(Request $request): array

return [
ID::make()->onlyOnForms(),
new Panel('Per-locale content overrides (optional)', [
\Laravel\Nova\Fields\Heading::make('Expand a locale below to override text for that language. Defaults come from resources/lang/{locale}/girls-in-digital.php.'),

Boolean::make('Use dynamic content', 'use_dynamic_content')
->help('When on, the Girls in Digital page uses the content below. When off, the static blade content is shown (current hardcoded text/images). Defaults to English for all languages; use locale overrides below for other languages.'),

new Panel('Hero', [
Textarea::make('Hero intro', 'hero_intro')
->nullable()
->help('Default (English) hero text. Leave empty to use static/lang.'),
Text::make('Hero video URL', 'hero_video_url')
->nullable()
->help('YouTube embed URL, e.g. https://www.youtube.com/embed/...'),
]),

new Panel('About', [
Text::make('About title', 'about_title')->nullable(),
Textarea::make('About description 1', 'about_description_1')->nullable(),
Textarea::make('About description 2', 'about_description_2')->nullable()->help('HTML allowed.'),
]),

new Panel('Resources', [
Text::make('Resources title', 'resource_title')->nullable(),
Text::make('Young person / parent title', 'resource_person_title')->nullable(),
Textarea::make('Young person / parent description 1', 'resource_person_description_1')->nullable(),
Textarea::make('Young person / parent description 2', 'resource_person_description_2')->nullable(),
Text::make('Educator title', 'resource_educator_title')->nullable(),
Textarea::make('Educator description', 'resource_educator_description')->nullable(),
]),

new Panel('Buttons', [
Code::make('Buttons', 'buttons')
->json()
->nullable()
->help('Array of buttons. Each: {"key":"gcib_sprint_hero","label":"Girls Code It Better Sprint","url":"https://...","open_new_tab":false,"enabled":true,"position":0}. Keys: gcib_sprint_hero, female_role_models, open_call_challenges, search_activity, meet_role_model, organise_gcib_sprint, activity_guideline, social_media_kit. Disabled buttons are hidden.'),
]),

new Panel('Per-locale overrides (optional)', [
\Laravel\Nova\Fields\Heading::make('Expand a locale to override text or button labels for that language. Defaults to English for all.'),
...$localePanels,
]),
];
Expand Down

This file was deleted.

Loading
Loading