Skip to content

Commit

Permalink
Merge pull request #11223 from colemanw/CRM-21382
Browse files Browse the repository at this point in the history
CRM-21382 - Fix print/merge document on case and support multiple cases
  • Loading branch information
colemanw authored Nov 8, 2017
2 parents 2a970e6 + fe61faf commit ed45309
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 34 deletions.
14 changes: 9 additions & 5 deletions CRM/Contact/Form/Task/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
39 changes: 21 additions & 18 deletions CRM/Contact/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))));
}
}

/**
Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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('<hr>', $perContactHtml[$contactId]);
}
Expand All @@ -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;
}

Expand Down
9 changes: 6 additions & 3 deletions CRM/Contribute/Form/Task/PDFLetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions CRM/Contribute/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 0 additions & 4 deletions CRM/Member/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions CRM/Utils/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
6 changes: 6 additions & 0 deletions CRM/Utils/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ed45309

Please sign in to comment.