finale 2.0.0
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Tag;
|
||||
use App\Models\Individuo;
|
||||
use App\Models\Gruppo;
|
||||
use App\Models\Evento;
|
||||
use App\Models\Documento;
|
||||
use App\Models\MailingList;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class RicercaController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->authorizeRead('individui');
|
||||
|
||||
$allTags = Tag::orderBy('name')->get();
|
||||
|
||||
$tagSlugs = (array) $request->input('tag', []);
|
||||
|
||||
if (empty($tagSlugs)) {
|
||||
return view('ricerca.index', compact('allTags'));
|
||||
}
|
||||
|
||||
$tagSlugs = array_filter($tagSlugs);
|
||||
$selectedTags = Tag::whereIn('slug', $tagSlugs)->get();
|
||||
|
||||
$results = [];
|
||||
$totals = [];
|
||||
|
||||
if (count($tagSlugs) === 1) {
|
||||
$tag = $selectedTags->first();
|
||||
$results['individui'] = $tag->individui()->orderBy('cognome')->orderBy('nome')->get();
|
||||
$results['gruppi'] = $tag->gruppi()->orderBy('nome')->get();
|
||||
$results['eventi'] = $tag->eventi()->orderBy('data_specifica')->orderBy('nome_evento')->get();
|
||||
$results['documenti'] = $tag->documenti()->orderBy('nome_file')->get();
|
||||
$results['mailingLists'] = $tag->mailingLists()->orderBy('nome')->get();
|
||||
} else {
|
||||
$results['individui'] = Individuo::withAllTags($tagSlugs)->orderBy('cognome')->orderBy('nome')->get();
|
||||
$results['gruppi'] = Gruppo::withAllTags($tagSlugs)->orderBy('nome')->get();
|
||||
$results['eventi'] = Evento::withAllTags($tagSlugs)->orderBy('data_specifica')->orderBy('nome_evento')->get();
|
||||
$results['documenti'] = Documento::withAllTags($tagSlugs)->orderBy('nome_file')->get();
|
||||
$results['mailingLists'] = MailingList::withAllTags($tagSlugs)->orderBy('nome')->get();
|
||||
}
|
||||
|
||||
$totals = [
|
||||
'individui' => $results['individui']->count(),
|
||||
'gruppi' => $results['gruppi']->count(),
|
||||
'eventi' => $results['eventi']->count(),
|
||||
'documenti' => $results['documenti']->count(),
|
||||
'mailingLists' => $results['mailingLists']->count(),
|
||||
];
|
||||
|
||||
return view('ricerca.index', compact('selectedTags', 'tagSlugs', 'results', 'totals', 'allTags'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
|
||||
class Tag extends Model
|
||||
{
|
||||
protected $table = 'tags';
|
||||
|
||||
protected $fillable = ['name', 'slug', 'color', 'order_column'];
|
||||
|
||||
protected $casts = [
|
||||
'order_column' => 'integer',
|
||||
];
|
||||
|
||||
protected static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function (self $tag) {
|
||||
if (empty($tag->slug)) {
|
||||
$tag->slug = str()->slug($tag->name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function individui(): MorphToMany
|
||||
{
|
||||
return $this->morphedByMany(Individuo::class, 'taggable');
|
||||
}
|
||||
|
||||
public function gruppi(): MorphToMany
|
||||
{
|
||||
return $this->morphedByMany(Gruppo::class, 'taggable');
|
||||
}
|
||||
|
||||
public function eventi(): MorphToMany
|
||||
{
|
||||
return $this->morphedByMany(Evento::class, 'taggable');
|
||||
}
|
||||
|
||||
public function documenti(): MorphToMany
|
||||
{
|
||||
return $this->morphedByMany(Documento::class, 'taggable');
|
||||
}
|
||||
|
||||
public function mailingLists(): MorphToMany
|
||||
{
|
||||
return $this->morphedByMany(MailingList::class, 'taggable');
|
||||
}
|
||||
|
||||
public function getCountAttribute(): int
|
||||
{
|
||||
return $this->individui()->count()
|
||||
+ $this->gruppi()->count()
|
||||
+ $this->eventi()->count()
|
||||
+ $this->documenti()->count()
|
||||
+ $this->mailingLists()->count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
|
||||
trait HasTagsLight
|
||||
{
|
||||
public function tags(): MorphToMany
|
||||
{
|
||||
return $this->morphToMany(Tag::class, 'taggable');
|
||||
}
|
||||
|
||||
public function scopeWithAllTags($query, array $tags)
|
||||
{
|
||||
foreach ($tags as $tag) {
|
||||
$query->whereHas('tags', fn ($q) => $q->where('slug', $tag));
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function scopeWithAnyTags($query, array $tags)
|
||||
{
|
||||
$query->whereHas('tags', fn ($q) => $q->whereIn('slug', $tags));
|
||||
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user