Skip to content

Commit

Permalink
Add onBeforeDeleteSubmission and onDeleteSubmission events
Browse files Browse the repository at this point in the history
  • Loading branch information
Hubert Prein committed May 22, 2017
1 parent d73ab0e commit 26fe4ed
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions services/AmForms_SubmissionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public function saveSubmission(AmForms_SubmissionModel $submission)
$submission->addErrors($submissionRecord->getErrors());

if (! $submission->hasErrors()) {

// Fire an 'onBeforeSaveSubmission' event
$event = new Event($this, array(
'submission' => $submission,
Expand Down Expand Up @@ -181,23 +180,37 @@ public function saveSubmission(AmForms_SubmissionModel $submission)
*/
public function deleteSubmission(AmForms_SubmissionModel $submission)
{
$transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
// Fire an 'onBeforeDeleteSubmission' event
$event = new Event($this, array(
'submission' => $submission,
));
$this->onBeforeDeleteSubmission($event);

try {
// Delete the element and submission
craft()->elements->deleteElementById($submission->id);
// Is the event giving us the go-ahead?
if ($event->performAction) {
$transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;

if ($transaction !== null) {
$transaction->commit();
}
try {
// Delete the element and submission
craft()->elements->deleteElementById($submission->id);

return true;
} catch (\Exception $e) {
if ($transaction !== null) {
$transaction->rollback();
}
if ($transaction !== null) {
$transaction->commit();
}

// Fire an 'onDeleteSubmission' event
$this->onDeleteSubmission(new Event($this, array(
'submission' => $submission,
)));

return true;
} catch (\Exception $e) {
if ($transaction !== null) {
$transaction->rollback();
}

throw $e;
throw $e;
}
}

return false;
Expand Down Expand Up @@ -462,6 +475,26 @@ public function onSaveSubmission(Event $event)
$this->raiseEvent('onSaveSubmission', $event);
}

/**
* Fires an 'onBeforeDeleteSubmission' event.
*
* @param Event $event
*/
public function onBeforeDeleteSubmission(Event $event)
{
$this->raiseEvent('onBeforeDeleteSubmission', $event);
}

/**
* Fires an 'onDeleteSubmission' event.
*
* @param Event $event
*/
public function onDeleteSubmission(Event $event)
{
$this->raiseEvent('onDeleteSubmission', $event);
}

/**
* Fires an 'onBeforeEmailSubmission' event.
*
Expand Down

0 comments on commit 26fe4ed

Please sign in to comment.