X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FCase%2FBAO%2FCase.php;h=96eb28eb6b225ead557e322458ad36a63e22ba5a;hb=999ddf9a8b4ee71c148154c5e8b1e0d4478b3887;hp=8b6e31247c3d459ef5a1f79a9e117994023631b5;hpb=f0292b1ff0df4493c9b09374e07fd64f75a7cb99;p=civicrm-core.git diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index 8b6e31247c..96eb28eb6b 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -34,7 +34,7 @@ */ /** - * This class contains the funtions for Case Management + * This class contains the functions for Case Management * */ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { @@ -46,6 +46,7 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { * @static */ static $_exportableFields = NULL; + function __construct() { parent::__construct(); } @@ -57,7 +58,7 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { * case object. the params array could contain additional unused name/value * pairs * - * @param array $params (reference ) an assoc array of name/value pairs + * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Case_BAO_Case object @@ -321,7 +322,7 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { * Delete the record that are associated with this case * record are deleted from case * - * @param int $caseId id of the case to delete + * @param int $caseId id of the case to delete * * @return void * @access public @@ -373,12 +374,12 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { /** * Function to enable disable case related relationships * - * @param int $caseId case id - * @param boolean $enable action + * @param int $caseId case id + * @param boolean $enable action * - * @return void - * @access public - * @static + * @return void + * @access public + * @static */ static function enableDisableCaseRelationships($caseId, $enable) { $contactIds = self::retrieveContactIdsByCaseId($caseId); @@ -387,8 +388,8 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { $roles = self::getCaseRoles($cid, $caseId); if (!empty($roles)) { $relationshipIds = implode(',', array_keys($roles)); - $enable = (int)$enable; - $query = "UPDATE civicrm_relationship SET is_active = {$enable} + $enable = (int) $enable; + $query = "UPDATE civicrm_relationship SET is_active = {$enable} WHERE id IN ( {$relationshipIds} )"; CRM_Core_DAO::executeQuery($query); } @@ -399,7 +400,7 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { /** * Delete the activities related to case * - * @param int $activityId id of the activity + * @param int $activityId id of the activity * * @return void * @access public @@ -414,7 +415,7 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { /** * Retrieve contact_id by case_id * - * @param int $caseId ID of the case + * @param int $caseId ID of the case * * @return array * @access public @@ -461,7 +462,7 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { /** * Retrieve contact names by caseId * - * @param int $caseId ID of the case + * @param int $caseId ID of the case * * @return array * @@ -506,7 +507,7 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { /** * Retrieve case_id by contact_id * - * @param int $contactId ID of the contact + * @param int $contactId ID of the contact * @param boolean $includeDeleted include the deleted cases in result * * @return array @@ -669,11 +670,11 @@ LEFT JOIN civicrm_option_group aog ON aog.name='activity_type' * Retrieve cases related to particular contact or whole contact * used in Dashboad and Tab * - * @param boolean $allCases + * @param boolean $allCases * - * @param int $userID + * @param int $userID * - * @param String $type /upcoming,recent,all/ + * @param String $type /upcoming,recent,all/ * * @return array Array of Cases * @@ -842,9 +843,9 @@ AND civicrm_case.status_id != $closedId"; $allCases = FALSE; } - $caseTypes = CRM_Case_PseudoConstant::caseType(); + $caseTypes = CRM_Case_PseudoConstant::caseType(); $caseStatuses = CRM_Case_PseudoConstant::caseStatus(); - $caseTypes = array_flip($caseTypes); + $caseTypes = array_flip($caseTypes); // get statuses as headers for the table $url = CRM_Utils_System::url('civicrm/case/search', "reset=1&force=1&all=1&status="); @@ -863,10 +864,10 @@ AND civicrm_case.status_id != $closedId"; $case_owner = 1; } else { - $all = 0; + $all = 0; $case_owner = 2; $myCaseWhereClause = " AND case_relationship.contact_id_b = {$userID}"; - $myGroupByClause = " GROUP BY CONCAT(case_relationship.case_id,'-',case_relationship.contact_id_b)"; + $myGroupByClause = " GROUP BY CONCAT(case_relationship.case_id,'-',case_relationship.contact_id_b)"; } $seperator = CRM_Core_DAO::VALUE_SEPARATOR; @@ -908,8 +909,8 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type /** * Function to get Case roles * - * @param int $contactID contact id - * @param int $caseID case id + * @param int $contactID contact id + * @param int $caseID case id * @return returns case role / relationships * * @static @@ -963,9 +964,9 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type /** * Function to get Case Activities * - * @param int $caseID case id - * @param array $params posted params - * @param int $contactID contact id + * @param int $caseID case id + * @param array $params posted params + * @param int $contactID contact id * * @return returns case activities * @@ -974,6 +975,11 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type static function getCaseActivity($caseID, &$params, $contactID, $context = NULL, $userID = NULL, $type = NULL) { $values = array(); + $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); + $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); + $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); + $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); + // CRM-5081 - formatting the dates to omit seconds. // Note the 00 in the date format string is needed otherwise later on it thinks scheduled ones are overdue. $select = "SELECT count(ca.id) as ismultiple, ca.id as id, @@ -995,24 +1001,26 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type ca.weight as weight, GROUP_CONCAT(ef.file_id) as attachment_ids "; - $from = 'FROM civicrm_case_activity cca + $from = " + FROM civicrm_case_activity cca INNER JOIN civicrm_activity ca ON ca.id = cca.activity_id - INNER JOIN civicrm_contact cc ON cc.id = ca.source_contact_id - INNER JOIN civicrm_option_group cog ON cog.name = "activity_type" + INNER JOIN civicrm_activity_contact cac ON cac.activity_id = ca.id AND cac.record_type_id = {$sourceID} + INNER JOIN civicrm_contact cc ON cc.id = cac.contact_id + INNER JOIN civicrm_option_group cog ON cog.name = 'activity_type' INNER JOIN civicrm_option_value cov ON cov.option_group_id = cog.id AND cov.value = ca.activity_type_id AND cov.is_active = 1 - LEFT JOIN civicrm_entity_file ef on ef.entity_table = "civicrm_activity" AND ef.entity_id = ca.id - LEFT OUTER JOIN civicrm_option_group og ON og.name="activity_status" - LEFT OUTER JOIN civicrm_option_value ov ON ov.option_group_id=og.id AND ov.name="Scheduled" + LEFT JOIN civicrm_entity_file ef on ef.entity_table = 'civicrm_activity' AND ef.entity_id = ca.id + LEFT OUTER JOIN civicrm_option_group og ON og.name = 'activity_status' + LEFT OUTER JOIN civicrm_option_value ov ON ov.option_group_id=og.id AND ov.name = 'Scheduled' LEFT JOIN civicrm_activity_contact caa - ON caa.activity_id = ca.id AND caa.record_type = "Assignee" - LEFT JOIN civicrm_contact acc ON acc.id = caa.contact_id '; + ON caa.activity_id = ca.id AND caa.record_type_id = {$assigneeID} + LEFT JOIN civicrm_contact acc ON acc.id = caa.contact_id "; $where = 'WHERE cca.case_id= %1 AND ca.is_current_revision = 1'; if (CRM_Utils_Array::value('reporter_id', $params)) { - $where .= " AND ca.source_contact_id = " . CRM_Utils_Type::escape($params['reporter_id'], 'Integer'); + $where .= " AND cac.contact_id = " . CRM_Utils_Type::escape($params['reporter_id'], 'Integer'); } if (CRM_Utils_Array::value('status_id', $params)) { @@ -1026,7 +1034,6 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type $where .= " AND ca.is_deleted = 0"; } - if (CRM_Utils_Array::value('activity_type_id', $params)) { $where .= " AND ca.activity_type_id = " . CRM_Utils_Type::escape($params['activity_type_id'], 'Integer'); } @@ -1076,31 +1083,30 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type $rp = CRM_Utils_Array::value('rp', $params); if (!$page) { - $page = 1; - } if (!$rp) { $rp = 10; } $start = (($page - 1) * $rp); - $query = $select . $from . $where . $groupBy . $orderBy; - $params = array(1 => array($caseID, 'Integer')); - $dao = CRM_Core_DAO::executeQuery($query, $params); + $params = array(1 => array($caseID, 'Integer')); + $dao = CRM_Core_DAO::executeQuery($query, $params); $params['total'] = $dao->N; //FIXME: need to optimize/cache these queries $limit = " LIMIT $start, $rp"; $query .= $limit; + + //EXIT; $dao = CRM_Core_DAO::executeQuery($query, $params); - $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE); - $activityStatus = CRM_Core_PseudoConstant::activityStatus(); - $activityPriority = CRM_Core_PseudoConstant::priority(); + $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE); + $activityStatus = CRM_Core_PseudoConstant::activityStatus(); + $activityPriority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'); $url = CRM_Utils_System::url("civicrm/case/activity", "reset=1&cid={$contactID}&caseid={$caseID}", FALSE, NULL, FALSE @@ -1110,10 +1116,10 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type if ($context == 'fulltext') { $contextUrl = "&context={$context}"; } - $editUrl = "{$url}&action=update{$contextUrl}"; - $deleteUrl = "{$url}&action=delete{$contextUrl}"; - $restoreUrl = "{$url}&action=renew{$contextUrl}"; - $viewTitle = ts('View this activity.'); + $editUrl = "{$url}&action=update{$contextUrl}"; + $deleteUrl = "{$url}&action=delete{$contextUrl}"; + $restoreUrl = "{$url}&action=renew{$contextUrl}"; + $viewTitle = ts('View this activity.'); $statusTitle = ts('Edit status'); $emailActivityTypeIDs = array( @@ -1159,8 +1165,8 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type while ($dao->fetch()) { - $allowView = self::checkPermission($dao->id, 'view', $dao->activity_type_id, $userID); - $allowEdit = self::checkPermission($dao->id, 'edit', $dao->activity_type_id, $userID); + $allowView = self::checkPermission($dao->id, 'view', $dao->activity_type_id, $userID); + $allowEdit = self::checkPermission($dao->id, 'edit', $dao->activity_type_id, $userID); $allowDelete = self::checkPermission($dao->id, 'delete', $dao->activity_type_id, $userID); //do not have sufficient permission @@ -1177,8 +1183,7 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type $reporterName = '' . $dao->reporter . ''; } $values[$dao->id]['reporter'] = $reporterName; - - $targetNames = CRM_Activity_BAO_ActivityTarget::getTargetNames($dao->id); + $targetNames = CRM_Activity_BAO_ActivityContact::getNames($dao->id, $targetID); $targetContactUrls = $withContacts = array(); foreach ($targetNames as $targetId => $targetName) { if (!in_array($targetId, $clientIds)) { @@ -1245,10 +1250,10 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type $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)){ - $attachmentIDs = explode(',',$dao->attachment_ids); + if (!empty($dao->attachment_ids)) { + $attachmentIDs = explode(',', $dao->attachment_ids); $values[$dao->id]['no_attachments'] = count($attachmentIDs); - if($values[$dao->id]['no_attachments'] == 1){ + if ($values[$dao->id]['no_attachments'] == 1) { // if there is only one it's easy to do a link - otherwise just flag it $attachmentViewUrl = CRM_Utils_System::url( "civicrm/file", @@ -1287,7 +1292,7 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type } if ($allowEdit) { - $values[$dao->id]['status'] = '' . $values[$dao->id]['status'] . ''; + $values[$dao->id]['status'] = '' . $values[$dao->id]['status'] . ''; } } $dao->free(); @@ -1298,7 +1303,7 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type /** * Function to get Case Related Contacts * - * @param int $caseID case id + * @param int $caseID case id * @param boolean $skipDetails if true include details of contacts * * @return returns $searchRows array of returnproperties @@ -1340,8 +1345,8 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type /** * Function that sends e-mail copy of activity * - * @param int $activityId activity Id - * @param array $contacts array of related contact + * @param int $activityId activity Id + * @param array $contacts array of related contact * * @return void * @access public @@ -1388,7 +1393,7 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type $session = CRM_Core_Session::singleton(); // CRM-8926 If user is not logged in, use the activity creator as userID if (!($userID = $session->get('userID'))) { - $userID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityId, 'source_contact_id'); + $userID = CRM_Activity_BAO_Activity::getSourceContactID($activityId); } //also create activities simultaneously of this copy. @@ -1422,13 +1427,13 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type $tplParams['contact'] = $info; self::buildPermissionLinks($tplParams, $activityParams); - $displayName = $info['display_name']; + $displayName = CRM_Utils_Array::value('display_name', $info); - list($result[$info['contact_id']], $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate( + list($result[CRM_Utils_Array::value('contact_id', $info)], $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate( array( 'groupName' => 'msg_tpl_workflow_case', 'valueName' => 'case_activity', - 'contactId' => $info['contact_id'], + 'contactId' => CRM_Utils_Array::value('contact_id', $info), 'tplParams' => $tplParams, 'from' => $receiptFrom, 'toName' => $displayName, @@ -1440,7 +1445,7 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type $activityParams['subject'] = $activitySubject . ' - copy sent to ' . $displayName; $activityParams['details'] = $message; - if ($result[$info['contact_id']]) { + if (!empty($result[$info['contact_id']])) { /* * Really only need to record one activity with all the targets combined. * Originally the template was going to possibly have different content, e.g. depending on permissions, @@ -1455,7 +1460,7 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type $recordedActivityParams['target_contact_id'][] = $info['contact_id']; } else { - unset($result[$info['contact_id']]); + unset($result[CRM_Utils_Array::value('contact_id', $info)]); } } @@ -1479,8 +1484,8 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type * Retrieve count of activities having a particular type, and * associated with a particular case. * - * @param int $caseId ID of the case - * @param int $activityTypeId ID of the activity type + * @param int $caseId ID of the case + * @param int $activityTypeId ID of the activity type * * @return array * @@ -1488,7 +1493,8 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type * */ static function getCaseActivityCount($caseId, $activityTypeId) { - $queryParam = array(1 => array($caseId, 'Integer'), + $queryParam = array( + 1 => array($caseId, 'Integer'), 2 => array($activityTypeId, 'Integer'), ); $query = "SELECT count(ca.id) as countact @@ -1509,7 +1515,7 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type /** * Create an activity for a case via email * - * @param int $file email sent + * @param int $file email sent * * @return $activity object of newly creted activity via email * @@ -1598,9 +1604,9 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type } /** - * Function to retrive the scheduled activity type and date + * Function to retrieve the scheduled activity type and date * - * @param array $cases Array of contact and case id + * @param array $cases Array of contact and case id * * @return array $activityInfo Array of scheduled activity type and date * @@ -1652,12 +1658,14 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type self::$_exportableFields = array(); } - $fields = CRM_Case_DAO_Case::export(); + $fields = CRM_Case_DAO_Case::export(); $fields['case_role'] = array('title' => ts('Role in Case')); - $fields['case_type'] = array('title' => ts('Case Type'), + $fields['case_type'] = array( + 'title' => ts('Case Type'), 'name' => 'case_type', ); - $fields['case_status'] = array('title' => ts('Case Status'), + $fields['case_status'] = array( + 'title' => ts('Case Status'), 'name' => 'case_status', ); @@ -1669,7 +1677,7 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type /** * Restore the record that are associated with this case * - * @param int $caseId id of the case to restore + * @param int $caseId id of the case to restore * * @return true if success. * @access public @@ -1684,8 +1692,8 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type } } //restore case - $case = new CRM_Case_DAO_Case(); - $case->id = $caseId; + $case = new CRM_Case_DAO_Case(); + $case->id = $caseId; $case->is_deleted = 0; $case->save(); @@ -1706,11 +1714,11 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type $results = array(); CRM_Contact_BAO_Group::retrieve($searchParams, $results); if ($results) { - $groupInfo['id'] = $results['id']; + $groupInfo['id'] = $results['id']; $groupInfo['title'] = $results['title']; - $params = array(array('group', 'IN', array($groupInfo['id'] => 1), 0, 0)); - $return = array('sort_name' => 1, 'display_name' => 1, 'email' => 1, 'phone' => 1); - $return = array('contact_id' => 1, 'sort_name' => 1, 'display_name' => 1, 'email' => 1, 'phone' => 1); + $params = array(array('group', 'IN', array($groupInfo['id'] => 1), 0, 0)); + $return = array('sort_name' => 1, 'display_name' => 1, 'email' => 1, 'phone' => 1); + $return = array('contact_id' => 1, 'sort_name' => 1, 'display_name' => 1, 'email' => 1, 'phone' => 1); list($globalContacts, $_) = CRM_Contact_BAO_Query::apiQuery($params, $return, NULL, $sort, $offset, $rowCount, TRUE, $returnOnlyCount); if ($returnOnlyCount) { @@ -1718,7 +1726,7 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type } if ($showLinks) { - foreach($globalContacts as $idx => $contact) { + foreach ($globalContacts as $idx => $contact) { $globalContacts[$idx]['sort_name'] = '' . $contact['sort_name'] . ''; } } @@ -1753,19 +1761,18 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type /** * Function to get Case ActivitiesDueDates with given criteria. * - * @param int $caseID case id - * @param array $criteriaParams given criteria - * @param boolean $latestDate if set newest or oldest date is selceted. + * @param int $caseID case id + * @param array $criteriaParams given criteria + * @param boolean $latestDate if set newest or oldest date is selceted. * * @return returns case activities due dates * * @static */ - static function getCaseActivityDates($caseID, $criteriaParams = array( - ), $latestDate = FALSE) { - $values = array(); + static function getCaseActivityDates($caseID, $criteriaParams = array(), $latestDate = FALSE) { + $values = array(); $selectDate = " ca.activity_date_time"; - $where = $groupBy = ' '; + $where = $groupBy = ' '; if (!$caseID) { return; @@ -1805,9 +1812,9 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type /** * Function to create activities when Case or Other roles assigned/modified/deleted. * - * @param int $caseID case id - * @param int $relationshipId relationship id - * @param int $relContactId case role assigne contactId. + * @param int $caseID case id + * @param int $relationshipId relationship id + * @param int $relContactId case role assignee contactId. * * @return void on success creates activity and case activity * @@ -1898,8 +1905,8 @@ SELECT case_status.label AS case_status, status_id, case_type.label AS case_type * Function to get case manger * contact which is assigned a case role of case manager. * - * @param int $caseType case type - * @param int $caseId case id + * @param int $caseType case type + * @param int $caseId case id * * @return array $caseManagerContact array of contact on success otherwise empty * @@ -1944,16 +1951,15 @@ SELECT civicrm_contact.id as casemanager_id, * * @return array of case and related data keyed on case id */ - static function getUnclosedCases($params = array( - ), $excludeCaseIds = array(), $excludeDeleted = TRUE) { + static function getUnclosedCases($params = array(), $excludeCaseIds = array(), $excludeDeleted = TRUE) { //params from ajax call. $where = array('( ca.end_date is null )'); if ($caseType = CRM_Utils_Array::value('case_type', $params)) { $where[] = "( ov.label LIKE '%$caseType%' )"; } if ($sortName = CRM_Utils_Array::value('sort_name', $params)) { - $config = CRM_Core_Config::singleton(); - $search = ($config->includeWildCardInName) ? "%$sortName%" : "$sortName%"; + $config = CRM_Core_Config::singleton(); + $search = ($config->includeWildCardInName) ? "%$sortName%" : "$sortName%"; $where[] = "( sort_name LIKE '$search' )"; } if (is_array($excludeCaseIds) && @@ -1970,8 +1976,8 @@ SELECT civicrm_contact.id as casemanager_id, $doFilterCases = FALSE; if (!CRM_Core_Permission::check('access all cases and activities')) { $doFilterCases = TRUE; - $session = CRM_Core_Session::singleton(); - $filterCases = CRM_Case_BAO_Case::getCases(FALSE, $session->get('userID')); + $session = CRM_Core_Session::singleton(); + $filterCases = CRM_Case_BAO_Case::getCases(FALSE, $session->get('userID')); } $whereClause = implode(' AND ', $where); @@ -2052,7 +2058,7 @@ LEFT JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.case /** * Retrieve cases related to particular contact. * - * @param int $contactId contact id + * @param int $contactId contact id * @param boolean $excludeDeleted do not include deleted cases. * * @return an array of cases. @@ -2095,8 +2101,8 @@ INNER JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.cas /** * Retrieve related cases for give case. * - * @param int $mainCaseId id of main case - * @param int $contactId id of contact + * @param int $mainCaseId id of main case + * @param int $contactId id of contact * @param boolean $excludeDeleted do not include deleted cases. * * @return an array of related cases. @@ -2158,8 +2164,8 @@ INNER JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.cas $doFilterCases = FALSE; if (!CRM_Core_Permission::check('access all cases and activities')) { $doFilterCases = TRUE; - $session = CRM_Core_Session::singleton(); - $filterCases = CRM_Case_BAO_Case::getCases(FALSE, $session->get('userID')); + $session = CRM_Core_Session::singleton(); + $filterCases = CRM_Case_BAO_Case::getCases(FALSE, $session->get('userID')); } //2. fetch the details of related cases. @@ -2176,7 +2182,7 @@ INNER JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.cas AND case_type_og.id = case_type_ov.option_group_id ) WHERE {$whereClause}"; - $dao = CRM_Core_DAO::executeQuery($query); + $dao = CRM_Core_DAO::executeQuery($query); $contactViewUrl = CRM_Utils_System::url("civicrm/contact/view", "reset=1&cid="); $hasViewContact = CRM_Core_Permission::giveMeAllACLs(); @@ -2185,7 +2191,7 @@ INNER JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.cas if (!$doFilterCases || array_key_exists($dao->id, $filterCases)) { $caseViewStr = "reset=1&id={$dao->id}&cid={$dao->client_id}&action=view&context=case&selectedChild=case"; $caseViewUrl = CRM_Utils_System::url("civicrm/contact/view/case", $caseViewStr); - $caseView = "" . ts('View Case') . ""; + $caseView = "" . ts('View Case') . ""; } $clientView = $dao->client_name; if ($hasViewContact) { @@ -2229,7 +2235,7 @@ INNER JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.cas * @static */ static function mergeCases($mainContactId, $mainCaseId = NULL, $otherContactId = NULL, - $otherCaseId = NULL, $changeClient = FALSE ) { + $otherCaseId = NULL, $changeClient = FALSE) { $moveToTrash = TRUE; $duplicateContacts = FALSE; @@ -2253,6 +2259,10 @@ INNER JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.cas $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name'); $activityStatuses = CRM_Core_PseudoConstant::activityStatus('name'); + $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); + $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); + $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); + $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); $processCaseIds = array($otherCaseId); if ($duplicateContacts && !$duplicateCases) { @@ -2279,7 +2289,31 @@ INNER JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.cas if (!$mainCaseId) { continue; } + + // CRM-11662 Copy Case custom data + $extends = array('case'); + $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends); + if ($groupTree) { + foreach ($groupTree as $groupID => $group) { + $table[$groupTree[$groupID]['table_name']] = array('entity_id'); + foreach ($group['fields'] as $fieldID => $field) { + $table[$groupTree[$groupID]['table_name']][] = $groupTree[$groupID]['fields'][$fieldID]['column_name']; + } + } + + foreach ($table as $tableName => $tableColumns) { + $insert = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $tableColumns) . ') '; + $tableColumns[0] = $mainCaseId; + $select = 'SELECT ' . implode(', ', $tableColumns); + $from = ' FROM ' . $tableName; + $where = " WHERE {$tableName}.entity_id = {$otherCaseId}"; + $query = $insert . $select . $from . $where; + $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); + } + } + $mainCase->free(); + $mainCaseIds[] = $mainCaseId; //insert record for case contact. $otherCaseContact = new CRM_Case_DAO_CaseContact(); @@ -2362,10 +2396,6 @@ SELECT id $mainActivity->copyValues($mainActVals); $mainActivity->id = NULL; $mainActivity->activity_date_time = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time); - //do check for merging contact, - if ($mainActivity->source_contact_id == $otherContactId) { - $mainActivity->source_contact_id = $mainContactId; - } $mainActivity->source_record_id = CRM_Utils_Array::value($mainActivity->source_record_id, $activityMappingIds ); @@ -2384,7 +2414,7 @@ SELECT id } $activityMappingIds[$otherActivityId] = $mainActivityId; - // insert log of all activites + // insert log of all activities CRM_Activity_BAO_Activity::logActivityAction($mainActivity); $otherActivity->free(); @@ -2398,14 +2428,35 @@ SELECT id $mainCaseActivity->save(); $mainCaseActivity->free(); + //migrate source activity. + $otherSourceActivity = new CRM_Activity_DAO_ActivityContact(); + $otherSourceActivity->activity_id = $otherActivityId; + $otherSourceActivity->record_type_id = $sourceID; + $otherSourceActivity->find(); + while ($otherSourceActivity->fetch()) { + $mainActivitySource = new CRM_Activity_DAO_ActivityContact(); + $mainActivitySource->record_type_id = $sourceID; + $mainActivitySource->activity_id = $mainActivityId; + $mainActivitySource->contact_id = $otherSourceActivity->contact_id; + if ($mainActivitySource->contact_id == $otherContactId) { + $mainActivitySource->contact_id = $mainContactId; + } + //avoid duplicate object. + if (!$mainActivitySource->find(TRUE)) { + $mainActivitySource->save(); + } + $mainActivitySource->free(); + } + $otherSourceActivity->free(); + //migrate target activities. $otherTargetActivity = new CRM_Activity_DAO_ActivityContact(); $otherTargetActivity->activity_id = $otherActivityId; - $otherTargetActivity->record_type = 'Target'; + $otherTargetActivity->record_type_id = $targetID; $otherTargetActivity->find(); while ($otherTargetActivity->fetch()) { $mainActivityTarget = new CRM_Activity_DAO_ActivityContact(); - $mainActivityTarget->record_type = 'Target'; + $mainActivityTarget->record_type_id = $targetID; $mainActivityTarget->activity_id = $mainActivityId; $mainActivityTarget->contact_id = $otherTargetActivity->contact_id; if ($mainActivityTarget->contact_id == $otherContactId) { @@ -2422,12 +2473,12 @@ SELECT id //migrate assignee activities. $otherAssigneeActivity = new CRM_Activity_DAO_ActivityContact(); $otherAssigneeActivity->activity_id = $otherActivityId; - $otherAssigneeActivity->record_type = 'Assignee'; + $otherAssigneeActivity->record_type_id = $assigneeID; $otherAssigneeActivity->find(); while ($otherAssigneeActivity->fetch()) { $mainAssigneeActivity = new CRM_Activity_DAO_ActivityContact(); $mainAssigneeActivity->activity_id = $mainActivityId; - $mainAssigneeActivity->record_type = 'Assignee'; + $mainAssigneeActivity->record_type_id = $assigneeID; $mainAssigneeActivity->contact_id = $otherAssigneeActivity->contact_id; if ($mainAssigneeActivity->contact_id == $otherContactId) { $mainAssigneeActivity->contact_id = $mainContactId; @@ -2439,6 +2490,13 @@ SELECT id $mainAssigneeActivity->free(); } $otherAssigneeActivity->free(); + + // copy custom fields and attachments + $aparams = array( + 'activityID' => $otherActivityId, + 'mainActivityId' => $mainActivityId, + ); + CRM_Activity_BAO_Activity::copyExtendedActivityData($aparams); } //copy case relationship. @@ -2509,8 +2567,10 @@ SELECT id $mergeActType = array_search('Reassigned Case', $activityTypes); $mergeActSubject = ts("Case %1 reassigned client from %2 to %3. New Case ID is %4.", array( - 1 => $otherCaseId, 2 => $otherContactDisplayName, - 3 => $mainContactDisplayName, 4 => $mainCaseId + 1 => $otherCaseId, + 2 => $otherContactDisplayName, + 3 => $mainContactDisplayName, + 4 => $mainCaseId ) ); } @@ -2518,8 +2578,10 @@ SELECT id $mergeActType = array_search('Merge Case', $activityTypes); $mergeActSubject = ts("Case %1 copied from contact id %2 to contact id %3 via merge. New Case ID is %4.", array( - 1 => $otherCaseId, 2 => $otherContactId, - 3 => $mainContactId, 4 => $mainCaseId + 1 => $otherCaseId, + 2 => $otherContactId, + 3 => $mainContactId, + 4 => $mainCaseId ) ); } @@ -2574,8 +2636,8 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; * Validate contact permission for * edit/view on activity record and build links. * - * @param array $tplParams params to be sent to template for sending email. - * @param array $activityParams info of the activity. + * @param array $tplParams params to be sent to template for sending email. + * @param array $activityParams info of the activity. * * @return void * @static @@ -2613,10 +2675,10 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; * Validate contact permission for * given operation on activity record. * - * @param int $activityId activity record id. - * @param string $operation user operation. - * @param int $actTypeId activity type id. - * @param int $contactId contact id/if not pass consider logged in + * @param int $activityId activity record id. + * @param string $operation user operation. + * @param int $actTypeId activity type id. + * @param int $contactId contact id/if not pass consider logged in * @param boolean $checkComponent do we need to check component enabled. * * @return boolean $allow true/false @@ -2711,7 +2773,10 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; if (!empty($hasPermissions)) { //need to check activity object specific. if (in_array($operation, array( - 'view', 'edit'))) { + 'view', + 'edit' + )) + ) { //do we have supper permission. if (in_array('access all cases and activities', $hasPermissions[$operation])) { $allowOperations[$operation] = $allow = TRUE; @@ -2723,9 +2788,13 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; //edit - contact must be source or assignee //view - contact must be source/assignee/target $isTarget = $isAssignee = $isSource = FALSE; + $activityContacts = CRM_Core_PseudoConstant::activityContacts('name'); + $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); + $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); + $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); $target = new CRM_Activity_DAO_ActivityContact(); - $target->record_type = 'Target'; + $target->record_type_id = $targetID; $target->activity_id = $activityId; $target->contact_id = $contactId; if ($target->find(TRUE)) { @@ -2734,16 +2803,17 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; $assignee = new CRM_Activity_DAO_ActivityContact(); $assignee->activity_id = $activityId; - $assignee->record_type = 'Assignee'; + $assignee->record_type_id = $assigneeID; $assignee->contact_id = $contactId; if ($assignee->find(TRUE)) { $isAssignee = TRUE; } - $activity = new CRM_Activity_DAO_Activity(); - $activity->id = $activityId; - $activity->source_contact_id = $contactId; - if ($activity->find(TRUE)) { + $source = new CRM_Activity_DAO_ActivityContact(); + $source->activity_id = $activityId; + $source->record_type_id = $sourceID; + $source->contact_id = $contactId; + if ($source->find(TRUE)) { $isSource = TRUE; } @@ -2783,7 +2853,15 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; $actTypeName = CRM_Utils_Array::value($actTypeId, $activityTypes); //do not allow multiple copy / edit action. - $singletonNames = array('Open Case', 'Reassigned Case', 'Merge Case', 'Link Cases', 'Assign Case Role', 'Email', 'Inbound Email'); + $singletonNames = array( + 'Open Case', + 'Reassigned Case', + 'Merge Case', + 'Link Cases', + 'Assign Case Role', + 'Email', + 'Inbound Email' + ); //do not allow to delete these activities, CRM-4543 $doNotDeleteNames = array('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date'); @@ -2792,7 +2870,16 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; $allowEditNames = array('Open Case'); // do not allow File on Case - $doNotFileNames = array('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date','Reassigned Case', 'Merge Case', 'Link Cases', 'Assign Case Role'); + $doNotFileNames = array( + 'Open Case', + 'Change Case Type', + 'Change Case Status', + 'Change Case Start Date', + 'Reassigned Case', + 'Merge Case', + 'Link Cases', + 'Assign Case Role' + ); if (in_array($actTypeName, $singletonNames)) { $allow = FALSE; @@ -2872,7 +2959,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; /** * Function to check whether activity is a case Activity * - * @param int $activityID activity id + * @param int $activityID activity id * * @return boolean $isCaseActivity true/false */ @@ -3067,16 +3154,16 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; break; } return $sql; - } - - /** - * Function to add/copy relationships, when new client is added for a case - * - * @param int $caseId case id - * @param int $contactId contact id / new client id - * - * @return void - */ + } + + /** + * Function to add/copy relationships, when new client is added for a case + * + * @param int $caseId case id + * @param int $contactId contact id / new client id + * + * @return void + */ static function addCaseRelationships($caseId, $contactId) { // get the case role / relationships for the case $caseRelationships = new CRM_Contact_DAO_Relationship(); @@ -3123,11 +3210,31 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; $caseContact->case_id = $caseId; $caseContact->find(); - while($caseContact->fetch()) { + while ($caseContact->fetch()) { $clients[] = $caseContact->contact_id; } return $clients; } + + /** + * Get options for a given case field. + * @see CRM_Core_DAO::buildOptions + * + * @param String $fieldName + * @param String $context: @see CRM_Core_DAO::buildOptionsContext + * @param Array $props: whatever is known about this dao object + */ + public static function buildOptions($fieldName, $context = NULL, $props = array()) { + $className = __CLASS__; + $params = array(); + switch ($fieldName) { + // This field is not part of this object but the api supports it + case 'medium_id': + $className = 'CRM_Activity_BAO_Activity'; + break; + } + return CRM_Core_PseudoConstant::get($className, $fieldName, $params, $context); + } }