diff --git a/CRM/Contact/Form/Task/PDF.php b/CRM/Contact/Form/Task/PDF.php index 217cf7af1121..82387229475b 100644 --- a/CRM/Contact/Form/Task/PDF.php +++ b/CRM/Contact/Form/Task/PDF.php @@ -58,10 +58,14 @@ public function preProcess() { CRM_Contact_Form_Task_PDFLetterCommon::preProcess($this); // store case id if present - $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'Positive', $this, FALSE); + $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'CommaSeparatedIntegers', $this, FALSE); + if (!empty($this->_caseId) && strpos($this->_caseId, ',')) { + $this->_caseIds = explode(',', $this->_caseId); + unset($this->_caseId); + } // retrieve contact ID if this is 'single' mode - $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE); + $cid = CRM_Utils_Request::retrieve('cid', 'CommaSeparatedIntegers', $this, FALSE); if ($cid) { // this is true in non-search context / single mode @@ -73,7 +77,6 @@ public function preProcess() { if ($cid) { CRM_Contact_Form_Task_PDFLetterCommon::preProcessSingle($this, $cid); $this->_single = TRUE; - $this->_cid = $cid; } else { parent::preProcess(); @@ -118,8 +121,9 @@ public function postProcess() { */ public function listTokens() { $tokens = CRM_Core_SelectValues::contactTokens(); - if (isset($this->_caseId)) { - $caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $this->_caseId, 'case_type_id'); + if (isset($this->_caseId) || isset($this->_caseIds)) { + // For a single case, list tokens relevant for only that case type + $caseTypeId = isset($this->_caseId) ? CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $this->_caseId, 'case_type_id') : NULL; $tokens += CRM_Core_SelectValues::caseTokens($caseTypeId); } return $tokens; diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index ce7e33a8f160..ae6d83ef53ad 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -78,9 +78,11 @@ public static function preProcess(&$form) { * @param int $cid */ public static function preProcessSingle(&$form, $cid) { - $form->_contactIds = array($cid); + $form->_contactIds = explode(',', $cid); // put contact display name in title for single contact mode - CRM_Utils_System::setTitle(ts('Print/Merge Document for %1', array(1 => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name')))); + if (count($form->_contactIds) === 1) { + CRM_Utils_System::setTitle(ts('Print/Merge Document for %1', array(1 => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name')))); + } } /** @@ -369,12 +371,7 @@ public static function postProcess(&$form) { // CRM-16725 Skip creation of activities if user is previewing their PDF letter(s) if ($isLiveMode) { - - // This seems silly, but the old behavior was to first check `_cid` - // and then use the provided `$contactIds`. Probably not even necessary, - // but difficult to audit. - $contactIds = $form->_cid ? array($form->_cid) : $form->_contactIds; - $activityIds = self::createActivities($form, $html_message, $contactIds, $formValues['subject'], CRM_Utils_Array::value('campaign_id', $formValues)); + $activityIds = self::createActivities($form, $html_message, $form->_contactIds, $formValues['subject'], CRM_Utils_Array::value('campaign_id', $formValues)); } if (!empty($formValues['document_file_path'])) { @@ -506,10 +503,17 @@ public static function createActivities($form, $html_message, $contactIds, $subj case 'multiple': // One activity per contact. - foreach ($contactIds as $contactId) { + foreach ($contactIds as $i => $contactId) { $fullParams = array( 'target_contact_id' => $contactId, ) + $activityParams; + if (!empty($form->_caseId)) { + $fullParams['case_id'] = $form->_caseId; + } + elseif (!empty($form->_caseIds[$i])) { + $fullParams['case_id'] = $form->_caseIds[$i]; + } + if (isset($perContactHtml[$contactId])) { $fullParams['details'] = implode('
', $perContactHtml[$contactId]); } @@ -525,21 +529,20 @@ public static function createActivities($form, $html_message, $contactIds, $subj $fullParams = array( 'target_contact_id' => $contactIds, ) + $activityParams; - $activity = CRM_Activity_BAO_Activity::create($fullParams); - $activityIds[] = $activity->id; + if (!empty($form->_caseId)) { + $fullParams['case_id'] = $form->_caseId; + } + elseif (!empty($form->_caseIds)) { + $fullParams['case_id'] = $form->_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')); } - if (!empty($form->_caseId)) { - foreach ($activityIds as $activityId) { - $caseActivityParams = array('activity_id' => $activityId, 'case_id' => $form->_caseId); - CRM_Case_BAO_Case::processCaseActivity($caseActivityParams); - } - } - return $activityIds; } diff --git a/CRM/Contribute/Form/Task/PDFLetter.php b/CRM/Contribute/Form/Task/PDFLetter.php index de0afcc2cbfd..e6b6677d2a7b 100644 --- a/CRM/Contribute/Form/Task/PDFLetter.php +++ b/CRM/Contribute/Form/Task/PDFLetter.php @@ -54,17 +54,20 @@ public function preProcess() { $this->skipOnHold = $this->skipDeceased = FALSE; CRM_Contact_Form_Task_PDFLetterCommon::preProcess($this); // store case id if present - $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'Positive', $this, FALSE); + $this->_caseId = CRM_Utils_Request::retrieve('caseid', 'CommaSeparatedIntegers', $this, FALSE); + if (!empty($this->_caseId) && strpos($this->_caseId, ',')) { + $this->_caseIds = explode(',', $this->_caseId); + unset($this->_caseId); + } // retrieve contact ID if this is 'single' mode - $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE); + $cid = CRM_Utils_Request::retrieve('cid', 'CommaSeparatedIntegers', $this, FALSE); $this->_activityId = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE); if ($cid) { CRM_Contact_Form_Task_PDFLetterCommon::preProcessSingle($this, $cid); $this->_single = TRUE; - $this->_cid = $cid; } else { parent::preProcess(); diff --git a/CRM/Contribute/Form/Task/PDFLetterCommon.php b/CRM/Contribute/Form/Task/PDFLetterCommon.php index 87bb38225b22..2aa925542ac0 100644 --- a/CRM/Contribute/Form/Task/PDFLetterCommon.php +++ b/CRM/Contribute/Form/Task/PDFLetterCommon.php @@ -105,10 +105,7 @@ public static function postProcess(&$form, $formValues = NULL) { } } - // This seems silly, but the old behavior was to first check `_cid` - // and then use the provided `$contactIds`. Probably not even necessary, - // but difficult to audit. - $contactIds = $form->_cid ? array($form->_cid) : array_keys($contacts); + $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); $html = array_diff_key($html, $emailedHtml); diff --git a/CRM/Member/Form/Task/PDFLetterCommon.php b/CRM/Member/Form/Task/PDFLetterCommon.php index 47d2dd4e675a..7ec3c55fff8a 100644 --- a/CRM/Member/Form/Task/PDFLetterCommon.php +++ b/CRM/Member/Form/Task/PDFLetterCommon.php @@ -31,10 +31,6 @@ public static function postProcessMembers(&$form, $membershipIDs, $skipOnHold, $ $html_message, $categories ); - // This seems silly, but the old behavior was to first check `_cid` - // and then use the provided `$contactIds`. Probably not even necessary, - // but difficult to audit. - $contactIDs = $form->_cid ? array($form->_cid) : $contactIDs; self::createActivities($form, $html_message, $contactIDs, $formValues['subject'], CRM_Utils_Array::value('campaign_id', $formValues)); CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues); diff --git a/CRM/Utils/Rule.php b/CRM/Utils/Rule.php index c9a92fa96791..9492542464d4 100644 --- a/CRM/Utils/Rule.php +++ b/CRM/Utils/Rule.php @@ -477,6 +477,20 @@ public static function positiveInteger($value) { return FALSE; } + /** + * @param $value + * + * @return bool + */ + public static function commaSeparatedIntegers($value) { + foreach (explode(',', $value) as $val) { + if (!self::positiveInteger($val)) { + return FALSE; + } + } + return TRUE; + } + /** * @param $value * diff --git a/CRM/Utils/Type.php b/CRM/Utils/Type.php index 43546f5567a8..b10fbc891621 100644 --- a/CRM/Utils/Type.php +++ b/CRM/Utils/Type.php @@ -412,6 +412,12 @@ public static function validate($data, $type, $abort = TRUE, $name = 'One of par } break; + case 'CommaSeparatedIntegers': + if (CRM_Utils_Rule::commaSeparatedIntegers($data)) { + return $data; + } + break; + case 'Boolean': if (CRM_Utils_Rule::boolean($data)) { return $data;