/** * Process the form after the input has been submitted and validated. * * @param CRM_Contribute_Form_Task $form */ public static function postProcess(&$form) { $formValues = $form->controller->exportValues($form->getName()); list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($formValues); $isPDF = FALSE; $emailParams = array(); if (!empty($formValues['email_options'])) { $returnProperties['email'] = $returnProperties['on_hold'] = $returnProperties['is_deceased'] = $returnProperties['do_not_email'] = 1; $emailParams = array('subject' => $formValues['subject']); // We need display_name for emailLetter() so add to returnProperties here $returnProperties['display_name'] = 1; if (stristr($formValues['email_options'], 'pdfemail')) { $isPDF = TRUE; } } // update dates ? $receipt_update = isset($formValues['receipt_update']) ? $formValues['receipt_update'] : FALSE; $thankyou_update = isset($formValues['thankyou_update']) ? $formValues['thankyou_update'] : FALSE; $nowDate = date('YmdHis'); $receipts = $thanks = $emailed = 0; $updateStatus = ''; $task = 'CRM_Contribution_Form_Task_PDFLetterCommon'; $realSeparator = ', '; $tableSeparators = array('td' => '</td><td>', 'tr' => '</td></tr><tr><td>'); //the original thinking was mutliple options - but we are going with only 2 (comma & td) for now in case // there are security (& UI) issues we need to think through if (isset($formValues['group_by_separator'])) { if (in_array($formValues['group_by_separator'], array('td', 'tr'))) { $realSeparator = $tableSeparators[$formValues['group_by_separator']]; } elseif ($formValues['group_by_separator'] == 'br') { $realSeparator = "<br />"; } } $separator = '****~~~~'; // a placeholder in case the separator is common in the string - e.g ', ' $validated = FALSE; $groupBy = $formValues['group_by']; // skip some contacts ? $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE; $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE; $contributionIDs = $form->getVar('_contributionIds'); if ($form->_includesSoftCredits) { //@todo - comment on what is stored there $contributionIDs = $form->getVar('_contributionContactIds'); } list($contributions, $contacts) = self::buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $form->_includesSoftCredits); $html = array(); foreach ($contributions as $contributionId => $contribution) { $contact =& $contacts[$contribution['contact_id']]; $grouped = $groupByID = 0; if ($groupBy) { $groupByID = empty($contribution[$groupBy]) ? 0 : $contribution[$groupBy]; $contribution = $contact['combined'][$groupBy][$groupByID]; $grouped = TRUE; } self::assignCombinedContributionValues($contact, $contributions, $groupBy, $groupByID); if (empty($groupBy) || empty($contact['is_sent'][$groupBy][$groupByID])) { if (!$validated && in_array($realSeparator, $tableSeparators) && !self::isValidHTMLWithTableSeparator($messageToken, $html_message)) { $realSeparator = ', '; CRM_Core_Session::setStatus(ts('You have selected the table cell separator, but one or more token fields are not placed inside a table cell. This would result in invalid HTML, so comma separators have been used instead.')); } $validated = TRUE; $html[$contributionId] = str_replace($separator, $realSeparator, self::resolveTokens($html_message, $contact, $contribution, $messageToken, $categories, $grouped, $separator)); $contact['is_sent'][$groupBy][$groupByID] = TRUE; if (!empty($formValues['email_options'])) { if (self::emailLetter($contact, $html[$contributionId], $isPDF, $formValues, $emailParams)) { $emailed++; if (!stristr($formValues['email_options'], 'both')) { unset($html[$contributionId]); } } } } // update dates (do it for each contribution including grouped recurring contribution) //@todo - the 2 calls below bypass all hooks. Using the api would possibly be slower than one call but not than 2 if ($receipt_update) { $result = CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'receipt_date', $nowDate); if ($result) { $receipts++; } } if ($thankyou_update) { $result = CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'thankyou_date', $nowDate); if ($result) { $thanks++; } } } //createActivities requires both $form->_contactIds and $contacts - //@todo - figure out why $form->_contactIds = array_keys($contacts); self::createActivities($form, $html_message, $form->_contactIds); if (!empty($html)) { CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues); } $form->postProcessHook(); if ($emailed) { $updateStatus = ts('Receipts have been emailed to %1 contributions.', array(1 => $emailed)); } if ($receipts) { $updateStatus = ts('Receipt date has been updated for %1 contributions.', array(1 => $receipts)); } if ($thanks) { $updateStatus .= ' ' . ts('Thank-you date has been updated for %1 contributions.', array(1 => $thanks)); } if ($updateStatus) { CRM_Core_Session::setStatus($updateStatus); } if (!empty($html)) { // ie. we have only sent emails - lets no show a white screen CRM_Utils_System::civiExit(1); } }