CRM/Utils add comments
[civicrm-core.git] / CRM / Utils / Migrate / ImportJSON.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Utils_Migrate_ImportJSON {
36
37 protected $_lookupCache;
38
39 protected $_saveMapping;
40
41 /**
42 *
43 */
44 function __construct() {
45 $this->_lookupCache = array();
46 $this->_saveMapping = array();
47 }
48
49 /**
50 * @param $file
51 */
52 function run($file) {
53 $json = file_get_contents($file);
54
55 $decodedContacts = json_decode($json);
56
57 //migrate contact data
58 $this->contact($decodedContacts->civicrm_contact);
59 $this->email($decodedContacts->civicrm_email);
60 $this->phone($decodedContacts->civicrm_phone);
61 $this->address($decodedContacts->civicrm_address);
62 $this->note($decodedContacts->civicrm_note);
63 $this->relationship($decodedContacts->civicrm_relationship);
64 $this->activity($decodedContacts->civicrm_activity,
65 $decodedContacts->civicrm_activity_contact
66 );
67 $this->group($decodedContacts->civicrm_group,
68 $decodedContacts->civicrm_group_contact
69 );
70 $this->tag($decodedContacts->civicrm_tag,
71 $decodedContacts->civicrm_entity_tag
72 );
73
74 // clean up all caches etc
75 CRM_Core_Config::clearDBCache();
76 }
77
78 /**
79 * @param $contact
80 */
81 function contact(&$contact) {
82 $this->restore($contact,
83 'CRM_Contact_DAO_Contact',
84 array('id' => 'civicrm_contact'),
85 array('birth_date', 'deceased_date', 'created_date', 'modified_date')
86 );
87 }
88
89 /**
90 * @param $email
91 */
92 function email(&$email) {
93 $this->restore($email,
94 'CRM_Core_DAO_Email',
95 array('contact_id' => 'civicrm_contact')
96 );
97 }
98
99 /**
100 * @param $phone
101 */
102 function phone(&$phone) {
103 $this->restore($phone,
104 'CRM_Core_DAO_Phone',
105 array('contact_id' => 'civicrm_contact')
106 );
107 }
108
109 /**
110 * @param $address
111 */
112 function address(&$address) {
113 $this->restore($address,
114 'CRM_Core_DAO_Address',
115 array('contact_id' => 'civicrm_contact')
116 );
117 }
118
119 /**
120 * @param $note
121 */
122 function note(&$note) {
123 $this->restore($note,
124 'CRM_Core_DAO_Note',
125 array('contact_id' => 'civicrm_contact'),
126 array('modified_date')
127 );
128 }
129
130 /**
131 * @param $relationship
132 */
133 function relationship(&$relationship) {
134 $this->restore($relationship,
135 'CRM_Contact_DAO_Relationship',
136 array(
137 'contact_id_a' => 'civicrm_contact',
138 'contact_id_b' => 'civicrm_contact',
139 )
140 );
141 }
142
143 /**
144 * @param $activity
145 * @param $activityContacts
146 */
147 function activity($activity, $activityContacts) {
148 $this->restore($activity,
149 'CRM_Activity_DAO_Activity',
150 NULL,
151 array('activity_date_time')
152 );
153
154 $this->restore($activityContacts,
155 'CRM_Activity_DAO_ActivityContact',
156 array(
157 'contact_id' => 'civicrm_contact',
158 'activity_id' => 'civicrm_activity',
159 )
160 );
161 }
162
163 /**
164 * @param $group
165 * @param $groupContact
166 */
167 function group($group, $groupContact) {
168 $this->restore($group,
169 'CRM_Contact_DAO_Group',
170 NULL,
171 array('cache_date', 'refresh_date')
172 );
173
174 $this->restore($groupContact,
175 'CRM_Contact_DAO_GroupContact',
176 array(
177 'group_id' => 'civicrm_group',
178 'contact_id' => 'civicrm_contact',
179 )
180 );
181 }
182
183 /**
184 * @param $tag
185 * @param $entityTag
186 */
187 function tag($tag, $entityTag) {
188 $this->restore($tag,
189 'CRM_Core_DAO_Tag',
190 array(
191 'created_id' => 'civicrm_contact',
192 'parent_id' => 'civicrm_tag',
193 )
194 );
195
196 $this->restore($entityTag,
197 'CRM_Core_DAO_EntityTag',
198 array(
199 'entity_id' => 'civicrm_contact',
200 'tag_id' => 'civicrm_tag',
201 )
202 );
203 }
204
205 /**
206 * @param $chunk
207 * @param $daoName
208 * @param null $lookUpMapping
209 * @param null $dateFields
210 */
211 function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) {
212 $object = new $daoName();
213 $tableName = $object->__table;
214
215 if (is_array($lookUpMapping)) {
216 $lookUpMapping['id'] = $tableName;
217 }
218 else {
219 $lookUpMapping = array('id' => $tableName);
220 }
221
222 foreach ($lookUpMapping as $columnName => $tableName) {
223 $this->populateCache($tableName);
224 }
225
226 $saveMapping = FALSE;
227 $columns = $chunk[0];
228 foreach ($chunk as $key => $value) {
229 if ($key) {
230 $object = new $daoName();
231 foreach ($columns as $k => $column) {
232 if ($column == 'id') {
233 $childID = $value[$k];
234 $masterID = CRM_Utils_Array::value($value[$k],
235 $this->_lookupCache[$tableName],
236 NULL
237 );
238 if ($masterID) {
239 $object->id = $masterID;
240 }
241 }
242 else {
243 if (array_key_exists($column, $lookUpMapping)) {
244 $object->$column = $this->_lookupCache[$lookUpMapping[$column]][$value[$k]];
245 }
246 elseif (!empty($dateFields) && in_array($column, $dateFields)) {
247 $object->$column = CRM_Utils_Date::isoToMysql($value[$k]);
248 }
249 else {
250 $object->$column = $value[$k];
251 }
252 }
253 }
254
255 $object->save();
256 if (!$masterID) {
257 $this->_lookupCache[$tableName][$childID] = $object->id;
258 $this->_saveMapping[$tableName] = TRUE;
259 }
260 }
261 }
262 }
263
264 function saveCache() {
265 $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES ";
266
267 foreach ($this->_lookupCache as $tableName => & $values) {
268 if (!$this->_saveMapping[$tableName]) {
269 continue;
270 }
271
272 $mapValues = array();
273 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_migration_mapping where entity_table = '$tableName'");
274 foreach ($values as $childID => $masterID) {
275 $mapValues[] = "($masterID,$childID,'$tableName' )";
276 }
277 $insertSQL = $sql . implode(",\n", $mapValues);
278 CRM_Core_DAO::executeQuery($insertSQL);
279 }
280 }
281
282 /**
283 * @param $tableName
284 */
285 function populateCache($tableName) {
286 if (isset($this->_lookupCache[$tableName])) {
287 return;
288 }
289
290 $this->_lookupCache[$tableName] = array();
291 $this->_saveMapping[$tableName] = FALSE;
292
293 $query = "SELECT master_id, slave_id
294 FROM civicrm_migration_mapping
295 WHERE entity_table = '{$tableName}'
296 ";
297
298 $dao = CRM_Core_DAO::executeQuery($query);
299 while ($dao->fetch()) {
300 $this->_lookupCache[$dao->slave_id] = $dao->master_id;
301 }
302 }
303 }
304