diff --git a/app/Http/Livewire/Frontend/Library.php b/app/Http/Livewire/Frontend/Library.php index d33c4a2c..389ca49b 100644 --- a/app/Http/Livewire/Frontend/Library.php +++ b/app/Http/Livewire/Frontend/Library.php @@ -18,7 +18,9 @@ class Library extends Component public function render() { return view('livewire.frontend.library', [ - 'libraries' => \App\Models\Library::get() + 'libraries' => \App\Models\Library::query() + ->where('is_public', true) + ->get() ->prepend(\App\Models\Library::make([ 'name' => 'Alle', ])), diff --git a/app/Http/Livewire/Frontend/SearchByTagInLibrary.php b/app/Http/Livewire/Frontend/SearchByTagInLibrary.php index 2627172a..a5f9232a 100644 --- a/app/Http/Livewire/Frontend/SearchByTagInLibrary.php +++ b/app/Http/Livewire/Frontend/SearchByTagInLibrary.php @@ -18,7 +18,17 @@ class SearchByTagInLibrary extends Component { return view('livewire.frontend.search-by-tag-in-library', [ 'tags' => Tag::query() + ->with([ + 'libraryItems.libraries', + 'libraryItems.lecturer', + ]) + ->withCount([ + 'libraryItems', + ]) ->where('type', 'library_item') + ->whereHas('libraryItems.libraries', function ($query) { + $query->where('is_public', true); + }) ->get(), ]); } diff --git a/app/Http/Livewire/Tables/LibraryItemTable.php b/app/Http/Livewire/Tables/LibraryItemTable.php index 2c0abec2..f3271f98 100644 --- a/app/Http/Livewire/Tables/LibraryItemTable.php +++ b/app/Http/Livewire/Tables/LibraryItemTable.php @@ -4,10 +4,12 @@ namespace App\Http\Livewire\Tables; use App\Models\Library; use App\Models\LibraryItem; +use App\Models\Tag; use Illuminate\Database\Eloquent\Builder; use Rappasoft\LaravelLivewireTables\DataTableComponent; use Rappasoft\LaravelLivewireTables\Views\Column; use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn; +use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectFilter; use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter; class LibraryItemTable extends DataTableComponent @@ -19,15 +21,43 @@ class LibraryItemTable extends DataTableComponent { $this ->setPrimaryKey('id') - ->setDefaultSort('order_column', 'asc'); + ->setDefaultSort('order_column', 'asc') + ->setThAttributes(function (Column $column) { + return [ + 'class' => 'px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider dark:bg-gray-800 dark:text-gray-400', + 'default' => false, + ]; + }) + ->setTdAttributes(function (Column $column, $row, $columnIndex, $rowIndex) { + return [ + 'class' => 'px-6 py-4 text-sm font-medium dark:text-white', + 'default' => false, + ]; + }) + ->setColumnSelectStatus(false) + ->setPerPage(50); } public function filters(): array { return [ + MultiSelectFilter::make('Tag') + ->options( + Tag::query() + ->where('type', 'library_item') + ->get() + ->pluck('name', 'id') + ->toArray() + ) + ->filter(function (Builder $builder, array $values) { + $builder->whereHas('tags', function (Builder $query) use ($values) { + $query->whereIn('tags.id', $values); + }); + }), SelectFilter::make('Bibliothek') ->options( Library::query() + ->where('is_public', true) ->get() ->prepend(new Library(['name' => 'Alle'])) ->pluck('name', 'name') @@ -73,9 +103,10 @@ class LibraryItemTable extends DataTableComponent ->sortable(), Column::make("Art", "type") ->sortable(), - Column::make("Sprache", "language_code") - ->sortable(), - + Column::make("Tags") + ->label( + fn($row, Column $column) => view('columns.library_items.tags')->withRow($row) + ), Column::make('') ->label( fn($row, Column $column) => view('columns.library_items.action')->withRow($row) @@ -86,6 +117,7 @@ class LibraryItemTable extends DataTableComponent public function builder(): Builder { return LibraryItem::query() + ->whereHas('libraries', fn($query) => $query->where('libraries.is_public', true)) ->when($this->currentTab !== 'Alle', fn($query) => $query->whereHas('libraries', fn($query) => $query->where('libraries.name', $this->currentTab))) ->withCount([ diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 6a67af09..42bed46f 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -8,4 +8,9 @@ class Tag extends \Spatie\Tags\Tag { return $this->morphedByMany(Course::class, 'taggable'); } + + public function libraryItems() + { + return $this->morphedByMany(LibraryItem::class, 'taggable'); + } } diff --git a/app/Nova/Library.php b/app/Nova/Library.php index e419186a..5969c683 100644 --- a/app/Nova/Library.php +++ b/app/Nova/Library.php @@ -4,6 +4,7 @@ namespace App\Nova; use Illuminate\Http\Request; use Laravel\Nova\Fields\BelongsToMany; +use Laravel\Nova\Fields\Boolean; use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\MultiSelect; use Laravel\Nova\Fields\Text; @@ -47,6 +48,9 @@ class Library extends Resource Text::make('Name') ->rules('required', 'string'), + Boolean::make('Is public', 'is_public') + ->rules('required', 'boolean'), + MultiSelect::make('Languages', 'language_codes') ->options( config('languages.languages'), diff --git a/app/Nova/LibraryItem.php b/app/Nova/LibraryItem.php index cfeb1abf..f12d392b 100644 --- a/app/Nova/LibraryItem.php +++ b/app/Nova/LibraryItem.php @@ -7,6 +7,7 @@ use Ebess\AdvancedNovaMediaLibrary\Fields\Images; use Illuminate\Http\Request; use Laravel\Nova\Fields\BelongsTo; use Laravel\Nova\Fields\BelongsToMany; +use Laravel\Nova\Fields\Boolean; use Laravel\Nova\Fields\Code; use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Select; @@ -91,7 +92,7 @@ class LibraryItem extends Resource BelongsTo::make('Lecturer'), - BelongsToMany::make('Library'), + BelongsToMany::make('Library', 'libraries', Library::class), ]; } diff --git a/database/migrations/2022_12_05_160932_create_libraries_table.php b/database/migrations/2022_12_05_160932_create_libraries_table.php index eef762c5..e79296ff 100644 --- a/database/migrations/2022_12_05_160932_create_libraries_table.php +++ b/database/migrations/2022_12_05_160932_create_libraries_table.php @@ -15,6 +15,8 @@ class CreateLibrariesTable extends Migration Schema::create('libraries', function (Blueprint $table) { $table->id(); $table->string('name'); + $table->boolean('is_public') + ->default(true); $table->json('language_codes') ->default('[]'); $table->timestamps(); diff --git a/database/migrations/2022_12_05_160933_create_library_items_table.php b/database/migrations/2022_12_05_160933_create_library_items_table.php index 65a25a00..d2cd8106 100644 --- a/database/migrations/2022_12_05_160933_create_library_items_table.php +++ b/database/migrations/2022_12_05_160933_create_library_items_table.php @@ -24,7 +24,8 @@ class CreateLibraryItemsTable extends Migration $table->string('name'); $table->string('type'); $table->string('language_code'); - $table->longText('value'); + $table->longText('value') + ->nullable(); $table->timestamps(); }); diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 68f46f29..e9e9adbe 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -254,11 +254,28 @@ class DatabaseSeeder extends Seeder ]); $libraryItem = LibraryItem::create([ 'lecturer_id' => 4, - 'name' => 'Cryptography is Not Enough - Gigi @ Baltic Honeybadger 2022 ️', + 'name' => 'Cryptography is Not Enough - Gigi @ Baltic Honeybadger 2022 ', 'type' => 'youtube_video', 'language_code' => 'de', 'value' => 'https://www.youtube.com/watch?v=C7ynm0Zkwfk', ]); $libraryItem->syncTagsWithType(['Proof of Work'], 'library_item'); + $library->libraryItems() + ->attach($libraryItem); + $nonPublicLibrary = Library::create([ + 'name' => 'Einundzwanzig Dozenten', + 'is_public' => false, + 'language_codes' => ['de', 'en'], + ]); + $libraryItem = LibraryItem::create([ + 'lecturer_id' => 4, + 'name' => 'Präsentation: Bitcoin VHS Kurs 2022', + 'type' => 'downloadable_file', + 'language_code' => 'de', + 'value' => null, + ]); + $libraryItem->syncTagsWithType(['Präsentationen'], 'library_item'); + $nonPublicLibrary->libraryItems() + ->attach($libraryItem); } } diff --git a/public.png b/public.png index ba070b8f..c521b063 100644 Binary files a/public.png and b/public.png differ diff --git a/resources/views/columns/library_items/tags.blade.php b/resources/views/columns/library_items/tags.blade.php new file mode 100644 index 00000000..ade0e598 --- /dev/null +++ b/resources/views/columns/library_items/tags.blade.php @@ -0,0 +1,5 @@ +
+ @foreach($row->tags as $tag) + {{ $tag->name }} + @endforeach +
diff --git a/resources/views/livewire/frontend/search-by-tag-in-library.blade.php b/resources/views/livewire/frontend/search-by-tag-in-library.blade.php index 574a1190..a94518bd 100644 --- a/resources/views/livewire/frontend/search-by-tag-in-library.blade.php +++ b/resources/views/livewire/frontend/search-by-tag-in-library.blade.php @@ -13,7 +13,7 @@ @endphp
- 0 + {{ $tag->libraryItems->pluck('lecturer.name')->unique()->count() }} Dozenten - 0 + {{ $tag->library_items_count }} Inhalte