-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookSettingsForm.inc.php
More file actions
131 lines (99 loc) · 3.63 KB
/
WebhookSettingsForm.inc.php
File metadata and controls
131 lines (99 loc) · 3.63 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
import('lib.pkp.classes.form.Form');
class WebhookSettingsForm extends Form
{
/** @var int Context ID */
var $contextId;
/** @var WebhookPlugin */
var $plugin;
/**
* Constructor
* @param WebhookPlugin $plugin
* @param int $contextId
*/
public function __construct($plugin, $contextId)
{
$this->contextId = $contextId;
$this->plugin = $plugin;
parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
}
/**
* @copydoc Form::fetch()
*/
function fetch($request, $template = null, $display = false)
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
$apiUrl = $request->getBaseUrl() . '/' . 'index.php' . '/' . $request->getContext()->getPath() . '/' . 'api' . '/' . 'v1' . '/' . 'webhook';
$templateMgr->assign('apiUrl', $apiUrl);
return parent::fetch($request, $template, $display);
}
public function initData()
{
$plugin = $this->plugin;
$contextId = $this->contextId;
$this->setData('webhooks', $plugin->getSetting($contextId, 'webhooks'));
}
public function readInputData()
{
$this->readUserVars(['webhooks']);
}
/**
* Save the form
*
* @copydoc Form::execute()
*/
public function execute(...$functionArgs)
{
parent::execute(...$functionArgs);
$plugin = $this->plugin;
$contextId = $this->contextId;
$webhooksData = $this->getData('webhooks');
error_log(json_encode($webhooksData));
$webhooks = [];
foreach ($webhooksData as $webhookData) {
$url = $webhookData['url'];
$events = array_keys(array_filter($webhookData['events'])); // Get selected event keys
$disabled = !$webhookData['disabled'];
$webhooks[] = ['url' => $url, 'events' => $events, 'disabled' => $disabled];
}
$plugin->updateSetting($contextId, 'webhooks', $webhooks);
$webhooksByEvent = [];
foreach ($webhooks as $webhook) {
foreach ($webhook['events'] as $event) {
if ($webhook['disabled']) {
continue;
}
$webhooksByEvent[$event][] = $webhook;
}
}
$plugin->updateSetting($contextId, 'webhooksByEvent', $webhooksByEvent);
error_log("webhook data: " . json_encode($webhooks));
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification(Application::get()->getRequest()->getUser()->getId(), NOTIFICATION_TYPE_SUCCESS);
}
/**
* @copydoc Form::validate()
*/
public function validate($callHooks = true)
{
$webhooks = $this->getData('webhooks');
$isValid = true;
foreach ($webhooks as $index => $webhook) {
if (empty($webhook['url']) || !filter_var($webhook['url'], FILTER_VALIDATE_URL)) {
$this->addError("webhookUrl-{$index}-error", __('plugins.generic.webhook.validation.invalidUrl'));
$isValid = false;
}
if (empty($webhook['events']) || !is_array($webhook['events'])) {
$this->addError("webhookEvents-{$index}-error", __('plugins.generic.webhook.validation.noEventsSelected'));
$isValid = false;
}
}
if (!$isValid) {
return false;
}
return parent::validate($callHooks);
}
}