tenant admin startseite schicker gestaltet und super-admin und tenant admin (filament) aufgesplittet.
Es gibt nun task collections und vordefinierte tasks für alle. Onboarding verfeinert und webseite-carousel gefixt (logging später entfernen!)
This commit is contained in:
157
app/Http/Controllers/Api/Tenant/EmotionController.php
Normal file
157
app/Http/Controllers/Api/Tenant/EmotionController.php
Normal file
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Tenant;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Tenant\EmotionStoreRequest;
|
||||
use App\Http\Requests\Tenant\EmotionUpdateRequest;
|
||||
use App\Http\Resources\Tenant\EmotionResource;
|
||||
use App\Models\Emotion;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class EmotionController extends Controller
|
||||
{
|
||||
public function index(Request $request): AnonymousResourceCollection
|
||||
{
|
||||
$tenantId = $request->tenant->id;
|
||||
|
||||
$query = Emotion::query()
|
||||
->whereNull('tenant_id')
|
||||
->orWhere('tenant_id', $tenantId)
|
||||
->with('eventTypes');
|
||||
|
||||
if ($request->boolean('only_tenant')) {
|
||||
$query->where('tenant_id', $tenantId);
|
||||
}
|
||||
|
||||
if ($request->boolean('only_global')) {
|
||||
$query->whereNull('tenant_id');
|
||||
}
|
||||
|
||||
$query->orderByRaw('tenant_id is null desc')->orderBy('sort_order')->orderBy('id');
|
||||
|
||||
$emotions = $query->paginate($request->integer('per_page', 25));
|
||||
|
||||
return EmotionResource::collection($emotions);
|
||||
}
|
||||
|
||||
public function store(EmotionStoreRequest $request): JsonResponse
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
$payload = [
|
||||
'tenant_id' => $request->tenant->id,
|
||||
'name' => $this->localizeValue($data['name']),
|
||||
'description' => $this->localizeValue($data['description'] ?? null, allowNull: true),
|
||||
'icon' => $data['icon'] ?? 'lucide-smile',
|
||||
'color' => $this->normalizeColor($data['color'] ?? '#6366f1'),
|
||||
'sort_order' => $data['sort_order'] ?? 0,
|
||||
'is_active' => $data['is_active'] ?? true,
|
||||
];
|
||||
|
||||
$emotion = null;
|
||||
|
||||
DB::transaction(function () use (&$emotion, $payload, $data) {
|
||||
$emotion = Emotion::create($payload);
|
||||
|
||||
if (! empty($data['event_type_ids'])) {
|
||||
$emotion->eventTypes()->sync($data['event_type_ids']);
|
||||
}
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'message' => __('Emotion erfolgreich erstellt.'),
|
||||
'data' => new EmotionResource($emotion->fresh('eventTypes')),
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function update(EmotionUpdateRequest $request, Emotion $emotion): JsonResponse
|
||||
{
|
||||
if ($emotion->tenant_id && $emotion->tenant_id !== $request->tenant->id) {
|
||||
abort(403, 'Emotion gehört nicht zu diesem Tenant.');
|
||||
}
|
||||
|
||||
if (is_null($emotion->tenant_id) && $request->hasAny(['name', 'description', 'icon', 'color', 'sort_order'])) {
|
||||
abort(403, 'Globale Emotions können nicht bearbeitet werden.');
|
||||
}
|
||||
|
||||
$data = $request->validated();
|
||||
|
||||
DB::transaction(function () use ($emotion, $data) {
|
||||
$update = [];
|
||||
|
||||
if (array_key_exists('name', $data)) {
|
||||
$update['name'] = $this->localizeValue($data['name'], allowNull: false, fallback: $emotion->name);
|
||||
}
|
||||
|
||||
if (array_key_exists('description', $data)) {
|
||||
$update['description'] = $this->localizeValue($data['description'], allowNull: true, fallback: $emotion->description);
|
||||
}
|
||||
|
||||
if (array_key_exists('icon', $data)) {
|
||||
$update['icon'] = $data['icon'] ?? $emotion->icon;
|
||||
}
|
||||
|
||||
if (array_key_exists('color', $data)) {
|
||||
$update['color'] = $this->normalizeColor($data['color'] ?? $emotion->color);
|
||||
}
|
||||
|
||||
if (array_key_exists('sort_order', $data)) {
|
||||
$update['sort_order'] = $data['sort_order'] ?? 0;
|
||||
}
|
||||
|
||||
if (array_key_exists('is_active', $data)) {
|
||||
$update['is_active'] = $data['is_active'];
|
||||
}
|
||||
|
||||
if (! empty($update)) {
|
||||
$emotion->update($update);
|
||||
}
|
||||
|
||||
if (array_key_exists('event_type_ids', $data)) {
|
||||
$emotion->eventTypes()->sync($data['event_type_ids'] ?? []);
|
||||
}
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'message' => __('Emotion aktualisiert.'),
|
||||
'data' => new EmotionResource($emotion->fresh('eventTypes')),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function localizeValue(mixed $value, bool $allowNull = false, ?array $fallback = null): ?array
|
||||
{
|
||||
if ($allowNull && ($value === null || $value === '')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$filtered = array_filter($value, static fn ($text) => is_string($text) && $text !== '');
|
||||
if (! empty($filtered)) {
|
||||
return $filtered;
|
||||
}
|
||||
|
||||
return $allowNull ? null : ($fallback ?? []);
|
||||
}
|
||||
|
||||
if (is_string($value) && $value !== '') {
|
||||
$locale = app()->getLocale() ?: 'de';
|
||||
return [$locale => $value];
|
||||
}
|
||||
|
||||
return $allowNull ? null : $fallback;
|
||||
}
|
||||
|
||||
protected function normalizeColor(string $color): string
|
||||
{
|
||||
$normalized = ltrim($color, '#');
|
||||
if (strlen($normalized) === 6) {
|
||||
return '#' . strtolower($normalized);
|
||||
}
|
||||
|
||||
return '#6366f1';
|
||||
}
|
||||
}
|
||||
94
app/Http/Controllers/Api/Tenant/TaskCollectionController.php
Normal file
94
app/Http/Controllers/Api/Tenant/TaskCollectionController.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Tenant;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Tenant\TaskCollectionResource;
|
||||
use App\Models\Event;
|
||||
use App\Models\TaskCollection;
|
||||
use App\Services\Tenant\TaskCollectionImportService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class TaskCollectionController extends Controller
|
||||
{
|
||||
public function index(Request $request): AnonymousResourceCollection
|
||||
{
|
||||
$tenantId = $request->tenant->id;
|
||||
|
||||
$query = TaskCollection::query()
|
||||
->forTenant($tenantId)
|
||||
->with('eventType')
|
||||
->withCount('tasks')
|
||||
->orderBy('position')
|
||||
->orderBy('id');
|
||||
|
||||
if ($search = $request->query('search')) {
|
||||
$query->where(function ($inner) use ($search) {
|
||||
$inner->where('name_translations->de', 'like', "%{$search}%")
|
||||
->orWhere('name_translations->en', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
if ($eventTypeSlug = $request->query('event_type')) {
|
||||
$query->whereHas('eventType', fn ($q) => $q->where('slug', $eventTypeSlug));
|
||||
}
|
||||
|
||||
if ($request->boolean('only_global')) {
|
||||
$query->whereNull('tenant_id');
|
||||
}
|
||||
|
||||
if ($request->boolean('only_tenant')) {
|
||||
$query->where('tenant_id', $tenantId);
|
||||
}
|
||||
|
||||
$perPage = $request->integer('per_page', 15);
|
||||
|
||||
return TaskCollectionResource::collection(
|
||||
$query->paginate($perPage)
|
||||
);
|
||||
}
|
||||
|
||||
public function show(Request $request, TaskCollection $collection): JsonResponse
|
||||
{
|
||||
$this->authorizeAccess($request, $collection);
|
||||
|
||||
$collection->load(['eventType', 'tasks' => fn ($query) => $query->with('assignedEvents')]);
|
||||
|
||||
return response()->json(new TaskCollectionResource($collection));
|
||||
}
|
||||
|
||||
public function activate(
|
||||
Request $request,
|
||||
TaskCollection $collection,
|
||||
TaskCollectionImportService $importService
|
||||
): JsonResponse {
|
||||
$this->authorizeAccess($request, $collection);
|
||||
|
||||
$data = $request->validate([
|
||||
'event_slug' => ['required', 'string', Rule::exists('events', 'slug')->where('tenant_id', $request->tenant->id)],
|
||||
]);
|
||||
|
||||
$event = Event::where('slug', $data['event_slug'])
|
||||
->where('tenant_id', $request->tenant->id)
|
||||
->firstOrFail();
|
||||
|
||||
$result = $importService->import($collection, $event);
|
||||
|
||||
return response()->json([
|
||||
'message' => __('Task-Collection erfolgreich importiert.'),
|
||||
'collection' => new TaskCollectionResource($result['collection']->load('eventType')->loadCount('tasks')),
|
||||
'created_task_ids' => $result['created_task_ids'],
|
||||
'attached_task_ids' => $result['attached_task_ids'],
|
||||
]);
|
||||
}
|
||||
|
||||
protected function authorizeAccess(Request $request, TaskCollection $collection): void
|
||||
{
|
||||
if ($collection->tenant_id && $collection->tenant_id !== $request->tenant->id) {
|
||||
abort(404);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,14 +23,27 @@ class TaskController extends Controller
|
||||
*/
|
||||
public function index(Request $request): AnonymousResourceCollection
|
||||
{
|
||||
$query = Task::where('tenant_id', $request->tenant->id)
|
||||
$tenantId = $request->tenant->id;
|
||||
|
||||
$query = Task::query()
|
||||
->where(function ($inner) use ($tenantId) {
|
||||
$inner->whereNull('tenant_id')
|
||||
->orWhere('tenant_id', $tenantId);
|
||||
})
|
||||
->with(['taskCollection', 'assignedEvents'])
|
||||
->orderByRaw('tenant_id is null desc')
|
||||
->orderBy('sort_order')
|
||||
->orderBy('created_at', 'desc');
|
||||
|
||||
// Search and filters
|
||||
if ($search = $request->get('search')) {
|
||||
$query->where('title', 'like', "%{$search}%")
|
||||
->orWhere('description', 'like', "%{$search}%");
|
||||
$query->where(function ($inner) use ($search) {
|
||||
$like = '%' . $search . '%';
|
||||
$inner->where('title->de', 'like', $like)
|
||||
->orWhere('title->en', 'like', $like)
|
||||
->orWhere('description->de', 'like', $like)
|
||||
->orWhere('description->en', 'like', $like);
|
||||
});
|
||||
}
|
||||
|
||||
if ($collectionId = $request->get('collection_id')) {
|
||||
@@ -55,15 +68,19 @@ class TaskController extends Controller
|
||||
*/
|
||||
public function store(TaskStoreRequest $request): JsonResponse
|
||||
{
|
||||
$task = Task::create(array_merge($request->validated(), [
|
||||
'tenant_id' => $request->tenant->id,
|
||||
]));
|
||||
$collectionId = $request->input('collection_id');
|
||||
$collection = $collectionId ? $this->resolveAccessibleCollection($request, $collectionId) : null;
|
||||
|
||||
if ($collectionId = $request->input('collection_id')) {
|
||||
$task->taskCollection()->associate(TaskCollection::findOrFail($collectionId));
|
||||
$task->save();
|
||||
$payload = $this->prepareTaskPayload($request->validated(), $request->tenant->id);
|
||||
$payload['tenant_id'] = $request->tenant->id;
|
||||
|
||||
if ($collection) {
|
||||
$payload['collection_id'] = $collection->id;
|
||||
$payload['source_collection_id'] = $collection->source_collection_id ?? $collection->id;
|
||||
}
|
||||
|
||||
$task = Task::create($payload);
|
||||
|
||||
$task->load(['taskCollection', 'assignedEvents']);
|
||||
|
||||
return response()->json([
|
||||
@@ -81,7 +98,7 @@ class TaskController extends Controller
|
||||
*/
|
||||
public function show(Request $request, Task $task): JsonResponse
|
||||
{
|
||||
if ($task->tenant_id !== $request->tenant->id) {
|
||||
if ($task->tenant_id && $task->tenant_id !== $request->tenant->id) {
|
||||
abort(404, 'Task nicht gefunden.');
|
||||
}
|
||||
|
||||
@@ -103,13 +120,18 @@ class TaskController extends Controller
|
||||
abort(404, 'Task nicht gefunden.');
|
||||
}
|
||||
|
||||
$task->update($request->validated());
|
||||
$collectionId = $request->input('collection_id');
|
||||
$collection = $collectionId ? $this->resolveAccessibleCollection($request, $collectionId) : null;
|
||||
|
||||
if ($collectionId = $request->input('collection_id')) {
|
||||
$task->taskCollection()->associate(TaskCollection::findOrFail($collectionId));
|
||||
$task->save();
|
||||
$payload = $this->prepareTaskPayload($request->validated(), $request->tenant->id, $task);
|
||||
|
||||
if ($collection) {
|
||||
$payload['collection_id'] = $collection->id;
|
||||
$payload['source_collection_id'] = $collection->source_collection_id ?? $collection->id;
|
||||
}
|
||||
|
||||
$task->update($payload);
|
||||
|
||||
$task->load(['taskCollection', 'assignedEvents']);
|
||||
|
||||
return response()->json([
|
||||
@@ -228,7 +250,7 @@ class TaskController extends Controller
|
||||
*/
|
||||
public function fromCollection(Request $request, TaskCollection $collection): AnonymousResourceCollection
|
||||
{
|
||||
if ($collection->tenant_id !== $request->tenant->id) {
|
||||
if ($collection->tenant_id && $collection->tenant_id !== $request->tenant->id) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
@@ -239,4 +261,98 @@ class TaskController extends Controller
|
||||
|
||||
return TaskResource::collection($tasks);
|
||||
}
|
||||
}
|
||||
|
||||
protected function resolveAccessibleCollection(Request $request, int|string $collectionId): TaskCollection
|
||||
{
|
||||
return TaskCollection::where('id', $collectionId)
|
||||
->where(function ($query) use ($request) {
|
||||
$query->whereNull('tenant_id');
|
||||
|
||||
if ($request->tenant?->id) {
|
||||
$query->orWhere('tenant_id', $request->tenant->id);
|
||||
}
|
||||
})
|
||||
->firstOrFail();
|
||||
}
|
||||
|
||||
protected function prepareTaskPayload(array $data, int $tenantId, ?Task $original = null): array
|
||||
{
|
||||
if (array_key_exists('title', $data)) {
|
||||
$data['title'] = $this->normalizeTranslations($data['title'], $original?->title);
|
||||
} elseif (array_key_exists('title_translations', $data)) {
|
||||
$data['title'] = $this->normalizeTranslations($data['title_translations'], $original?->title);
|
||||
}
|
||||
|
||||
if (array_key_exists('description', $data)) {
|
||||
$data['description'] = $this->normalizeTranslations($data['description'], $original?->description, true);
|
||||
} elseif (array_key_exists('description_translations', $data)) {
|
||||
$data['description'] = $this->normalizeTranslations(
|
||||
$data['description_translations'],
|
||||
$original?->description,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
if (array_key_exists('example_text', $data)) {
|
||||
$data['example_text'] = $this->normalizeTranslations($data['example_text'], $original?->example_text, true);
|
||||
} elseif (array_key_exists('example_text_translations', $data)) {
|
||||
$data['example_text'] = $this->normalizeTranslations(
|
||||
$data['example_text_translations'],
|
||||
$original?->example_text,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
unset(
|
||||
$data['title_translations'],
|
||||
$data['description_translations'],
|
||||
$data['example_text_translations']
|
||||
);
|
||||
|
||||
if (! array_key_exists('difficulty', $data) || $data['difficulty'] === null) {
|
||||
$data['difficulty'] = $original?->difficulty ?? 'easy';
|
||||
}
|
||||
|
||||
if (! array_key_exists('priority', $data) || $data['priority'] === null) {
|
||||
$data['priority'] = $original?->priority ?? 'medium';
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param array<string, string>|null $fallback
|
||||
*
|
||||
* @return array<string, string>|null
|
||||
*/
|
||||
protected function normalizeTranslations(mixed $value, ?array $fallback = null, bool $allowNull = false): ?array
|
||||
{
|
||||
if ($allowNull && ($value === null || $value === '')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$filtered = array_filter(
|
||||
$value,
|
||||
static fn ($text) => is_string($text) && $text !== ''
|
||||
);
|
||||
|
||||
if (! empty($filtered)) {
|
||||
return $filtered;
|
||||
}
|
||||
|
||||
return $allowNull ? null : ($fallback ?? []);
|
||||
}
|
||||
|
||||
if (is_string($value) && $value !== '') {
|
||||
$locale = app()->getLocale() ?: 'de';
|
||||
|
||||
return [
|
||||
$locale => $value,
|
||||
];
|
||||
}
|
||||
|
||||
return $allowNull ? null : $fallback;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user