Skip to content

Commit

Permalink
feat: Allow to localize image tags
Browse files Browse the repository at this point in the history
  • Loading branch information
octfx committed Dec 22, 2023
1 parent 695bc42 commit 1442f87
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/V2/AbstractApiV2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ protected function setLocale(string $localeCode): void
{
if (in_array($localeCode, config('language.codes'), true)) {
$this->localeCode = $localeCode;
app()->setLocale(substr($localeCode, 0, 2));
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/V2/Rsi/CommLink/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Controllers\Api\V2\Rsi\CommLink;

use App\Http\Controllers\Api\V2\AbstractApiV2Controller;
use App\Http\Filters\ImageTagFilter;
use App\Http\Resources\AbstractBaseResource;
use App\Http\Resources\Rsi\CommLink\CommLinkResource;
use App\Http\Resources\Rsi\CommLink\Image\ImageResource;
Expand Down Expand Up @@ -44,7 +45,7 @@ public function index(Request $request): AnonymousResourceCollection
{
$query = QueryBuilder::for(Image::class, $request)
->allowedFilters([
AllowedFilter::partial('tags', 'tags.name'),
AllowedFilter::custom('tags', new ImageTagFilter()),
])
->orderByDesc('id')
->paginate($this->limit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ function (Builder $query) use ($request) {
*
* @return Factory|View
*/
public function indexByTag(Tag $tag)
public function indexByTag(Request $request)
{
$tag = Tag::query()->where('name', $request->tag)->orWhere('name_en', $request->tag)->first();

return view(
'user.rsi.comm_links.images.index',
[
'images' => $tag->images,
'images' => optional($tag)->images ?? [],
]
);
}
Expand Down
20 changes: 20 additions & 0 deletions app/Http/Filters/ImageTagFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace App\Http\Filters;

use Illuminate\Database\Eloquent\Builder;
use Spatie\QueryBuilder\Filters\Filter;

class ImageTagFilter implements Filter
{
/**
* @inheritDoc
*/
public function __invoke(Builder $query, $value, string $property)
{
$query->whereRelation('tags', 'name', $value)
->orWhereRelation('tags', 'name_en', $value);
}
}
2 changes: 1 addition & 1 deletion app/Http/Resources/Rsi/CommLink/Image/ImageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function toArray($request): array
'average_hash' => $this->hash->average_hash,
]),
$this->mergeWhen($this->whenLoaded('tags'), [
'tags' => $this->tags->map(fn ($tag) => $tag->name)
'tags' => $this->tags->map(fn ($tag) => $tag->translated_name)
]),
'similar_url' => $this->makeApiUrl(static::COMM_LINK_IMAGES_SIMILAR, $this->getRouteKey() . '/similar')
];
Expand Down
14 changes: 14 additions & 0 deletions app/Models/Rsi/CommLink/Image/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Tag extends Model

protected $fillable = [
'name',
'name_en',
];

protected $withCount = [
Expand All @@ -46,4 +47,17 @@ public function images(): BelongsToMany
{
return $this->belongsToMany(Image::class, 'comm_link_image_tag');
}

/**
* @return mixed
*/
public function getTranslatedNameAttribute()
{
$locale = app()->getLocale();
if ($locale === 'en' && !empty($this["name_$locale"])) {
return $this["name_$locale"];
}

return $this->name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function up(): void
Schema::create('comm_link_image_tags', static function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('name_en')->nullable();
$table->timestamps();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div style="position: relative;" class="comm-link-card-image">
@unless($image->tags->isEmpty())
<a href="{{ route('web.user.rsi.comm-links.images.index-by-tag', $image->tags->first()->getRouteKey()) }}" class="first-tag badge badge-secondary">
{{ $image->tags->first()->name }}
{{ $image->tags->first()->translated_name }}
</a>
@endunless
<span class="file-type badge badge-{{ $image->metadata->mime_class }}">{{ $image->metadata->mime }}</span>
Expand All @@ -16,7 +16,7 @@
<div style="position: relative;" class="comm-link-card-image">
@unless($image->tags->isEmpty())
<a href="{{ route('web.user.rsi.comm-links.images.index-by-tag', $image->tags->first()->getRouteKey()) }}" class="first-tag badge badge-secondary">
{{ $image->tags->first()->name }}
{{ $image->tags->first()->translated_name }}
</a>
@endunless
<span class="file-type badge badge-{{ $image->metadata->mime_class }}">{{ $image->metadata->mime }}</span>
Expand Down Expand Up @@ -91,7 +91,7 @@ class="card-img-top"
<div class="tag-container">
@foreach($image->tags as $tag)
<a class="badge badge-secondary m-0" href="{{ route('web.user.rsi.comm-links.images.index-by-tag', $tag->getRouteKey()) }}" title="{{ $tag->images_count }} @lang('Bilder mit diesem Tag')">
{{ $tag->name }}
{{ $tag->translated_name }}
</a>
@endforeach
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/user/rsi/comm_links/tags/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<span class="help-block d-block mb-2">@lang('Neue Einträge können durch Tippen in der Auswahl hinzugefügt werden.')</span>
<select class="form-select custom-select form-control" multiple size="15" name="tags[]" id="tags">
@foreach($tags as $tag)
<option value="id:{{ $tag->id }}" @php if ($image_tags->contains($tag->name)) echo "selected"; @endphp>{{ $tag->name }}</option>
<option value="id:{{ $tag->id }}" @php if ($image_tags->contains($tag->name)) echo "selected"; @endphp>{{ $tag->translated_name }}</option>
@endforeach
</select>
</div>
Expand Down
4 changes: 4 additions & 0 deletions resources/views/user/rsi/comm_links/tags/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<tr>
<th>@lang('ID')</th>
<th>@lang('Name')</th>
<th>@lang('Englisch') @lang('Name')</th>
<th>@lang('Bilder')</th>
<th data-orderable="false"></th>
</tr>
Expand All @@ -40,6 +41,9 @@
<td>
<a href="{{ route('web.user.rsi.comm-links.images.index-by-tag', $tag->getRouteKey()) }}">{{ $tag->name }}</a>
</td>
<td>
{{ $tag->name_en }}
</td>
<td>
{{ $tag->images_count }}
</td>
Expand Down

0 comments on commit 1442f87

Please sign in to comment.