hasPermission('r-task-cert-admin')) { return [ 'error' => 'Keine Berechtigung für die Aufgaben-Administration.', ]; } $taskId = TaskCertRequest::queryInt('id', 0); $services = task_cert_services(); $task = null; $rules = [ 'methods' => [], 'target_rules' => [], 'target_overrides' => [], 'reminder_policy' => [ 'base_mode' => 'due_date', 'first_escalation_days' => 0, 'step_days' => 7, 'max_level' => 3, 'active' => 1, ], ]; if ($taskId > 0) { $task = $services['taskDefinitionService']->getTaskDetail($taskId); if ($task === null) { return [ 'error' => 'Aufgabe nicht gefunden.', ]; } $rules = $task['rules'] ?? $rules; } $methodConfig = task_cert_normalize_method_config($rules['methods'] ?? []); $targetSelection = task_cert_normalize_target_selection($rules['target_rules'] ?? [], $rules['target_overrides'] ?? []); $targetSelection['responsible_contact_ids'] = is_array($rules['responsible_contact_ids'] ?? null) ? array_values(array_unique(array_map('intval', $rules['responsible_contact_ids']))) : []; $languageId = (int) ($GLOBALS['language']['id'] ?? 1); $lookupService = $services['lookupService'] ?? null; $lookup = ($lookupService instanceof TaskLookupService) ? $lookupService->getAdminFormLookups($languageId) : [ 'task_categories' => [], 'mandants' => [], 'departments' => [], 'roles' => [], 'contacts' => [], 'knowledge_posts' => [], ]; $taskCategoryId = (int) (($task['task_category_id'] ?? 0)); if ($taskCategoryId > 0) { $taskCategories = is_array($lookup['task_categories'] ?? null) ? $lookup['task_categories'] : []; $hasSelectedCategory = false; foreach ($taskCategories as $category) { if ((int) ($category['id'] ?? 0) === $taskCategoryId) { $hasSelectedCategory = true; break; } } if (!$hasSelectedCategory) { $taskCategories[] = [ 'id' => $taskCategoryId, 'name' => 'Kategorie #' . $taskCategoryId . ' (nicht gefunden)', 'slug' => '', 'sort_order' => 999999, 'active' => 0, ]; } $lookup['task_categories'] = $taskCategories; } return [ 'error' => null, 'task' => $task, 'rules' => $rules, 'method_config' => $methodConfig, 'target_selection' => $targetSelection, 'lookup' => $lookup, 'csrf_token' => TaskCertCsrf::token(), ]; } function task_cert_normalize_method_config(array $methods): array { $types = TaskCertConstants::METHOD_TYPES; $enabled = array_fill_keys($types, 0); $config = [ 'enabled' => $enabled, 'knowledge_post_ids' => [], 'form_fields' => [], 'quiz_questions' => [], ]; foreach ($methods as $method) { $type = (string) ($method['method_type'] ?? ''); if (!array_key_exists($type, $config['enabled'])) { continue; } $config['enabled'][$type] = ((int) ($method['active'] ?? 1) === 1) ? 1 : 0; if ($type === 'knowledge_read') { foreach (($method['knowledge_posts'] ?? []) as $post) { $postId = (int) ($post['knowledge_post_id'] ?? 0); if ($postId > 0) { $config['knowledge_post_ids'][] = $postId; } } } if ($type === 'form_submit') { $fields = is_array($method['form_fields'] ?? null) ? $method['form_fields'] : []; foreach ($fields as $field) { $options = []; foreach ((array) ($field['options'] ?? []) as $option) { if (is_array($option)) { $value = trim((string) ($option['value'] ?? $option['label'] ?? '')); if ($value !== '') { $options[] = $value; } } else { $value = trim((string) $option); if ($value !== '') { $options[] = $value; } } } $config['form_fields'][] = [ 'field_key' => (string) ($field['field_key'] ?? ''), 'label' => (string) ($field['label'] ?? ''), 'field_type' => (string) ($field['field_type'] ?? 'text'), 'is_required' => (int) ($field['is_required'] ?? 0), 'placeholder' => (string) ($field['placeholder'] ?? ''), 'prefill_column' => (string) ($field['prefill_column'] ?? ''), 'options_text' => implode(', ', $options), ]; } } if ($type === 'quiz') { foreach ((array) ($method['quiz_questions'] ?? []) as $question) { $normalizedQuestion = [ 'question_text' => (string) ($question['question_text'] ?? ''), 'option_a' => '', 'option_b' => '', 'option_c' => '', 'option_d' => '', 'correct_option' => 'a', ]; $options = array_values((array) ($question['options'] ?? [])); $letters = ['a', 'b', 'c', 'd']; foreach ($letters as $index => $letter) { if (!isset($options[$index])) { continue; } $option = $options[$index]; $normalizedQuestion['option_' . $letter] = (string) ($option['option_text'] ?? ''); if ((int) ($option['is_correct'] ?? 0) === 1) { $normalizedQuestion['correct_option'] = $letter; } } $config['quiz_questions'][] = $normalizedQuestion; } } } $config['knowledge_post_ids'] = array_values(array_unique(array_map('intval', $config['knowledge_post_ids']))); return $config; } function task_cert_normalize_target_selection(array $targetRules, array $targetOverrides): array { $selection = [ 'mandant_ids' => [], 'department_ids' => [], 'role_ids' => [], 'einricht_ids' => [], 'fachbereich_ids' => [], 'override_include_contact_ids' => [], 'override_exclude_contact_ids' => [], 'override_include_reason' => '', 'override_exclude_reason' => '', ]; foreach ($targetRules as $rule) { $mandantId = (int) ($rule['main_mandant_id'] ?? 0); $departmentId = (int) ($rule['main_department_id'] ?? 0); $roleId = (int) ($rule['main_role_id'] ?? 0); $einrichtId = (int) ($rule['main_einricht_id'] ?? 0); $fachbereichId = (int) ($rule['main_bereich_id'] ?? 0); if ($mandantId > 0) { $selection['mandant_ids'][] = $mandantId; } if ($departmentId > 0) { $selection['department_ids'][] = $departmentId; } if ($roleId > 0) { $selection['role_ids'][] = $roleId; } if ($einrichtId > 0) { $selection['einricht_ids'][] = $einrichtId; } if ($fachbereichId > 0) { $selection['fachbereich_ids'][] = $fachbereichId; } } foreach ($targetOverrides as $override) { $contactId = (int) ($override['main_contact_id'] ?? 0); if ($contactId <= 0) { continue; } $type = (string) ($override['override_type'] ?? ''); if ($type === 'include') { $selection['override_include_contact_ids'][] = $contactId; if ($selection['override_include_reason'] === '') { $selection['override_include_reason'] = (string) ($override['reason'] ?? ''); } } if ($type === 'exclude') { $selection['override_exclude_contact_ids'][] = $contactId; if ($selection['override_exclude_reason'] === '') { $selection['override_exclude_reason'] = (string) ($override['reason'] ?? ''); } } } $arrayKeys = [ 'mandant_ids', 'department_ids', 'role_ids', 'einricht_ids', 'fachbereich_ids', 'override_include_contact_ids', 'override_exclude_contact_ids', ]; foreach ($arrayKeys as $key) { $values = is_array($selection[$key] ?? null) ? $selection[$key] : []; $selection[$key] = array_values(array_unique(array_map('intval', $values))); } return $selection; }