73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../config/config_enhanced.php';
|
|
require_once __DIR__ . '/../config/config.php';
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
if (function_exists('requireAuthentication')) requireAuthentication();
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true) ?: $_POST;
|
|
|
|
// Allow single id or array of ids
|
|
$ids = [];
|
|
if (isset($input['id'])) {
|
|
if (is_array($input['id'])) {
|
|
$ids = array_map('intval', $input['id']);
|
|
} else {
|
|
$ids = [intval($input['id'])];
|
|
}
|
|
}
|
|
|
|
if (empty($ids) || array_filter($ids) === []) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'id is required']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$db = Database::getInstance();
|
|
|
|
// Verify existence
|
|
$placeholders = implode(',', array_fill(0, count($ids), '?'));
|
|
$existing = $db->fetchAll("SELECT id, name, template_name FROM message_templates WHERE id IN ($placeholders)", $ids);
|
|
if (!$existing) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'error' => 'No templates found for given id(s)']);
|
|
exit;
|
|
}
|
|
|
|
// Start transaction
|
|
$db->beginTransaction();
|
|
try {
|
|
$db->execute("DELETE FROM message_templates WHERE id IN ($placeholders)", $ids);
|
|
|
|
// Log operator activity if session present
|
|
try {
|
|
$operatorId = $_SESSION['admin_user']['id'] ?? null;
|
|
$now = date('Y-m-d H:i:s');
|
|
foreach ($existing as $t) {
|
|
$db->insert('operator_activity', [
|
|
'user_id' => null,
|
|
'operator_id' => $operatorId,
|
|
'action' => 'delete_template',
|
|
'details' => 'Deleted template: ' . ($t['name'] ?? $t['template_name'] ?? $t['id']),
|
|
'created_at' => $now
|
|
]);
|
|
}
|
|
} catch (Exception $e) {
|
|
// ignore logging errors
|
|
}
|
|
|
|
$db->commit();
|
|
|
|
echo json_encode(['success' => true, 'deleted' => array_map(function($t){ return $t['id']; }, $existing)]);
|
|
} catch (Exception $e) {
|
|
$db->rollback();
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'error' => 'Failed to delete templates: ' . $e->getMessage()]);
|
|
}
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|