Various phpdoc fixes
[civicrm-core.git] / CRM / Utils / Migrate / ImportJSON.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Utils_Migrate_ImportJSON {
18
19 protected $_lookupCache;
20
21 protected $_saveMapping;
22
23 /**
24 * Class constructor.
25 */
26 public function __construct() {
27 $this->_lookupCache = [];
28 $this->_saveMapping = [];
29 }
30
31 /**
32 * Run import.
33 *
34 * @param string $file
35 */
36 public function run($file) {
37 $json = file_get_contents($file);
38
39 $decodedContacts = json_decode($json);
40
41 // migrate contact data
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,
49 $decodedContacts->civicrm_activity_contact
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
62 /**
63 * @param $contact
64 */
65 public function contact(&$contact) {
66 $this->restore($contact,
67 'CRM_Contact_DAO_Contact',
68 ['id' => 'civicrm_contact'],
69 ['birth_date', 'deceased_date', 'created_date', 'modified_date']
70 );
71 }
72
73 /**
74 * @param $email
75 */
76 public function email(&$email) {
77 $this->restore($email,
78 'CRM_Core_DAO_Email',
79 ['contact_id' => 'civicrm_contact']
80 );
81 }
82
83 /**
84 * @param $phone
85 */
86 public function phone(&$phone) {
87 $this->restore($phone,
88 'CRM_Core_DAO_Phone',
89 ['contact_id' => 'civicrm_contact']
90 );
91 }
92
93 /**
94 * @param $address
95 */
96 public function address(&$address) {
97 $this->restore($address,
98 'CRM_Core_DAO_Address',
99 ['contact_id' => 'civicrm_contact']
100 );
101 }
102
103 /**
104 * @param $note
105 */
106 public function note(&$note) {
107 $this->restore($note,
108 'CRM_Core_DAO_Note',
109 ['contact_id' => 'civicrm_contact'],
110 ['modified_date']
111 );
112 }
113
114 /**
115 * @param $relationship
116 */
117 public function relationship(&$relationship) {
118 $this->restore($relationship,
119 'CRM_Contact_DAO_Relationship',
120 [
121 'contact_id_a' => 'civicrm_contact',
122 'contact_id_b' => 'civicrm_contact',
123 ]
124 );
125 }
126
127 /**
128 * @param $activity
129 * @param $activityContacts
130 */
131 public function activity($activity, $activityContacts) {
132 $this->restore($activity,
133 'CRM_Activity_DAO_Activity',
134 NULL,
135 ['activity_date_time']
136 );
137
138 $this->restore($activityContacts,
139 'CRM_Activity_DAO_ActivityContact',
140 [
141 'contact_id' => 'civicrm_contact',
142 'activity_id' => 'civicrm_activity',
143 ]
144 );
145 }
146
147 /**
148 * @param $group
149 * @param $groupContact
150 */
151 public function group($group, $groupContact) {
152 $this->restore($group,
153 'CRM_Contact_DAO_Group',
154 NULL,
155 ['cache_date', 'refresh_date']
156 );
157
158 $this->restore($groupContact,
159 'CRM_Contact_DAO_GroupContact',
160 [
161 'group_id' => 'civicrm_group',
162 'contact_id' => 'civicrm_contact',
163 ]
164 );
165 }
166
167 /**
168 * @param $tag
169 * @param $entityTag
170 */
171 public function tag($tag, $entityTag) {
172 $this->restore($tag,
173 'CRM_Core_DAO_Tag',
174 [
175 'created_id' => 'civicrm_contact',
176 'parent_id' => 'civicrm_tag',
177 ]
178 );
179
180 $this->restore($entityTag,
181 'CRM_Core_DAO_EntityTag',
182 [
183 'entity_id' => 'civicrm_contact',
184 'tag_id' => 'civicrm_tag',
185 ]
186 );
187 }
188
189 /**
190 * @param $chunk
191 * @param string $daoName
192 * @param array|null $lookUpMapping
193 * @param array|null $dateFields
194 */
195 public function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) {
196 $object = new $daoName();
197 $tableName = $object->__table;
198
199 if (is_array($lookUpMapping)) {
200 $lookUpMapping['id'] = $tableName;
201 }
202 else {
203 $lookUpMapping = ['id' => $tableName];
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) {
214 $object = new $daoName();
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 }
230 elseif (!empty($dateFields) && in_array($column, $dateFields)) {
231 $object->$column = CRM_Utils_Date::isoToMysql($value[$k]);
232 }
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
248 public function saveCache() {
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
256 $mapValues = [];
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
266 /**
267 * @param string $tableName
268 */
269 public function populateCache($tableName) {
270 if (isset($this->_lookupCache[$tableName])) {
271 return;
272 }
273
274 $this->_lookupCache[$tableName] = [];
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 }
287
288 }