diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index a515e054f02..d6aaceed4d4 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -751,8 +751,8 @@ public static function getActivities($params) { GROUP BY activity_id', [ 1 => [ CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets'), - 'Integer' - ] + 'Integer', + ], ])->fetchAll(); } foreach ($targetCount as $activityTarget) { diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index aaa37f5a595..fc7b07cab5e 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -1077,7 +1077,8 @@ public static function getCaseActivity($caseID, &$params, $contactID, $context = $caseActivity['no_attachments'] = count($attachmentIDs); } - $caseActivities[$caseActivityId]['links'] = self::addCaseActivityLinks($caseID, $contactID, $userID, $context, $dao); + $caseActivities[$caseActivityId]['links'] + = CRM_Case_Selector_Search::addCaseActivityLinks($caseID, $contactID, $userID, $context, $dao); } $caseActivitiesDT = array(); @@ -1088,80 +1089,6 @@ public static function getCaseActivity($caseID, &$params, $contactID, $context = return $caseActivitiesDT; } - /** - * FIXME: This is a transitional function to facilitate a refactor of this to use CRM_Core_Action and actionLinks - * Add the set of "actionLinks" to the case activity - * - * @param int $caseID - * @param int $contactID - * @param int $userID - * @param string $context - * @param \CRM_Core_DAO $dao - * - * @return string - * HTML formatted Link - */ - private static function addCaseActivityLinks($caseID, $contactID, $userID, $context, $dao) { - // FIXME: Why are we not using CRM_Core_Action for these links? This is too much manual work and likely to get out-of-sync with core markup. - $caseActivityId = $dao->id; - $allowView = self::checkPermission($caseActivityId, 'view', $dao->activity_type_id, $userID); - $allowEdit = self::checkPermission($caseActivityId, 'edit', $dao->activity_type_id, $userID); - $allowDelete = self::checkPermission($caseActivityId, 'delete', $dao->activity_type_id, $userID); - $emailActivityTypeIDs = [ - 'Email' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Email'), - 'Inbound Email' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound Email'), - ]; - $url = CRM_Utils_System::url("civicrm/case/activity", - "reset=1&cid={$contactID}&caseid={$caseID}", FALSE, NULL, FALSE - ); - $contextUrl = ''; - if ($context == 'fulltext') { - $contextUrl = "&context={$context}"; - } - $editUrl = "{$url}&action=update{$contextUrl}"; - $deleteUrl = "{$url}&action=delete{$contextUrl}"; - $restoreUrl = "{$url}&action=renew{$contextUrl}"; - $viewTitle = ts('View activity'); - $caseDeleted = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $caseID, 'is_deleted'); - - $url = ""; - $css = 'class="action-item crm-hover-button"'; - if ($allowView) { - $viewUrl = CRM_Utils_System::url('civicrm/case/activity/view', array('cid' => $contactID, 'aid' => $caseActivityId)); - $url = '' . ts('View') . ''; - } - $additionalUrl = "&id={$caseActivityId}"; - if (!$dao->deleted) { - //hide edit link of activity type email.CRM-4530. - if (!in_array($dao->type, $emailActivityTypeIDs)) { - //hide Edit link if activity type is NOT editable (special case activities).CRM-5871 - if ($allowEdit) { - $url .= '' . ts('Edit') . ' '; - } - } - if ($allowDelete) { - $url .= ' ' . ts('Delete') . ''; - } - } - elseif (!$caseDeleted) { - $url = ' ' . ts('Restore') . ''; - } - - //check for operations. - if (self::checkPermission($caseActivityId, 'Move To Case', $dao->activity_type_id)) { - $url .= ' ' . ts('Move To Case') . ' '; - } - if (self::checkPermission($caseActivityId, 'Copy To Case', $dao->activity_type_id)) { - $url .= ' ' . ts('Copy To Case') . ' '; - } - // if there are file attachments we will return how many and, if only one, add a link to it - if (!empty($dao->attachment_ids)) { - $url .= implode(' ', CRM_Core_BAO_File::paperIconAttachment('civicrm_activity', $caseActivityId)); - } - - return $url; - } - /** * Helper function to generate a formatted contact link/name for display in the Case activities tab * diff --git a/CRM/Case/Form/ActivityView.php b/CRM/Case/Form/ActivityView.php index b18be74fdc3..af9c5c4b708 100644 --- a/CRM/Case/Form/ActivityView.php +++ b/CRM/Case/Form/ActivityView.php @@ -204,6 +204,17 @@ public function preProcess() { ], ]; CRM_Utils_System::appendBreadCrumb($breadcrumb); + + $this->addButtons([ + [ + 'type' => 'cancel', + 'name' => ts('Done'), + ], + ]); + // Add additional action links + $activityDeleted = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityID, 'is_deleted'); + $actionLinks = CRM_Case_Selector_Search::permissionedActionLinks($caseID, $contactID, CRM_Core_Session::getLoggedInContactID(), NULL, $activityTypeID, $activityDeleted, $activityID, FALSE); + $this->assign('actionLinks', $actionLinks); } } diff --git a/CRM/Case/Selector/Search.php b/CRM/Case/Selector/Search.php index 04517c0a80e..ff4cf457c2b 100644 --- a/CRM/Case/Selector/Search.php +++ b/CRM/Case/Selector/Search.php @@ -486,4 +486,203 @@ public function getExportFileName($output = 'csv') { return ts('Case Search'); } + /** + * Add the set of "actionLinks" to the case activity + * + * @param int $caseID + * @param int $contactID + * @param int $userID + * @param string $context + * @param \CRM_Activity_BAO_Activity $dao + * @param bool $allowView + * + * @return string $linksMarkup + */ + public static function addCaseActivityLinks($caseID, $contactID, $userID, $context, $dao, $allowView = TRUE) { + $caseDeleted = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $caseID, 'is_deleted'); + + $actionLinks = self::actionLinks(); + + // Check logged in user for permission. + if (CRM_Case_BAO_Case::checkPermission($dao->id, 'view', $dao->activity_type_id, $userID)) { + $permissions[] = CRM_Core_Permission::VIEW; + } + if (!$allowView) { + unset($actionLinks[CRM_Core_Action::VIEW]); + } + if (!$dao->deleted) { + // Activity is not deleted, allow user to edit/delete if they have permission + + // hide Edit link if: + // 1. User does not have edit permission. + // 2. Activity type is NOT editable (special case activities).CRM-5871 + if (CRM_Case_BAO_Case::checkPermission($dao->id, 'edit', $dao->activity_type_id, $userID)) { + $permissions[] = CRM_Core_Permission::EDIT; + } + if (in_array($dao->activity_type_id, CRM_Activity_BAO_Activity::getViewOnlyActivityTypeIDs())) { + unset($actionLinks[CRM_Core_Action::UPDATE]); + } + if (CRM_Case_BAO_Case::checkPermission($dao->id, 'delete', $dao->activity_type_id, $userID)) { + $permissions[] = CRM_Core_Permission::DELETE; + } + unset($actionLinks[CRM_Core_Action::RENEW]); + } + $extraMask = 0; + if ($dao->deleted && !$caseDeleted + && (CRM_Case_BAO_Case::checkPermission($dao->id, 'delete', $dao->activity_type_id, $userID))) { + // Case is not deleted but activity is. + // Allow user to restore activity if they have delete permissions + unset($actionLinks[CRM_Core_Action::DELETE]); + $extraMask = CRM_Core_Action::RENEW; + } + if (!CRM_Case_BAO_Case::checkPermission($dao->id, 'Move To Case', $dao->activity_type_id)) { + unset($actionLinks[CRM_Core_Action::DETACH]); + } + if (!CRM_Case_BAO_Case::checkPermission($dao->id, 'Copy To Case', $dao->activity_type_id)) { + unset($actionLinks[CRM_Core_Action::COPY]); + } + + $actionMask = CRM_Core_Action::mask($permissions) | $extraMask; + + $values = [ + 'aid' => $dao->id, + 'cid' => $contactID, + 'cxt' => empty($context) ? '' : "&context={$context}", + 'caseid' => $caseID, + ]; + $linksMarkup = CRM_Core_Action::formLink($actionLinks, + $actionMask, + $values, + ts('more'), + FALSE, + 'case.tab.row', + 'Activity', + $dao->id + ); + + // if there are file attachments we will return how many and, if only one, add a link to it + if (!empty($dao->attachment_ids)) { + $linksMarkup .= implode(' ', CRM_Core_BAO_File::paperIconAttachment('civicrm_activity', $dao->id)); + } + + return $linksMarkup; + } + + /** + * @param int $caseID + * @param int $contactID + * @param int $userID + * @param string $context + * @param int $activityTypeID + * @param int $activityDeleted + * @param int $activityID + * @param bool $allowView + * + * @return array|null + */ + public static function permissionedActionLinks($caseID, $contactID, $userID, $context, $activityTypeID, $activityDeleted, $activityID, $allowView = TRUE) { + $caseDeleted = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $caseID, 'is_deleted'); + $values = [ + 'aid' => $activityID, + 'cid' => $contactID, + 'cxt' => empty($context) ? '' : "&context={$context}", + 'caseid' => $caseID, + ]; + $actionLinks = self::actionLinks(); + + // Check logged in user for permission. + if (CRM_Case_BAO_Case::checkPermission($activityID, 'view', $activityTypeID, $userID)) { + $permissions[] = CRM_Core_Permission::VIEW; + } + if (!$allowView) { + unset($actionLinks[CRM_Core_Action::VIEW]); + } + if (!$activityDeleted) { + // Activity is not deleted, allow user to edit/delete if they have permission + + // hide Edit link if: + // 1. User does not have edit permission. + // 2. Activity type is NOT editable (special case activities).CRM-5871 + if (CRM_Case_BAO_Case::checkPermission($activityID, 'edit', $activityTypeID, $userID)) { + $permissions[] = CRM_Core_Permission::EDIT; + } + if (in_array($activityTypeID, CRM_Activity_BAO_Activity::getViewOnlyActivityTypeIDs())) { + unset($actionLinks[CRM_Core_Action::UPDATE]); + } + if (CRM_Case_BAO_Case::checkPermission($activityID, 'delete', $activityTypeID, $userID)) { + $permissions[] = CRM_Core_Permission::DELETE; + } + unset($actionLinks[CRM_Core_Action::RENEW]); + } + $extraMask = 0; + if ($activityDeleted && !$caseDeleted + && (CRM_Case_BAO_Case::checkPermission($activityID, 'delete', $activityTypeID, $userID))) { + // Case is not deleted but activity is. + // Allow user to restore activity if they have delete permissions + unset($actionLinks[CRM_Core_Action::DELETE]); + $extraMask = CRM_Core_Action::RENEW; + } + if (!CRM_Case_BAO_Case::checkPermission($activityID, 'Move To Case', $activityTypeID)) { + unset($actionLinks[CRM_Core_Action::DETACH]); + } + if (!CRM_Case_BAO_Case::checkPermission($activityID, 'Copy To Case', $activityTypeID)) { + unset($actionLinks[CRM_Core_Action::COPY]); + } + + $actionMask = CRM_Core_Action::mask($permissions) | $extraMask; + return CRM_Core_Action::filterLinks($actionLinks, $actionMask, $values, 'case.activity', 'Activity', $activityID); + } + + /** + * Get the action links for this page. + * + * @return array + */ + public static function actionLinks() { + // check if variable _actionsLinks is populated + if (!isset(self::$_actionLinks)) { + self::$_actionLinks = [ + CRM_Core_Action::VIEW => [ + 'name' => ts('View'), + 'url' => 'civicrm/case/activity/view', + 'qs' => 'reset=1&cid=%%cid%%&caseid=%%caseid%%&aid=%%aid%%', + 'title' => ts('View'), + ], + CRM_Core_Action::UPDATE => [ + 'name' => ts('Edit'), + 'url' => 'civicrm/case/activity', + 'qs' => 'reset=1&cid=%%cid%%&caseid=%%caseid%%&id=%%aid%%&action=update%%cxt%%', + 'title' => ts('Edit'), + 'icon' => 'fa-pencil', + ], + CRM_Core_Action::DELETE => [ + 'name' => ts('Delete'), + 'url' => 'civicrm/case/activity', + 'qs' => 'reset=1&cid=%%cid%%&caseid=%%caseid%%&id=%%aid%%&action=delete%%cxt%%', + 'title' => ts('Delete'), + 'icon' => 'fa-trash', + ], + CRM_Core_Action::RENEW => [ + 'name' => ts('Restore'), + 'url' => 'civicrm/case/activity', + 'qs' => 'reset=1&cid=%%cid%%&caseid=%%caseid%%&id=%%aid%%&action=renew%%cxt%%', + 'title' => ts('Restore'), + ], + CRM_Core_Action::DETACH => [ + 'name' => ts('Move To Case'), + 'ref' => 'move_to_case_action', + 'title' => ts('Move To Case'), + 'extra' => 'onclick = "Javascript:fileOnCase( \'move\', %%aid%%, %%caseid%%, this ); return false;"', + ], + CRM_Core_Action::COPY => [ + 'name' => ts('Copy To Case'), + 'ref' => 'copy_to_case_action', + 'title' => ts('Copy To Case'), + 'extra' => 'onclick = "Javascript:fileOnCase( \'copy\', %%aid%%, %%caseid%%, this ); return false;"', + ], + ]; + } + return self::$_actionLinks; + } + } diff --git a/CRM/Report/Form/Activity.php b/CRM/Report/Form/Activity.php index 0f4facd7cff..a0aba07f908 100644 --- a/CRM/Report/Form/Activity.php +++ b/CRM/Report/Form/Activity.php @@ -909,20 +909,48 @@ public function alterDisplay(&$rows) { $cid = $rows[$rowNum]['civicrm_contact_contact_source_id']; } - $actActionLinks = CRM_Activity_Selector_Activity::actionLinks($row['civicrm_activity_activity_type_id'], - CRM_Utils_Array::value('civicrm_activity_source_record_id', $rows[$rowNum]), - FALSE, - $rows[$rowNum]['civicrm_activity_id'] - ); - - $actLinkValues = [ - 'id' => $rows[$rowNum]['civicrm_activity_id'], - 'cid' => $cid, - 'cxt' => $context, - ]; - $actUrl = CRM_Utils_System::url($actActionLinks[CRM_Core_Action::VIEW]['url'], - CRM_Core_Action::replace($actActionLinks[CRM_Core_Action::VIEW]['qs'], $actLinkValues), TRUE - ); + if (!empty($this->_params['include_case_activities_value'])) { + try { + $caseId = civicrm_api3('Activity', 'getvalue', [ + 'id' => $rows[$rowNum]['civicrm_activity_id'], + 'return' => 'case_id', + ]); + if (is_array($caseId)) { + $caseId = CRM_Utils_Array::first($caseId); + } + } + catch (CiviCRM_API3_Exception $e) { + // No case ID + } + } + if (empty($caseId)) { + $actActionLinks = CRM_Activity_Selector_Activity::actionLinks($row['civicrm_activity_activity_type_id'], + CRM_Utils_Array::value('civicrm_activity_source_record_id', $rows[$rowNum]), + FALSE, + $rows[$rowNum]['civicrm_activity_id'] + ); + + $actLinkValues = [ + 'id' => $rows[$rowNum]['civicrm_activity_id'], + 'cid' => $cid, + 'cxt' => $context, + ]; + $actUrl = CRM_Utils_System::url($actActionLinks[CRM_Core_Action::VIEW]['url'], + CRM_Core_Action::replace($actActionLinks[CRM_Core_Action::VIEW]['qs'], $actLinkValues), TRUE + ); + } + else { + $caseActionLinks = CRM_Case_Selector_Search::actionLinks(); + $caseLinkValues = [ + 'aid' => $rows[$rowNum]['civicrm_activity_id'], + 'caseid' => $caseId, + 'cid' => $cid, + 'cxt' => $context, + ]; + $actUrl = CRM_Utils_System::url($caseActionLinks[CRM_Core_Action::VIEW]['url'], + CRM_Core_Action::replace($caseActionLinks[CRM_Core_Action::VIEW]['qs'], $caseLinkValues), TRUE + ); + } } if (array_key_exists('civicrm_contact_contact_source', $row)) { diff --git a/templates/CRM/Case/Form/ActivityView.tpl b/templates/CRM/Case/Form/ActivityView.tpl index b1ac29b9113..cf4c0e92907 100644 --- a/templates/CRM/Case/Form/ActivityView.tpl +++ b/templates/CRM/Case/Form/ActivityView.tpl @@ -25,6 +25,7 @@ *} {* View Case Activities *}