Skip to content

Commit

Permalink
Remove File On Case from activity View; add Manage Case
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Lott / Artful Robot committed Jan 27, 2020
1 parent 422a2e3 commit bb7bc26
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 26 deletions.
5 changes: 5 additions & 0 deletions CRM/Activity/Form/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,11 @@ public function buildQuickForm() {
'name' => ts('Done'),
],
]);

// PR #16360 - look up case details if there are any.
$caseDetails = CRM_Case_BAO_Case::getCaseDetailsByActivityId($this->_activityId);
$this->assign('caseId', $caseDetails['caseId']);
$this->assign('caseClientId', $caseDetails['clientContactId']);
}
else {
$this->addButtons([
Expand Down
15 changes: 5 additions & 10 deletions CRM/Activity/Selector/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,26 +246,21 @@ public static function actionLinks(
// It's horrible to have to do this work here (much more efficient if it
// were passed in; done in bulk outside the loop), but it avoids
// changing the method signature! Part of PR #16360
$_ = (int) $activityId;
$alreadyOnACase = CRM_Core_DAO::executeQuery(
"SELECT ca.case_id, cc.contact_id clientContactId
FROM civicrm_case_activity ca
INNER JOIN civicrm_case_contact cc ON ca.case_id = cc.case_id
WHERE activity_id = $_
LIMIT 1;");
// We do NOT want to offer 'File on case' if the activity is already on a case.
if ($alreadyOnACase->fetch()) {
$caseDetails = CRM_Case_BAO_Case::getCaseDetailsByActivityId($activityId);
$alreadyOnACase = (bool) $caseDetails['caseId'];
if ($alreadyOnACase) {
$actionLinks += [
CRM_Core_Action::
BROWSE => [
'name' => ts('Manage Case'),
'url' => 'civicrm/contact/view/case',
'qs' => 'reset=1&id=' . $alreadyOnACase->case_id . '&action=view&context=case&selectChild=case&cid=' . $alreadyOnACase->clientContactId,
'qs' => 'reset=1&id=' . $caseDetails['caseId'] . '&action=view&context=case&selectChild=case&cid=' . $caseDetails['clientContactId'],
'title' => ts('Manage Case'),
],
];
}
else {
// We do NOT want to offer 'File on case' if the activity is already on a case.
$alreadyOnACase = FALSE;
}
}
Expand Down
59 changes: 46 additions & 13 deletions CRM/Case/BAO/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/


/**
* This class contains the functions for Case Management.
*/
Expand Down Expand Up @@ -284,21 +285,53 @@ public static function retrieveContactIdsByCaseId($caseId, $contactID = NULL, $s
*
* @param int $activityId
*
* @return int, case ID
* @return int|null, case ID
*/
public static function getCaseIdByActivityId($activityId) {
$originalId = CRM_Core_DAO::singleValueQuery(
'SELECT original_id FROM civicrm_activity WHERE id = %1',
['1' => [$activityId, 'Integer']]
);
$caseId = CRM_Core_DAO::singleValueQuery(
'SELECT case_id FROM civicrm_case_activity WHERE activity_id in (%1,%2)',
[
'1' => [$activityId, 'Integer'],
'2' => [$originalId ? $originalId : $activityId, 'Integer'],
]
);
return $caseId;
return static::getCaseDetailsByActivityId($activityId)['caseId'];
}

/**
* Look up a case ID, client contact ID and subject from activity ID.
*
* @param int $activityId
*
* @return Array
* - clientContactId
* - caseId
* - activityId (may be the 'original_id' or the one passed in)
* - caseSubject
*/
public static function getCaseDetailsByActivityId($activityId) {

// Note: it's possible that there are multiple client contacts on a case.
// We just return a random one, as elsewhere, as the main purpose for
// returning a client Id is so that we can make a link to Manage Case
// which requires a (single) client contact Id.
$dao = CRM_Core_DAO::executeQuery(
'SELECT ca.activity_id activityId,
cs.id caseId,
cc.contact_id clientContactId,
cs.subject caseSubject
FROM civicrm_activity a
INNER JOIN civicrm_case_activity ca ON ca.activity_id IN (a.id, a.original_id)
INNER JOIN civicrm_case cs ON ca.case_id = cs.id
INNER JOIN civicrm_case_contact cc ON cc.case_id = cs.id
WHERE a.id = %1
LIMIT 1',
[1 => [$activityId, 'Integer']]);
if ($dao->fetch()) {
// Found case
return $dao->toArray();
}

// No case, return nulls.
return [
'clientContactId' => NULL,
'activityId' => NULL,
'caseId' => NULL,
'caseSubject' => NULL,
];
}

/**
Expand Down
14 changes: 11 additions & 3 deletions templates/CRM/Activity/Form/Activity.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,17 @@
<a href="{crmURL p='civicrm/contact/view/activity' q=$urlParams}" class="delete button" title="{ts}Delete{/ts}"><span><i class="crm-i fa-trash"></i> {ts}Delete{/ts}</span></a>
{/if}
{/if}
{if $action eq 4 and $context != 'case' and call_user_func(array('CRM_Case_BAO_Case','checkPermission'), $activityId, 'File On Case', $atype)}
<a href="#" onclick="fileOnCase('file', {$activityId}, null, this); return false;" class="cancel button" title="{ts}File On Case{/ts}"><span><i class="crm-i fa-clipboard"></i> {ts}File on Case{/ts}</span></a>
{include file="CRM/Case/Form/ActivityToCase.tpl"}
{if $action eq 4 and $context != 'case' }
{* Viewing an activity, from a context other than the case *}
{if $caseId === null and call_user_func(array('CRM_Case_BAO_Case','checkPermission'), $activityId, 'File On Case', $atype)}
{* This activity is not on a case, and we have permission to File On Case *}
<a href="#" onclick="fileOnCase('file', {$activityId}, null, this); return false;" class="cancel button" title="{ts}File On Case{/ts}"><span><i class="crm-i fa-clipboard"></i> {ts}File on Case{/ts}</span></a>
{include file="CRM/Case/Form/ActivityToCase.tpl"}
{elseif $caseId}
{* This activity is on a case, offer the Manage Case link *}
{assign var='urlParams' value="reset=1&id=$caseId&action=view&context=case&selectChild=case&cid=$caseClientId"}
<a href="{crmURL p='civicrm/contact/view/case' q=$urlParams}" class="browse button" ><span><i class="crm-i fa-folder-o"></i> {ts}Manage Case{/ts}</span></a>
{/if}
{/if}
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
Expand Down

0 comments on commit bb7bc26

Please sign in to comment.