['any', 'technical', 'billing'], 'internal' => ['any', 'specific']]; $required_user_id = 0; $refuse = $task_locked; if ($refuse === '' && !empty($task['task_completed_at'])) { $refuse = 'Task ' . $task['task_name'] . ' is done already.'; } elseif ($refuse === '' && !isset($types[$scope])) { $refuse = 'approval_scope is who approves it - client or internal.'; } elseif ($refuse === '' && !in_array($type, $types[$scope], true)) { $refuse = "approval_type for $scope is one of: " . implode(', ', $types[$scope]) . '.'; } elseif ($refuse === '' && $type === 'specific') { $required_user_id = ticketAgentId($_POST['approval_required_user_id'] ?? 0); if (!$required_user_id) { $refuse = 'approval_required_user_id is the agent who has to approve it - an active one, see technicians/read.php.'; } } elseif ($refuse === '' && $scope === 'client' && !$client_id) { $refuse = "An internal ticket has no client to approve it - use approval_scope internal."; } if ($refuse !== '') { http_response_code(400); $return_arr['success'] = "False"; $return_arr['message'] = $refuse; echo json_encode($return_arr); exit(); } $url_key = randomString(32); mysqli_query($mysqli, "INSERT INTO task_approvals SET approval_scope = '$scope', approval_type = '$type', approval_required_user_id = " . ($required_user_id ?: 'NULL') . ", approval_status = 'pending', approval_created_by = $session_user_id, approval_url_key = '$url_key', approval_task_id = $task_id"); $approval_id = intval(mysqli_insert_id($mysqli)); if ($approval_id) { $insert_id = $approval_id; $task_name = escapeSql($task['task_name']); $ticket_id = intval($task['ticket_id']); $by = html_entity_decode($api_key_name, ENT_QUOTES, 'UTF-8'); $by = stripslashes($session_name) . " (the API, $by)"; // The agent named hears in the app too if ($scope === 'internal' && $type === 'specific' && $required_user_id !== $session_user_id) { mysqli_query($mysqli, "INSERT INTO notifications SET notification_type = 'Ticket', notification = '" . escapeSql("$by needs your approval for ticket " . ticketReference($task) . " task " . $task['task_name']) . "', notification_action = 'ticket.php?ticket_id=$ticket_id', notification_client_id = 0, notification_user_id = $required_user_id"); } // Who is asked, by email (functions/ticket.php) $return_arr['emailed'] = intval(ticketMailTaskApproval($approval_id, $by)); logAudit("Task", "Edit", "Added a task approver for $task_name via API ($api_key_name)", $client_id, $task_id); logAudit("API", "Success", "Asked for approval of task $task_name via API ($api_key_name)", $client_id); } } // Output require_once '../create_output.php';