From 9be8686dd99acbae16354e26662b56c97e43b8eb Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 26 Apr 2021 11:24:37 +1200 Subject: [PATCH] Move more functions to the pdfLetter function This 'common' form isn't really common - it's silly. This PR removes all the gotcha instances of 'self::' to make it easier to decommission this file Note that there are no calls to the class outside core in git universe --- CRM/Contribute/Form/Task/PDFLetter.php | 77 +++++++++++++++- CRM/Contribute/Form/Task/PDFLetterCommon.php | 87 ++----------------- .../Form/Task/PDFLetterCommonTest.php | 2 +- 3 files changed, 82 insertions(+), 84 deletions(-) diff --git a/CRM/Contribute/Form/Task/PDFLetter.php b/CRM/Contribute/Form/Task/PDFLetter.php index 576bd26816..af8a140657 100644 --- a/CRM/Contribute/Form/Task/PDFLetter.php +++ b/CRM/Contribute/Form/Task/PDFLetter.php @@ -144,7 +144,8 @@ class CRM_Contribute_Form_Task_PDFLetter extends CRM_Contribute_Form_Task { * Process the form after the input has been submitted and validated. */ public function postProcess() { - CRM_Contribute_Form_Task_PDFLetterCommon::postProcess($this); + $formValues = $this->controller->exportValues($this->getName()); + CRM_Contribute_Form_Task_PDFLetterCommon::postProcess($this, $formValues); } /** @@ -300,4 +301,78 @@ class CRM_Contribute_Form_Task_PDFLetter extends CRM_Contribute_Form_Task { return $html; } + /** + * Send pdf by email. + * + * @param array $contact + * @param string $html + * + * @param $is_pdf + * @param array $format + * @param array $params + * + * @return bool + */ + public static function emailLetter($contact, $html, $is_pdf, $format = [], $params = []) { + try { + if (empty($contact['email'])) { + return FALSE; + } + $mustBeEmpty = ['do_not_email', 'is_deceased', 'on_hold']; + foreach ($mustBeEmpty as $emptyField) { + if (!empty($contact[$emptyField])) { + return FALSE; + } + } + + $defaults = [ + 'toName' => $contact['display_name'], + 'toEmail' => $contact['email'], + 'text' => '', + 'html' => $html, + ]; + if (empty($params['from'])) { + $emails = CRM_Core_BAO_Email::getFromEmail(); + $emails = array_keys($emails); + $defaults['from'] = array_pop($emails); + } + else { + $defaults['from'] = $params['from']; + } + if (!empty($params['subject'])) { + $defaults['subject'] = $params['subject']; + } + else { + $defaults['subject'] = ts('Thank you for your contribution/s'); + } + if ($is_pdf) { + $defaults['html'] = ts('Please see attached'); + $defaults['attachments'] = [CRM_Utils_Mail::appendPDF('ThankYou.pdf', $html, $format)]; + } + $params = array_merge($defaults); + return CRM_Utils_Mail::send($params); + } + catch (CRM_Core_Exception $e) { + return FALSE; + } + } + + /** + * Check that the token only appears in a table cell. The '' separator cannot otherwise work + * Calculate the number of times it appears IN the cell & the number of times it appears - should be the same! + * + * @param string $token + * @param string $entity + * @param string $textToSearch + * + * @return bool + */ + public static function isHtmlTokenInTableCell($token, $entity, $textToSearch) { + $tokenToMatch = $entity . '\.' . $token; + $pattern = '|).)*\{' . $tokenToMatch . '\}.*?|si'; + $within = preg_match_all($pattern, $textToSearch); + $total = preg_match_all("|{" . $tokenToMatch . "}|", $textToSearch); + return ($within == $total); + } + } diff --git a/CRM/Contribute/Form/Task/PDFLetterCommon.php b/CRM/Contribute/Form/Task/PDFLetterCommon.php index 28f810cc0d..6fadead3c9 100644 --- a/CRM/Contribute/Form/Task/PDFLetterCommon.php +++ b/CRM/Contribute/Form/Task/PDFLetterCommon.php @@ -29,10 +29,7 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF * @throws \CRM_Core_Exception */ public static function postProcess(&$form, $formValues = NULL) { - if (empty($formValues)) { - $formValues = $form->controller->exportValues($form->getName()); - } - [$formValues, $categories, $html_message, $messageToken, $returnProperties] = self::processMessageTemplate($formValues); + [$formValues, $categories, $html_message, $messageToken, $returnProperties] = CRM_Contact_Form_Task_PDFLetterCommon::processMessageTemplate($formValues); $isPDF = FALSE; $emailParams = []; if (!empty($formValues['email_options'])) { @@ -105,7 +102,7 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF $html[$contributionId] = CRM_Contribute_Form_Task_PDFLetter::generateHtml($contact, $contribution, $groupBy, $contributions, $realSeparator, $tableSeparators, $messageToken, $html_message, $separator, $grouped, $groupByID); $contactHtml[$contact['contact_id']][] = $html[$contributionId]; if (!empty($formValues['email_options'])) { - if (self::emailLetter($contact, $html[$contributionId], $isPDF, $formValues, $emailParams)) { + if (CRM_Contribute_Form_Task_PDFLetter::emailLetter($contact, $html[$contributionId], $isPDF, $formValues, $emailParams)) { $emailed++; if (!stristr($formValues['email_options'], 'both')) { $emailedHtml[$contributionId] = TRUE; @@ -130,7 +127,7 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF } $contactIds = array_keys($contacts); - self::createActivities($form, $html_message, $contactIds, CRM_Utils_Array::value('subject', $formValues, ts('Thank you letter')), CRM_Utils_Array::value('campaign_id', $formValues), $contactHtml); + CRM_Contact_Form_Task_PDFLetterCommon::createActivities($form, $html_message, $contactIds, CRM_Utils_Array::value('subject', $formValues, ts('Thank you letter')), CRM_Utils_Array::value('campaign_id', $formValues), $contactHtml); $html = array_diff_key($html, $emailedHtml); if (!empty($formValues['is_unit_test'])) { @@ -185,7 +182,7 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF foreach ($relevantEntities as $entity) { if (isset($tokens[$entity]) && is_array($tokens[$entity])) { foreach ($tokens[$entity] as $token) { - if (!self::isHtmlTokenInTableCell($token, $entity, $html)) { + if (!CRM_Contribute_Form_Task_PDFLetter::isHtmlTokenInTableCell($token, $entity, $html)) { return FALSE; } } @@ -194,24 +191,6 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF return TRUE; } - /** - * Check that the token only appears in a table cell. The '' separator cannot otherwise work - * Calculate the number of times it appears IN the cell & the number of times it appears - should be the same! - * - * @param string $token - * @param string $entity - * @param string $textToSearch - * - * @return bool - */ - public static function isHtmlTokenInTableCell($token, $entity, $textToSearch) { - $tokenToMatch = $entity . '\.' . $token; - $pattern = '|).)*\{' . $tokenToMatch . '\}.*?|si'; - $within = preg_match_all($pattern, $textToSearch); - $total = preg_match_all("|{" . $tokenToMatch . "}|", $textToSearch); - return ($within == $total); - } - /** * * @param string $html_message @@ -228,7 +207,7 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF * @throws \CRM_Core_Exception */ public static function resolveTokens(string $html_message, $contact, $contribution, $messageToken, $grouped, $separator, $contributions): string { - $categories = self::getTokenCategories(); + $categories = CRM_Contact_Form_Task_PDFLetterCommon::getTokenCategories(); $domain = CRM_Core_BAO_Domain::getDomain(); $tokenHtml = CRM_Utils_Token::replaceDomainTokens($html_message, $domain, TRUE, $messageToken); $tokenHtml = CRM_Utils_Token::replaceContactTokens($tokenHtml, $contact, TRUE, $messageToken); @@ -249,60 +228,4 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF return $tokenHtml; } - /** - * Send pdf by email. - * - * @param array $contact - * @param string $html - * - * @param $is_pdf - * @param array $format - * @param array $params - * - * @return bool - */ - public static function emailLetter($contact, $html, $is_pdf, $format = [], $params = []) { - try { - if (empty($contact['email'])) { - return FALSE; - } - $mustBeEmpty = ['do_not_email', 'is_deceased', 'on_hold']; - foreach ($mustBeEmpty as $emptyField) { - if (!empty($contact[$emptyField])) { - return FALSE; - } - } - - $defaults = [ - 'toName' => $contact['display_name'], - 'toEmail' => $contact['email'], - 'text' => '', - 'html' => $html, - ]; - if (empty($params['from'])) { - $emails = CRM_Core_BAO_Email::getFromEmail(); - $emails = array_keys($emails); - $defaults['from'] = array_pop($emails); - } - else { - $defaults['from'] = $params['from']; - } - if (!empty($params['subject'])) { - $defaults['subject'] = $params['subject']; - } - else { - $defaults['subject'] = ts('Thank you for your contribution/s'); - } - if ($is_pdf) { - $defaults['html'] = ts('Please see attached'); - $defaults['attachments'] = [CRM_Utils_Mail::appendPDF('ThankYou.pdf', $html, $format)]; - } - $params = array_merge($defaults); - return CRM_Utils_Mail::send($params); - } - catch (CRM_Core_Exception $e) { - return FALSE; - } - } - } diff --git a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php index c297ecdda4..9ece04b7c3 100644 --- a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php @@ -435,7 +435,7 @@ value=$contact_aggregate+$contribution.total_amount} */ public function testIsHtmlTokenInTableCell($token, $entity, $textToSearch, $expected) { $this->assertEquals($expected, - CRM_Contribute_Form_Task_PDFLetterCommon::isHtmlTokenInTableCell($token, $entity, $textToSearch) + CRM_Contribute_Form_Task_PDFLetter::isHtmlTokenInTableCell($token, $entity, $textToSearch) ); } -- 2.25.1