Skip to content
Merged

Dev #3305

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
36 changes: 32 additions & 4 deletions app/HomeSlide.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
class HomeSlide extends Model
{
protected $fillable = [
'title',
'description',
'title_translations',
'description_translations',
'url',
'button_text',
'button_text_translations',
'open_primary_new_tab',
'url2',
'button2_text',
'button2_text_translations',
'open_second_new_tab',
'image',
'position',
Expand All @@ -23,6 +23,10 @@ class HomeSlide extends Model
];

protected $casts = [
'title_translations' => 'array',
'description_translations' => 'array',
'button_text_translations' => 'array',
'button2_text_translations' => 'array',
'active' => 'boolean',
'show_countdown' => 'boolean',
'countdown_target' => 'datetime',
Expand All @@ -31,6 +35,30 @@ class HomeSlide extends Model
'open_second_new_tab' => 'boolean',
];

/**
* Get translated string for a key (title_translations, description_translations, etc.).
* Uses current app locale with fallback to 'en' then first available.
*/
public function getTranslation(string $key, ?string $locale = null): string
{
$locale = $locale ?? app()->getLocale();
$translations = $this->getAttribute($key);
if (! is_array($translations)) {
return '';
}
$value = $translations[$locale] ?? $translations['en'] ?? null;
if ($value !== null && $value !== '') {
return (string) $value;
}
return (string) (array_values($translations)[0] ?? '');
}

/** For Nova index/detail: show title in current locale. */
public function getTitleAttribute(): string
{
return $this->getTranslation('title_translations');
}

public function scopeActive($query)
{
return $query->where('active', true);
Expand Down
11 changes: 6 additions & 5 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ private function getActivities()
if (Schema::hasTable('home_slides')) {
$slides = HomeSlide::active()->ordered()->get();
if ($slides->isNotEmpty()) {
return $slides->map(function (HomeSlide $slide) {
$locale = app()->getLocale();
return $slides->map(function (HomeSlide $slide) use ($locale) {
return [
'title' => $slide->title,
'description' => $slide->description ?? '',
'title' => $slide->getTranslation('title_translations', $locale),
'description' => $slide->getTranslation('description_translations', $locale),
'url' => $slide->url,
'btn_lang' => $slide->button_text,
'btn_lang' => $slide->getTranslation('button_text_translations', $locale),
'open_primary_new_tab' => $slide->open_primary_new_tab ?? false,
'url2' => $slide->url2,
'btn2_lang' => $slide->button2_text,
'btn2_lang' => $slide->getTranslation('button2_text_translations', $locale),
'open_second_new_tab' => $slide->open_second_new_tab ?? false,
'image' => $slide->image_url,
'show_countdown' => $slide->show_countdown,
Expand Down
30 changes: 18 additions & 12 deletions app/Nova/HomeSlide.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\KeyValue;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Http\Requests\NovaRequest;

class HomeSlide extends Resource
Expand All @@ -19,7 +19,7 @@ class HomeSlide extends Resource

public static $title = 'title';

public static $search = ['title', 'description'];
public static $search = [];

public static function label()
{
Expand All @@ -40,22 +40,28 @@ public function fields(Request $request): array
{
return [
ID::make()->sortable(),
Text::make('Title', 'title')
KeyValue::make('Title', 'title_translations')
->keyLabel('Locale (e.g. en, fr, de)')
->valueLabel('Text')
->rules('required')
->help('Use a lang key (e.g. home.banner1_title) for translated content, or type plain text.'),
Textarea::make('Description', 'description')
->nullable()
->help('Use a lang key for translated content, or plain text.'),
->help('Add one row per language. Key = locale code (en, fr, de, …), Value = translated title.'),
KeyValue::make('Description', 'description_translations')
->keyLabel('Locale')
->valueLabel('Text')
->help('Add one row per language.'),
Text::make('Primary button URL', 'url')->rules('required')->hideFromIndex(),
Text::make('Primary button label', 'button_text')
KeyValue::make('Primary button label', 'button_text_translations')
->keyLabel('Locale')
->valueLabel('Text')
->rules('required')
->help('Lang key (e.g. home.learn_more) for translation, or plain text.'),
->help('Add one row per language.'),
Boolean::make('Open primary link in new tab', 'open_primary_new_tab')
->help('Open the primary button link in a new window/tab.'),
Text::make('Second button URL', 'url2')->nullable()->hideFromIndex(),
Text::make('Second button label', 'button2_text')
->nullable()
->help('Leave empty to hide second button. Use lang key for translation.'),
KeyValue::make('Second button label', 'button2_text_translations')
->keyLabel('Locale')
->valueLabel('Text')
->help('Add one row per language. Leave empty to hide second button.'),
Boolean::make('Open second link in new tab', 'open_second_new_tab')
->help('Open the second button link in a new window/tab.'),
Text::make('Image', 'image')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
* Adds JSON columns for per-locale translations and migrates existing string data into 'en'.
*/
public function up(): void
{
Schema::table('home_slides', function (Blueprint $table) {
$table->json('title_translations')->nullable()->after('id');
$table->json('description_translations')->nullable()->after('title_translations');
$table->json('button_text_translations')->nullable()->after('url');
$table->json('button2_text_translations')->nullable()->after('button2_text');
});

$slides = DB::table('home_slides')->get();
foreach ($slides as $row) {
DB::table('home_slides')->where('id', $row->id)->update([
'title_translations' => json_encode(['en' => $row->title ?? '']),
'description_translations' => json_encode(['en' => $row->description ?? '']),
'button_text_translations' => json_encode(['en' => $row->button_text ?? '']),
'button2_text_translations' => json_encode(['en' => $row->button2_text ?? '']),
]);
}

Schema::table('home_slides', function (Blueprint $table) {
$table->dropColumn(['title', 'description', 'button_text', 'button2_text']);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('home_slides', function (Blueprint $table) {
$table->string('title')->nullable()->after('id');
$table->text('description')->nullable()->after('title');
$table->string('button_text')->nullable()->after('url');
$table->string('button2_text')->nullable()->after('open_second_new_tab');
});

$slides = DB::table('home_slides')->get();
foreach ($slides as $row) {
$titleTrans = $row->title_translations ? json_decode($row->title_translations, true) : [];
$descTrans = $row->description_translations ? json_decode($row->description_translations, true) : [];
$btnTrans = $row->button_text_translations ? json_decode($row->button_text_translations, true) : [];
$btn2Trans = $row->button2_text_translations ? json_decode($row->button2_text_translations, true) : [];
DB::table('home_slides')->where('id', $row->id)->update([
'title' => $titleTrans['en'] ?? '',
'description' => $descTrans['en'] ?? null,
'button_text' => $btnTrans['en'] ?? '',
'button2_text' => $btn2Trans['en'] ?? null,
]);
}

Schema::table('home_slides', function (Blueprint $table) {
$table->dropColumn([
'title_translations',
'description_translations',
'button_text_translations',
'button2_text_translations',
]);
});
}
};
4 changes: 2 additions & 2 deletions resources/views/static/home.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class="text-[#1C4DA1] text-[30px] md:text-[60px] leading-9 md:leading-[72px] fon
</h2>
<p
class="text-xl md:text-2xl leading-8 text-[#333E48] p-0 mb-4 max-md:max-w-full max-w-[525px]">
{{ strip_tags(__($activity['description'] ?? '')) }}
{!! strip_tags(__($activity['description'] ?? '')) !!}
</p>
<div class="flex flex-col space-y-4 md:flex-row md:space-x-4 md:space-y-0">
<a class="inline-block bg-primary hover:bg-hover-orange rounded-full py-4 px-6 md:px-10 font-semibold text-base w-full md:w-auto text-center text-[#20262C] transition-all duration-300"
Expand All @@ -103,7 +103,7 @@ class="text-xl md:text-2xl leading-8 text-[#333E48] p-0 mb-4 max-md:max-w-full m
{{ __($activity['btn_lang']) }}
</a>

@if (isset($activity['btn2_lang']) && !is_null($activity['btn2_lang']) && $activity['btn2_lang'] !== '')
@if (isset($activity['btn2_lang']) && (string)$activity['btn2_lang'] !== '')
<a class="flex justify-center items-center gap-2 text-[#1C4DA1] border-solid border-2 border-[#1C4DA1] rounded-full py-3 px-8 font-semibold text-lg transition-all duration-300 hover:bg-[#E8EDF6] group"
href="{{ $activity['url2'] }}"
@if(!empty($activity['open_second_new_tab'])) target="_blank" rel="noopener noreferrer" @endif>
Expand Down
Loading