3131
3232import ('lib.pkp.classes.plugins.GenericPlugin ' );
3333
34+
3435class 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 )
0 commit comments