From: Donald A. Lobo Date: Mon, 8 Apr 2013 20:33:34 +0000 (-0700) Subject: CRM-12274 - more cleanup X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1d85d2412198a17bc9ec07ccb006cf51263c77a4;p=civicrm-core.git CRM-12274 - more cleanup ---------------------------------------- * CRM-12274: Merge activity tables into one unified table activity contact table http://issues.civicrm.org/jira/browse/CRM-12274 --- diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index 1dab9c9ce1..258439ff1d 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -198,8 +198,10 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { //log activty delete.CRM-4525. $logMsg = 'Case Activity deleted for'; $msgs = array(); - $sourceContactId = CRM_Core_DAO::getfieldValue('CRM_Activity_DAO_Activity', - $activity->id, 'source_contact_id' + $sourceContactId = CRM_Core_DAO::getFieldValue( + 'CRM_Activity_DAO_Activity', + $activity->id, + 'source_contact_id' ); if ($sourceContactId) { $msgs[] = " source={$sourceContactId}"; @@ -247,67 +249,13 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { * @return null * @access public */ - public static function deleteActivityAssignment($activityId) { - $assignment = new CRM_Activity_BAO_ActivityAssignment(); - $assignment->activity_id = $activityId; - $assignment->record_type = 'Assignee'; - $assignment->delete(); - } - - /** - * Delete activity target record - * - * @param int $id activity id - * - * @return null - * @access public - */ - public static function deleteActivityTarget($activityId) { - $target = new CRM_Activity_BAO_ActivityTarget(); - $target->activity_id = $activityId; - $target->record_type = 'Target'; - $target->delete(); - } - - /** - * Create activity target record - * - * @param array activity_id, target_contact_id - * - * @return null - * @access public - */ - static function createActivityTarget($params) { - if (!$params['target_contact_id']) { - return; - } - - $target = new CRM_Activity_BAO_ActivityTarget(); - $target->activity_id = $params['activity_id']; - $target->target_contact_id = $params['target_contact_id']; - // avoid duplicate entries, CRM-7484 - // happens if sending email to the same contact with different email addresses - if (!$target->find(TRUE)) { - $target->save(); - } - } - - /** - * Create activity assignment record - * - * @param array activity_id, assignee_contact_id - * - * @return null - * @access public - */ - static function createActivityAssignment($params) { - if (!$params['assignee_contact_id']) { - return; + public static function deleteActivityContact($activityId, $recordType = NULL) { + $activityContact = new CRM_Activity_BAO_ActivityContact(); + $activityContact->activity_id = $activityId; + if ($recordType) { + $activityContact->record_type = $recordType; } - $assignee = new CRM_Activity_BAO_ActivityAssignment(); - $assignee->activity_id = $params['activity_id']; - $assignee->assignee_contact_id = $params['assignee_contact_id']; - $assignee->save(); + $activityContact->delete(); } /** @@ -394,9 +342,19 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { $activityId = $activity->id; + if (isset($params['source_contact_id'])) { + $acParams = array( + 'activity_id' => $activityId, + 'contact_id' => $params['source_contact_id'], + 'record_type' => 'Source' + ); + CRM_Activity_BAO_ActivityContact::create($acParams); + } + // check and attach and files as needed CRM_Core_BAO_File::processAttachment($params, 'civicrm_activity', $activityId); + // attempt to save activity assignment $resultAssignment = NULL; if (CRM_Utils_Array::value('assignee_contact_id', $params)) { @@ -406,7 +364,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { if (is_array($params['assignee_contact_id'])) { if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) { // first delete existing assignments if any - self::deleteActivityAssignment($activityId); + self::deleteActivityContact($activityId, 'Assignee'); } $values = array(); @@ -442,7 +400,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { } else { if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) { - self::deleteActivityAssignment($activityId); + self::deleteActivityContact($activityId, 'Assignee'); } } @@ -460,7 +418,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { if (is_array($params['target_contact_id'])) { if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) { // first delete existing targets if any - self::deleteActivityTarget($activityId); + self::deleteActivityContact($activityId, 'Target'); } $values = array(); @@ -497,7 +455,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { } else { if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) { - self::deleteActivityTarget($activityId); + self::deleteActivityContact($activityId, 'Target'); } } @@ -1524,9 +1482,10 @@ INNER JOIN civicrm_contact contact ON activityContact.contact_id = contact.id // add activity target record for every sms that is send $activityTargetParams = array( 'activity_id' => $activityID, - 'target_contact_id' => $toID, + 'contact_id' => $toID, + 'record_type' => 'Target' ); - self::createActivityTarget($activityTargetParams); + CRM_Activity_BAO_ActivityContact::create($activityTargetParams); return TRUE; } @@ -1592,9 +1551,10 @@ INNER JOIN civicrm_contact contact ON activityContact.contact_id = contact.id // add activity target record for every mail that is send $activityTargetParams = array( 'activity_id' => $activityID, - 'target_contact_id' => $toID, + 'contact_id' => $toID, + 'record_type' => 'Target' ); - self::createActivityTarget($activityTargetParams); + CRM_Activity_BAO_ActivityContact::create($activityTargetParams); return TRUE; } @@ -2174,44 +2134,29 @@ AND cl.modified_id = c.id $transaction = new CRM_Core_Transaction(); - // delete activity if there are no record in - // civicrm_activity_assignment or civicrm_activity_target + // delete activity if there is no record in + // civicrm_activity_contact // pointing to any other contact record. - - $activity = new CRM_Activity_DAO_Activity(); $activity->source_contact_id = $contactId; $activity->find(); while ($activity->fetch()) { - $noTarget = $noAssignee = TRUE; - - // check for target activity record. - $target = new CRM_Activity_DAO_ActivityTarget(); - $target->activity_id = $activity->id; - $target->find(); - while ($target->fetch()) { - if ($target->target_contact_id != $contactId) { - $noTarget = FALSE; - break; - } - } - $target->free(); + $noOther = TRUE; - // check for assignee activity record. - $assignee = new CRM_Activity_DAO_ActivityAssignment(); - $assignee->activity_id = $activity->id; - $assignee->find(); - while ($assignee->fetch()) { - if ($assignee->assignee_contact_id != $contactId) { - $noAssignee = FALSE; - break; - } - } - $assignee->free(); + $sql = " +SELECT count(*) +FROM civicrm_activity_contact +WHERE activity_id = %1 +AND contact_id <> %2 +"; + $params = array( + 1 => array($activity->id, 'Integer'), + 2 => array($contactId, 'Integer') + ); // finally delete activity. - if ($noTarget && $noAssignee) { + if (CRM_Core_DAO::singleValueQuery($sql)) { $activityParams = array('id' => $activity->id); $result = self::deleteActivity($activityParams); } diff --git a/CRM/Activity/BAO/ActivityAssignment.php b/CRM/Activity/BAO/ActivityAssignment.php index 2669de8195..34e8bf5c48 100644 --- a/CRM/Activity/BAO/ActivityAssignment.php +++ b/CRM/Activity/BAO/ActivityAssignment.php @@ -84,8 +84,8 @@ class CRM_Activity_BAO_ActivityAssignment extends CRM_Activity_DAO_ActivityConta } $sql = " -SELECT assignee_contact_id -FROM civicrm_activity_assignment +SELECT contact_id +FROM civicrm_activity_contact INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id WHERE activity_id = %1 AND record_type = 'Assignee' @@ -125,7 +125,7 @@ AND civicrm_contact.is_deleted = 0 $query = " SELECT contact_a.id, contact_a.sort_name, contact_a.display_name, ce.email FROM civicrm_contact contact_a -INNER JOIN civicrm_activity_contact ON civicrm_activity_contact_contact_id = contact_a.id +INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id LEFT JOIN civicrm_email ce ON ce.contact_id = contact_a.id WHERE civicrm_activity_contact.activity_id = %1 AND contact_a.is_deleted = 0 diff --git a/CRM/Activity/BAO/Query.php b/CRM/Activity/BAO/Query.php index a1c6e1afb5..85ca36d96d 100644 --- a/CRM/Activity/BAO/Query.php +++ b/CRM/Activity/BAO/Query.php @@ -390,13 +390,13 @@ class CRM_Activity_BAO_Query { break; case 'activity_status': - $from = " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')"; + $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')"; $from .= " $side JOIN civicrm_option_value activity_status ON (civicrm_activity.status_id = activity_status.value AND option_group_activity_status.id = activity_status.option_group_id ) "; break; case 'activity_type': - $from = " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')"; + $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')"; $from .= " $side JOIN civicrm_option_value activity_type ON (civicrm_activity.activity_type_id = activity_type.value AND option_group_activity_type.id = activity_type.option_group_id ) "; break; diff --git a/CRM/Activity/Page/AJAX.php b/CRM/Activity/Page/AJAX.php index 454f531f8a..bcd6a06988 100644 --- a/CRM/Activity/Page/AJAX.php +++ b/CRM/Activity/Page/AJAX.php @@ -75,47 +75,47 @@ class CRM_Activity_Page_AJAX { $sortMapper = array( 0 => 'sort_name', 1 => 'phone', 2 => 'email', ); - + $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer'); $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0; $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25; $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL; $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc'; - + $params = $_POST; if ($sort && $sortOrder) { $sortSQL = $sort .' '.$sortOrder; } - + // get the activities related to given case $globalGroupInfo = array(); - + // get the total row count $relGlobalTotalCount = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL); // limit the rows - $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, $sortSQL, $showLinks = TRUE, FALSE, $offset, $rowCount); - + $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, $sortSQL, $showLinks = TRUE, FALSE, $offset, $rowCount); + $iFilteredTotal = $iTotal = $relGlobalTotalCount; $selectorElements = array('sort_name', 'phone', 'email'); - + echo CRM_Utils_JSON::encodeDataTableSelector($relGlobal, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); CRM_Utils_System::civiExit(); - } - + } + static function getCaseClientRelationships() { $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer'); $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer'); - + $sortMapper = array( 0 => 'relation', 1 => 'name', 2 => 'phone', 3 => 'email' ); - + $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer'); $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0; $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25; $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation'; $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc'; - + $params = $_POST; if ($sort && $sortOrder) { $sortSQL = $sort .' '.$sortOrder; @@ -126,9 +126,9 @@ class CRM_Activity_Page_AJAX { CRM_Contact_BAO_Relationship::CURRENT, 0, 0, 0, NULL, NULL, FALSE ); - + $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID); - + // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles // so they don't show up twice. $clientRelationships = array(); @@ -137,47 +137,47 @@ class CRM_Activity_Page_AJAX { $clientRelationships[] = $r; } } - + // sort clientRelationships array using jquery call params foreach ($clientRelationships as $key => $row) { - $sortArray[$key] = $row[$sort]; + $sortArray[$key] = $row[$sort]; } $sort_type = "SORT_".strtoupper($sortOrder); $sort_function = "array_multisort(\$sortArray, ".$sort_type.", \$clientRelationships);"; eval($sort_function); - + //limit the rows $allClientRelationships = $clientRelationships; $clientRelationships = array_slice($allClientRelationships, $offset, $rowCount, TRUE); - + // after sort we can update username fields to be a url foreach($clientRelationships as $key => $value) { $clientRelationships[$key]['name'] = ''.$clientRelationships[$key]['name'].''; } - + $iFilteredTotal = $iTotal = $params['total'] = count($allClientRelationships); $selectorElements = array('relation', 'name', 'phone', 'email'); - + echo CRM_Utils_JSON::encodeDataTableSelector($clientRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); CRM_Utils_System::civiExit(); - } - - + } + + static function getCaseRoles() { $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer'); $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer'); - + $sortMapper = array( 0 => 'relation', 1 => 'name', 2 => 'phone', 3 => 'email', 4 => 'actions' ); - + $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer'); $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0; $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25; $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation'; $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc'; - + $params = $_POST; if ($sort && $sortOrder) { $sortSQL = $sort .' '.$sortOrder; @@ -187,14 +187,14 @@ class CRM_Activity_Page_AJAX { $caseTypeName = CRM_Case_BAO_Case::getCaseType($caseID, 'name'); $xmlProcessor = new CRM_Case_XMLProcessor_Process(); $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles'); - + $hasAccessToAllCases = CRM_Core_Permission::check('access all cases and activities'); - + $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName); if (!empty($managerRoleId)) { $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '
' . '(' . ts('Case Manager') . ')'; } - + foreach ($caseRelationships as $key => $value) { //calculate roles that don't have relationships if (CRM_Utils_Array::value($value['relation_type'], $caseRoles)) { @@ -205,7 +205,7 @@ class CRM_Activity_Page_AJAX { // mark orginal case relationships record to use on setting edit links below $caseRelationships[$key]['source'] = 'caseRel'; } - + $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID); // move/transform caseRoles array data to caseRelationships @@ -266,12 +266,12 @@ class CRM_Activity_Page_AJAX { if ($hasAccessToAllCases) { switch($caseRelationships[$key]['source']){ case 'caseRel': - $caseRelationships[$key]['actions'] = + $caseRelationships[$key]['actions'] = '
  
'; break; - + case 'caseRoles': - $caseRelationships[$key]['actions'] = + $caseRelationships[$key]['actions'] = '
'; break; } @@ -282,11 +282,11 @@ class CRM_Activity_Page_AJAX { } $iFilteredTotal = $iTotal = $params['total'] = count($allCaseRelationships); $selectorElements = array('relation', 'name', 'phone', 'email', 'actions'); - + echo CRM_Utils_JSON::encodeDataTableSelector($caseRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); CRM_Utils_System::civiExit(); - } - + } + static function convertToCaseActivity() { $params = array('caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode'); foreach ($params as $param) { @@ -364,9 +364,10 @@ class CRM_Activity_Page_AJAX { foreach ($targetContacts as $key => $value) { $targ_params = array( 'activity_id' => $mainActivityId, - 'target_contact_id' => $value, + 'contact_id' => $value, + 'record_type' => 'Target' ); - CRM_Activity_BAO_Activity::createActivityTarget($targ_params); + CRM_Activity_BAO_ActivityContact::create($targ_params); } // typically this will be empty, since assignees on another case may be completely different @@ -377,9 +378,10 @@ class CRM_Activity_Page_AJAX { foreach ($assigneeContacts as $key => $value) { $assigneeParams = array( 'activity_id' => $mainActivityId, - 'assignee_contact_id' => $value, + 'contact_id' => $value, + 'record_type' => 'Assignee' ); - CRM_Activity_BAO_Activity::createActivityAssignment($assigneeParams); + CRM_Activity_BAO_ActivityContact::create($assigneeParams); } //attach newly created activity to case. @@ -429,7 +431,7 @@ class CRM_Activity_Page_AJAX { $session = CRM_Core_Session::singleton(); $userID = $session->get('userID'); if ($userID) { - $activityFilter = + $activityFilter = array('activity_type_filter_id' => CRM_Utils_Array::value('activity_type_id', $params), 'activity_type_exclude_filter_id' => CRM_Utils_Array::value('activity_type_exclude_id', $params)); CRM_Core_BAO_Setting::setItem($activityFilter, @@ -440,7 +442,7 @@ class CRM_Activity_Page_AJAX { $userID ); } - + $iFilteredTotal = $iTotal = $params['total']; $selectorElements = array( 'activity_type', 'subject', 'source_contact', diff --git a/CRM/Case/Form/Activity.php b/CRM/Case/Form/Activity.php index b8f2a57bcc..b9a856492c 100644 --- a/CRM/Case/Form/Activity.php +++ b/CRM/Case/Form/Activity.php @@ -594,7 +594,10 @@ class CRM_Case_Form_Activity extends CRM_Activity_Form_Activity { // create activity assignee records - $assigneeParams = array('activity_id' => $activity->id); + $assigneeParams = array( + 'activity_id' => $activity->id, + 'record_type' => 'Assignee' + ); if (!CRM_Utils_Array::crmIsEmptyArray($params['assignee_contact_id'])) { //skip those assignee contacts which are already assigned @@ -605,8 +608,8 @@ class CRM_Case_Form_Activity extends CRM_Activity_Form_Activity { $activityAssigned = array_diff_key($activityAssigned, $assigneeContacts); foreach ($params['assignee_contact_id'] as $key => $id) { - $assigneeParams['assignee_contact_id'] = $id; - CRM_Activity_BAO_Activity::createActivityAssignment($assigneeParams); + $assigneeParams['contact_id'] = $id; + CRM_Activity_BAO_ActivityContact::create($assigneeParams); } //modify assigne_contact as per newly assigned contact before sending copy. CRM-4509. $params['assignee_contact_id'] = $activityAssigned; diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index b4207a0d95..74269f0c7d 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -357,7 +357,8 @@ class CRM_Contact_Form_Task_PDFLetterCommon { $session = CRM_Core_Session::singleton(); $userID = $session->get('userID'); - $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', + $activityTypeID = CRM_Core_OptionGroup::getValue( + 'activity_type', 'Print PDF Letter', 'name' ); @@ -385,9 +386,10 @@ class CRM_Contact_Form_Task_PDFLetterCommon { foreach ($form->_contactIds as $contactId) { $activityTargetParams = array( 'activity_id' => empty($activity->id) ? $activityIds[$contactId] : $activity->id, - 'target_contact_id' => $contactId, + 'contact_id' => $contactId, + 'record_type' => 'Target' ); - CRM_Activity_BAO_Activity::createActivityTarget($activityTargetParams); + CRM_Activity_BAO_ActivityContact::create($activityTargetParams); } }