add email sending functionality
authorKurund Jalmi <kurundjalmi@thirdsectordesign.org>
Fri, 15 Sep 2023 15:05:49 +0000 (16:05 +0100)
committerKurund Jalmi <kurundjalmi@thirdsectordesign.org>
Wed, 6 Dec 2023 00:23:33 +0000 (00:23 +0000)
ext/afform/admin/ang/afGuiEditor/config-form.html
ext/afform/core/Civi/Afform/Tokens.php
ext/afform/core/Civi/Api4/Action/Afform/Submit.php

index 7f95e8613c22111d2462d0dd59b0ebd5ddde13a9..df4abab1e3a246e59b9d5e4a68c4c18132b5a351 100644 (file)
         <p class="help-block">{{:: ts('Keep a log of the date, time, user, and items saved by each form submission.') }}</p>
       </div>
 
-      <div class="form-inline" ng-if="editor.afform.create_submission">
+      <div class="form-group" ng-if="editor.afform.create_submission">
         <label for="submit_limit">{{:: ts('Maximum Submissions') }}</label>
         <input type="number" min="1" step="1" id="submit_limit" ng-model="editor.afform.submit_limit" placeholder="{{:: ts('Unlimited') }}">
       </div>
 
-      <div class="form-group" >
+      <div class="form-group">
         <label>
           <input type="checkbox" ng-model="editor.afform.manual_processing" ng-click="editor.toggleManualProcessing()" ng-class="{'disabled': !!editor.afform.allow_verification_by_email}">
           {{:: ts('Verify submission before processing') }}
index 8e4e120c33505e98309323d038a5cf8a86b5b49f..5061a2718a371fb1a31efc1881c4e00a9c5de849 100644 (file)
@@ -64,6 +64,8 @@ class Tokens extends AutoService implements EventSubscriberInterface {
     foreach ($tokenForms as $tokenName => $afform) {
       $e->tokens['afform']["afform.{$tokenName}Url"] = E::ts('%1 (URL)', [1 => $afform['title'] ?? $afform['name']]);
       $e->tokens['afform']["afform.{$tokenName}Link"] = E::ts('%1 (Full Hyperlink)', [1 => $afform['title'] ?? $afform['name']]);
+      $e->tokens['afform']["afform.{$tokenName}ValidateSubmissionUrl"] = E::ts('%1 Validate Submission URL)', [1 => $afform['title'] ?? $afform['name']]);
+      $e->tokens['afform']["afform.{$tokenName}ValidateSubmissionLink"] = E::ts('%1 Validate Submission (Full Hyperlink)', [1 => $afform['title'] ?? $afform['name']]);
     }
   }
 
index 4654a9b94e59d9b3761afda69bfd5a08ae0a254e..27c9557547b7f218046a16215011ed1e94294f26 100644 (file)
@@ -73,6 +73,8 @@ class Submit extends AbstractProcessor {
 
     // let's not save the data in other CiviCRM table if manual verification is needed.
     if (!empty($this->_afform['manual_processing']) && empty($this->args['sid'])) {
+      // check for verification email
+      $this->processVerficationEmail($submission['id']);
       return [];
     }
 
@@ -469,4 +471,70 @@ class Submit extends AbstractProcessor {
     ]);
   }
 
+  /**
+   * Function to send the verification email if configured
+   *
+   * @param int $submissionId
+   *
+   * @return void
+   */
+  private function processVerficationEmail(int $submissionId):void {
+    // check if email verification configured and message template is set
+    if (empty($this->_afform['allow_verification_by_email']) || empty($this->_afform['email_confirmation_template_id'])) {
+      return;
+    }
+
+    $emailValue = '';
+    $submittedValues = $this->getValues();
+    foreach ($this->_formDataModel->getEntities() as $entityName => $entity) {
+      foreach ($submittedValues[$entityName] ?? [] as $values) {
+        $values['joins'] = array_intersect_key($values['joins'] ?? [], $entity['joins']);
+        foreach ($values['joins'] as $joinEntity => &$joinValues) {
+          if ($joinEntity === 'Email') {
+            foreach ($joinValues as $fld => $val) {
+              if (!empty($val['email'])) {
+                $emailValue = $val['email'];
+                break;
+              }
+            }
+          }
+        }
+      }
+    }
+
+    // processing sending of email only if email field exists in the form
+    if (!empty($emailValue)) {
+      $this->sendEmail($emailValue, $submissionId);
+    }
+  }
+
+  /**
+   * Function to send email
+   *
+   * @param string $emailAddress
+   * @param int $submissionId
+   *
+   * @return void
+   */
+  private function sendEmail(string $emailAddress, int $submissionId) {
+    // get domain email address
+    [$domainEmailName, $domainEmailAddress] = \CRM_Core_BAO_Domain::getNameAndEmail();
+
+    $tokenContext = [
+      'validateAfformSubmission' => [
+        'submissionId' => $submissionId,
+      ],
+    ];
+
+    // send email
+    $emailParams = [
+      'id' => $this->_afform['email_confirmation_template_id'],
+      'from' => "$domainEmailName <" . $domainEmailAddress . ">",
+      'toEmail' => $emailAddress,
+      'tokenContext' => $tokenContext,
+    ];
+
+    civicrm_api3('MessageTemplate', 'send', $emailParams);
+  }
+
 }