Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Utils_Migrate_ImportJSON { | |
18 | ||
19 | protected $_lookupCache; | |
20 | ||
21 | protected $_saveMapping; | |
3302658e | 22 | |
70599df6 | 23 | /** |
24 | * Class constructor. | |
25 | */ | |
00be9182 | 26 | public function __construct() { |
be2fb01f CW |
27 | $this->_lookupCache = []; |
28 | $this->_saveMapping = []; | |
6a488035 TO |
29 | } |
30 | ||
5bc392e6 | 31 | /** |
70599df6 | 32 | * Run import. |
33 | * | |
34 | * @param string $file | |
5bc392e6 | 35 | */ |
00be9182 | 36 | public function run($file) { |
6a488035 TO |
37 | $json = file_get_contents($file); |
38 | ||
39 | $decodedContacts = json_decode($json); | |
40 | ||
50bfb460 | 41 | // migrate contact data |
6a488035 TO |
42 | $this->contact($decodedContacts->civicrm_contact); |
43 | $this->email($decodedContacts->civicrm_email); | |
44 | $this->phone($decodedContacts->civicrm_phone); | |
45 | $this->address($decodedContacts->civicrm_address); | |
46 | $this->note($decodedContacts->civicrm_note); | |
47 | $this->relationship($decodedContacts->civicrm_relationship); | |
48 | $this->activity($decodedContacts->civicrm_activity, | |
42d58fac | 49 | $decodedContacts->civicrm_activity_contact |
6a488035 TO |
50 | ); |
51 | $this->group($decodedContacts->civicrm_group, | |
52 | $decodedContacts->civicrm_group_contact | |
53 | ); | |
54 | $this->tag($decodedContacts->civicrm_tag, | |
55 | $decodedContacts->civicrm_entity_tag | |
56 | ); | |
57 | ||
58 | // clean up all caches etc | |
59 | CRM_Core_Config::clearDBCache(); | |
60 | } | |
61 | ||
5bc392e6 EM |
62 | /** |
63 | * @param $contact | |
64 | */ | |
00be9182 | 65 | public function contact(&$contact) { |
6a488035 TO |
66 | $this->restore($contact, |
67 | 'CRM_Contact_DAO_Contact', | |
be2fb01f CW |
68 | ['id' => 'civicrm_contact'], |
69 | ['birth_date', 'deceased_date', 'created_date', 'modified_date'] | |
6a488035 TO |
70 | ); |
71 | } | |
72 | ||
5bc392e6 EM |
73 | /** |
74 | * @param $email | |
75 | */ | |
00be9182 | 76 | public function email(&$email) { |
6a488035 TO |
77 | $this->restore($email, |
78 | 'CRM_Core_DAO_Email', | |
be2fb01f | 79 | ['contact_id' => 'civicrm_contact'] |
6a488035 TO |
80 | ); |
81 | } | |
82 | ||
5bc392e6 EM |
83 | /** |
84 | * @param $phone | |
85 | */ | |
00be9182 | 86 | public function phone(&$phone) { |
6a488035 TO |
87 | $this->restore($phone, |
88 | 'CRM_Core_DAO_Phone', | |
be2fb01f | 89 | ['contact_id' => 'civicrm_contact'] |
6a488035 TO |
90 | ); |
91 | } | |
92 | ||
5bc392e6 EM |
93 | /** |
94 | * @param $address | |
95 | */ | |
00be9182 | 96 | public function address(&$address) { |
6a488035 TO |
97 | $this->restore($address, |
98 | 'CRM_Core_DAO_Address', | |
be2fb01f | 99 | ['contact_id' => 'civicrm_contact'] |
6a488035 TO |
100 | ); |
101 | } | |
102 | ||
5bc392e6 EM |
103 | /** |
104 | * @param $note | |
105 | */ | |
00be9182 | 106 | public function note(&$note) { |
6a488035 TO |
107 | $this->restore($note, |
108 | 'CRM_Core_DAO_Note', | |
be2fb01f CW |
109 | ['contact_id' => 'civicrm_contact'], |
110 | ['modified_date'] | |
6a488035 TO |
111 | ); |
112 | } | |
113 | ||
5bc392e6 EM |
114 | /** |
115 | * @param $relationship | |
116 | */ | |
00be9182 | 117 | public function relationship(&$relationship) { |
6a488035 TO |
118 | $this->restore($relationship, |
119 | 'CRM_Contact_DAO_Relationship', | |
be2fb01f | 120 | [ |
6a488035 TO |
121 | 'contact_id_a' => 'civicrm_contact', |
122 | 'contact_id_b' => 'civicrm_contact', | |
be2fb01f | 123 | ] |
6a488035 TO |
124 | ); |
125 | } | |
126 | ||
5bc392e6 EM |
127 | /** |
128 | * @param $activity | |
129 | * @param $activityContacts | |
130 | */ | |
00be9182 | 131 | public function activity($activity, $activityContacts) { |
6a488035 TO |
132 | $this->restore($activity, |
133 | 'CRM_Activity_DAO_Activity', | |
42d58fac | 134 | NULL, |
be2fb01f | 135 | ['activity_date_time'] |
6a488035 TO |
136 | ); |
137 | ||
42d58fac PJ |
138 | $this->restore($activityContacts, |
139 | 'CRM_Activity_DAO_ActivityContact', | |
be2fb01f | 140 | [ |
42d58fac | 141 | 'contact_id' => 'civicrm_contact', |
6a488035 | 142 | 'activity_id' => 'civicrm_activity', |
be2fb01f | 143 | ] |
6a488035 TO |
144 | ); |
145 | } | |
146 | ||
5bc392e6 EM |
147 | /** |
148 | * @param $group | |
149 | * @param $groupContact | |
150 | */ | |
00be9182 | 151 | public function group($group, $groupContact) { |
6a488035 | 152 | $this->restore($group, |
42d58fac PJ |
153 | 'CRM_Contact_DAO_Group', |
154 | NULL, | |
be2fb01f | 155 | ['cache_date', 'refresh_date'] |
6a488035 TO |
156 | ); |
157 | ||
158 | $this->restore($groupContact, | |
159 | 'CRM_Contact_DAO_GroupContact', | |
be2fb01f | 160 | [ |
6a488035 TO |
161 | 'group_id' => 'civicrm_group', |
162 | 'contact_id' => 'civicrm_contact', | |
be2fb01f | 163 | ] |
6a488035 TO |
164 | ); |
165 | } | |
166 | ||
5bc392e6 EM |
167 | /** |
168 | * @param $tag | |
169 | * @param $entityTag | |
170 | */ | |
00be9182 | 171 | public function tag($tag, $entityTag) { |
6a488035 TO |
172 | $this->restore($tag, |
173 | 'CRM_Core_DAO_Tag', | |
be2fb01f | 174 | [ |
6a488035 TO |
175 | 'created_id' => 'civicrm_contact', |
176 | 'parent_id' => 'civicrm_tag', | |
be2fb01f | 177 | ] |
6a488035 TO |
178 | ); |
179 | ||
180 | $this->restore($entityTag, | |
181 | 'CRM_Core_DAO_EntityTag', | |
be2fb01f | 182 | [ |
6a488035 TO |
183 | 'entity_id' => 'civicrm_contact', |
184 | 'tag_id' => 'civicrm_tag', | |
be2fb01f | 185 | ] |
6a488035 TO |
186 | ); |
187 | } | |
188 | ||
5bc392e6 EM |
189 | /** |
190 | * @param $chunk | |
c490a46a | 191 | * @param string $daoName |
a2f24340 BT |
192 | * @param array|null $lookUpMapping |
193 | * @param array|null $dateFields | |
5bc392e6 | 194 | */ |
00be9182 | 195 | public function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) { |
353ffa53 | 196 | $object = new $daoName(); |
6a488035 TO |
197 | $tableName = $object->__table; |
198 | ||
199 | if (is_array($lookUpMapping)) { | |
200 | $lookUpMapping['id'] = $tableName; | |
201 | } | |
202 | else { | |
be2fb01f | 203 | $lookUpMapping = ['id' => $tableName]; |
6a488035 TO |
204 | } |
205 | ||
206 | foreach ($lookUpMapping as $columnName => $tableName) { | |
207 | $this->populateCache($tableName); | |
208 | } | |
209 | ||
210 | $saveMapping = FALSE; | |
211 | $columns = $chunk[0]; | |
212 | foreach ($chunk as $key => $value) { | |
213 | if ($key) { | |
353ffa53 | 214 | $object = new $daoName(); |
6a488035 TO |
215 | foreach ($columns as $k => $column) { |
216 | if ($column == 'id') { | |
217 | $childID = $value[$k]; | |
218 | $masterID = CRM_Utils_Array::value($value[$k], | |
219 | $this->_lookupCache[$tableName], | |
220 | NULL | |
221 | ); | |
222 | if ($masterID) { | |
223 | $object->id = $masterID; | |
224 | } | |
225 | } | |
226 | else { | |
227 | if (array_key_exists($column, $lookUpMapping)) { | |
228 | $object->$column = $this->_lookupCache[$lookUpMapping[$column]][$value[$k]]; | |
229 | } | |
42d58fac PJ |
230 | elseif (!empty($dateFields) && in_array($column, $dateFields)) { |
231 | $object->$column = CRM_Utils_Date::isoToMysql($value[$k]); | |
232 | } | |
6a488035 TO |
233 | else { |
234 | $object->$column = $value[$k]; | |
235 | } | |
236 | } | |
237 | } | |
238 | ||
239 | $object->save(); | |
240 | if (!$masterID) { | |
241 | $this->_lookupCache[$tableName][$childID] = $object->id; | |
242 | $this->_saveMapping[$tableName] = TRUE; | |
243 | } | |
244 | } | |
245 | } | |
246 | } | |
247 | ||
00be9182 | 248 | public function saveCache() { |
6a488035 TO |
249 | $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES "; |
250 | ||
251 | foreach ($this->_lookupCache as $tableName => & $values) { | |
252 | if (!$this->_saveMapping[$tableName]) { | |
253 | continue; | |
254 | } | |
255 | ||
be2fb01f | 256 | $mapValues = []; |
6a488035 TO |
257 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_migration_mapping where entity_table = '$tableName'"); |
258 | foreach ($values as $childID => $masterID) { | |
259 | $mapValues[] = "($masterID,$childID,'$tableName' )"; | |
260 | } | |
261 | $insertSQL = $sql . implode(",\n", $mapValues); | |
262 | CRM_Core_DAO::executeQuery($insertSQL); | |
263 | } | |
264 | } | |
265 | ||
5bc392e6 | 266 | /** |
100fef9d | 267 | * @param string $tableName |
5bc392e6 | 268 | */ |
00be9182 | 269 | public function populateCache($tableName) { |
6a488035 TO |
270 | if (isset($this->_lookupCache[$tableName])) { |
271 | return; | |
272 | } | |
273 | ||
be2fb01f | 274 | $this->_lookupCache[$tableName] = []; |
6a488035 TO |
275 | $this->_saveMapping[$tableName] = FALSE; |
276 | ||
277 | $query = "SELECT master_id, slave_id | |
278 | FROM civicrm_migration_mapping | |
279 | WHERE entity_table = '{$tableName}' | |
280 | "; | |
281 | ||
282 | $dao = CRM_Core_DAO::executeQuery($query); | |
283 | while ($dao->fetch()) { | |
284 | $this->_lookupCache[$dao->slave_id] = $dao->master_id; | |
285 | } | |
286 | } | |
96025800 | 287 | |
6a488035 | 288 | } |