Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-10-02-11-18-44
[civicrm-core.git] / CRM / Utils / Migrate / ImportJSON.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 $object = new $daoName();
175 $tableName = $object->__table;
176
177 if (is_array($lookUpMapping)) {
178 $lookUpMapping['id'] = $tableName;
179 }
180 else {
181 $lookUpMapping = array('id' => $tableName);
182 }
183
184 foreach ($lookUpMapping as $columnName => $tableName) {
185 $this->populateCache($tableName);
186 }
187
188 $saveMapping = FALSE;
189 $columns = $chunk[0];
190 foreach ($chunk as $key => $value) {
191 if ($key) {
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],
198 NULL
199 );
200 if ($masterID) {
201 $object->id = $masterID;
202 }
203 }
204 else {
205 if (array_key_exists($column, $lookUpMapping)) {
206 $object->$column = $this->_lookupCache[$lookUpMapping[$column]][$value[$k]];
207 }
208 else {
209 $object->$column = $value[$k];
210 }
211 }
212 }
213
214 $object->save();
215 if (!$masterID) {
216 $this->_lookupCache[$tableName][$childID] = $object->id;
217 $this->_saveMapping[$tableName] = TRUE;
218 }
219 }
220 }
221 }
222
223 function saveCache() {
224 $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES ";
225
226 foreach ($this->_lookupCache as $tableName => & $values) {
227 if (!$this->_saveMapping[$tableName]) {
228 continue;
229 }
230
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' )";
235 }
236 $insertSQL = $sql . implode(",\n", $mapValues);
237 CRM_Core_DAO::executeQuery($insertSQL);
238 }
239 }
240
241 function populateCache($tableName) {
242 if (isset($this->_lookupCache[$tableName])) {
243 return;
244 }
245
246 $this->_lookupCache[$tableName] = array();
247 $this->_saveMapping[$tableName] = FALSE;
248
249 $query = "SELECT master_id, slave_id
250 FROM civicrm_migration_mapping
251 WHERE entity_table = '{$tableName}'
252 ";
253
254 $dao = CRM_Core_DAO::executeQuery($query);
255 while ($dao->fetch()) {
256 $this->_lookupCache[$dao->slave_id] = $dao->master_id;
257 }
258 }
259 }
260