$transition) { if (!isset($transition['from']) || !isset($transition['to'])) { http_response_code(422); throw new WorkflowRuleFailedException("Transition at index {$index} is missing 'from' or 'to' property."); } if (!isset($data['nodes'][$transition['from']])) { http_response_code(422); throw new WorkflowRuleFailedException("Transition from an unknown node: '{$transition['from']}'."); } if (!isset($data['nodes'][$transition['to']])) { http_response_code(422); throw new WorkflowRuleFailedException("Transition to an unknown node: '{$transition['to']}'."); } } } try { if ($_SERVER['REQUEST_METHOD'] === 'POST') { $processId = $_POST['process_id'] ?? null; $name = $_POST['name'] ?? ''; $definition_json = $_POST['definition_json'] ?? ''; validate_definition_json($definition_json); // Generate a simple code from the name $code = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $name))); if (empty($name)) { throw new WorkflowRuleFailedException('Process name is required.'); } $pdo = db(); $start_node = json_decode($definition_json, true)['start_node_id'] ?? null; if (empty($processId)) { // Create new process $sql = 'INSERT INTO process_definitions (name, code, definition_json, start_node_id, is_active) VALUES (?, ?, ?, ?, 1)'; $params = [$name, $code, $definition_json, $start_node]; $message = 'Process created successfully.'; } else { // Update existing process $is_active = isset($_POST['is_active']) ? (int)$_POST['is_active'] : 0; $sql = 'UPDATE process_definitions SET name = ?, definition_json = ?, start_node_id = ?, is_active = ? WHERE id = ?'; $params = [$name, $definition_json, $start_node, $is_active, $processId]; $message = 'Process updated successfully.'; } $stmt = $pdo->prepare($sql); $stmt->execute($params); if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false) { header('Content-Type: application/json'); echo json_encode(['message' => $message]); } else { $_SESSION['success_message'] = $message; header('Location: process_definitions.php'); exit(); } } } catch (WorkflowRuleFailedException $e) { header('Content-Type: application/json'); echo json_encode(['error' => $e->getMessage()]); }