-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdelete_trigger.php
More file actions
73 lines (59 loc) · 1.72 KB
/
delete_trigger.php
File metadata and controls
73 lines (59 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
/**
* @var \BCCHR\CustomTemplateEngine\CustomTemplateEngine $module
*/
// schema 1
header('Content-Type: application/json; charset=utf-8');
try {
$pid = (int)($_GET['pid'] ?? 0);
if ($pid <= 0) {
throw new Exception('Missing pid');
}
$template = trim((string)($_POST['template'] ?? ''));
if ($template === '') {
throw new Exception('Template is required');
}
$cte = new \BCCHR\CustomTemplateEngine\CustomTemplateEngine();
$cte->setPaths();
$raw = $cte->getProjectSetting('cte_trigger_config', $pid);
// Schema 1 only
$cfg = ['schema' => 1, 'templates' => []];
if (is_string($raw) && trim($raw) !== '') {
$tmp = json_decode($raw, true);
if (
is_array($tmp)
&& (int)($tmp['schema'] ?? 0) === 1
&& is_array($tmp['templates'] ?? null)
) {
$cfg = $tmp;
}
}
if (!is_array($cfg['templates'] ?? null)) {
$cfg['templates'] = [];
}
if (!array_key_exists($template, $cfg['templates'])) {
echo json_encode([
'success' => true,
'deleted' => false,
'message' => 'No trigger existed for this template.'
]);
exit;
}
unset($cfg['templates'][$template]);
if (empty($cfg['templates'])) {
$cte->setProjectSetting('cte_trigger_config', '', $pid);
} else {
$cte->setProjectSetting('cte_trigger_config', json_encode($cfg), $pid);
}
echo json_encode([
'success' => true,
'deleted' => true,
]);
} catch (Throwable $e) {
http_response_code(400);
echo json_encode([
'success' => false,
'error' => $e->getMessage()
]);
}