3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $daoName) . ".php");
175 eval('$object = new ' . $daoName . '( );');
176 $tableName = $object->__table
;
178 if (is_array($lookUpMapping)) {
179 $lookUpMapping['id'] = $tableName;
182 $lookUpMapping = array('id' => $tableName);
185 foreach ($lookUpMapping as $columnName => $tableName) {
186 $this->populateCache($tableName);
189 $saveMapping = FALSE;
190 $columns = $chunk[0];
191 foreach ($chunk as $key => $value) {
193 eval('$object = new ' . $daoName . '( );');
194 foreach ($columns as $k => $column) {
195 if ($column == 'id') {
196 $childID = $value[$k];
197 $masterID = CRM_Utils_Array
::value($value[$k],
198 $this->_lookupCache
[$tableName],
202 $object->id
= $masterID;
206 if (array_key_exists($column, $lookUpMapping)) {
207 $object->$column = $this->_lookupCache
[$lookUpMapping[$column]][$value[$k]];
210 $object->$column = $value[$k];
217 $this->_lookupCache
[$tableName][$childID] = $object->id
;
218 $this->_saveMapping
[$tableName] = TRUE;
224 function saveCache() {
225 $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES ";
227 foreach ($this->_lookupCache
as $tableName => & $values) {
228 if (!$this->_saveMapping
[$tableName]) {
232 $mapValues = array();
233 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_migration_mapping where entity_table = '$tableName'");
234 foreach ($values as $childID => $masterID) {
235 $mapValues[] = "($masterID,$childID,'$tableName' )";
237 $insertSQL = $sql . implode(",\n", $mapValues);
238 CRM_Core_DAO
::executeQuery($insertSQL);
242 function populateCache($tableName) {
243 if (isset($this->_lookupCache
[$tableName])) {
247 $this->_lookupCache
[$tableName] = array();
248 $this->_saveMapping
[$tableName] = FALSE;
250 $query = "SELECT master_id, slave_id
251 FROM civicrm_migration_mapping
252 WHERE entity_table = '{$tableName}'
255 $dao = CRM_Core_DAO
::executeQuery($query);
256 while ($dao->fetch()) {
257 $this->_lookupCache
[$dao->slave_id
] = $dao->master_id
;