71 lines
3.3 KiB
Python
71 lines
3.3 KiB
Python
import re
|
|
|
|
with open('WorkflowEngine.php', 'r') as f:
|
|
content = f.read()
|
|
|
|
content = content.replace(
|
|
'SELECT id, name, definition_json, is_active FROM process_definitions WHERE is_active = 1 ORDER BY sort_order, name',
|
|
'SELECT id, code, name, definition_json, is_active FROM process_definitions WHERE is_active = 1 AND is_latest = 1 ORDER BY sort_order, name'
|
|
)
|
|
|
|
content = content.replace(
|
|
"'name' => $def['name'],",
|
|
"'code' => $def['code'],\n 'name' => $def['name'],"
|
|
)
|
|
|
|
content = content.replace(
|
|
'$stmt_instances = $this->pdo->prepare("SELECT * FROM process__instances WHERE person_id IN ($placeholders)");\n $stmt_instances->execute($person_ids);\n $instances_data = $stmt_instances->fetchAll(PDO::FETCH_ASSOC);',
|
|
"""
|
|
$stmt_instances = $this->pdo->prepare(
|
|
SELECT pi.*, pd.code as process_code, pd.id as definition_actual_id
|
|
FROM process_instances pi
|
|
JOIN process_definitions pd ON pi.process_definition_id = pd.id
|
|
WHERE pi.person_id IN ($placeholders)
|
|
ORDER BY pi.last_activity_at DESC, pi.id DESC
|
|
");
|
|
$stmt_instances->execute($person_ids);
|
|
$instances_data = $stmt_instances->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Map code to latest definition id
|
|
$codeToIdMap = [];
|
|
foreach ($definitions as $defId => $def) {
|
|
$codeToIdMap[$def['code']] = $defId;
|
|
}""")
|
|
|
|
content = content.replace(
|
|
"$instances[$instance['person_id']][$def_id] = $enriched_instance;",
|
|
"""
|
|
// Use process_code to map to the latest active column
|
|
$code = $instance['process_code'] ?? null;
|
|
if ($code && isset($codeToIdMap[$code])) {
|
|
$latestDefId = $codeToIdMap[$code];
|
|
// Only keep the most recently active instance per code (since ordered by last_activity_at DESC)
|
|
if (!isset($instances[$instance['person_id']][$latestDefId])) {
|
|
$instances[$instance['person_id']][$latestDefId] = $enriched_instance;
|
|
}
|
|
}""")
|
|
|
|
content = re.sub(
|
|
r"public function getInstanceByDefId\(int \$personId, int \$processDefinitionId\): \?array \{.*?return \$instance \?: null;\s*\}",
|
|
"""public function getInstanceByDefId(int $personId, int $processDefinitionId): ?array {
|
|
$stmt_code = $this->pdo->prepare("SELECT code FROM process_definitions WHERE id = ?");
|
|
$stmt_code->execute([$processDefinitionId]);
|
|
$code = $stmt_code->fetchColumn();
|
|
|
|
if (!$code) return null;
|
|
|
|
$stmt = $this->pdo->prepare("\n SELECT pi.* \n FROM process_instances pi\n JOIN process_definitions pd ON pi.process_definition_id = pd.id\n WHERE pi.person_id = ? AND pd.code = ?\n ORDER BY pi.last_activity_at DESC, pi.id DESC\n LIMIT 1\n ");
|
|
$stmt->execute([$personId, $code]);
|
|
$instance = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
return $instance ?: null;
|
|
}""",
|
|
content, flags=re.DOTALL
|
|
)
|
|
|
|
# Wait, checkEligibility needs to use the NEW version definition ID?
|
|
# Actually checkEligibility uses $processDefinitionId which IS the new version's ID!
|
|
|
|
with open('WorkflowEngine.php', 'w') as f:
|
|
f.write(content)
|
|
|
|
print("Patched.") |