Move more functions to the pdfLetter function
authoreileen <emcnaughton@wikimedia.org>
Sun, 25 Apr 2021 23:24:37 +0000 (11:24 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 26 Apr 2021 01:00:18 +0000 (13:00 +1200)
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
CRM/Contribute/Form/Task/PDFLetterCommon.php
tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php

index 576bd2681690e6128175a5e525bc2261af85d93b..af8a1406576c3b4822e8e4391cfa86779ccd2fb8 100644 (file)
@@ -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 '</td><td>' 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 = '|<td(?![\w-])((?!</td>).)*\{' . $tokenToMatch . '\}.*?</td>|si';
+    $within = preg_match_all($pattern, $textToSearch);
+    $total = preg_match_all("|{" . $tokenToMatch . "}|", $textToSearch);
+    return ($within == $total);
+  }
+
 }
index 28f810cc0d94714df5abe98aacbac9dcd1be9253..6fadead3c9dd20fda2516bf3ffaa6f91ece0c2ae 100644 (file)
@@ -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 '</td><td>' 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 = '|<td(?![\w-])((?!</td>).)*\{' . $tokenToMatch . '\}.*?</td>|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;
-    }
-  }
-
 }
index c297ecdda4030a0929ed8d85136a1929a55c4001..9ece04b7c3aa3ed96aabf24385f2c89b56147b57 100644 (file)
@@ -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)
     );
   }