Files
espocrm/custom/scripts/workflow_manager.php
bsiggel 8c83e54650 Enhance documentation and workflow management in EspoCRM
- Update README.md to include a table of contents and detailed sections on workflow management and custom scripts.
- Remove deprecated 'runWorkflow' field from CVmhErstgespraech localization files.
- Add 'runWorkflow' field to detail layout for CVmhErstgespraech.
- Update workflow_manager.php to improve workflow listing and management functionality.
- Adjust cache timestamps in config.php.
- Create CUSTOM_DIRECTORY.md to outline the custom directory structure and best practices.
- Add README.md for workflow definitions with usage examples and JSON format specifications.
- Introduce new workflow definitions for 'vmh-erstberatung-abschließen' and its backup.
2026-01-20 23:58:29 +01:00

254 lines
8.4 KiB
PHP

<?php
/**
* Workflow Manager Script for EspoCRM
*
* Manages both BPM Flowcharts and Simple Workflows
*
* Usage: php workflow_manager.php <action> [options]
*
* Actions:
* list - List all workflows (BPM and Simple)
* read <id> - Read workflow details
* delete <id> - Delete a workflow
* import <file> - Import workflow from JSON file
* export <id> <file> - Export workflow to JSON file
*/
$config = require '/var/www/html/data/config-internal.php';
$dsn = 'mysql:host=' . $config['database']['host'] . ';dbname=' . $config['database']['dbname'];
$user = $config['database']['user'];
$pass = $config['database']['password'];
function connectDB() {
global $dsn, $user, $pass;
try {
$pdo = new PDO($dsn, $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
} catch (PDOException $e) {
die("DB Fehler: " . $e->getMessage() . "\n");
}
}
function listWorkflows($pdo) {
echo "=== BPM Flowcharts ===\n";
$stmt = $pdo->query("SELECT id, name, target_type, is_active FROM bpmn_flowchart WHERE deleted = 0");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $row) {
$active = $row['is_active'] ? '[AKTIV]' : '[INAKTIV]';
echo " {$active} ID: {$row['id']}, Name: {$row['name']}, Entity: {$row['target_type']}\n";
}
echo "\n=== Simple Workflows ===\n";
$stmt = $pdo->query("SELECT id, name, entity_type, type, is_active FROM workflow WHERE deleted = 0");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $row) {
$active = $row['is_active'] ? '[AKTIV]' : '[INAKTIV]';
$name = $row['name'] ?: '(unnamed)';
echo " {$active} ID: {$row['id']}, Name: {$name}, Entity: {$row['entity_type']}, Type: {$row['type']}\n";
}
}
function readWorkflow($pdo, $id) {
// Try BPM first
$stmt = $pdo->prepare("SELECT * FROM bpmn_flowchart WHERE id = ? AND deleted = 0");
$stmt->execute([$id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row) {
echo "=== BPM Flowchart ===\n";
echo json_encode($row, JSON_PRETTY_PRINT) . "\n";
return;
}
// Try Simple Workflow
$stmt = $pdo->prepare("SELECT * FROM workflow WHERE id = ? AND deleted = 0");
$stmt->execute([$id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row) {
echo "=== Simple Workflow ===\n";
echo json_encode($row, JSON_PRETTY_PRINT) . "\n";
return;
}
echo "Workflow nicht gefunden.\n";
}
function deleteWorkflow($pdo, $id) {
echo "Bist du sicher, dass du den Workflow $id löschen möchtest? (ja/nein): ";
$input = trim(fgets(STDIN));
if (strtolower($input) !== 'ja') {
echo "Abgebrochen.\n";
return;
}
// Try BPM first
$stmt = $pdo->prepare("UPDATE bpmn_flowchart SET deleted = 1 WHERE id = ?");
$stmt->execute([$id]);
if ($stmt->rowCount() > 0) {
echo "BPM Flowchart gelöscht.\n";
return;
}
// Try Simple Workflow
$stmt = $pdo->prepare("UPDATE workflow SET deleted = 1 WHERE id = ?");
$stmt->execute([$id]);
if ($stmt->rowCount() > 0) {
echo "Simple Workflow gelöscht.\n";
return;
}
echo "Workflow nicht gefunden.\n";
}
function importWorkflow($pdo, $file) {
if (!file_exists($file)) {
die("Datei nicht gefunden: $file\n");
}
$json = file_get_contents($file);
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
die("Ungültiges JSON in Datei.\n");
}
$type = $data['type'] ?? 'unknown';
$now = date('Y-m-d H:i:s');
if ($type === 'simple') {
// Import Simple Workflow
$id = substr(bin2hex(random_bytes(12)), 0, 17);
$stmt = $pdo->prepare("
INSERT INTO workflow (
id, name, entity_type, type, is_active, is_internal,
description, conditions_all, conditions_any, conditions_formula,
actions, portal_only, scheduling, scheduling_apply_timezone,
process_order, created_at, modified_at, deleted
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)
");
$stmt->execute([
$id,
$data['name'],
$data['entity_type'],
$data['trigger_type'],
$data['is_active'] ?? 1,
0,
$data['description'] ?? null,
json_encode($data['conditions_all'] ?? []),
json_encode($data['conditions_any'] ?? []),
$data['conditions_formula'] ?? null,
json_encode($data['actions'] ?? []),
0,
'0 0 * * *',
1,
10,
$now,
$now
]);
echo "✓ Simple Workflow importiert: $id - {$data['name']}\n";
} elseif ($type === 'bpm') {
// Import BPM Flowchart
$id = substr(bin2hex(random_bytes(12)), 0, 17);
$stmt = $pdo->prepare("
INSERT INTO bpmn_flowchart (
id, name, target_type, is_active,
created_at, modified_at, data, elements_data_hash,
deleted, has_none_start_event, event_start_id_list, event_start_all_id_list
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 0, '[]', ?)
");
$stmt->execute([
$id,
$data['name'],
$data['target_type'],
$data['is_active'] ?? 1,
$now,
$now,
json_encode($data['data']),
json_encode($data['elements_data_hash']),
json_encode($data['event_start_all_id_list'] ?? [])
]);
echo "✓ BPM Flowchart importiert: $id - {$data['name']}\n";
} else {
die("Unbekannter Workflow-Typ: $type\n");
}
}
function exportWorkflow($pdo, $id, $file) {
// Try BPM first
$stmt = $pdo->prepare("SELECT * FROM bpmn_flowchart WHERE id = ? AND deleted = 0");
$stmt->execute([$id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row) {
$export = [
'type' => 'bpm',
'name' => $row['name'],
'target_type' => $row['target_type'],
'is_active' => (bool)$row['is_active'],
'description' => $row['description'],
'data' => json_decode($row['data'], true),
'elements_data_hash' => json_decode($row['elements_data_hash'], true),
'event_start_all_id_list' => json_decode($row['event_start_all_id_list'], true)
];
file_put_contents($file, json_encode($export, JSON_PRETTY_PRINT));
echo "✓ BPM Flowchart exportiert nach: $file\n";
return;
}
// Try Simple Workflow
$stmt = $pdo->prepare("SELECT * FROM workflow WHERE id = ? AND deleted = 0");
$stmt->execute([$id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row) {
$export = [
'type' => 'simple',
'name' => $row['name'],
'entity_type' => $row['entity_type'],
'trigger_type' => $row['type'],
'is_active' => (bool)$row['is_active'],
'description' => $row['description'],
'conditions_all' => json_decode($row['conditions_all'], true),
'conditions_any' => json_decode($row['conditions_any'], true),
'conditions_formula' => $row['conditions_formula'],
'actions' => json_decode($row['actions'], true)
];
file_put_contents($file, json_encode($export, JSON_PRETTY_PRINT));
echo "✓ Simple Workflow exportiert nach: $file\n";
return;
}
echo "Workflow nicht gefunden.\n";
}
$pdo = connectDB();
$action = $argv[1] ?? 'list';
switch ($action) {
case 'list':
listWorkflows($pdo);
break;
case 'read':
if (!isset($argv[2])) die("Usage: read <id>\n");
readWorkflow($pdo, $argv[2]);
break;
case 'delete':
if (!isset($argv[2])) die("Usage: delete <id>\n");
deleteWorkflow($pdo, $argv[2]);
break;
case 'import':
if (!isset($argv[2])) die("Usage: import <file>\n");
importWorkflow($pdo, $argv[2]);
break;
case 'export':
if (!isset($argv[2]) || !isset($argv[3])) die("Usage: export <id> <file>\n");
exportWorkflow($pdo, $argv[2], $argv[3]);
break;
default:
echo "Unbekannte Aktion.\n";
echo "Verfügbare Aktionen: list, read, delete, import, export\n";
}
?>