Skip to content

Commit 22f0e38

Browse files
committed
fix: small fixes
1 parent a36aec5 commit 22f0e38

2 files changed

Lines changed: 111 additions & 51 deletions

File tree

WebhookPlugin.inc.php

Lines changed: 108 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,60 @@
3131

3232
import('lib.pkp.classes.plugins.GenericPlugin');
3333

34+
3435
class WebhookPlugin extends GenericPlugin
3536
{
3637

3738
var $submissionMap = array('', 'accept', 'revisions', 'resubmit', 'decline', '', '', 'production', 'review', '', '', '', '', '', '', '', 'round');
38-
var $urls = ['https://auto.trialanderror.org/webhook-test/cda048ab-cbb1-42d4-8fa7-3116f20bea48', 'https://auto.trialanderror.org/webhook/cda048ab-cbb1-42d4-8fa7-3116f20bea48', 'https://play.svix.com/in/e_OlxpPyfrm1bj6kOtaAWFX6v2w91/', 'https://typedwebhook.tools/webhook/0d27b246-0df7-49d5-9629-84582558c664'];
39+
40+
var $decisionMap = [
41+
// SUBMISSION_EDITOR_DECISION_ACCEPT
42+
1 => 'accept',
43+
44+
// SUBMISSION_EDITOR_DECISION_PENDING_REVISIONS
45+
2 => 'requestRevisions',
46+
47+
// SUBMISSION_EDITOR_DECISION_RESUBMIT
48+
3 => 'resubmit',
49+
50+
// SUBMISSION_EDITOR_DECISION_DECLINE
51+
4 => 'decline',
52+
53+
// SUBMISSION_EDITOR_DECISION_SEND_TO_PRODUCTION
54+
7 => 'sendToProduction',
55+
56+
// SUBMISSION_EDITOR_DECISION_EXTERNAL_REVIEW
57+
8 => 'skipReview',
58+
59+
// SUBMISSION_EDITOR_DECISION_INITIAL_DECLINE
60+
9 => 'initialDecline',
61+
62+
//SUBMISSION_EDITOR_DECISION_NEW_ROUND
63+
16 => 'newRound',
64+
65+
// SUBMISSION_EDITOR_DECISION_REVERT_DECLINE
66+
17 => 'revertDecline',
67+
];
68+
69+
var $statusMap = [
70+
1 => [
71+
'label' => 'queued',
72+
'text' => 'submissions.queued'
73+
],
74+
3 => [
75+
'label' => 'published',
76+
'text' => 'submission.status.published'
77+
],
78+
4 => [
79+
'label' => 'declined',
80+
'text' => 'submission.status.declined'
81+
],
82+
5 => [
83+
'label' => 'scheduled',
84+
'text' => 'submission.status.scheduled'
85+
]
86+
];
87+
// var $urls = ['https://auto.trialanderror.org/webhook-test/cda048ab-cbb1-42d4-8fa7-3116f20bea48', 'https://auto.trialanderror.org/webhook/cda048ab-cbb1-42d4-8fa7-3116f20bea48', 'https://play.svix.com/in/e_OlxpPyfrm1bj6kOtaAWFX6v2w91/', 'https://typedwebhook.tools/webhook/0d27b246-0df7-49d5-9629-84582558c664'];
3988

4089

4190
/** @var WebhookEventManager */
@@ -79,26 +128,6 @@ public function register($category, $path, $mainContextId = null)
79128
['updateStatus', 'Submission::updateStatus', [$this, 'updateStatus']],
80129
];
81130

82-
error_log("Default events" . json_encode($defaultEvents[0][0]));
83-
// $this->webhookEventManager->addEvent('publicationEdit', 'Publication::validate', [$this, 'handleEditPublication']);
84-
85-
// // Add other events without custom data provider functions
86-
// $this->webhookEventManager->addEvent('decision', 'EditorAction::recordDecision', [$this, 'editorDecision']);
87-
// $this->webhookEventManager->addEvent('add', 'Submission::add', [$this, 'addSubmission']);
88-
// $this->webhookEventManager->addEvent('confirmReview', 'ReviewerAction::confirmReview', [$this, 'confirmReview']);
89-
// $this->webhookEventManager->addEvent('updateStatus', 'Submission::updateStatus', [$this, 'updateStatus']);
90-
91-
// HookRegistry::register(
92-
// 'EditorAction::recordDecision',
93-
// [$this, 'editorDecision']
94-
// );
95-
// HookRegistry::register('Submission::add', [$this, 'newSubmission']);
96-
// HookRegistry::register('ReviewerAction::confirmReview', [$this, 'confirmReview']);
97-
// HookRegistry::register('Submission::updateStatus', [$this, 'updateStatus']);
98-
99-
// HookRegistry::register('Publication::validate', [$this, 'handleEditPublication']);
100-
101-
102131
HookRegistry::call('Plugin::Webhook::addEvent', [&$defaultEvents]);
103132

104133
foreach ($defaultEvents as $event) {
@@ -121,10 +150,6 @@ public function register($category, $path, $mainContextId = null)
121150
return false;
122151
});
123152

124-
// function (string $eventName, string $hookName, ?callable $dataProvider = null) {
125-
// $this->webhookEventManager->addEvent($eventName, $hookName, $dataProvider);
126-
// });
127-
128153
}
129154
return true;
130155
}
@@ -146,23 +171,72 @@ public function editorDecision(string $hookName, $args)
146171
{
147172
list($submission, $editorDecision, $result, $recommendation) = $args;
148173

149-
// error_log(json_encode($args));
150-
// error_log(json_encode($this->submissionMap));
151-
return ["submission" => $submission, "decision" => $editorDecision, "result" => $result, "recommendation" => $recommendation];
152-
// error_log(json_encode($results));
153-
// return false;
174+
/** @var Submission $submission */
175+
$submission = $args[0];
176+
$editorDecision = $args[1];
177+
178+
$decisionLabel = $this->decisionMap[$editorDecision['decision']] ?? 'unknown';
179+
180+
$decisionText = __('editor.submission.decision.' . $decisionLabel);
181+
182+
$editorDecision['decisionText'] = $decisionText;
183+
$editorDecision['decisionLabel'] = $decisionLabel;
184+
185+
186+
$payload = ["submission" => $submission, "decision" => $editorDecision, "result" => $result, "recommendation" => $recommendation];
187+
188+
error_log(json_encode($payload));
189+
190+
return $payload;
154191
}
155192
public function addSubmission(string $hookName, $args)
156193
{
157-
list($submission, $request) = $args;
194+
/** @var Submission $submission */
195+
$submission = $args[0];
196+
197+
/** @var Request $request*/
198+
$request = $args[1];
199+
158200
return ["submission" => $submission, "request" => $request];
159-
// return false;
160201
}
161202

162203
public function updateStatus(string $hookname, $args)
163204
{
164-
list($status, $submission) = $args;
165-
return ["status" => $status, "submission" => $submission];
205+
/** @var number $status */
206+
$status = $args[0];
207+
/** @var Submission $submission */
208+
$submission = $args[1];
209+
210+
$newStatus = $status;
211+
if ($status !== STATUS_DECLINED) {
212+
213+
$newStatus = STATUS_QUEUED;
214+
$publication = $submission->getCurrentPublication();
215+
if ($publication && $publication->getData('status') === STATUS_PUBLISHED) {
216+
$newStatus = STATUS_PUBLISHED;
217+
}
218+
if ($publication && $publication->getData('status') === STATUS_SCHEDULED) {
219+
$newStatus = STATUS_SCHEDULED;
220+
}
221+
}
222+
$statusLabel = $this->statusMap[$status] ? $this->statusMap[$status]['label'] : 'unknown';
223+
$statusTextLabel = $statusLabel ? $this->statusMap[$status]['text'] : null;
224+
$statusText = $statusTextLabel ? __($statusTextLabel) : null;
225+
226+
$newStatusLabel = $this->statusMap[$newStatus] ? $this->statusMap[$newStatus]['label'] : 'unknown';
227+
$newStatusTextLabel = $newStatusLabel ? $this->statusMap[$newStatus]['text'] : null;
228+
$newStatusText = $newStatusTextLabel ? __($newStatusTextLabel) : null;
229+
230+
231+
return [
232+
"status" => $status,
233+
"statusLabel" => $statusLabel,
234+
"statusText" => $statusText,
235+
"newStatus" => $newStatus,
236+
"newStatusLabel" => $newStatusLabel,
237+
"newStatusText" => $newStatusText,
238+
"submission" => $submission
239+
];
166240
// return false;
167241
}
168242
public function confirmReview(string $hookName, $args)

templates/settingsForm.tpl

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
1616
// Load existing webhooks
1717
let existingWebhooks = {/literal}{$webhooks|json_encode}{literal};
18-
console.log(existingWebhooks)
19-
existingWebhooks.forEach((webhook) => {
18+
existingWebhooks?.forEach((webhook) => {
2019
if (webhook.disabled === '1') {
2120
webhook.disabled = false;
2221
}
@@ -54,7 +53,7 @@
5453
}
5554
5655
let eventCheckboxes = newWebhook.querySelectorAll('input[name="webhookEvents"]');
57-
eventCheckboxes.forEach((checkbox) => {
56+
eventCheckboxes?.forEach((checkbox) => {
5857
let eventTypeKey = checkbox.value;
5958
checkbox.name = `webhooks[${webhookKey}][events][${eventTypeKey}]`;
6059
checkbox.id = `webhookEvents-new-${eventTypeKey}`;
@@ -90,7 +89,7 @@
9089
}
9190
9291
93-
document.querySelectorAll('.testWebhook').forEach((button) => {
92+
document.querySelectorAll('.testWebhook')?.forEach((button) => {
9493
button.addEventListener('click', function() {
9594
let webhookUrlInput = button.closest('.webhook').querySelector(
9695
'input[type="text"]');
@@ -160,19 +159,6 @@
160159
{/literal}
161160
</script>
162161
163-
{foreach from=$formErrors item=error key=key}
164-
<script>
165-
{
166-
console.log("something")
167-
const errorElement = document.getElementById('{$key|escape:'javascript'}-error');
168-
if (errorElement) {
169-
errorElement.textContent = '{$error|escape:'javascript'}';
170-
errorElement.style.display = 'block';
171-
}
172-
}
173-
</script>
174-
{/foreach}
175-
176162
<style>
177163
.template {
178164
display: none;

0 commit comments

Comments
 (0)