Merge pull request #790 from JoeMurray/CRM-12657
[civicrm-core.git] / CRM / Utils / Migrate / ImportJSON.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35 class CRM_Utils_Migrate_ImportJSON {
36
37 protected $_lookupCache;
38
39 protected $_saveMapping;
40
41 function __construct() {
42 $this->_lookupCache = array();
43 $this->_saveMapping = array();
44 }
45
46 function run($file) {
47 $json = file_get_contents($file);
48
49 $decodedContacts = json_decode($json);
50
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
61 );
62 $this->group($decodedContacts->civicrm_group,
63 $decodedContacts->civicrm_group_contact
64 );
65 $this->tag($decodedContacts->civicrm_tag,
66 $decodedContacts->civicrm_entity_tag
67 );
68
69 // clean up all caches etc
70 CRM_Core_Config::clearDBCache();
71 }
72
73 function contact(&$contact) {
74 $this->restore($contact,
75 'CRM_Contact_DAO_Contact',
76 array('id' => 'civicrm_contact')
77 );
78 }
79
80 function email(&$email) {
81 $this->restore($email,
82 'CRM_Core_DAO_Email',
83 array('contact_id' => 'civicrm_contact')
84 );
85 }
86
87 function phone(&$phone) {
88 $this->restore($phone,
89 'CRM_Core_DAO_Phone',
90 array('contact_id' => 'civicrm_contact')
91 );
92 }
93
94 function address(&$address) {
95 $this->restore($address,
96 'CRM_Core_DAO_Address',
97 array('contact_id' => 'civicrm_contact')
98 );
99 }
100
101 function note(&$note) {
102 $this->restore($note,
103 'CRM_Core_DAO_Note',
104 array('contact_id' => 'civicrm_contact')
105 );
106 }
107
108 function relationship(&$relationship) {
109 $this->restore($relationship,
110 'CRM_Contact_DAO_Relationship',
111 array(
112 'contact_id_a' => 'civicrm_contact',
113 'contact_id_b' => 'civicrm_contact',
114 )
115 );
116 }
117
118 function activity($activity, $activityTarget, $activityAssignment) {
119 $this->restore($activity,
120 'CRM_Activity_DAO_Activity',
121 array('source_contact_id' => 'civicrm_contact')
122 );
123
124 $this->restore($activityTarget,
125 'CRM_Activity_DAO_ActivityTarget',
126 array(
127 'target_contact_id' => 'civicrm_contact',
128 'activity_id' => 'civicrm_activity',
129 )
130 );
131
132 $this->restore($activityAssignment,
133 'CRM_Activity_DAO_ActivityAssignment',
134 array(
135 'assignee_contact_id' => 'civicrm_contact',
136 'activity_id' => 'civicrm_activity',
137 )
138 );
139 }
140
141 function group($group, $groupContact) {
142 $this->restore($group,
143 'CRM_Contact_DAO_Group'
144 );
145
146 $this->restore($groupContact,
147 'CRM_Contact_DAO_GroupContact',
148 array(
149 'group_id' => 'civicrm_group',
150 'contact_id' => 'civicrm_contact',
151 )
152 );
153 }
154
155 function tag($tag, $entityTag) {
156 $this->restore($tag,
157 'CRM_Core_DAO_Tag',
158 array(
159 'created_id' => 'civicrm_contact',
160 'parent_id' => 'civicrm_tag',
161 )
162 );
163
164 $this->restore($entityTag,
165 'CRM_Core_DAO_EntityTag',
166 array(
167 'entity_id' => 'civicrm_contact',
168 'tag_id' => 'civicrm_tag',
169 )
170 );
171 }
172
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;
177
178 if (is_array($lookUpMapping)) {
179 $lookUpMapping['id'] = $tableName;
180 }
181 else {
182 $lookUpMapping = array('id' => $tableName);
183 }
184
185 foreach ($lookUpMapping as $columnName => $tableName) {
186 $this->populateCache($tableName);
187 }
188
189 $saveMapping = FALSE;
190 $columns = $chunk[0];
191 foreach ($chunk as $key => $value) {
192 if ($key) {
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],
199 NULL
200 );
201 if ($masterID) {
202 $object->id = $masterID;
203 }
204 }
205 else {
206 if (array_key_exists($column, $lookUpMapping)) {
207 $object->$column = $this->_lookupCache[$lookUpMapping[$column]][$value[$k]];
208 }
209 else {
210 $object->$column = $value[$k];
211 }
212 }
213 }
214
215 $object->save();
216 if (!$masterID) {
217 $this->_lookupCache[$tableName][$childID] = $object->id;
218 $this->_saveMapping[$tableName] = TRUE;
219 }
220 }
221 }
222 }
223
224 function saveCache() {
225 $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES ";
226
227 foreach ($this->_lookupCache as $tableName => & $values) {
228 if (!$this->_saveMapping[$tableName]) {
229 continue;
230 }
231
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' )";
236 }
237 $insertSQL = $sql . implode(",\n", $mapValues);
238 CRM_Core_DAO::executeQuery($insertSQL);
239 }
240 }
241
242 function populateCache($tableName) {
243 if (isset($this->_lookupCache[$tableName])) {
244 return;
245 }
246
247 $this->_lookupCache[$tableName] = array();
248 $this->_saveMapping[$tableName] = FALSE;
249
250 $query = "SELECT master_id, slave_id
251 FROM civicrm_migration_mapping
252 WHERE entity_table = '{$tableName}'
253 ";
254
255 $dao = CRM_Core_DAO::executeQuery($query);
256 while ($dao->fetch()) {
257 $this->_lookupCache[$dao->slave_id] = $dao->master_id;
258 }
259 }
260 }
261