Move crazy submit pre-processing out of buildForm
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 6 Oct 2021 04:28:17 +0000 (17:28 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 7 Oct 2021 01:04:55 +0000 (14:04 +1300)
CRM/Contact/Form/Task/EmailTrait.php

index 7c803f521ef676bfb0791d0fb7fac0cad9e29355..278b256018fac07772c5d5c37fcc792484d8bb1a 100644 (file)
@@ -161,7 +161,7 @@ trait CRM_Contact_Form_Task_EmailTrait {
     $emailAttributes = [
       'class' => 'huge',
     ];
-    $to = $this->add('text', 'to', ts('To'), $emailAttributes, TRUE);
+    $this->add('text', 'to', ts('To'), $emailAttributes, TRUE);
 
     $this->addEntityRef('cc_id', ts('CC'), [
       'entity' => 'Email',
@@ -173,9 +173,6 @@ trait CRM_Contact_Form_Task_EmailTrait {
       'multiple' => TRUE,
     ]);
 
-    if ($to->getValue()) {
-      $this->_toContactIds = $this->_contactIds = [];
-    }
     $setDefaults = TRUE;
     if (property_exists($this, '_context') && $this->_context === 'standalone') {
       $setDefaults = FALSE;
@@ -183,17 +180,6 @@ trait CRM_Contact_Form_Task_EmailTrait {
 
     $this->_allContactIds = $this->_toContactIds = $this->_contactIds;
 
-    if ($to->getValue()) {
-      foreach ($this->getEmails($to) as $value) {
-        $contactId = $value['contact_id'];
-        if ($contactId) {
-          $this->_contactIds[] = $this->_toContactIds[] = $contactId;
-          $this->_allContactIds[] = $contactId;
-        }
-      }
-      $setDefaults = TRUE;
-    }
-
     //get the group of contacts as per selected by user in case of Find Activities
     if (!empty($this->_activityHolderIds)) {
       $contact = $this->get('contacts');
@@ -374,7 +360,6 @@ trait CRM_Contact_Form_Task_EmailTrait {
    */
   public function submit($formValues): void {
     $this->saveMessageTemplate($formValues);
-
     $from = $formValues['from_email_address'];
     // dev/core#357 User Emails are keyed by their id so that the Signature is able to be added
     // If we have had a contact email used here the value returned from the line above will be the
@@ -391,19 +376,8 @@ trait CRM_Contact_Form_Task_EmailTrait {
 
     // format contact details array to handle multiple emails from same contact
     $formattedContactDetails = [];
-    foreach ($this->_contactIds as $key => $contactId) {
-      // if we dont have details on this contactID, we should ignore
-      // potentially this is due to the contact not wanting to receive email
-      if (!isset($this->_contactDetails[$contactId])) {
-        continue;
-      }
-      $email = $this->getEmail($key);
-      // prevent duplicate emails if same email address is selected CRM-4067
-      // we should allow same emails for different contacts
-      $details = $this->_contactDetails[$contactId];
-      $details['email'] = $email;
-      unset($details['email_id']);
-      $formattedContactDetails["{$contactId}::{$email}"] = $details;
+    foreach ($this->getEmails() as $details) {
+      $formattedContactDetails[$details['contact_id'] . '::' . $details['email']] = $details;
     }
 
     // send the mail
@@ -486,12 +460,10 @@ trait CRM_Contact_Form_Task_EmailTrait {
   /**
    * Get the emails from the added element.
    *
-   * @param HTML_QuickForm_Element $element
-   *
    * @return array
    */
-  protected function getEmails($element): array {
-    $allEmails = explode(',', $element->getValue());
+  protected function getEmails(): array {
+    $allEmails = explode(',', $this->getSubmittedValue('to'));
     $return = [];
     foreach ($allEmails as $value) {
       $values = explode('::', $value);