A lot of things
This commit is contained in:
64
app/Livewire/ActivityLogs.php
Normal file
64
app/Livewire/ActivityLogs.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use Livewire\Attributes\Url;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
use Spatie\Activitylog\Models\Activity;
|
||||
|
||||
class ActivityLogs extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
#[Url(except: '')]
|
||||
public string $search = '';
|
||||
|
||||
#[Url(except: '')]
|
||||
public string $logName = '';
|
||||
|
||||
#[Url(except: '')]
|
||||
public string $event = '';
|
||||
|
||||
#[Url(except: '')]
|
||||
public string $dateFrom = '';
|
||||
|
||||
#[Url(except: '')]
|
||||
public string $dateTo = '';
|
||||
|
||||
#[Url(except: '')]
|
||||
public string $causerId = '';
|
||||
|
||||
public function updating(): void { $this->resetPage(); }
|
||||
|
||||
public function clearFilters(): void
|
||||
{
|
||||
$this->reset('search', 'logName', 'event', 'dateFrom', 'dateTo', 'causerId');
|
||||
$this->resetPage();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$logs = Activity::query()
|
||||
->with(['causer'])
|
||||
->when($this->search, fn($q) => $q
|
||||
->where('description', 'like', "%{$this->search}%")
|
||||
->orWhere('subject_type', 'like', "%{$this->search}%")
|
||||
->orWhere('log_name', 'like', "%{$this->search}%")
|
||||
)
|
||||
->when($this->logName, fn($q) => $q->where('log_name', $this->logName))
|
||||
->when($this->event, fn($q) => $q->where('event', $this->event))
|
||||
->when($this->causerId, fn($q) => $q->where('causer_id', $this->causerId))
|
||||
->when($this->dateFrom, fn($q) => $q->whereDate('created_at', '>=', $this->dateFrom))
|
||||
->when($this->dateTo, fn($q) => $q->whereDate('created_at', '<=', $this->dateTo))
|
||||
->latest()
|
||||
->paginate(50);
|
||||
|
||||
$logNames = Activity::distinct()->orderBy('log_name')->pluck('log_name')->filter()->values();
|
||||
$events = Activity::distinct()->orderBy('event')->pluck('event')->filter()->values();
|
||||
|
||||
$hasFilters = $this->logName || $this->event || $this->dateFrom || $this->dateTo || $this->causerId;
|
||||
|
||||
return view('livewire.activity-logs', compact('logs', 'logNames', 'events', 'hasFilters'));
|
||||
}
|
||||
}
|
||||
72
app/Livewire/EntrySelector.php
Normal file
72
app/Livewire/EntrySelector.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Models\Entry;
|
||||
use Livewire\Component;
|
||||
|
||||
class EntrySelector extends Component
|
||||
{
|
||||
public string $search = '';
|
||||
public ?int $selectedEntryId = null;
|
||||
|
||||
public ?string $entryName = null;
|
||||
|
||||
public bool $dropdown = false;
|
||||
|
||||
public function mount( ?int $oldEntryId = null ): void
|
||||
{
|
||||
if( $oldEntryId ) {
|
||||
$entry = Entry::find($oldEntryId);
|
||||
if( $entry ) {
|
||||
$this->selectedEntryId = $oldEntryId;
|
||||
$this->entryName = $entry->complete_title ?? $entry->title;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function updatedSearch(): void
|
||||
{
|
||||
if( $this->selectedEntryId ) {
|
||||
$this->selectedEntryId = null;
|
||||
$this->entryName = null;
|
||||
}
|
||||
$this->dropdown = strlen($this->search) > 2;
|
||||
}
|
||||
|
||||
public function selectEntry( int $id ){
|
||||
$entry = Entry::find($id);
|
||||
if( $entry ) {
|
||||
$this->selectedEntryId = $id;
|
||||
$this->entryName = $entry->complete_title ?? $entry->title;
|
||||
$this->search = $entry->complete_title ?? $entry->title;
|
||||
$this->dropdown = false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function clearEntry(): void
|
||||
{
|
||||
$this->selectedEntryId = null;
|
||||
$this->entryName = null;
|
||||
$this->search = '';
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$entries = collect();
|
||||
|
||||
if( $this->dropdown && strlen($this->search) > 2 ) {
|
||||
$entries = Entry::where('complete_title', 'like', '%' . $this->search . '%')
|
||||
->orWhere('title', 'like', '%' . $this->search . '%')
|
||||
->orderBy('complete_title', 'asc')
|
||||
->orderBy('title', 'asc')
|
||||
->limit(20)
|
||||
->get();
|
||||
}
|
||||
|
||||
$data = [ 'entries' => $entries, 'required_chars' => 3 ];
|
||||
|
||||
return view('livewire.entry-selector', $data);
|
||||
}
|
||||
}
|
||||
104
app/Livewire/NewsDatabase.php
Normal file
104
app/Livewire/NewsDatabase.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\News;
|
||||
use Livewire\Attributes\Url;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
|
||||
class NewsDatabase extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
/**
|
||||
* News title search
|
||||
* @var string
|
||||
*/
|
||||
#[Url(as: 's', except: '')]
|
||||
public string $search = '';
|
||||
|
||||
/**
|
||||
* Categories IDs filter.
|
||||
* @var array
|
||||
*/
|
||||
#[Url(except:[])]
|
||||
public array $categories = [];
|
||||
|
||||
/**
|
||||
* Sort by field.
|
||||
* @var string
|
||||
*/
|
||||
#[Url(as: 'sort',except: 'created_at')]
|
||||
public string $sortBy = 'created_at';
|
||||
|
||||
/**
|
||||
* asc/desc
|
||||
* @var string
|
||||
*/
|
||||
#[Url(as: 'dir',except: 'desc')]
|
||||
public string $sortDir = 'desc';
|
||||
|
||||
/**
|
||||
* Translation of sort options key.
|
||||
*/
|
||||
public const array SORT_OPTIONS = [
|
||||
'created_at' => 'Date added',
|
||||
'title' => 'Title'
|
||||
];
|
||||
|
||||
public const int PAGINATION = 30;
|
||||
|
||||
public function updatedSearch(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||||
public function updatedCategories(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||||
public function updatedCategoriesMode(): void { $this->resetPage(); $this->dispatch('filters-updated'); }
|
||||
|
||||
public function clearFilters(): void
|
||||
{
|
||||
$this->reset([
|
||||
'search', 'categories',
|
||||
]);
|
||||
$this->resetPage();
|
||||
}
|
||||
|
||||
public function setSort(string $field): void
|
||||
{
|
||||
if( $this->sortBy === $field ) {
|
||||
$this->sortDir = $this->sortDir === 'asc' ? 'desc' : 'asc';
|
||||
} else {
|
||||
$this->sortBy = $field;
|
||||
$this->sortDir = 'asc';
|
||||
}
|
||||
$this->resetPage();
|
||||
$this->dispatch('filters-updated');
|
||||
}
|
||||
|
||||
private function buildQuery()
|
||||
{
|
||||
$query = News::query()->published();
|
||||
|
||||
if( $this->search ){
|
||||
$query->where(function($q){
|
||||
$q->where('title', 'like', '%'.$this->search.'%');
|
||||
});
|
||||
}
|
||||
|
||||
if( $this->categories ) {
|
||||
$query->whereIn('category_id', $this->categories);
|
||||
}
|
||||
|
||||
return $query->orderBy($this->sortBy, $this->sortDir);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.news-database', [
|
||||
'news' => $this->buildQuery()->paginate(self::PAGINATION),
|
||||
'allCategories' => Category::where(function ($query) {
|
||||
$query->whereJsonContains('restricted_to', "news")
|
||||
->orWhereNull('restricted_to');
|
||||
})->orderBy('name')->get()
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user