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