3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
35 class CRM_Utils_Migrate_ImportJSON
{
37 protected $_lookupCache;
39 protected $_saveMapping;
41 function __construct() {
42 $this->_lookupCache
= array();
43 $this->_saveMapping
= array();
47 $json = file_get_contents($file);
49 $decodedContacts = json_decode($json);
51 //migrate contact data
52 $this->contact($decodedContacts->civicrm_contact
);
53 $this->email($decodedContacts->civicrm_email
);
54 $this->phone($decodedContacts->civicrm_phone
);
55 $this->address($decodedContacts->civicrm_address
);
56 $this->note($decodedContacts->civicrm_note
);
57 $this->relationship($decodedContacts->civicrm_relationship
);
58 $this->activity($decodedContacts->civicrm_activity
,
59 $decodedContacts->civicrm_activity_target
,
60 $decodedContacts->civicrm_activity_assignment
62 $this->group($decodedContacts->civicrm_group
,
63 $decodedContacts->civicrm_group_contact
65 $this->tag($decodedContacts->civicrm_tag
,
66 $decodedContacts->civicrm_entity_tag
69 // clean up all caches etc
70 CRM_Core_Config
::clearDBCache();
73 function contact(&$contact) {
74 $this->restore($contact,
75 'CRM_Contact_DAO_Contact',
76 array('id' => 'civicrm_contact')
80 function email(&$email) {
81 $this->restore($email,
83 array('contact_id' => 'civicrm_contact')
87 function phone(&$phone) {
88 $this->restore($phone,
90 array('contact_id' => 'civicrm_contact')
94 function address(&$address) {
95 $this->restore($address,
96 'CRM_Core_DAO_Address',
97 array('contact_id' => 'civicrm_contact')
101 function note(&$note) {
102 $this->restore($note,
104 array('contact_id' => 'civicrm_contact')
108 function relationship(&$relationship) {
109 $this->restore($relationship,
110 'CRM_Contact_DAO_Relationship',
112 'contact_id_a' => 'civicrm_contact',
113 'contact_id_b' => 'civicrm_contact',
118 function activity($activity, $activityTarget, $activityAssignment) {
119 $this->restore($activity,
120 'CRM_Activity_DAO_Activity',
121 array('source_contact_id' => 'civicrm_contact')
124 $this->restore($activityTarget,
125 'CRM_Activity_DAO_ActivityTarget',
127 'target_contact_id' => 'civicrm_contact',
128 'activity_id' => 'civicrm_activity',
132 $this->restore($activityAssignment,
133 'CRM_Activity_DAO_ActivityAssignment',
135 'assignee_contact_id' => 'civicrm_contact',
136 'activity_id' => 'civicrm_activity',
141 function group($group, $groupContact) {
142 $this->restore($group,
143 'CRM_Contact_DAO_Group'
146 $this->restore($groupContact,
147 'CRM_Contact_DAO_GroupContact',
149 'group_id' => 'civicrm_group',
150 'contact_id' => 'civicrm_contact',
155 function tag($tag, $entityTag) {
159 'created_id' => 'civicrm_contact',
160 'parent_id' => 'civicrm_tag',
164 $this->restore($entityTag,
165 'CRM_Core_DAO_EntityTag',
167 'entity_id' => 'civicrm_contact',
168 'tag_id' => 'civicrm_tag',
173 function restore(&$chunk, $daoName, $lookUpMapping = NULL) {
174 $object = new $daoName();
175 $tableName = $object->__table
;
177 if (is_array($lookUpMapping)) {
178 $lookUpMapping['id'] = $tableName;
181 $lookUpMapping = array('id' => $tableName);
184 foreach ($lookUpMapping as $columnName => $tableName) {
185 $this->populateCache($tableName);
188 $saveMapping = FALSE;
189 $columns = $chunk[0];
190 foreach ($chunk as $key => $value) {
192 $object = new $daoName();
193 foreach ($columns as $k => $column) {
194 if ($column == 'id') {
195 $childID = $value[$k];
196 $masterID = CRM_Utils_Array
::value($value[$k],
197 $this->_lookupCache
[$tableName],
201 $object->id
= $masterID;
205 if (array_key_exists($column, $lookUpMapping)) {
206 $object->$column = $this->_lookupCache
[$lookUpMapping[$column]][$value[$k]];
209 $object->$column = $value[$k];
216 $this->_lookupCache
[$tableName][$childID] = $object->id
;
217 $this->_saveMapping
[$tableName] = TRUE;
223 function saveCache() {
224 $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES ";
226 foreach ($this->_lookupCache
as $tableName => & $values) {
227 if (!$this->_saveMapping
[$tableName]) {
231 $mapValues = array();
232 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_migration_mapping where entity_table = '$tableName'");
233 foreach ($values as $childID => $masterID) {
234 $mapValues[] = "($masterID,$childID,'$tableName' )";
236 $insertSQL = $sql . implode(",\n", $mapValues);
237 CRM_Core_DAO
::executeQuery($insertSQL);
241 function populateCache($tableName) {
242 if (isset($this->_lookupCache
[$tableName])) {
246 $this->_lookupCache
[$tableName] = array();
247 $this->_saveMapping
[$tableName] = FALSE;
249 $query = "SELECT master_id, slave_id
250 FROM civicrm_migration_mapping
251 WHERE entity_table = '{$tableName}'
254 $dao = CRM_Core_DAO
::executeQuery($query);
255 while ($dao->fetch()) {
256 $this->_lookupCache
[$dao->slave_id
] = $dao->master_id
;