Merge pull request #22664 from braders/membershipview-default-values
[civicrm-core.git] / CRM / Mailing / Form / Optout.php
index e809d497b76f73e090b193bfd6a50dbab7d5e580..39707570e905dd742a1af1dd52238dc9c93d0fb1 100644 (file)
  */
 class CRM_Mailing_Form_Optout extends CRM_Core_Form {
 
-  public function preProcess() {
-
-    $this->_type = 'optout';
+  /**
+   * Prevent people double-submitting the form (e.g. by double-clicking).
+   * https://lab.civicrm.org/dev/core/-/issues/1773
+   *
+   * @var bool
+   */
+  public $submitOnce = TRUE;
+
+  /**
+   * @var int
+   */
+  private $_job_id;
+
+  /**
+   * @var int
+   */
+  private $_queue_id;
+
+  /**
+   * @var string
+   */
+  private $_hash;
+
+  /**
+   * @var string
+   */
+  private $_email;
 
+  public function preProcess() {
     $this->_job_id = $job_id = CRM_Utils_Request::retrieve('jid', 'Integer', $this);
     $this->_queue_id = $queue_id = CRM_Utils_Request::retrieve('qid', 'Integer', $this);
     $this->_hash = $hash = CRM_Utils_Request::retrieve('h', 'String', $this);
 
-    if (!$job_id ||
-      !$queue_id ||
-      !$hash
-    ) {
+    if (!$job_id || !$queue_id || !$hash) {
       throw new CRM_Core_Exception(ts("Missing input parameters"));
     }
 
@@ -47,10 +69,7 @@ class CRM_Mailing_Form_Optout extends CRM_Core_Form {
 
   public function buildQuickForm() {
     CRM_Utils_System::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">');
-    CRM_Utils_System::setTitle(ts('Opt Out Confirmation'));
-
-    $this->add('text', 'email_confirm', ts('Verify email address to opt out:'));
-    $this->addRule('email_confirm', ts('Email address is required to opt out.'), 'required');
+    $this->setTitle(ts('Opt Out Confirmation'));
 
     $buttons = [
       [
@@ -68,42 +87,17 @@ class CRM_Mailing_Form_Optout extends CRM_Core_Form {
   }
 
   public function postProcess() {
-
-    $values = $this->exportValues();
-
-    // check if EmailTyped matches Email address
-    $result = CRM_Utils_String::compareStr($this->_email, $values['email_confirm'], TRUE);
-
-    $job_id = $this->_job_id;
-    $queue_id = $this->_queue_id;
-    $hash = $this->_hash;
-
-    $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1");
+    $confirmURL = CRM_Utils_System::url("civicrm/mailing/optout", "reset=1&jid={$this->_job_id}&qid={$this->_queue_id}&h={$this->_hash}&confirm=1");
     $this->assign('confirmURL', $confirmURL);
-    $session = CRM_Core_Session::singleton();
-    $session->pushUserContext($confirmURL);
-
-    if ($result == TRUE) {
-      // Email address verified
-      if (CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($job_id, $queue_id, $hash)) {
-        CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, NULL, TRUE, $job_id);
-      }
-
-      $statusMsg = ts('%1 opt out confirmed.',
-        [1 => $values['email_confirm']]
-      );
-
-      CRM_Core_Session::setStatus($statusMsg, '', 'success');
-    }
-    elseif ($result == FALSE) {
-      // Email address not verified
-      $statusMsg = ts('%1 is not associated with this opt out request.',
-        [1 => $values['email_confirm']]
-      );
+    CRM_Core_Session::singleton()->pushUserContext($confirmURL);
 
-      CRM_Core_Session::setStatus($statusMsg, '', 'error');
+    // Email address verified
+    if (CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($this->_job_id, $this->_queue_id, $this->_hash)) {
+      CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($this->_queue_id, NULL, TRUE, $this->_job_id);
     }
 
+    $statusMsg = ts('%1 opt out confirmed.', [1 => CRM_Utils_String::maskEmail($this->_email)]);
+    CRM_Core_Session::setStatus($statusMsg, '', 'success');
   }
 
 }