up
This commit is contained in:
+51
-4
@@ -7,9 +7,18 @@ header('Content-Type: application/json; charset=utf-8');
|
||||
if (function_exists('requireAuthentication')) requireAuthentication();
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true) ?: $_POST;
|
||||
$id = intval($input['id'] ?? 0);
|
||||
|
||||
if (!$id) {
|
||||
// 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;
|
||||
@@ -17,8 +26,46 @@ if (!$id) {
|
||||
|
||||
try {
|
||||
$db = Database::getInstance();
|
||||
$db->execute("DELETE FROM message_templates WHERE id = ?", [$id]);
|
||||
echo json_encode(['success' => true]);
|
||||
|
||||
// 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()]);
|
||||
|
||||
+12
-3
@@ -1100,14 +1100,20 @@ class WhatsAppBotManager {
|
||||
async loadTemplates() {
|
||||
try {
|
||||
const response = await this.apiCall('get_templates.php');
|
||||
let templates = [];
|
||||
if (response && response.success && Array.isArray(response.data)) {
|
||||
this.updateTemplatesTable(response.data);
|
||||
templates = response.data;
|
||||
} else if (response && Array.isArray(response)) {
|
||||
// Retrocompatibilidad por si la API devuelve directamente el array
|
||||
this.updateTemplatesTable(response);
|
||||
templates = response;
|
||||
} else {
|
||||
this.showError('Error: formato de datos inválido para plantillas');
|
||||
return;
|
||||
}
|
||||
|
||||
// Guardar en memoria para usos posteriores (ej. confirmaciones más amigables)
|
||||
this.templates = templates;
|
||||
this.updateTemplatesTable(templates);
|
||||
} catch (error) {
|
||||
this.showError('Error cargando plantillas: ' + error.message);
|
||||
}
|
||||
@@ -1230,7 +1236,10 @@ class WhatsAppBotManager {
|
||||
}
|
||||
|
||||
async deleteTemplate(templateId) {
|
||||
if (!confirm('¿Estás seguro de que deseas eliminar esta plantilla?')) {
|
||||
const tmpl = (this.templates || []).find(t => t.id === templateId) || {};
|
||||
const name = tmpl.name || tmpl.template_name || `ID ${templateId}`;
|
||||
|
||||
if (!confirm(`¿Estás seguro de que deseas eliminar la plantilla "${name}"? Esta acción no se puede deshacer.`)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
+10
-1
@@ -869,7 +869,10 @@ class SimpleWhatsAppManager {
|
||||
const response = await this.apiCall('get_templates.php');
|
||||
|
||||
if (response && response.success) {
|
||||
this.updateTemplatesList(response.data || []);
|
||||
const tmpl = response.data || [];
|
||||
this.templatesData = tmpl;
|
||||
this.templates = tmpl; // compatibilidad con app.js
|
||||
this.updateTemplatesList(tmpl);
|
||||
} else {
|
||||
this.showError('Error cargando plantillas');
|
||||
}
|
||||
@@ -1553,6 +1556,12 @@ class SimpleWhatsAppManager {
|
||||
}
|
||||
|
||||
async deleteTemplateAction(templateId) {
|
||||
// intentar obtener el nombre de la plantilla para confirmar
|
||||
const tmpl = (this.templatesData || []).find(t => t.id === templateId) || {};
|
||||
const name = tmpl.name || tmpl.template_name || `ID ${templateId}`;
|
||||
|
||||
if (!confirm(`¿Estás seguro de que quiere eliminar la plantilla "${name}"? Esta acción no se puede deshacer.`)) return;
|
||||
|
||||
this.log(`Eliminando plantilla ID: ${templateId}`);
|
||||
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../config/config.php';
|
||||
|
||||
// Define a no-op requireAuthentication to bypass auth for test
|
||||
if (!function_exists('requireAuthentication')) {
|
||||
function requireAuthentication() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
session_start();
|
||||
// Mock admin session so requireAuthentication passes
|
||||
$_SESSION['admin_user'] = ['id' => 1, 'username' => 'admin'];
|
||||
|
||||
$db = Database::getInstance();
|
||||
// Clean up any existing test template called 'test_delete_tpl'
|
||||
try { $db->execute('DELETE FROM message_templates WHERE name = ?', ['test_delete_tpl']); } catch (Exception $e) { }
|
||||
// Insert a test template
|
||||
$id = $db->insert('message_templates', [
|
||||
'name' => 'test_delete_tpl',
|
||||
'template_name' => 'test_delete_tpl_name',
|
||||
'language_code' => 'en_US',
|
||||
'category' => 'utility',
|
||||
'status' => 'approved',
|
||||
'created_at' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
echo "Inserted template id: $id\n";
|
||||
|
||||
// Simulate POST body
|
||||
$_POST = ['id' => $id];
|
||||
|
||||
// Capture output
|
||||
ob_start();
|
||||
include __DIR__ . '/../api/delete_template.php';
|
||||
$out = ob_get_clean();
|
||||
|
||||
echo "API Output: $out\n";
|
||||
|
||||
// Verify it was deleted
|
||||
$check = $db->fetch('SELECT * FROM message_templates WHERE id = :id', ['id' => $id]);
|
||||
if (!$check) {
|
||||
echo "Template successfully deleted from DB.\n";
|
||||
} else {
|
||||
echo "Template still exists in DB: " . json_encode($check) . "\n";
|
||||
}
|
||||
Reference in New Issue
Block a user