Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Icon Suggestion #1001

Merged
merged 10 commits into from
Jan 28, 2025
25 changes: 25 additions & 0 deletions app/Facades/BookmarkCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

/**
* Class BookmarkCache
* @package App\Facades
*
* @see \App\Services\Caches\BookmarkCacheService
* @mixin \App\Services\Caches\BookmarkCacheService
*/
class BookmarkCache extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'bookmarkcache';
}
}
25 changes: 25 additions & 0 deletions app/Facades/EntityAssetCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

/**
* Class EntityAssetCache
* @package App\Facades
*
* @see \App\Services\Caches\EntityAssetCacheService
* @mixin \App\Services\Caches\EntityAssetCacheService
*/
class EntityAssetCache extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'entityassetcache';
}
}
6 changes: 6 additions & 0 deletions app/Models/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Facades\Dashboard;
use App\Models\Concerns\HasCampaign;
use App\Models\Concerns\HasFilters;
use App\Models\Concerns\HasSuggestions;
use App\Models\Concerns\LastSync;
use App\Models\Concerns\Orderable;
use App\Models\Concerns\Privatable;
Expand Down Expand Up @@ -54,6 +55,7 @@
use HasCampaign;
use HasFactory;
use HasFilters;
use HasSuggestions;
use LastSync;
use Orderable;
use Privatable;
Expand Down Expand Up @@ -121,6 +123,10 @@
'target',
];

protected array $suggestions = [
BookmarkCache::class => 'clearSuggestion',

Check failure on line 127 in app/Models/Bookmark.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Class App\Models\BookmarkCache not found.
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
];

/**
* Set to false if this entity type doesn't have relations
*/
Expand Down
7 changes: 7 additions & 0 deletions app/Models/EntityAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Models;

use App\Facades\EntityAssetCache;
use App\Facades\Img;
use App\Models\Concerns\Blameable;
use App\Models\Concerns\HasSuggestions;
use App\Models\Concerns\HasVisibility;
use App\Models\Concerns\Sanitizable;
use App\Models\Scopes\EntityAssetScopes;
Expand Down Expand Up @@ -38,6 +40,7 @@ class EntityAsset extends Model
use EntityAssetScopes;
use HasFactory;
use HasVisibility;
use HasSuggestions;
use Pinnable;
use Sanitizable;

Expand Down Expand Up @@ -66,6 +69,10 @@ class EntityAsset extends Model
'metadata.url',
];

protected array $suggestions = [
EntityAssetCache::class => 'clearSuggestion',
];

public function entity(): BelongsTo
{
return $this->belongsTo(Entity::class);
Expand Down
18 changes: 18 additions & 0 deletions app/Providers/CacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use App\Facades\CampaignLocalization;
use App\Services\Caches\AdCacheService;
use App\Services\Caches\BookmarkCacheService;
use App\Services\Caches\CampaignCacheService;
use App\Services\Caches\CharacterCacheService;
use App\Services\Caches\EntityAssetCacheService;
use App\Services\Caches\MarketplaceCacheService;
use App\Services\Caches\QuestCacheService;
use App\Services\Caches\MapMarkerCacheService;
Expand Down Expand Up @@ -75,6 +77,20 @@ public function register()
}
return $service;
});
$this->app->singleton(EntityAssetCacheService::class, function () {
$service = new EntityAssetCacheService();
if (CampaignLocalization::hasCampaign()) {
$service->campaign(CampaignLocalization::getCampaign());
}
return $service;
});
$this->app->singleton(BookmarkCacheService::class, function () {
$service = new BookmarkCacheService();
if (CampaignLocalization::hasCampaign()) {
$service->campaign(CampaignLocalization::getCampaign());
}
return $service;
});
$this->app->singleton(TimelineElementCacheService::class, function () {
$service = new TimelineElementCacheService();
if (CampaignLocalization::hasCampaign()) {
Expand All @@ -96,6 +112,8 @@ public function register()
$this->app->alias(FrontCacheService::class, 'frontcache');
$this->app->alias(AdCacheService::class, 'adcache');
$this->app->alias(MapMarkerCacheService::class, 'mapmarkercache');
$this->app->alias(EntityAssetCacheService::class, 'entityassetcache');
$this->app->alias(BookmarkCacheService::class, 'bookmarkcache');
$this->app->alias(TimelineElementCacheService::class, 'timelineelementcache');
$this->app->alias(MarketplaceCacheService::class, 'marketplacecache');
}
Expand Down
78 changes: 78 additions & 0 deletions app/Services/Caches/BookmarkCacheService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Services\Caches;

use App\Models\Bookmark;
use App\Traits\CampaignAware;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class BookmarkCacheService extends BaseCache
{
use CampaignAware;

/**
*/
public function iconSuggestion(): array
{
$key = $this->iconSuggestionKey();
if (Cache::has($key)) {
return Cache::get($key);
}

$default = [
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
'ra ra-tower',
'fa-solid fa-home',
'ra ra-capitol',
'ra ra-skull',
'fa-solid fa-coins',
'ra ra-beer',
'fa-solid fa-map-marker-alt',
'fa-solid fa-thumbtack',
'ra ra-wooden-sign',
'fa-solid fa-map-pin'
];


$data = Bookmark::where('campaign_id', $this->campaign->id)
->whereNotNull('icon')
->select(DB::raw('icon, MAX(created_at) as cmat'))
->groupBy('icon')
->orderBy('cmat', 'DESC')
->take(10)
->pluck('icon')
->all();


foreach ($default as $value) {
if (!in_array($value, $data)) {
$data[] = $value;
}
}

$data = array_slice($data, 0, 10);

Cache::put($key, $data, 24 * 3600);
return $data;
}

/**
* @return $this
*/
public function clearSuggestion(): self
{
$this->forget(
$this->iconSuggestionKey()
);
return $this;
}


/**
* Type suggestion cache key
*/
protected function iconSuggestionKey(): string
{
return 'campaign_' . $this->campaign->id . '_bookmark_suggestions';
}
}
83 changes: 83 additions & 0 deletions app/Services/Caches/EntityAssetCacheService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Services\Caches;

use App\Models\EntityAsset;
use App\Traits\CampaignAware;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class EntityAssetCacheService extends BaseCache
{
use CampaignAware;

/**
*/
public function iconSuggestion(): array
{
$key = $this->iconSuggestionKey();
if (Cache::has($key)) {
return Cache::get($key);
}

$default = [
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
'ra ra-tower',
'fa-solid fa-home',
'ra ra-capitol',
'ra ra-skull',
'fa-solid fa-coins',
'ra ra-beer',
'fa-solid fa-map-marker-alt',
'fa-solid fa-thumbtack',
'ra ra-wooden-sign',
'fa-solid fa-map-pin'
];

$settings = EntityAsset::leftJoin('entities as e', 'e.id', 'entity_assets.entity_id')
->where('e.campaign_id', $this->campaign->id)
->select(DB::raw('metadata, MAX(entity_assets.created_at) as cmat'))
->groupBy('metadata')
->whereNotNull('metadata')
->where('entity_assets.type_id', EntityAsset::TYPE_LINK)
->orderBy('cmat', 'DESC')
->take(10)
->pluck('metadata')
->all();


foreach ($settings as $setting) {
$data[] = $setting['icon'];
}

foreach ($default as $value) {
if (!in_array($value, $data)) {

Check failure on line 53 in app/Services/Caches/EntityAssetCacheService.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Variable $data might not be defined.
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
$data[] = $value;
}
}

$data = array_slice($data, 0, 10);

Cache::put($key, $data, 24 * 3600);
return $data;
}

/**
* @return $this
*/
public function clearSuggestion(): self
{
$this->forget(
$this->iconSuggestionKey()
);
return $this;
}


/**
* Type suggestion cache key
*/
protected function iconSuggestionKey(): string
{
return 'campaign_' . $this->campaign->id . '_entity_asset_suggestions';
}
}
2 changes: 1 addition & 1 deletion resources/views/bookmarks/form/_entry.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<input type="text" name="icon" value="{{ old('text', $source->icon ?? $model->icon ?? null) }}" placeholder="fa-solid fa-users" list="link-icon-list" data-paste="fontawesome" maxlength="45" />
<div class="hidden">
<datalist id="link-icon-list">
@foreach (\App\Facades\MapMarkerCache::iconSuggestion() as $icon)
@foreach (\App\Facades\BookmarkCache::iconSuggestion() as $icon)
<option value="{{ $icon }}">{{ $icon }}</option>
@endforeach
</datalist>
Expand Down
7 changes: 0 additions & 7 deletions resources/views/campaigns/modules/_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,5 @@
field="icon"
:label="__('campaigns/modules.fields.icon')">
<input type="text" name="icon" value="{{ old('icon', $icon) }}" maxlength="40" class="w-full" list="module-icon-list" />
<div class="hidden">
<datalist id="module-icon-list">
@foreach (\App\Facades\MapMarkerCache::iconSuggestion() as $icon)
<option value="{{ $icon }}">{{ $icon }}</option>
@endforeach
</datalist>
</div>
</x-forms.field>
</x-grid>
9 changes: 8 additions & 1 deletion resources/views/cruds/fields/icon.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
field="icon"
:label="__('entities/links.fields.icon')">
@if($campaign->boosted())
<input type="text" name="{{ $fieldname }}" value="{{ !isset($bulk) ? old($fieldname, $source->{$fieldname} ?? $model->icon ?? null) : null }}" placeholder="{{ $placeholder ?? 'fa-solid fa-users' }}" class="w-full" autocomplete="off" data-paste="fontawesome" maxlength="45" />
<input type="text" name="{{ $fieldname }}" value="{{ !isset($bulk) ? old($fieldname, $source->{$fieldname} ?? $model->icon ?? null) : null }}" placeholder="{{ $placeholder ?? 'fa-solid fa-users' }}" list="link-icon-list" class="w-full" autocomplete="off" data-paste="fontawesome" maxlength="45" />
<div class="hidden">
<datalist id="link-icon-list">
@foreach (\App\Facades\EntityAssetCache::iconSuggestion() as $icon)
<option value="{{ $icon }}">{{ $icon }}</option>
@endforeach
</datalist>
</div>
<x-helper>
{!! __('entities/links.helpers.icon', [
'fontawesome' => '<a target="_blank" href="' . config('fontawesome.search') .'">FontAwesome</a>',
Expand Down
Loading