| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * This class does pre processing for viewing an activity or their revisions. |
| 20 | */ |
| 21 | class CRM_Case_Form_ActivityView extends CRM_Core_Form { |
| 22 | |
| 23 | /** |
| 24 | * Process the view. |
| 25 | */ |
| 26 | public function preProcess() { |
| 27 | $contactID = CRM_Utils_Request::retrieve('cid', 'Integer', $this, TRUE); |
| 28 | $activityID = CRM_Utils_Request::retrieve('aid', 'Integer', $this, TRUE); |
| 29 | $revs = CRM_Utils_Request::retrieve('revs', 'Boolean'); |
| 30 | $caseID = CRM_Utils_Request::retrieve('caseID', 'Boolean'); |
| 31 | $activitySubject = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', |
| 32 | $activityID, |
| 33 | 'subject' |
| 34 | ); |
| 35 | |
| 36 | //check for required permissions, CRM-6264 |
| 37 | if ($activityID && |
| 38 | !CRM_Activity_BAO_Activity::checkPermission($activityID, CRM_Core_Action::VIEW) |
| 39 | ) { |
| 40 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
| 41 | } |
| 42 | |
| 43 | $this->assign('contactID', $contactID); |
| 44 | $this->assign('caseID', $caseID); |
| 45 | // CRM-9145 |
| 46 | $this->assign('activityID', $activityID); |
| 47 | |
| 48 | $xmlProcessor = new CRM_Case_XMLProcessor_Report(); |
| 49 | $report = $xmlProcessor->getActivityInfo($contactID, $activityID, TRUE); |
| 50 | |
| 51 | $attachmentUrl = CRM_Core_BAO_File::attachmentInfo('civicrm_activity', $activityID); |
| 52 | if ($attachmentUrl) { |
| 53 | $report['fields'][] = [ |
| 54 | 'label' => 'Attachment(s)', |
| 55 | 'value' => $attachmentUrl, |
| 56 | 'type' => 'Link', |
| 57 | ]; |
| 58 | } |
| 59 | |
| 60 | $tags = CRM_Core_BAO_EntityTag::getTag($activityID, 'civicrm_activity'); |
| 61 | if (!empty($tags)) { |
| 62 | $allTag = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', ['onlyActive' => FALSE]); |
| 63 | foreach ($tags as $tid) { |
| 64 | $tags[$tid] = $allTag[$tid]; |
| 65 | } |
| 66 | $report['fields'][] = [ |
| 67 | 'label' => 'Tags', |
| 68 | 'value' => implode('<br />', $tags), |
| 69 | 'type' => 'String', |
| 70 | ]; |
| 71 | } |
| 72 | |
| 73 | $this->assign('report', $report); |
| 74 | |
| 75 | $latestRevisionID = CRM_Activity_BAO_Activity::getLatestActivityId($activityID); |
| 76 | |
| 77 | $viewPriorActivities = []; |
| 78 | $priorActivities = CRM_Activity_BAO_Activity::getPriorAcitivities($activityID); |
| 79 | foreach ($priorActivities as $activityId => $activityValues) { |
| 80 | if (CRM_Case_BAO_Case::checkPermission($activityId, 'view', NULL, $contactID)) { |
| 81 | $viewPriorActivities[$activityId] = $activityValues; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if ($revs) { |
| 86 | CRM_Utils_System::setTitle(ts('Activity Revision History')); |
| 87 | $this->assign('revs', $revs); |
| 88 | $this->assign('result', $viewPriorActivities); |
| 89 | $this->assign('subject', $activitySubject); |
| 90 | $this->assign('latestRevisionID', $latestRevisionID); |
| 91 | } |
| 92 | else { |
| 93 | if (count($viewPriorActivities) > 1) { |
| 94 | $this->assign('activityID', $activityID); |
| 95 | } |
| 96 | |
| 97 | if ($latestRevisionID != $activityID) { |
| 98 | $this->assign('latestRevisionID', $latestRevisionID); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | $parentID = CRM_Activity_BAO_Activity::getParentActivity($activityID); |
| 103 | if ($parentID) { |
| 104 | $this->assign('parentID', $parentID); |
| 105 | } |
| 106 | |
| 107 | //viewing activity should get diplayed in recent list.CRM-4670 |
| 108 | $activityTypeID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityID, 'activity_type_id'); |
| 109 | |
| 110 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
| 111 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 112 | $activityTargetContacts = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activityID, $targetID); |
| 113 | if (!empty($activityTargetContacts)) { |
| 114 | $recentContactId = $activityTargetContacts[0]; |
| 115 | } |
| 116 | else { |
| 117 | $recentContactId = $contactID; |
| 118 | } |
| 119 | |
| 120 | if (!isset($caseID)) { |
| 121 | $caseID = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseActivity', $activityID, 'case_id', 'activity_id'); |
| 122 | } |
| 123 | |
| 124 | $url = CRM_Utils_System::url('civicrm/case/activity/view', |
| 125 | "reset=1&aid={$activityID}&cid={$recentContactId}&caseID={$caseID}&context=home" |
| 126 | ); |
| 127 | |
| 128 | $recentContactDisplay = CRM_Contact_BAO_Contact::displayName($recentContactId); |
| 129 | // add the recently created Activity |
| 130 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE); |
| 131 | |
| 132 | $title = ""; |
| 133 | if (isset($activitySubject)) { |
| 134 | $title = $activitySubject . ' - '; |
| 135 | } |
| 136 | |
| 137 | $title = $title . $recentContactDisplay . ' (' . $activityTypes[$activityTypeID] . ')'; |
| 138 | |
| 139 | $recentOther = []; |
| 140 | if (CRM_Case_BAO_Case::checkPermission($activityID, 'edit')) { |
| 141 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/case/activity', |
| 142 | "reset=1&action=update&id={$activityID}&cid={$recentContactId}&caseid={$caseID}&context=home" |
| 143 | ); |
| 144 | } |
| 145 | if (CRM_Case_BAO_Case::checkPermission($activityID, 'delete')) { |
| 146 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/case/activity', |
| 147 | "reset=1&action=delete&id={$activityID}&cid={$recentContactId}&caseid={$caseID}&context=home" |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | CRM_Utils_Recent::add($title, |
| 152 | $url, |
| 153 | $activityID, |
| 154 | 'Activity', |
| 155 | $recentContactId, |
| 156 | $recentContactDisplay, |
| 157 | $recentOther |
| 158 | ); |
| 159 | |
| 160 | // Set breadcrumb to take the user back to the case being viewed |
| 161 | $caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $caseID, 'case_type_id'); |
| 162 | $caseType = CRM_Core_PseudoConstant::getLabel('CRM_Case_BAO_Case', 'case_type_id', $caseTypeId); |
| 163 | $caseContact = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $caseID, 'contact_id', 'case_id'); |
| 164 | |
| 165 | CRM_Utils_System::resetBreadCrumb(); |
| 166 | $breadcrumb = [ |
| 167 | [ |
| 168 | 'title' => ts('Home'), |
| 169 | 'url' => CRM_Utils_System::url(), |
| 170 | ], |
| 171 | [ |
| 172 | 'title' => ts('CiviCRM'), |
| 173 | 'url' => CRM_Utils_System::url('civicrm', 'reset=1'), |
| 174 | ], |
| 175 | [ |
| 176 | 'title' => ts('CiviCase Dashboard'), |
| 177 | 'url' => CRM_Utils_System::url('civicrm/case', 'reset=1'), |
| 178 | ], |
| 179 | [ |
| 180 | 'title' => $caseType, |
| 181 | 'url' => CRM_Utils_System::url('civicrm/contact/view/case', [ |
| 182 | 'reset' => 1, |
| 183 | 'id' => $caseID, |
| 184 | 'context' => 'case', |
| 185 | 'action' => 'view', |
| 186 | 'cid' => $caseContact, |
| 187 | ]), |
| 188 | ], |
| 189 | ]; |
| 190 | CRM_Utils_System::appendBreadCrumb($breadcrumb); |
| 191 | |
| 192 | $this->addButtons([ |
| 193 | [ |
| 194 | 'type' => 'cancel', |
| 195 | 'name' => ts('Done'), |
| 196 | ], |
| 197 | ]); |
| 198 | // Add additional action links |
| 199 | $activityDeleted = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityID, 'is_deleted'); |
| 200 | $actionLinks = CRM_Case_Selector_Search::permissionedActionLinks($caseID, $contactID, CRM_Core_Session::getLoggedInContactID(), NULL, $activityTypeID, $activityDeleted, $activityID, FALSE); |
| 201 | $this->assign('actionLinks', $actionLinks); |
| 202 | } |
| 203 | |
| 204 | } |