Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
6a488035 TO |
32 | */ |
33 | class CRM_Utils_Migrate_ImportJSON { | |
34 | ||
35 | protected $_lookupCache; | |
36 | ||
37 | protected $_saveMapping; | |
3302658e | 38 | |
70599df6 | 39 | /** |
40 | * Class constructor. | |
41 | */ | |
00be9182 | 42 | public function __construct() { |
be2fb01f CW |
43 | $this->_lookupCache = []; |
44 | $this->_saveMapping = []; | |
6a488035 TO |
45 | } |
46 | ||
5bc392e6 | 47 | /** |
70599df6 | 48 | * Run import. |
49 | * | |
50 | * @param string $file | |
5bc392e6 | 51 | */ |
00be9182 | 52 | public function run($file) { |
6a488035 TO |
53 | $json = file_get_contents($file); |
54 | ||
55 | $decodedContacts = json_decode($json); | |
56 | ||
50bfb460 | 57 | // migrate contact data |
6a488035 TO |
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, | |
42d58fac | 65 | $decodedContacts->civicrm_activity_contact |
6a488035 TO |
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 | ||
5bc392e6 EM |
78 | /** |
79 | * @param $contact | |
80 | */ | |
00be9182 | 81 | public function contact(&$contact) { |
6a488035 TO |
82 | $this->restore($contact, |
83 | 'CRM_Contact_DAO_Contact', | |
be2fb01f CW |
84 | ['id' => 'civicrm_contact'], |
85 | ['birth_date', 'deceased_date', 'created_date', 'modified_date'] | |
6a488035 TO |
86 | ); |
87 | } | |
88 | ||
5bc392e6 EM |
89 | /** |
90 | * @param $email | |
91 | */ | |
00be9182 | 92 | public function email(&$email) { |
6a488035 TO |
93 | $this->restore($email, |
94 | 'CRM_Core_DAO_Email', | |
be2fb01f | 95 | ['contact_id' => 'civicrm_contact'] |
6a488035 TO |
96 | ); |
97 | } | |
98 | ||
5bc392e6 EM |
99 | /** |
100 | * @param $phone | |
101 | */ | |
00be9182 | 102 | public function phone(&$phone) { |
6a488035 TO |
103 | $this->restore($phone, |
104 | 'CRM_Core_DAO_Phone', | |
be2fb01f | 105 | ['contact_id' => 'civicrm_contact'] |
6a488035 TO |
106 | ); |
107 | } | |
108 | ||
5bc392e6 EM |
109 | /** |
110 | * @param $address | |
111 | */ | |
00be9182 | 112 | public function address(&$address) { |
6a488035 TO |
113 | $this->restore($address, |
114 | 'CRM_Core_DAO_Address', | |
be2fb01f | 115 | ['contact_id' => 'civicrm_contact'] |
6a488035 TO |
116 | ); |
117 | } | |
118 | ||
5bc392e6 EM |
119 | /** |
120 | * @param $note | |
121 | */ | |
00be9182 | 122 | public function note(&$note) { |
6a488035 TO |
123 | $this->restore($note, |
124 | 'CRM_Core_DAO_Note', | |
be2fb01f CW |
125 | ['contact_id' => 'civicrm_contact'], |
126 | ['modified_date'] | |
6a488035 TO |
127 | ); |
128 | } | |
129 | ||
5bc392e6 EM |
130 | /** |
131 | * @param $relationship | |
132 | */ | |
00be9182 | 133 | public function relationship(&$relationship) { |
6a488035 TO |
134 | $this->restore($relationship, |
135 | 'CRM_Contact_DAO_Relationship', | |
be2fb01f | 136 | [ |
6a488035 TO |
137 | 'contact_id_a' => 'civicrm_contact', |
138 | 'contact_id_b' => 'civicrm_contact', | |
be2fb01f | 139 | ] |
6a488035 TO |
140 | ); |
141 | } | |
142 | ||
5bc392e6 EM |
143 | /** |
144 | * @param $activity | |
145 | * @param $activityContacts | |
146 | */ | |
00be9182 | 147 | public function activity($activity, $activityContacts) { |
6a488035 TO |
148 | $this->restore($activity, |
149 | 'CRM_Activity_DAO_Activity', | |
42d58fac | 150 | NULL, |
be2fb01f | 151 | ['activity_date_time'] |
6a488035 TO |
152 | ); |
153 | ||
42d58fac PJ |
154 | $this->restore($activityContacts, |
155 | 'CRM_Activity_DAO_ActivityContact', | |
be2fb01f | 156 | [ |
42d58fac | 157 | 'contact_id' => 'civicrm_contact', |
6a488035 | 158 | 'activity_id' => 'civicrm_activity', |
be2fb01f | 159 | ] |
6a488035 TO |
160 | ); |
161 | } | |
162 | ||
5bc392e6 EM |
163 | /** |
164 | * @param $group | |
165 | * @param $groupContact | |
166 | */ | |
00be9182 | 167 | public function group($group, $groupContact) { |
6a488035 | 168 | $this->restore($group, |
42d58fac PJ |
169 | 'CRM_Contact_DAO_Group', |
170 | NULL, | |
be2fb01f | 171 | ['cache_date', 'refresh_date'] |
6a488035 TO |
172 | ); |
173 | ||
174 | $this->restore($groupContact, | |
175 | 'CRM_Contact_DAO_GroupContact', | |
be2fb01f | 176 | [ |
6a488035 TO |
177 | 'group_id' => 'civicrm_group', |
178 | 'contact_id' => 'civicrm_contact', | |
be2fb01f | 179 | ] |
6a488035 TO |
180 | ); |
181 | } | |
182 | ||
5bc392e6 EM |
183 | /** |
184 | * @param $tag | |
185 | * @param $entityTag | |
186 | */ | |
00be9182 | 187 | public function tag($tag, $entityTag) { |
6a488035 TO |
188 | $this->restore($tag, |
189 | 'CRM_Core_DAO_Tag', | |
be2fb01f | 190 | [ |
6a488035 TO |
191 | 'created_id' => 'civicrm_contact', |
192 | 'parent_id' => 'civicrm_tag', | |
be2fb01f | 193 | ] |
6a488035 TO |
194 | ); |
195 | ||
196 | $this->restore($entityTag, | |
197 | 'CRM_Core_DAO_EntityTag', | |
be2fb01f | 198 | [ |
6a488035 TO |
199 | 'entity_id' => 'civicrm_contact', |
200 | 'tag_id' => 'civicrm_tag', | |
be2fb01f | 201 | ] |
6a488035 TO |
202 | ); |
203 | } | |
204 | ||
5bc392e6 EM |
205 | /** |
206 | * @param $chunk | |
c490a46a | 207 | * @param string $daoName |
5bc392e6 EM |
208 | * @param null $lookUpMapping |
209 | * @param null $dateFields | |
210 | */ | |
00be9182 | 211 | public function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) { |
353ffa53 | 212 | $object = new $daoName(); |
6a488035 TO |
213 | $tableName = $object->__table; |
214 | ||
215 | if (is_array($lookUpMapping)) { | |
216 | $lookUpMapping['id'] = $tableName; | |
217 | } | |
218 | else { | |
be2fb01f | 219 | $lookUpMapping = ['id' => $tableName]; |
6a488035 TO |
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) { | |
353ffa53 | 230 | $object = new $daoName(); |
6a488035 TO |
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 | } | |
42d58fac PJ |
246 | elseif (!empty($dateFields) && in_array($column, $dateFields)) { |
247 | $object->$column = CRM_Utils_Date::isoToMysql($value[$k]); | |
248 | } | |
6a488035 TO |
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 | ||
00be9182 | 264 | public function saveCache() { |
6a488035 TO |
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 | ||
be2fb01f | 272 | $mapValues = []; |
6a488035 TO |
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 | ||
5bc392e6 | 282 | /** |
100fef9d | 283 | * @param string $tableName |
5bc392e6 | 284 | */ |
00be9182 | 285 | public function populateCache($tableName) { |
6a488035 TO |
286 | if (isset($this->_lookupCache[$tableName])) { |
287 | return; | |
288 | } | |
289 | ||
be2fb01f | 290 | $this->_lookupCache[$tableName] = []; |
6a488035 TO |
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 | } | |
96025800 | 303 | |
6a488035 | 304 | } |