From 42d58face421793193f7bf8ac95517a94fdec135 Mon Sep 17 00:00:00 2001 From: Pratik Joshi Date: Fri, 8 Nov 2013 16:45:37 +0530 Subject: [PATCH] cRM-13718 --- CRM/Utils/Migrate/ExportJSON.php | 81 +++++++++----------------------- CRM/Utils/Migrate/ImportJSON.php | 37 +++++++-------- 2 files changed, 41 insertions(+), 77 deletions(-) diff --git a/CRM/Utils/Migrate/ExportJSON.php b/CRM/Utils/Migrate/ExportJSON.php index f9b9bddbef..b56d590c21 100644 --- a/CRM/Utils/Migrate/ExportJSON.php +++ b/CRM/Utils/Migrate/ExportJSON.php @@ -378,34 +378,39 @@ AND entity_table = 'civicrm_contact' $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); - $ids = implode(',', $contactIDs); - $sql = "( - SELECT a.* - FROM civicrm_activity a - INNER JOIN civicrm_activity_contact aa ON aa.activity_id = a.id AND aa.record_type_id = $assigneeID - WHERE aa.contact_id IN ( $ids ) - AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 ) -) UNION ( - SELECT a.* - FROM civicrm_activity a - INNER JOIN civicrm_activity_contact at ON at.activity_id = a.id AND at.record_type_id = $targetID - WHERE at.contact_id IN ( $ids ) - AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 ) -) + // query framing returning all contacts in valid activity + $sql = " +SELECT a.*, ac.id as acID, ac.activity_id, ac.contact_id, ac.record_type_id +FROM civicrm_activity a +INNER JOIN civicrm_activity_contact ac ON ac.activity_id = a.id +WHERE ac.contact_id IN ( $ids ) + AND (a.activity_type_id != 3 AND a.activity_type_id != 20) "; $fields = & $this->dbFields('CRM_Activity_DAO_Activity', TRUE); - $activityIDs = array(); $dao = & CRM_Core_DAO::executeQuery($sql); while ($dao->fetch()) { + // adding source, target and assignee contacts in additional contacts array + $this->addAdditionalContacts(array($dao->contact_id), + $additionalContacts + ); + + // append values of activity contacts + $activityContacts = array( + 'id' => $dao->acID, + 'contact_id' => $dao->contact_id, + 'activity_id' => $dao->activity_id, + 'record_type_id' => $dao->record_type_id + ); + $this->appendValue($dao->acID, 'civicrm_activity_contact', $activityContacts); + if (isset($_activitiesHandled[$dao->id])) { continue; } $_activitiesHandled[$dao->id] = $dao->id; - $activityIDs[] = $dao->id; $activity = array(); foreach ($fields as $fld) { @@ -417,50 +422,10 @@ AND entity_table = 'civicrm_contact' } } + // append activity value $this->appendValue($dao->id, 'civicrm_activity', $activity); - $this->addAdditionalContacts(array($dao->source_contact_id), - $additionalContacts - ); } $dao->free(); - - if (empty($activityIDs)) { - return; - } - - $activityIDString = implode(",", $activityIDs); - - // now get all assignee contact ids and target contact ids for this activity - $sql = "SELECT * FROM civicrm_activity_contact WHERE activity_id IN ($activityIDString) AND record_type = 'Assignee'"; - $aaDAO = &CRM_Core_DAO::executeQuery($sql); - - $activityContacts = array(); - while ($aaDAO->fetch()) { - $activityAssignee = array( - 'id' => $aaDAO->id, - 'assignee_contact_id' => $aaDAO->assignee_contact_id, - 'activity_id' => $aaDAO->activity_id, - ); - $this->appendValue($aaDAO->id, 'civicrm_activity_assignment', $activityAssignee); - $activityContacts[] = $aaDAO->assignee_contact_id; - } - $aaDAO->free(); - - $sql = "SELECT * FROM civicrm_activity_contact WHERE activity_id IN ($activityIDString) AND record_type = 'Target'"; - $atDAO = &CRM_Core_DAO::executeQuery($sql); - - while ($atDAO->fetch()) { - $activityTarget = array( - 'id' => $atDAO->id, - 'target_contact_id' => $atDAO->target_contact_id, - 'activity_id' => $atDAO->activity_id, - ); - $this->appendValue($atDAO->id, 'civicrm_activity_target', $activityTarget); - $activityContacts[] = $atDAO->target_contact_id; - } - $atDAO->free(); - - $this->addAdditionalContacts($activityContacts, $additionalContacts); } function appendValue($id, $name, $value) { @@ -581,7 +546,7 @@ WHERE date >= $lastExportTime $json ); - // print_r( json_decode( $json ) ); + print_r( json_decode( $json ) ); } } diff --git a/CRM/Utils/Migrate/ImportJSON.php b/CRM/Utils/Migrate/ImportJSON.php index 40cc321cd8..bc25abc161 100644 --- a/CRM/Utils/Migrate/ImportJSON.php +++ b/CRM/Utils/Migrate/ImportJSON.php @@ -56,8 +56,7 @@ class CRM_Utils_Migrate_ImportJSON { $this->note($decodedContacts->civicrm_note); $this->relationship($decodedContacts->civicrm_relationship); $this->activity($decodedContacts->civicrm_activity, - $decodedContacts->civicrm_activity_target, - $decodedContacts->civicrm_activity_assignment + $decodedContacts->civicrm_activity_contact ); $this->group($decodedContacts->civicrm_group, $decodedContacts->civicrm_group_contact @@ -73,7 +72,8 @@ class CRM_Utils_Migrate_ImportJSON { function contact(&$contact) { $this->restore($contact, 'CRM_Contact_DAO_Contact', - array('id' => 'civicrm_contact') + array('id' => 'civicrm_contact'), + array('birth_date', 'deceased_date', 'created_date', 'modified_date') ); } @@ -101,7 +101,8 @@ class CRM_Utils_Migrate_ImportJSON { function note(&$note) { $this->restore($note, 'CRM_Core_DAO_Note', - array('contact_id' => 'civicrm_contact') + array('contact_id' => 'civicrm_contact'), + array('modified_date') ); } @@ -115,24 +116,17 @@ class CRM_Utils_Migrate_ImportJSON { ); } - function activity($activity, $activityTarget, $activityAssignment) { + function activity($activity, $activityContacts) { $this->restore($activity, 'CRM_Activity_DAO_Activity', - array('source_contact_id' => 'civicrm_contact') - ); - - $this->restore($activityTarget, - 'CRM_Activity_DAO_ActivityTarget', - array( - 'target_contact_id' => 'civicrm_contact', - 'activity_id' => 'civicrm_activity', - ) + NULL, + array('activity_date_time') ); - $this->restore($activityAssignment, - 'CRM_Activity_DAO_ActivityAssignment', + $this->restore($activityContacts, + 'CRM_Activity_DAO_ActivityContact', array( - 'assignee_contact_id' => 'civicrm_contact', + 'contact_id' => 'civicrm_contact', 'activity_id' => 'civicrm_activity', ) ); @@ -140,7 +134,9 @@ class CRM_Utils_Migrate_ImportJSON { function group($group, $groupContact) { $this->restore($group, - 'CRM_Contact_DAO_Group' + 'CRM_Contact_DAO_Group', + NULL, + array('cache_date', 'refresh_date') ); $this->restore($groupContact, @@ -170,7 +166,7 @@ class CRM_Utils_Migrate_ImportJSON { ); } - function restore(&$chunk, $daoName, $lookUpMapping = NULL) { + function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) { $object = new $daoName(); $tableName = $object->__table; @@ -205,6 +201,9 @@ class CRM_Utils_Migrate_ImportJSON { if (array_key_exists($column, $lookUpMapping)) { $object->$column = $this->_lookupCache[$lookUpMapping[$column]][$value[$k]]; } + elseif (!empty($dateFields) && in_array($column, $dateFields)) { + $object->$column = CRM_Utils_Date::isoToMysql($value[$k]); + } else { $object->$column = $value[$k]; } -- 2.25.1