Add tags to application list, and fix home dashboard tag

This commit is contained in:
Chris Hunt
2024-02-19 10:58:19 +00:00
parent 09e4bb8cad
commit f3bc6ab618
4 changed files with 42 additions and 14 deletions

View File

@@ -47,7 +47,9 @@ class ItemController extends Controller
} elseif ($treat_tags_as == 'tags') { } elseif ($treat_tags_as == 'tags') {
$data['apps'] = Item::with('parents')->where('type', 0)->pinned()->orderBy('order', 'asc')->get(); $data['apps'] = Item::with('parents')->where('type', 0)->pinned()->orderBy('order', 'asc')->get();
$data['all_apps'] = Item::where('type', 0)->orderBy('order', 'asc')->get(); $data['all_apps'] = Item::where('type', 0)->orderBy('order', 'asc')->get();
$data['taglist'] = Item::where('type', 1)->pinned()->orderBy('order', 'asc')->get(); $data['taglist'] = Item::where('id', 0)->orWhere(function($query) {
$query->where('type', 1)->pinned();
})->orderBy('order', 'asc')->get();
} else { } else {
$data['apps'] = Item::whereHas('parents', function ($query) { $data['apps'] = Item::whereHas('parents', function ($query) {
@@ -56,7 +58,9 @@ class ItemController extends Controller
$data['all_apps'] = Item::whereHas('parents', function ($query) { $data['all_apps'] = Item::whereHas('parents', function ($query) {
$query->where('id', 0); $query->where('id', 0);
})->orWhere('type', 1)->orderBy('order', 'asc')->get(); })->orWhere(function ($query) {
$query->where('type', 1)->whereNot('id', 0);
})->orderBy('order', 'asc')->get();
} }
//$data['all_apps'] = Item::doesntHave('parents')->get(); //$data['all_apps'] = Item::doesntHave('parents')->get();

View File

@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Casts\Attribute;
use stdClass; use stdClass;
use Symfony\Component\ClassLoader\ClassMapGenerator; use Symfony\Component\ClassLoader\ClassMapGenerator;
@@ -133,26 +134,33 @@ class Item extends Model
$id = $this->id; $id = $this->id;
$tags = ItemTag::select('tag_id')->where('item_id', $id)->pluck('tag_id')->toArray(); $tags = ItemTag::select('tag_id')->where('item_id', $id)->pluck('tag_id')->toArray();
$tagdetails = self::select('id', 'title', 'url', 'pinned')->whereIn('id', $tags)->get(); $tagdetails = self::select('id', 'title', 'url', 'pinned')->whereIn('id', $tags)->get();
//print_r($tags);
if (in_array(0, $tags)) {
$details = new self([
'id' => 0,
'title' => __('app.dashboard'),
'url' => '',
'pinned' => 0,
]);
$tagdetails->prepend($details);
}
return $tagdetails; return $tagdetails;
} }
protected function title(): Attribute
{
return Attribute::make(
get: fn (string $value) => ($value === 'app.dashboard' ? __('app.dashboard') : $value),
);
}
protected function tagUrl(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes) => ($attributes['id'] === 0 ? '0-dash' : $attributes['url']),
);
}
public function getTagClass(): string public function getTagClass(): string
{ {
$tags = $this->tags(); $tags = $this->tags();
$slugs = []; $slugs = [];
foreach ($tags as $tag) { foreach ($tags as $tag) {
if ($tag->id === 0) {
$tag->url = '0-dash';
}
if ($tag->url) { if ($tag->url) {
$slugs[] = 'tag-'.$tag->url; $slugs[] = 'tag-'.$tag->url;
} }
@@ -161,6 +169,20 @@ class Item extends Model
return implode(' ', $slugs); return implode(' ', $slugs);
} }
public function getTagList(): string
{
$tags = $this->tags();
$titles = [];
// print_r($tags);
foreach ($tags as $tag) {
if ($tag->title) {
$titles[] = $tag->title;
}
}
return implode(', ', $titles);
}
public function parents(): BelongsToMany public function parents(): BelongsToMany
{ {
return $this->belongsToMany(Item::class, 'item_tag', 'item_id', 'tag_id'); return $this->belongsToMany(Item::class, 'item_tag', 'item_id', 'tag_id');

View File

@@ -22,6 +22,7 @@
<tr> <tr>
<th>{{ __('app.title') }}</th> <th>{{ __('app.title') }}</th>
<th>{{ __('app.url') }}</th> <th>{{ __('app.url') }}</th>
<th>{{ __('app.apps.tags') }}</th>
<th class="text-center" width="100">{{ __('app.settings.edit') }}</th> <th class="text-center" width="100">{{ __('app.settings.edit') }}</th>
<th class="text-center" width="100">{{ __('app.delete') }}</th> <th class="text-center" width="100">{{ __('app.delete') }}</th>
</tr> </tr>
@@ -32,6 +33,7 @@
<tr> <tr>
<td>{{ $app->title }}</td> <td>{{ $app->title }}</td>
<td><a href="{{ $app->url }}">{{ $app->link }}</a></td> <td><a href="{{ $app->url }}">{{ $app->link }}</a></td>
<td>{{ $app->getTagList() }}</td>
<td class="text-center"><a{{ $app->target }} href="{!! route('items.edit', [$app->id]) !!}" title="{{ __('app.settings.edit') }} {{ $app->title }}"><i class="fas fa-edit"></i></a></td> <td class="text-center"><a{{ $app->target }} href="{!! route('items.edit', [$app->id]) !!}" title="{{ __('app.settings.edit') }} {{ $app->title }}"><i class="fas fa-edit"></i></a></td>
<td class="text-center"> <td class="text-center">
{!! Form::open(['method' => 'DELETE','route' => ['items.destroy', $app->id],'style'=>'display:inline']) !!} {!! Form::open(['method' => 'DELETE','route' => ['items.destroy', $app->id],'style'=>'display:inline']) !!}
@@ -42,7 +44,7 @@
@endforeach @endforeach
@else @else
<tr> <tr>
<td colspan="4" class="form-error text-center"> <td colspan="5" class="form-error text-center">
<strong>{{ __('app.settings.no_items') }}</strong> <strong>{{ __('app.settings.no_items') }}</strong>
</td> </td>
</tr> </tr>

View File

@@ -6,7 +6,7 @@ $treat_tags_as = \App\Setting::fetch('treat_tags_as');
<div id="taglist" class="taglist"> <div id="taglist" class="taglist">
<div class="tag white current" data-tag="all">All</div> <div class="tag white current" data-tag="all">All</div>
@foreach($taglist as $tag) @foreach($taglist as $tag)
<div class="tag link{{ title_color($tag->colour) }}" style="background-color: {{ $tag->colour }}" data-tag="tag-{{ $tag->url }}">{{ $tag->title }}</div> <div class="tag link{{ title_color($tag->colour) }}" style="background-color: {{ $tag->colour }}" data-tag="tag-{{ $tag->tag_url }}">{{ $tag->title }}</div>
@endforeach @endforeach
</div> </div>
@endif @endif