Skip to content

Commit

Permalink
Function cleanup following move to non-static function
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Oct 2, 2021
1 parent fdec580 commit 07bbd83
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions CRM/Contact/Form/Task/PDFTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,23 @@ protected function isLiveMode(): bool {
* @throws \API_Exception
*/
public function postProcess(): void {
$form = $this;
$formValues = $this->controller->exportValues($this->getName());
[$formValues, $html_message] = $this->processMessageTemplate($formValues);
$html = $activityIds = [];

// CRM-16725 Skip creation of activities if user is previewing their PDF letter(s)
if ($this->isLiveMode()) {
$activityIds = $this->createActivities($html_message, $form->_contactIds, $formValues['subject'], CRM_Utils_Array::value('campaign_id', $formValues));
$activityIds = $this->createActivities($html_message, $this->_contactIds, $formValues['subject'], CRM_Utils_Array::value('campaign_id', $formValues));
}

if (!empty($formValues['document_file_path'])) {
[$html_message, $zip] = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']);
}

foreach ($form->_contactIds as $item => $contactId) {
$caseId = $form->getVar('_caseId');
if (empty($caseId) && !empty($form->_caseIds[$item])) {
$caseId = $form->_caseIds[$item];
foreach ($this->_contactIds as $item => $contactId) {
$caseId = $this->getVar('_caseId');
if (empty($caseId) && !empty($this->_caseIds[$item])) {
$caseId = $this->_caseIds[$item];
}

$tokenHtml = CRM_Core_BAO_MessageTemplate::renderTemplate([
Expand All @@ -266,7 +265,7 @@ public function postProcess(): void {
$mimeType = $this->getMimeType($type);
// ^^ Useful side-effect: consistently throws error for unrecognized types.

$fileName = method_exists($form, 'getFileName') ? ($form->getFileName() . '.' . $type) : 'CiviLetter.' . $type;
$fileName = $this->getFileName();

if ($type === 'pdf') {
CRM_Utils_PDF_Utils::html2pdf($html, $fileName, FALSE, $formValues);
Expand Down Expand Up @@ -298,7 +297,7 @@ public function postProcess(): void {
}
}

$form->postProcessHook();
$this->postProcessHook();

CRM_Utils_System::civiExit();
}
Expand Down Expand Up @@ -337,10 +336,10 @@ protected function getMimeType($type) {
* There may be 1 or more, depending on the system-settings
* and use-case.
*
* @throws CRM_Core_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
protected function createActivities($html_message, $contactIds, $subject, $campaign_id, $perContactHtml = []) {
$form = $this;
protected function createActivities($html_message, $contactIds, $subject, $campaign_id, $perContactHtml = []): array {
$activityParams = [
'subject' => $subject,
'campaign_id' => $campaign_id,
Expand All @@ -349,8 +348,8 @@ protected function createActivities($html_message, $contactIds, $subject, $campa
'activity_date_time' => date('YmdHis'),
'details' => $html_message,
];
if (!empty($form->_activityId)) {
$activityParams += ['id' => $form->_activityId];
if (!empty($this->_activityId)) {
$activityParams += ['id' => $this->_activityId];
}

$activityIds = [];
Expand All @@ -362,11 +361,11 @@ protected function createActivities($html_message, $contactIds, $subject, $campa
// One activity per contact.
foreach ($contactIds as $i => $contactId) {
$fullParams = ['target_contact_id' => $contactId] + $activityParams;
if (!empty($form->_caseId)) {
$fullParams['case_id'] = $form->_caseId;
if (!empty($this->_caseId)) {
$fullParams['case_id'] = $this->_caseId;
}
elseif (!empty($form->_caseIds[$i])) {
$fullParams['case_id'] = $form->_caseIds[$i];
elseif (!empty($this->_caseIds[$i])) {
$fullParams['case_id'] = $this->_caseIds[$i];
}

if (isset($perContactHtml[$contactId])) {
Expand All @@ -382,18 +381,18 @@ protected function createActivities($html_message, $contactIds, $subject, $campa
case 'combined-attached':
// One activity with all contacts.
$fullParams = ['target_contact_id' => $contactIds] + $activityParams;
if (!empty($form->_caseId)) {
$fullParams['case_id'] = $form->_caseId;
if (!empty($this->_caseId)) {
$fullParams['case_id'] = $this->_caseId;
}
elseif (!empty($form->_caseIds)) {
$fullParams['case_id'] = $form->_caseIds;
elseif (!empty($this->_caseIds)) {
$fullParams['case_id'] = $this->_caseIds;
}
$activity = civicrm_api3('Activity', 'create', $fullParams);
$activityIds[] = $activity['id'];
break;

default:
throw new CRM_Core_Exception("Unrecognized option in recordGeneratedLetters: " . Civi::settings()->get('recordGeneratedLetters'));
throw new CRM_Core_Exception('Unrecognized option in recordGeneratedLetters: ' . Civi::settings()->get('recordGeneratedLetters'));
}

return $activityIds;
Expand Down

0 comments on commit 07bbd83

Please sign in to comment.