Skip to content

Commit b78152f

Browse files
committed
fix tags and categories
1 parent 561a1f6 commit b78152f

File tree

2 files changed

+100
-31
lines changed

2 files changed

+100
-31
lines changed

packages/category/src/Models/Category.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Kalnoy\Nestedset\NodeTrait;
1515
use Moox\Category\Database\Factories\CategoryFactory;
1616
use Moox\Core\Entities\Items\Draft\BaseDraftModel;
17+
use Moox\Localization\Models\Localization;
1718
use Moox\Media\Traits\HasMediaUsable;
1819
use Override;
1920
use Spatie\Image\Enums\Fit;
@@ -138,4 +139,25 @@ public function mediaThroughUsables()
138139
'media_id'
139140
)->where('media_usables.media_usable_type', '=', static::class);
140141
}
142+
143+
public function getDisplayTitleAttribute(): string
144+
{
145+
$defaultLocalization = Localization::where('is_default', true)->first();
146+
$mainLocale = $defaultLocalization?->locale_variant ?? config('app.locale', 'en');
147+
$currentLocale = request()->query('lang') ?? $mainLocale;
148+
149+
if (method_exists($this, 'translate')) {
150+
$translation = $this->translate($currentLocale);
151+
if ($translation && ! empty($translation->title)) {
152+
return $translation->title;
153+
}
154+
155+
$mainTranslation = $this->translate($mainLocale);
156+
if ($mainTranslation && ! empty($mainTranslation->title)) {
157+
return $mainTranslation->title.' ('.$mainLocale.')';
158+
}
159+
}
160+
161+
return $this->attributes['title'] ?? ('ID: '.$this->id);
162+
}
141163
}

packages/core/src/Traits/Taxonomy/HasResourceTaxonomy.php

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Database\Eloquent\Builder;
1111
use Illuminate\Database\Eloquent\Model;
1212
use Illuminate\Support\Facades\DB;
13+
use Moox\Localization\Models\Localization;
1314
use Moox\Category\Moox\Entities\Categories\Category\Forms\TaxonomyCreateForm as CoreTaxonomyCreateForm;
1415
use Moox\Core\Services\TaxonomyService;
1516

@@ -49,8 +50,11 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
4950
'searchable' => true,
5051
'createOptionForm' => $createFormClass::getSchema(),
5152
'createOptionUsing' => function (array $data, $livewire) use ($modelClass) {
52-
$currentLocale = $livewire->lang ?? app()->getLocale();
53-
$mainLocale = config('app.locale', 'en');
53+
$defaultLocalization = Localization::where('is_default', true)->first();
54+
$mainLocale = $defaultLocalization?->locale_variant ?? config('app.locale', 'en');
55+
$currentLocale = $livewire->lang
56+
?? request()->query('lang')
57+
?? $mainLocale;
5458

5559
if ($currentLocale !== $mainLocale) {
5660
return __('core::core.taxonomy_creation_only_in_main_language');
@@ -67,7 +71,9 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
6771
$model = app($modelClass);
6872

6973
if (method_exists($model, 'createTranslation')) {
70-
$locale = $livewire->lang ?? app()->getLocale();
74+
$locale = $livewire->lang
75+
?? request()->query('lang')
76+
?? $mainLocale;
7177

7278
$translatableAttributes = property_exists($model, 'translatedAttributes') ? $model->translatedAttributes : [];
7379
$translationData = array_intersect_key($data, array_flip($translatableAttributes));
@@ -98,15 +104,16 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
98104
},
99105
];
100106

101-
$currentLocale = request()->query('lang') ?? app()->getLocale();
102-
$mainLocale = config('app.locale', 'en');
107+
$defaultLocalization = Localization::where('is_default', true)->first();
108+
$mainLocale = $defaultLocalization?->locale_variant ?? config('app.locale', 'en');
109+
$currentLocale = request()->query('lang') ?? $mainLocale;
103110
$canCreate = $currentLocale === $mainLocale;
104111

105112
if ($isHierarchical) {
106113
$selectTree = SelectTree::make($taxonomy)
107114
->relationship(
108115
relationship: $taxonomy,
109-
titleAttribute: 'title',
116+
titleAttribute: 'display_title',
110117
parentAttribute: 'parent_id'
111118
)
112119
->enableBranchNode()
@@ -124,20 +131,26 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
124131
$select = Select::make($taxonomy)
125132
->multiple()
126133
->options(function () use ($modelClass) {
127-
$locale = request()->query('lang') ?? app()->getLocale();
134+
$defaultLocalization = Localization::where('is_default', true)->first();
135+
$mainLocale = $defaultLocalization?->locale_variant ?? config('app.locale', 'en');
136+
$locale = request()->query('lang') ?? $mainLocale;
128137

129-
return app($modelClass)::with('translations')->get()->mapWithKeys(function ($item) use ($locale) {
138+
return app($modelClass)::with('translations')->get()->mapWithKeys(function ($item) use ($locale, $mainLocale) {
130139
if (method_exists($item, 'translations')) {
131140
$translation = $item->translations()->where('locale', $locale)->first();
132-
141+
$isFallback = false;
133142
if (! $translation || ! $translation->title) {
134-
$mainLocale = config('app.locale', 'en');
135143
$mainTranslation = $item->translations()->where('locale', $mainLocale)->first();
136144
$title = $mainTranslation && $mainTranslation->title ? $mainTranslation->title : 'ID: '.$item->id;
145+
$isFallback = true;
137146
} else {
138147
$title = $translation->title;
139148
}
140149

150+
if ($isFallback) {
151+
$title = $title.' ('.$mainLocale.')';
152+
}
153+
141154
return [$item->id => $title];
142155
}
143156

@@ -146,7 +159,9 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
146159
})
147160
->getSearchResultsUsing(
148161
function (string $search) use ($modelClass) {
149-
$locale = request()->query('lang') ?? app()->getLocale();
162+
$defaultLocalization = Localization::where('is_default', true)->first();
163+
$mainLocale = $defaultLocalization?->locale_variant ?? config('app.locale', 'en');
164+
$locale = request()->query('lang') ?? $mainLocale;
150165

151166
return app($modelClass)::query()
152167
->when(method_exists($modelClass, 'with'), fn ($query) => $query->with('translations'))
@@ -160,18 +175,22 @@ function (string $search) use ($modelClass) {
160175
})
161176
->limit(50)
162177
->get()
163-
->mapWithKeys(function ($item) use ($locale) {
178+
->mapWithKeys(function ($item) use ($locale, $mainLocale) {
164179
if (method_exists($item, 'translate')) {
165180
$translation = $item->translate($locale);
166-
181+
$isFallback = false;
167182
if (! $translation || ! $translation->title) {
168-
$mainLocale = config('app.locale', 'en');
169183
$mainTranslation = $item->translate($mainLocale);
170184
$title = $mainTranslation && $mainTranslation->title ? $mainTranslation->title : 'ID: '.$item->id;
185+
$isFallback = true;
171186
} else {
172187
$title = $translation->title;
173188
}
174189

190+
if ($isFallback) {
191+
$title = $title.' ('.$mainLocale.')';
192+
}
193+
175194
return [$item->id => $title];
176195
}
177196

@@ -211,7 +230,9 @@ public static function getTaxonomyFilters(): array
211230
->multiple()
212231
->options(
213232
function () use ($taxonomyModel, $pivotTable, $foreignKey, $relatedKey, $resourceTable) {
214-
$locale = request()->query('lang') ?? app()->getLocale();
233+
$defaultLocalization = Localization::where('is_default', true)->first();
234+
$mainLocale = $defaultLocalization?->locale_variant ?? config('app.locale', 'en');
235+
$locale = request()->query('lang') ?? $mainLocale;
215236

216237
$usedTaxonomyIds = DB::table($pivotTable)
217238
->join($resourceTable, $pivotTable.'.'.$foreignKey, '=', $resourceTable.'.id')
@@ -228,18 +249,22 @@ function () use ($taxonomyModel, $pivotTable, $foreignKey, $relatedKey, $resourc
228249
->whereIn('id', $usedTaxonomyIds)
229250
->when(method_exists($taxonomyModel, 'with'), fn ($query) => $query->with('translations'))
230251
->get()
231-
->mapWithKeys(function ($item) use ($locale) {
252+
->mapWithKeys(function ($item) use ($locale, $mainLocale) {
232253
if (method_exists($item, 'translate')) {
233254
$translation = $item->translate($locale);
234-
255+
$isFallback = false;
235256
if (! $translation || ! $translation->title) {
236-
$mainLocale = config('app.locale', 'en');
237257
$mainTranslation = $item->translate($mainLocale);
238258
$title = $mainTranslation && $mainTranslation->title ? $mainTranslation->title : 'ID: '.$item->id;
259+
$isFallback = true;
239260
} else {
240261
$title = $translation->title;
241262
}
242263

264+
if ($isFallback) {
265+
$title = $title.' ('.$mainLocale.')';
266+
}
267+
243268
return [$item->id => $title];
244269
}
245270

@@ -280,23 +305,45 @@ protected static function getTaxonomyColumns(): array
280305
$model = app($modelClass);
281306
$modelTable = $model->getTable();
282307

283-
$currentLocale = request()->get('lang') ?? app()->getLocale();
308+
$defaultLocalization = Localization::where('is_default', true)->first();
309+
$mainLocale = $defaultLocalization?->locale_variant ?? config('app.locale', 'en');
310+
$currentLocale = request()->get('lang') ?? $mainLocale;
284311

285-
return DB::table($table)
312+
$items = DB::table($table)
286313
->join($modelTable, sprintf('%s.%s', $table, $relatedKey), '=', $modelTable.'.id')
287314
->where(sprintf('%s.%s', $table, $foreignKey), $record->id)
288-
->when(method_exists($model, 'translations'), function ($query) use ($modelTable, $modelClass, $currentLocale) {
289-
$translationTable = strtolower(class_basename($modelClass)).'_translations';
290-
291-
return $query->leftJoin($translationTable, function ($join) use ($modelTable, $translationTable, $currentLocale, $modelClass) {
292-
$foreignKeyColumn = strtolower(class_basename($modelClass)).'_id';
293-
$join->on($translationTable.'.'.$foreignKeyColumn, '=', $modelTable.'.id')
294-
->where($translationTable.'.locale', '=', $currentLocale);
295-
})->pluck($translationTable.'.title');
296-
}, function ($query) use ($modelTable) {
297-
return $query->pluck($modelTable.'.title');
298-
})
315+
->select($modelTable.'.id')
316+
->pluck('id')
299317
->toArray();
318+
319+
if (! method_exists($model, 'translations')) {
320+
return DB::table($table)
321+
->join($modelTable, sprintf('%s.%s', $table, $relatedKey), '=', $modelTable.'.id')
322+
->where(sprintf('%s.%s', $table, $foreignKey), $record->id)
323+
->pluck($modelTable.'.title')
324+
->toArray();
325+
}
326+
327+
// For translatable models, build labels with fallback and (locale) suffix when needed
328+
$labels = [];
329+
foreach ($items as $itemId) {
330+
$entity = app($modelClass)::with('translations')->find($itemId);
331+
if (! $entity) {
332+
continue;
333+
}
334+
335+
$translation = $entity->translations->firstWhere('locale', $currentLocale);
336+
if ($translation && $translation->title) {
337+
$labels[] = $translation->title;
338+
continue;
339+
}
340+
341+
$mainTranslation = $entity->translations->firstWhere('locale', $mainLocale);
342+
$title = $mainTranslation && $mainTranslation->title ? $mainTranslation->title : 'ID: '.$entity->id;
343+
$labels[] = $title.' ('.$mainLocale.')';
344+
}
345+
346+
return $labels;
300347
})
301348
->toggleable(isToggledHiddenByDefault: true)
302349
->separator(',')

0 commit comments

Comments
 (0)