kind instanceof CompositionKind ? $subject->kind : CompositionKind::tryFrom((string) $subject->kind)); } return null; } public static function compositionContext(?CompositionKind $kind): ?string { return match ($kind) { CompositionKind::Kurs => 'programm', CompositionKind::Uebung => 'modul', default => null, }; } /** * Auswahl der Materialart in einem Editor: aktive, im Kontext zulässige Arten in Pflegereihenfolge, dazu die * bereits zugeordnete Art mit Kennzeichnung, falls sie inaktiv oder hier nicht vorgesehen ist. * * @return array */ public function kindOptions(string $context, ?int $currentId = null): array { $options = []; foreach (EditorialContentKind::query()->orderBy('sort_order')->orderBy('id')->get() as $kind) { $offered = $kind->is_active && $kind->allows($context); if ($offered) { $options[$kind->id] = (string) $kind->label; } elseif ($currentId !== null && $kind->id === $currentId) { $options[$kind->id] = $this->kindLabel($kind, $context); } } return $options; } /** * Prüft eine neue Materialart serverseitig. Die bereits gespeicherte Art bleibt immer zulässig. */ public function kindError(string $context, ?int $newId, ?int $storedId): ?string { if ($newId === null || $newId === $storedId) { return null; } $kind = EditorialContentKind::query()->find($newId); if (! $kind instanceof EditorialContentKind) { return 'Diese fachliche Materialart gibt es nicht mehr.'; } if (! $kind->is_active) { return 'Die fachliche Materialart „'.$kind->label.'“ ist deaktiviert und kann nicht neu gewählt werden.'; } if (! $kind->allows($context)) { return 'Die fachliche Materialart „'.$kind->label.'“ ist für '.(EditorialContentKind::CONTEXTS[$context] ?? $context).' nicht vorgesehen.'; } return null; } /** * Filteroptionen der Materialart für einen Bestand: im Kontext zulässige Arten und Arten, die dort tatsächlich * vorkommen (gekennzeichnet), dazu ein bereits gewählter Filter. * * @return array */ public function kindFilterOptions(string $context, string $selected = ''): array { $used = $this->usedKindIds($context); $options = []; foreach (EditorialContentKind::query()->orderBy('sort_order')->orderBy('id')->get() as $kind) { $show = ($kind->allows($context) && $kind->is_active) || in_array($kind->id, $used, true) || (string) $kind->id === $selected; if ($show) { $options[(string) $kind->id] = $this->kindLabel($kind, $context); } } return $options; } public function kindLabel(EditorialContentKind $kind, string $context): string { return (string) $kind->label .(! $kind->is_active ? self::INACTIVE_SUFFIX : (! $kind->allows($context) ? self::OUT_OF_CONTEXT_SUFFIX : '')); } /** * @return list */ private function usedKindIds(string $context): array { $query = match ($context) { 'medium' => DB::table('content_elements'), 'lizenzartikel' => DB::table('license_articles'), 'programm' => DB::table('compositions')->where('kind', CompositionKind::Kurs->value), 'modul' => DB::table('compositions')->where('kind', CompositionKind::Uebung->value), default => null, }; return $query === null ? [] : $query->whereNotNull('editorial_content_kind_id')->distinct() ->pluck('editorial_content_kind_id')->map(fn ($id): int => (int) $id)->all(); } /** * Einordnung im Editor: je angebotener Dimension aktive Werte und bereits zugeordnete inaktive Werte (gekennzeichnet). * * @param list $assignedIds * @return list}> */ public function dimensionGroups(Model $subject, array $assignedIds): array { $groups = []; foreach (EditorialDimension::forSubject($subject) as $dimension) { $values = []; foreach ($dimension->values as $value) { if ($value->is_active || in_array((int) $value->id, $assignedIds, true)) { $values[] = [ 'id' => (int) $value->id, 'label' => (string) $value->label.($value->is_active ? '' : self::INACTIVE_SUFFIX), 'inactive' => ! $value->is_active, ]; } } if ($values !== []) { $groups[] = ['label' => (string) $dimension->label, 'values' => $values]; } } return $groups; } /** * Filtergruppen der Einordnung: aktive Werte, verwendete inaktive Werte (gekennzeichnet) und gewählte Filter. * * @param list $selectedIds * @return list}> */ public function dimensionFilterGroups(array $selectedIds = [], bool $withPillars = true): array { $used = DB::table('identity_dimension_values')->distinct()->pluck('editorial_dimension_value_id')->map(fn ($id): int => (int) $id)->all(); $groups = []; foreach (EditorialDimension::query()->with('values')->orderBy('label')->get() as $dimension) { if (! $withPillars && $dimension->slug === EditorialDimension::PILLAR_SLUG) { continue; } $values = []; foreach ($dimension->values as $value) { $id = (int) $value->id; if ($value->is_active || in_array($id, $used, true) || in_array($id, $selectedIds, true)) { $values[] = ['id' => $id, 'label' => (string) $value->label.($value->is_active ? '' : self::INACTIVE_SUFFIX)]; } } if ($values !== []) { $groups[] = ['label' => (string) $dimension->label, 'values' => $values]; } } return $groups; } /** * Öffentliche Filter der Mitgliederbibliothek: nur aktive Werte, ein bereits gewählter Wert bleibt sichtbar. * * @param list $selectedIds * @return Collection */ public function memberFilterDimensions(array $selectedIds = []): Collection { $dimensions = EditorialDimension::query()->with('values')->orderBy('label')->get(); foreach ($dimensions as $dimension) { $dimension->setRelation('values', $dimension->values ->filter(fn (EditorialDimensionValue $value): bool => $value->is_active || in_array((int) $value->id, $selectedIds, true)) ->values()); } return $dimensions->filter(fn (EditorialDimension $dimension): bool => $dimension->values->isNotEmpty())->values(); } /** * Für replaceOffered: Werte, die bei diesem Fachobjekt gesetzt oder entfernt werden dürfen. Aktive Werte der * angebotenen Dimensionen und die bereits zugeordneten inaktiven (der Editor zeigt sie gekennzeichnet an). * * @param list $assignedIds * @return list */ public function editableValueIds(Model $subject, array $assignedIds): array { $ids = []; foreach (EditorialDimension::forSubject($subject) as $dimension) { foreach ($dimension->values as $value) { if ($value->is_active || in_array((int) $value->id, $assignedIds, true)) { $ids[] = (int) $value->id; } } } return $ids; } }