Skip to content
Merged

refresh #3639

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
69 changes: 67 additions & 2 deletions app/Nova/TrainingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Fields\Trix;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;

class TrainingResource extends Resource
{
Expand All @@ -40,9 +41,64 @@ public static function authorizedToViewAny(Request $request): bool
return true;
}

private static function localesSorted(): array
{
$locales = config('app.locales', ['en']);
if (is_string($locales)) {
$locales = array_map('trim', explode(',', $locales));
}
$locales = array_values(array_filter($locales));
if ($locales === []) {
$locales = ['en'];
}
sort($locales);

return $locales;
}

public function fields(Request $request): array
{
return [
$pdfTranslationFields = [];
foreach (self::localesSorted() as $locale) {
if ($locale === 'en') {
continue;
}

$pdfTranslationFields[] = Textarea::make(
'PDF links ('.strtoupper($locale).')',
'locale_'.$locale.'_pdf_links_section'
)
->nullable()
->rows(10)
->hideFromIndex()
->help('Leave empty to keep the default English PDF links for this language. Paste the full HTML (same structure as PDF links section) with translated file URLs after uploading to S3.')
->resolveUsing(function () use ($locale) {
$overrides = $this->resource->locale_overrides ?? [];

return $overrides[$locale]['pdf_links_section'] ?? '';
})
->fillUsing(function ($request, $model, $attribute, $requestAttribute) use ($locale) {
$overrides = $model->locale_overrides ?? [];
if (! isset($overrides[$locale]) || ! is_array($overrides[$locale])) {
$overrides[$locale] = [];
}

$value = $request->get($requestAttribute);
if ($value === null || trim((string) $value) === '') {
unset($overrides[$locale]['pdf_links_section']);
} else {
$overrides[$locale]['pdf_links_section'] = $value;
}

if ($overrides[$locale] === []) {
unset($overrides[$locale]);
}

$model->locale_overrides = $overrides === [] ? null : $overrides;
});
}

$fields = [
ID::make()->sortable(),

Text::make('Slug', 'slug')
Expand Down Expand Up @@ -135,7 +191,7 @@ public function fields(Request $request): array

Trix::make('PDF links section', 'pdf_links_section')
->nullable()
->help('Optional area for numbered downloadable resources (e.g. 1-6 links).'),
->help('Default (English) downloadable resources. For other languages, use the “Translated PDF links” panel below. Use [[key_one_pagers_locale_note]] for the locale-aware intro sentence.'),

Trix::make('Contacts section', 'contacts_section')
->nullable()
Expand Down Expand Up @@ -248,6 +304,15 @@ public function fields(Request $request): array
Boolean::make('Published', 'active')
->help('Turn off to keep this page hidden publicly. Preview URL still works.'),
];

if ($pdfTranslationFields !== []) {
$fields[] = Panel::make('Translated PDF links', $pdfTranslationFields)
->help('Only fill languages that have translated files. When the site language switches, that language’s links are shown if present; otherwise visitors keep the default English links.')
->collapsable()
->collapsedByDefault();
}

return $fields;
}

public static function indexQuery(NovaRequest $request, $query)
Expand Down
56 changes: 56 additions & 0 deletions app/TrainingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class TrainingResource extends Model
'body_image_alt',
'content',
'pdf_links_section',
'locale_overrides',
'contacts_section',
'register_box_section',
'about_box_section',
Expand All @@ -55,6 +56,7 @@ class TrainingResource extends Model
'active' => 'boolean',
'position' => 'integer',
'anchor_offset' => 'integer',
'locale_overrides' => 'array',
];

public function scopeActive($query)
Expand Down Expand Up @@ -160,4 +162,58 @@ public function getYoutubeVideoIdAttribute(): ?string

return null;
}

/**
* Resolve PDF links HTML for the active (or given) locale.
*
* Priority:
* 1. Full locale override of pdf_links_section, if present
* 2. Default pdf_links_section with optional per-URL replacements
* 3. Default pdf_links_section
*/
public function pdfLinksSectionForLocale(?string $locale = null): string
{
$locale = $locale ?? app()->getLocale();
$overrides = $this->locale_overrides ?? [];
$localeOverrides = is_array($overrides[$locale] ?? null) ? $overrides[$locale] : [];

if (! empty($localeOverrides['pdf_links_section']) && is_string($localeOverrides['pdf_links_section'])) {
return $localeOverrides['pdf_links_section'];
}

$section = (string) ($this->pdf_links_section ?? '');
$replacements = $localeOverrides['pdf_link_replacements'] ?? null;

if (! is_array($replacements) || $replacements === [] || $section === '') {
return $section;
}

foreach ($replacements as $from => $to) {
if (! is_string($from) || ! is_string($to) || $from === '' || $to === '') {
continue;
}

$section = str_replace($from, $to, $section);
}

return $section;
}

/**
* Whether the given locale has dedicated PDF-link content (full section or URL map).
*/
public function hasPdfLinksOverrideForLocale(?string $locale = null): bool
{
$locale = $locale ?? app()->getLocale();
$overrides = $this->locale_overrides ?? [];
$localeOverrides = is_array($overrides[$locale] ?? null) ? $overrides[$locale] : [];

if (! empty($localeOverrides['pdf_links_section']) && is_string($localeOverrides['pdf_links_section'])) {
return true;
}

$replacements = $localeOverrides['pdf_link_replacements'] ?? null;

return is_array($replacements) && $replacements !== [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
if (Schema::hasTable('training_resources') && ! Schema::hasColumn('training_resources', 'locale_overrides')) {
Schema::table('training_resources', function (Blueprint $table) {
$table->json('locale_overrides')->nullable()->after('pdf_links_section');
});
}
}

/**
* Reverse the migrations.
*/
public function down(): void
{
if (Schema::hasColumn('training_resources', 'locale_overrides')) {
Schema::table('training_resources', function (Blueprint $table) {
$table->dropColumn('locale_overrides');
});
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function run(): void
'body_image_alt' => 'Discover Digital Programme roadmap',
'pdf_links_section' => <<<HTML
<h2 id="key-one-pagers">Key one-pagers</h2>
[[key_one_pagers_locale_note]]
<p>These documents summarise the main operational parts of the toolkit.</p>
<ul>
<li><a href="https://codeweek-s3.s3.eu-west-1.amazonaws.com/files/Essential_Preparation_for_STEM_Engagement+Activities.pdf" target="_blank" rel="noopener noreferrer">Essential Preparation for STEM Engagement Activities</a></li>
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/training.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,5 @@
'share-inspire-text1' =>'Once you\'ve completed one or more Code Week Learning Bits, we encourage you to bring digital creativity into your educational environment and pin your activity on the <a href="/events" class="font-semibold underline text-dark-blue">Code Week Map</a>!',
'share-inspire-text2' =>'You can organise a lesson in your classroom, a workshop at a community centre, or an open event at your organisation. Simply pick a date and register your activity on the <a href="/events" class="font-semibold underline text-dark-blue">Code Week Map</a>. Each activity organiser will get a participation certificate for their effort.',
'share-inspire-text3' =>'If you would like to connect with an international group of enthusiastic teachers, join the <a href="https://www.facebook.com/groups/774720866253044/?source_id=377506999042215" target="_blank" class="font-semibold underline text-dark-blue">EU Code Week Facebook group for teachers</a>! To take a step further and collaborate with other schools in your country or across borders – join the EU <a href="/codeweek4all" class="font-semibold underline text-dark-blue">Code Week 4 All challenge</a>.',
'discover_digital_key_one_pagers_note' => 'The documents listed in the "Key one-pagers" section summarise the main operational components of the Discover Digital Programme Toolkit. These documents are also available in Romanian.',
];
1 change: 1 addition & 0 deletions resources/lang/ro/training.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,5 @@
'share-inspire-text1' =>'După ce ați finalizat unul sau mai multe biți de învățare din Săptămâna Programării, vă încurajăm să aduceți creativitatea digitală în mediul dumneavoastră educațional și să vă fixați activitatea pe harta <a href="/events" class="font-semibold text-dark-blue underline">Săptămânii Programării</a>!',
'share-inspire-text2' =>'Puteți organiza o lecție în clasă, un atelier la un centru comunitar sau un eveniment deschis la organizația dvs. Pur și simplu alegeți o dată și înregistrați-vă activitatea pe Harta Săptămânii <a href="/events" class="font-semibold text-dark-blue underline">de Programare</a>. Fiecare organizator de activitate va primi un certificat de participare pentru efortul său.',
'share-inspire-text3' =>'Dacă doriți să luați legătura cu un grup internațional de profesori entuziaști, alăturați-vă <a href="https://www.facebook.com/groups/774720866253044/?source_id=377506999042215" target="_blank" class="font-semibold text-dark-blue underline">grupului de Facebook EU Code Week pentru</a>! profesori! Pentru a face un pas mai departe și a colabora cu alte școli din țara dumneavoastră sau dincolo de granițe – alăturați-vă provocării EU <a href="/codeweek4all" class="font-semibold text-dark-blue underline">Code Week 4 Toți</a>.',
'discover_digital_key_one_pagers_note' => 'Documentele din secțiunea "Key one-pagers" ("Fișe informative esențiale") prezintă pe scurt principalele componente operaționale ale setului de instrumente ale programului Discover Digital. Aceste documente sunt disponibile și în limba română.',
];
20 changes: 16 additions & 4 deletions resources/views/training/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,22 @@ class="mb-12 w-full h-full max-h-[630px] object-contain"
</div>
@endif

@if(!empty($trainingResource->pdf_links_section))
<div class="{{ $pdfClass }}">
{!! $trainingResource->pdf_links_section !!}
</div>
@if(!empty($trainingResource->pdf_links_section) || $trainingResource->hasPdfLinksOverrideForLocale())
@php
$pdfLinksSection = $trainingResource->pdfLinksSectionForLocale();
if (str_contains($pdfLinksSection, '[[key_one_pagers_locale_note]]')) {
$pdfLinksSection = str_replace(
'[[key_one_pagers_locale_note]]',
'<span class="block mb-4">'.e(__('training.discover_digital_key_one_pagers_note')).'</span>',
$pdfLinksSection
);
}
@endphp
@if($pdfLinksSection !== '')
<div class="{{ $pdfClass }}">
{!! $pdfLinksSection !!}
</div>
@endif
@endif

@if(!empty($trainingResource->contacts_section))
Expand Down
Loading