Skip to content

Commit

Permalink
Move crazy submit pre-processing out of buildForm
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Oct 6, 2021
1 parent e3d61d2 commit e0380a1
Showing 1 changed file with 5 additions and 33 deletions.
38 changes: 5 additions & 33 deletions CRM/Contact/Form/Task/EmailTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function buildQuickForm() {
$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',
Expand All @@ -173,27 +173,13 @@ public function buildQuickForm() {
'multiple' => TRUE,
]);

if ($to->getValue()) {
$this->_toContactIds = $this->_contactIds = [];
}
$setDefaults = TRUE;
if (property_exists($this, '_context') && $this->_context === 'standalone') {
$setDefaults = FALSE;
}

$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');
Expand Down Expand Up @@ -374,7 +360,6 @@ protected function bounceIfSimpleMailLimitExceeded($count): void {
*/
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
Expand All @@ -391,19 +376,8 @@ public function submit($formValues): void {

// 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
Expand Down Expand Up @@ -487,12 +461,10 @@ public function listTokens(): array {
/**
* 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);
Expand Down

0 comments on commit e0380a1

Please sign in to comment.