50 lines
1.5 KiB
PHP
50 lines
1.5 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
require_once 'lib/ErrorHandler.php';
|
|
|
|
session_start();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$processId = $_POST['process_id'] ?? null;
|
|
$name = $_POST['name'] ?? '';
|
|
$definition_json = $_POST['definition_json'] ?? '';
|
|
|
|
if (empty($name)) {
|
|
throw new WorkflowRuleFailedException('Process name is required.');
|
|
}
|
|
|
|
// Validate JSON
|
|
if (!empty($definition_json)) {
|
|
json_decode($definition_json);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
throw new WorkflowRuleFailedException('Invalid JSON format in definition.');
|
|
}
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
if (empty($processId)) {
|
|
// Create new process
|
|
$sql = 'INSERT INTO process_definitions (name, definition_json, is_active) VALUES (?, ?, 1)';
|
|
$params = [$name, $definition_json, 1];
|
|
$message = 'Process created successfully.';
|
|
} else {
|
|
// Update existing process
|
|
$sql = 'UPDATE process_definitions SET name = ?, definition_json = ? WHERE id = ?';
|
|
$params = [$name, $definition_json, $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();
|
|
}
|