From 2c280a2b8a661bd46db200269374f17dcf00b29e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 12 May 2023 17:02:32 +1200 Subject: [PATCH] Extract sendEmail function This is a straight forward extraction There is test cover --- CRM/Event/Form/Participant.php | 347 +++++++++++++++++---------------- 1 file changed, 183 insertions(+), 164 deletions(-) diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index cfd7a5b2c2..efb4d249ff 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -1395,170 +1395,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $sent = []; $notSent = []; if (!empty($params['send_receipt'])) { - - $this->assign('module', 'Event Registration'); - $this->assignEventDetailsToTpl($params['event_id'], CRM_Utils_Array::value('role_id', $params), CRM_Utils_Array::value('receipt_text', $params), $this->_isPaidEvent); - $this->assign('isAmountzero', 1); - - if ($this->_isPaidEvent) { - $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); - if (!$this->_mode) { - if (isset($params['payment_instrument_id'])) { - $this->assign('paidBy', - CRM_Utils_Array::value($params['payment_instrument_id'], - $paymentInstrument - ) - ); - } - } - - $this->assign('totalAmount', $params['total_amount'] ?? $contributionParams['total_amount']); - $this->assign('isPrimary', 1); - $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params)); - } - if ($this->_mode) { - $this->assignBillingName($params); - $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters( - $this->_params, - $this->_bltID - )); - - $valuesForForm = CRM_Contribute_Form_AbstractEditPayment::formatCreditCardDetails($params); - $this->assignVariables($valuesForForm, ['credit_card_exp_date', 'credit_card_type', 'credit_card_number']); - - // The concept of contributeMode is deprecated. - $this->assign('contributeMode', 'direct'); - $this->assign('isAmountzero', 0); - $this->assign('is_pay_later', 0); - $this->assign('isPrimary', 1); - } - - $this->assign('register_date', $params['register_date']); - if (isset($params['receive_date'])) { - $this->assign('receive_date', $params['receive_date']); - } - - $customGroup = []; - //format submitted data - foreach ($params['custom'] as $fieldID => $values) { - foreach ($values as $fieldValue) { - $isPublic = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $fieldValue['custom_group_id'], 'is_public'); - if ($isPublic) { - $customFields[$fieldID]['id'] = $fieldID; - $formattedValue = CRM_Core_BAO_CustomField::displayValue($fieldValue['value'], $fieldID, $participants[0]->id); - $customGroup[$customFields[$fieldID]['groupTitle']][$customFields[$fieldID]['label']] = str_replace(' ', '', $formattedValue); - } - } - } - - foreach ($this->_contactIds as $num => $contactID) { - // Retrieve the name and email of the contact - this will be the TO for receipt email - [$this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail] = CRM_Contact_BAO_Contact::getContactDetails($contactID); - - $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName; - - $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); - if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) { - $this->assign('isOnWaitlist', TRUE); - } - - $this->assign('customGroup', $customGroup); - $this->assign('contactID', $contactID); - $this->assign('participantID', $participants[$num]->id); - - $this->_id = $participants[$num]->id; - - if ($this->_isPaidEvent) { - // fix amount for each of participants ( for bulk mode ) - $eventAmount = []; - $totalTaxAmount = 0; - - //add dataArray in the receipts in ADD and UPDATE condition - $dataArray = []; - if ($this->_action & CRM_Core_Action::ADD) { - $line = $lineItem[0] ?? []; - } - elseif ($this->_action & CRM_Core_Action::UPDATE) { - $line = $this->_values['line_items']; - } - if (Civi::settings()->get('invoicing')) { - foreach ($line as $key => $value) { - if (isset($value['tax_amount'])) { - $totalTaxAmount += $value['tax_amount']; - if (isset($dataArray[(string) $value['tax_rate']])) { - $dataArray[(string) $value['tax_rate']] = $dataArray[(string) $value['tax_rate']] + CRM_Utils_Array::value('tax_amount', $value); - } - else { - $dataArray[(string) $value['tax_rate']] = $value['tax_amount'] ?? NULL; - } - } - } - $this->assign('taxTerm', $this->getSalesTaxTerm()); - $this->assign('dataArray', $dataArray); - } - if (!empty($additionalParticipantDetails)) { - $params['amount_level'] = preg_replace('//', '', $params['amount_level']) . ' - ' . $this->_contributorDisplayName; - } - - $eventAmount[$num] = [ - 'label' => preg_replace('//', '', $params['amount_level']), - 'amount' => $params['fee_amount'], - ]; - //as we are using same template for online & offline registration. - //So we have to build amount as array. - $eventAmount = array_merge($eventAmount, $additionalParticipantDetails); - $this->assign('amount', $eventAmount); - } - $this->assign('totalTaxAmount', $totalTaxAmount ?? 0); - $sendTemplateParams = [ - 'workflow' => 'event_offline_receipt', - 'contactId' => $contactID, - 'isTest' => !empty($this->_defaultValues['is_test']), - 'PDFFilename' => ts('confirmation') . '.pdf', - 'modelProps' => [ - 'participantID' => $this->_id, - 'eventID' => $params['event_id'], - ], - ]; - - // try to send emails only if email id is present - // and the do-not-email option is not checked for that contact - if ($this->_contributorEmail and !$this->_toDoNotEmail) { - if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) { - $receiptFrom = $params['from_email_address']; - } - $sendTemplateParams['from'] = $receiptFrom; - $sendTemplateParams['toName'] = $this->_contributorDisplayName; - $sendTemplateParams['toEmail'] = $this->_contributorEmail; - $sendTemplateParams['cc'] = $this->_fromEmails['cc'] ?? NULL; - $sendTemplateParams['bcc'] = $this->_fromEmails['bcc'] ?? NULL; - } - - //send email with pdf invoice - $template = CRM_Core_Smarty::singleton(); - $taxAmt = $template->get_template_vars('dataArray'); - $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', - $this->_id, 'contribution_id', 'participant_id' - ); - if (Civi::settings()->get('invoice_is_email_pdf')) { - $sendTemplateParams['isEmailPdf'] = TRUE; - $sendTemplateParams['contributionId'] = $contributionId; - } - [$mailSent, $subject, $message, $html] = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); - if ($mailSent) { - $sent[] = $contactID; - foreach ($participants as $ids => $values) { - if ($values->contact_id == $contactID) { - $values->details = $params['receipt_text'] ?? NULL; - CRM_Activity_BAO_Activity::addActivity($values, 'Email'); - break; - } - } - } - else { - $notSent[] = $contactID; - } - } + [$sent, $notSent] = $this->sendReceipts($params, $contributionParams['total_amount'], $customFields, $participants, $lineItem[0], $line, $value, $additionalParticipantDetails, $sent, $notSent); } // set the participant id if it is not set @@ -2282,4 +2119,186 @@ INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_ } } + /** + * @param $params + * @param $total_amount + * @param array $customFields + * @param array $participants + * @param $lineItem + * @param $line + * @param $value + * @param $additionalParticipantDetails + * @param array $sent + * @param array $notSent + * + * @return array + * @throws \CRM_Core_Exception + */ + protected function sendReceipts($params, $total_amount, array $customFields, array $participants, $lineItem, $line, $value, $additionalParticipantDetails, array $sent, array $notSent): array { + $this->assign('module', 'Event Registration'); + $this->assignEventDetailsToTpl($params['event_id'], CRM_Utils_Array::value('role_id', $params), CRM_Utils_Array::value('receipt_text', $params), $this->_isPaidEvent); + $this->assign('isAmountzero', 1); + + if ($this->_isPaidEvent) { + $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); + if (!$this->_mode) { + if (isset($params['payment_instrument_id'])) { + $this->assign('paidBy', + CRM_Utils_Array::value($params['payment_instrument_id'], + $paymentInstrument + ) + ); + } + } + + $this->assign('totalAmount', $params['total_amount'] ?? $total_amount); + $this->assign('isPrimary', 1); + $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params)); + } + if ($this->_mode) { + $this->assignBillingName($params); + $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters( + $this->_params, + $this->_bltID + )); + + $valuesForForm = CRM_Contribute_Form_AbstractEditPayment::formatCreditCardDetails($params); + $this->assignVariables($valuesForForm, ['credit_card_exp_date', 'credit_card_type', 'credit_card_number']); + + // The concept of contributeMode is deprecated. + $this->assign('contributeMode', 'direct'); + $this->assign('isAmountzero', 0); + $this->assign('is_pay_later', 0); + $this->assign('isPrimary', 1); + } + + $this->assign('register_date', $params['register_date']); + if (isset($params['receive_date'])) { + $this->assign('receive_date', $params['receive_date']); + } + + $customGroup = []; + //format submitted data + foreach ($params['custom'] as $fieldID => $values) { + foreach ($values as $fieldValue) { + $isPublic = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $fieldValue['custom_group_id'], 'is_public'); + if ($isPublic) { + $customFields[$fieldID]['id'] = $fieldID; + $formattedValue = CRM_Core_BAO_CustomField::displayValue($fieldValue['value'], $fieldID, $participants[0]->id); + $customGroup[$customFields[$fieldID]['groupTitle']][$customFields[$fieldID]['label']] = str_replace(' ', '', $formattedValue); + } + } + } + + foreach ($this->_contactIds as $num => $contactID) { + // Retrieve the name and email of the contact - this will be the TO for receipt email + [$this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail] = CRM_Contact_BAO_Contact::getContactDetails($contactID); + + $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName; + + $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); + if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) { + $this->assign('isOnWaitlist', TRUE); + } + + $this->assign('customGroup', $customGroup); + $this->assign('contactID', $contactID); + $this->assign('participantID', $participants[$num]->id); + + $this->_id = $participants[$num]->id; + + if ($this->_isPaidEvent) { + // fix amount for each of participants ( for bulk mode ) + $eventAmount = []; + $totalTaxAmount = 0; + + //add dataArray in the receipts in ADD and UPDATE condition + $dataArray = []; + if ($this->_action & CRM_Core_Action::ADD) { + $line = $lineItem ?? []; + } + elseif ($this->_action & CRM_Core_Action::UPDATE) { + $line = $this->_values['line_items']; + } + if (Civi::settings()->get('invoicing')) { + foreach ($line as $key => $value) { + if (isset($value['tax_amount'])) { + $totalTaxAmount += $value['tax_amount']; + if (isset($dataArray[(string) $value['tax_rate']])) { + $dataArray[(string) $value['tax_rate']] = $dataArray[(string) $value['tax_rate']] + CRM_Utils_Array::value('tax_amount', $value); + } + else { + $dataArray[(string) $value['tax_rate']] = $value['tax_amount'] ?? NULL; + } + } + } + $this->assign('taxTerm', $this->getSalesTaxTerm()); + $this->assign('dataArray', $dataArray); + } + if (!empty($additionalParticipantDetails)) { + $params['amount_level'] = preg_replace('//', '', $params['amount_level']) . ' - ' . $this->_contributorDisplayName; + } + + $eventAmount[$num] = [ + 'label' => preg_replace('//', '', $params['amount_level']), + 'amount' => $params['fee_amount'], + ]; + //as we are using same template for online & offline registration. + //So we have to build amount as array. + $eventAmount = array_merge($eventAmount, $additionalParticipantDetails); + $this->assign('amount', $eventAmount); + } + $this->assign('totalTaxAmount', $totalTaxAmount ?? 0); + $sendTemplateParams = [ + 'workflow' => 'event_offline_receipt', + 'contactId' => $contactID, + 'isTest' => !empty($this->_defaultValues['is_test']), + 'PDFFilename' => ts('confirmation') . '.pdf', + 'modelProps' => [ + 'participantID' => $this->_id, + 'eventID' => $params['event_id'], + ], + ]; + + // try to send emails only if email id is present + // and the do-not-email option is not checked for that contact + if ($this->_contributorEmail and !$this->_toDoNotEmail) { + if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) { + $receiptFrom = $params['from_email_address']; + } + $sendTemplateParams['from'] = $receiptFrom; + $sendTemplateParams['toName'] = $this->_contributorDisplayName; + $sendTemplateParams['toEmail'] = $this->_contributorEmail; + $sendTemplateParams['cc'] = $this->_fromEmails['cc'] ?? NULL; + $sendTemplateParams['bcc'] = $this->_fromEmails['bcc'] ?? NULL; + } + + //send email with pdf invoice + $template = CRM_Core_Smarty::singleton(); + $taxAmt = $template->get_template_vars('dataArray'); + $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', + $this->_id, 'contribution_id', 'participant_id' + ); + if (Civi::settings()->get('invoice_is_email_pdf')) { + $sendTemplateParams['isEmailPdf'] = TRUE; + $sendTemplateParams['contributionId'] = $contributionId; + } + [$mailSent, $subject, $message, $html] = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); + if ($mailSent) { + $sent[] = $contactID; + foreach ($participants as $ids => $values) { + if ($values->contact_id == $contactID) { + $values->details = $params['receipt_text'] ?? NULL; + CRM_Activity_BAO_Activity::addActivity($values, 'Email'); + break; + } + } + } + else { + $notSent[] = $contactID; + } + } + return [$sent, $notSent]; + } + } -- 2.25.1