- Create knowledgecenter_category_einricht, _fachbereich, knowledgecenter_post_einricht, knowledgecenter_post_fachbereich tables; extend task_target_rule with main_einricht_id and main_bereich_id columns - Middleware: load main_einricht_id/main_bereich_id from main_contact_department, expose $allowedEinrichts/$allowedFachbereiche globals (same pattern as existing 3 types) - Ajax: add update_category/post_einrichts/fachbereiche functions, extend update_category_links() and search filter query - Views: add einricht/fachbereich LEFT JOINs and WHERE filters in all 5 query sites (categories_posts_listform, category_posts_widget, post_announcement_listform, post_cardform userHasAccessForPost, Ajax search) - post_cardform_settings: add Einrichtungen/Fachbereiche tagify pickers with save/load - Tasks: add listEinrichts/listFachbereiche to repo+service, add UI selects in task_form, extend target-rule normalization and upsert builder, extend AssignmentResolverService scope loading and matchesAnyRule to include einricht/fachbereich matching Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
274 lines
9.0 KiB
PHP
274 lines
9.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once dirname(dirname(__DIR__)) . '/bootstrap.php';
|
|
|
|
function task_cert_admin_task_form_action(): array
|
|
{
|
|
$auth = TaskCertAuthContext::fromGlobals();
|
|
if (!$auth->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;
|
|
}
|