82 lines
4.1 KiB
PHP
82 lines
4.1 KiB
PHP
<?php
|
||
|
||
namespace Inhaus\Plugins\Kleinanzeigen;
|
||
|
||
use App\Dashboard\Widgets;
|
||
use App\Digest\Digest;
|
||
use App\Editorial\Editorial;
|
||
use App\Models\User;
|
||
use App\Plugins\Plugin;
|
||
use App\Search\GlobalSearch;
|
||
use App\Support\Abilities;
|
||
use App\Support\Navigation;
|
||
use Carbon\CarbonImmutable;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Schedule;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use Inhaus\Plugins\Kleinanzeigen\Models\Listing;
|
||
|
||
/**
|
||
* Kleinanzeigen – von Kolleginnen und Kollegen für Kolleginnen und Kollegen.
|
||
*/
|
||
class KleinanzeigenPlugin extends Plugin
|
||
{
|
||
public function register(): void
|
||
{
|
||
$this->app->make(Navigation::class)->add('kleinanzeigen', 'Kleinanzeigen', '/kleinanzeigen', active: 'kleinanzeigen.*', sort: 78);
|
||
$this->app->make(Abilities::class)->define('kleinanzeigen.moderate', 'Kleinanzeigen freigeben und moderieren', 'Kleinanzeigen', portal: true);
|
||
|
||
$this->app->make(GlobalSearch::class)->add('kleinanzeigen', 'Kleinanzeigen', 'shopping-bag', function (User $user, string $search, int $limit) {
|
||
$query = Listing::query()->visible();
|
||
GlobalSearch::whereTerms($query, ['title', 'description', 'location'], $search);
|
||
|
||
return $query->latest('published_at')->limit($limit)->get()->map(fn (Listing $l) => [
|
||
'title' => $l->title,
|
||
'url' => '/kleinanzeigen/'.$l->id,
|
||
'excerpt' => GlobalSearch::excerpt($l->description, $search),
|
||
'meta' => collect([Listing::TYPES[$l->type], $l->priceLabel(), $l->location])->filter()->implode(' · '),
|
||
])->all();
|
||
}, sort: 70);
|
||
|
||
// Startseite: die neuesten Anzeigen
|
||
$this->app->make(Widgets::class)->add('kleinanzeigen.latest', 'kleinanzeigen::Latest', 'aside', sort: 40, props: function () {
|
||
$listings = Listing::query()->visible()->with('images')->latest('published_at')->limit(4)->get();
|
||
|
||
return $listings->isEmpty() ? null : ['listings' => $listings->map(fn (Listing $l) => Presenter::card($l))->all()];
|
||
}, label: 'Kleinanzeigen', mobileSort: 45);
|
||
|
||
// Redaktion: was auf Freigabe wartet
|
||
$this->app->make(Editorial::class)->add('kleinanzeigen', 'Kleinanzeigen', 'kleinanzeigen::EditorialListings', 36,
|
||
fn (User $user) => $user->can('kleinanzeigen.moderate'),
|
||
fn () => ['pending' => Listing::query()->where('status', 'pending')->with('images')->oldest('updated_at')->limit(20)->get()->map(fn (Listing $l) => Presenter::card($l))->all()]);
|
||
|
||
// Ab Inhaus 0.42.0: Symbol, offene Freigaben als Zahl und „Neu anlegen“ in der Redaktion
|
||
$editorial = $this->app->make(Editorial::class);
|
||
|
||
if (method_exists($editorial, 'meta')) {
|
||
$editorial->meta('kleinanzeigen', icon: 'shopping-bag',
|
||
todo: function () {
|
||
$count = Listing::query()->where('status', 'pending')->count() + DB::table('kleinanzeigen_reports')->whereNull('resolved_at')->distinct()->count('listing_id');
|
||
|
||
return ['count' => $count, 'label' => $count === 1 ? 'Anzeige zu prüfen' : 'Anzeigen zu prüfen'];
|
||
},
|
||
create: fn () => ['label' => 'Anzeige aufgeben', 'href' => route('kleinanzeigen.create')]);
|
||
}
|
||
|
||
if (class_exists(Digest::class)) {
|
||
$this->app->make(Digest::class)->add('kleinanzeigen', 'Neue Kleinanzeigen', 60, fn (User $user, CarbonImmutable $since) => Listing::query()
|
||
->visible()->where('published_at', '>=', $since)->latest('published_at')->limit(6)->get()
|
||
->map(fn (Listing $l) => ['title' => $l->title, 'url' => route('kleinanzeigen.show', $l), 'meta' => collect([Listing::TYPES[$l->type], $l->priceLabel()])->filter()->implode(' · ')])->all());
|
||
}
|
||
}
|
||
|
||
public function boot(): void
|
||
{
|
||
Schedule::call(fn () => $this->app->make(Market::class)->housekeeping())->dailyAt('06:10')->name('kleinanzeigen-housekeeping')->withoutOverlapping(30);
|
||
}
|
||
|
||
public function uninstall(): void
|
||
{
|
||
Storage::disk(Photos::DISK)->deleteDirectory('kleinanzeigen');
|
||
}
|
||
}
|