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:
@@ -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