@@ -9,29 +9,67 @@ class GirlsInDigitalPage extends Model
99{
1010 protected $ table = 'girls_in_digital_page ' ;
1111
12+ /** Content key => DB column (when different). */
13+ protected static $ contentKeyToColumn = [
14+ 'landing_header ' => 'hero_intro ' ,
15+ 'about_girls_title ' => 'about_title ' ,
16+ 'about_girls_description_1 ' => 'about_description_1 ' ,
17+ 'about_girls_description_2 ' => 'about_description_2 ' ,
18+ ];
19+
1220 protected $ fillable = [
13- 'use_dynamic_content ' ,
21+ 'hero_dynamic ' ,
22+ 'about_dynamic ' ,
23+ 'resources_dynamic ' ,
24+ 'matters_dynamic ' ,
25+ 'faq_dynamic ' ,
1426 'hero_intro ' ,
1527 'hero_video_url ' ,
28+ 'hero_image ' ,
1629 'about_title ' ,
1730 'about_description_1 ' ,
1831 'about_description_2 ' ,
32+ 'about_image ' ,
1933 'resource_title ' ,
2034 'resource_person_title ' ,
2135 'resource_person_description_1 ' ,
2236 'resource_person_description_2 ' ,
2337 'resource_educator_title ' ,
2438 'resource_educator_description ' ,
25- 'buttons ' ,
39+ 'resource_young_image ' ,
40+ 'resource_educator_image ' ,
41+ 'matters_title ' ,
42+ 'matters_graph1_image ' ,
43+ 'matters_graph1_link ' ,
44+ 'matters_graph1_caption ' ,
45+ 'matters_graph2_image ' ,
46+ 'matters_graph2_link ' ,
47+ 'matters_graph2_caption ' ,
48+ 'matters_graph3_image ' ,
49+ 'matters_graph3_link ' ,
50+ 'matters_graph3_caption ' ,
51+ 'matters_paragraph_1 ' ,
52+ 'matters_paragraph_2 ' ,
53+ 'faq_title ' ,
54+ 'faq_items ' ,
2655 'locale_overrides ' ,
2756 ];
2857
2958 protected $ casts = [
30- 'use_dynamic_content ' => 'boolean ' ,
31- 'buttons ' => 'array ' ,
59+ 'hero_dynamic ' => 'boolean ' ,
60+ 'about_dynamic ' => 'boolean ' ,
61+ 'resources_dynamic ' => 'boolean ' ,
62+ 'matters_dynamic ' => 'boolean ' ,
63+ 'faq_dynamic ' => 'boolean ' ,
64+ 'faq_items ' => 'array ' ,
3265 'locale_overrides ' => 'array ' ,
3366 ];
3467
68+ public function buttons ()
69+ {
70+ return $ this ->hasMany (GirlsInDigitalButton::class, 'page_id ' )->orderBy ('position ' );
71+ }
72+
3573 /**
3674 * Get the singleton page config (id = 1). Create if missing.
3775 */
@@ -60,9 +98,24 @@ public static function contentKeys(): array
6098 'resource_person_description_2 ' ,
6199 'resource_educator_title ' ,
62100 'resource_educator_description ' ,
101+ 'matters_title ' ,
102+ 'matters_graph1_caption ' ,
103+ 'matters_graph2_caption ' ,
104+ 'matters_graph3_caption ' ,
105+ 'matters_paragraph_1 ' ,
106+ 'matters_paragraph_2 ' ,
107+ 'faq_title ' ,
63108 ];
64109 }
65110
111+ /**
112+ * Resolve content key to DB column name.
113+ */
114+ public static function contentKeyToColumn (string $ key ): string
115+ {
116+ return self ::$ contentKeyToColumn [$ key ] ?? $ key ;
117+ }
118+
66119 /**
67120 * Get content for a key in the current locale: locale override, then main (English) column, then lang file.
68121 */
@@ -73,8 +126,9 @@ public function contentForLocale(string $key, ?string $locale = null): string
73126 if (! empty ($ overrides [$ locale ][$ key ])) {
74127 return (string ) $ overrides [$ locale ][$ key ];
75128 }
76- if (in_array ($ key , self ::contentKeys (), true )) {
77- $ value = $ this ->getAttribute ($ key );
129+ $ column = self ::contentKeyToColumn ($ key );
130+ if (in_array ($ column , self ::contentKeys (), true )) {
131+ $ value = $ this ->getAttribute ($ column );
78132 if ($ value !== null && $ value !== '' ) {
79133 return (string ) $ value ;
80134 }
@@ -83,43 +137,50 @@ public function contentForLocale(string $key, ?string $locale = null): string
83137 }
84138
85139 /**
86- * Get a button by key. Returns stdClass with label, url, open_new_tab, enabled or null.
140+ * Get a button by key from the buttons relationship . Returns stdClass with label, url, open_new_tab, enabled or null.
87141 */
88142 public function getButton (string $ key , ?string $ locale = null ): ?object
89143 {
90- $ buttons = $ this ->getButtonsForLocale ($ locale );
91- $ btn = $ buttons ->firstWhere ('key ' , $ key );
92- if (! $ btn || empty ($ btn ['enabled ' ])) {
144+ $ button = $ this ->buttons ()->where ('key ' , $ key )->first ();
145+ if (! $ button || ! $ button ->enabled ) {
93146 return null ;
94147 }
95148 $ o = new \stdClass ;
96- $ o ->key = $ btn [ ' key ' ] ;
97- $ o ->label = $ btn [ ' label ' ] ?? '' ;
98- $ o ->url = $ btn [ ' url ' ] ?? '# ' ;
99- $ o ->open_new_tab = ! empty ( $ btn [ ' open_new_tab ' ]) ;
149+ $ o ->key = $ button -> key ;
150+ $ o ->label = $ button -> label ;
151+ $ o ->url = $ button -> url ?: '# ' ;
152+ $ o ->open_new_tab = $ button -> open_new_tab ;
100153 $ o ->enabled = true ;
101154 return $ o ;
102155 }
103156
104157 /**
105- * Buttons for locale: merge locale_overrides[locale].buttons onto main buttons .
158+ * All buttons for this page (for Nova / admin). Keys preserved from relationship .
106159 */
107160 public function getButtonsForLocale (?string $ locale = null ): Collection
161+ {
162+ return $ this ->buttons ()->get ()->map (fn ($ b ) => [
163+ 'key ' => $ b ->key ,
164+ 'label ' => $ b ->label ,
165+ 'url ' => $ b ->url ,
166+ 'open_new_tab ' => $ b ->open_new_tab ,
167+ 'enabled ' => $ b ->enabled ,
168+ 'position ' => $ b ->position ,
169+ ]);
170+ }
171+
172+ /**
173+ * FAQ items for locale: locale_overrides[locale].faq_items or main faq_items.
174+ * Each item: ['question' => string, 'answer' => string].
175+ */
176+ public function faqItemsForLocale (?string $ locale = null ): array
108177 {
109178 $ locale = $ locale ?? app ()->getLocale ();
110- $ main = collect ($ this ->buttons ?? []);
111179 $ overrides = $ this ->locale_overrides ?? [];
112- $ localeButtons = $ overrides [$ locale ]['buttons ' ] ?? null ;
113- if (! is_array ($ localeButtons )) {
114- return $ main ;
115- }
116- $ byKey = $ main ->keyBy ('key ' );
117- foreach ($ localeButtons as $ b ) {
118- $ k = $ b ['key ' ] ?? null ;
119- if ($ k !== null ) {
120- $ byKey ->put ($ k , array_merge ($ byKey ->get ($ k ) ?? [], $ b ));
121- }
180+ if (! empty ($ overrides [$ locale ]['faq_items ' ]) && is_array ($ overrides [$ locale ]['faq_items ' ])) {
181+ return $ overrides [$ locale ]['faq_items ' ];
122182 }
123- return $ byKey ->values ()->sortBy ('position ' )->values ();
183+ $ items = $ this ->faq_items ?? [];
184+ return is_array ($ items ) ? $ items : [];
124185 }
125186}
0 commit comments