61 lines
2.9 KiB
Python
61 lines
2.9 KiB
Python
import re
|
|
|
|
with open('_save_process_definition.php', 'r') as f:
|
|
content = f.read()
|
|
|
|
old_update_logic = """ 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);"""
|
|
|
|
new_update_logic = """ if (empty($processId)) {
|
|
// Create new process
|
|
$sql = 'INSERT INTO process_definitions (name, code, definition_json, start_node_id, is_active, version, is_latest) VALUES (?, ?, ?, ?, 1, 1, 1)';
|
|
$params = [$name, $code, $definition_json, $start_node];
|
|
$message = 'Process created successfully.';
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
} else {
|
|
// "Update" existing process by creating a new version
|
|
$stmt_old = $pdo->prepare('SELECT code, version, sort_order, is_active FROM process_definitions WHERE id = ?');
|
|
$stmt_old->execute([$processId]);
|
|
$old = $stmt_old->fetch();
|
|
|
|
if ($old) {
|
|
$is_active = isset($_POST['is_active']) ? (int)$_POST['is_active'] : $old['is_active'];
|
|
$new_version = $old['version'] + 1;
|
|
$db_code = $old['code'];
|
|
|
|
// Mark all previous versions as not latest
|
|
$stmt_update = $pdo->prepare('UPDATE process_definitions SET is_latest = 0 WHERE code = ?');
|
|
$stmt_update->execute([$db_code]);
|
|
|
|
// Insert new version
|
|
$sql = 'INSERT INTO process_definitions (name, code, definition_json, start_node_id, is_active, version, supersedes_definition_id, is_latest, sort_order) VALUES (?, ?, ?, ?, ?, ?, ?, 1, ?)';
|
|
$params = [$name, $db_code, $definition_json, $start_node, $is_active, $new_version, $processId, $old['sort_order']];
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
$message = 'Process updated successfully (new version created).';
|
|
} else {
|
|
throw new WorkflowRuleFailedException('Process not found.');
|
|
}
|
|
}"""
|
|
|
|
content = content.replace(old_update_logic, new_update_logic)
|
|
|
|
with open('_save_process_definition.php', 'w') as f:
|
|
f.write(content)
|
|
|
|
print("Patched.")
|