3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Utils_Migrate_ImportJSON
{
19 protected $_lookupCache;
21 protected $_saveMapping;
26 public function __construct() {
27 $this->_lookupCache
= [];
28 $this->_saveMapping
= [];
36 public function run($file) {
37 $json = file_get_contents($file);
39 $decodedContacts = json_decode($json);
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
51 $this->group($decodedContacts->civicrm_group
,
52 $decodedContacts->civicrm_group_contact
54 $this->tag($decodedContacts->civicrm_tag
,
55 $decodedContacts->civicrm_entity_tag
58 // clean up all caches etc
59 CRM_Core_Config
::clearDBCache();
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']
76 public function email(&$email) {
77 $this->restore($email,
79 ['contact_id' => 'civicrm_contact']
86 public function phone(&$phone) {
87 $this->restore($phone,
89 ['contact_id' => 'civicrm_contact']
96 public function address(&$address) {
97 $this->restore($address,
98 'CRM_Core_DAO_Address',
99 ['contact_id' => 'civicrm_contact']
106 public function note(&$note) {
107 $this->restore($note,
109 ['contact_id' => 'civicrm_contact'],
115 * @param $relationship
117 public function relationship(&$relationship) {
118 $this->restore($relationship,
119 'CRM_Contact_DAO_Relationship',
121 'contact_id_a' => 'civicrm_contact',
122 'contact_id_b' => 'civicrm_contact',
129 * @param $activityContacts
131 public function activity($activity, $activityContacts) {
132 $this->restore($activity,
133 'CRM_Activity_DAO_Activity',
135 ['activity_date_time']
138 $this->restore($activityContacts,
139 'CRM_Activity_DAO_ActivityContact',
141 'contact_id' => 'civicrm_contact',
142 'activity_id' => 'civicrm_activity',
149 * @param $groupContact
151 public function group($group, $groupContact) {
152 $this->restore($group,
153 'CRM_Contact_DAO_Group',
155 ['cache_date', 'refresh_date']
158 $this->restore($groupContact,
159 'CRM_Contact_DAO_GroupContact',
161 'group_id' => 'civicrm_group',
162 'contact_id' => 'civicrm_contact',
171 public function tag($tag, $entityTag) {
175 'created_id' => 'civicrm_contact',
176 'parent_id' => 'civicrm_tag',
180 $this->restore($entityTag,
181 'CRM_Core_DAO_EntityTag',
183 'entity_id' => 'civicrm_contact',
184 'tag_id' => 'civicrm_tag',
191 * @param string $daoName
192 * @param null $lookUpMapping
193 * @param null $dateFields
195 public function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) {
196 $object = new $daoName();
197 $tableName = $object->__table
;
199 if (is_array($lookUpMapping)) {
200 $lookUpMapping['id'] = $tableName;
203 $lookUpMapping = ['id' => $tableName];
206 foreach ($lookUpMapping as $columnName => $tableName) {
207 $this->populateCache($tableName);
210 $saveMapping = FALSE;
211 $columns = $chunk[0];
212 foreach ($chunk as $key => $value) {
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],
223 $object->id
= $masterID;
227 if (array_key_exists($column, $lookUpMapping)) {
228 $object->$column = $this->_lookupCache
[$lookUpMapping[$column]][$value[$k]];
230 elseif (!empty($dateFields) && in_array($column, $dateFields)) {
231 $object->$column = CRM_Utils_Date
::isoToMysql($value[$k]);
234 $object->$column = $value[$k];
241 $this->_lookupCache
[$tableName][$childID] = $object->id
;
242 $this->_saveMapping
[$tableName] = TRUE;
248 public function saveCache() {
249 $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES ";
251 foreach ($this->_lookupCache
as $tableName => & $values) {
252 if (!$this->_saveMapping
[$tableName]) {
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' )";
261 $insertSQL = $sql . implode(",\n", $mapValues);
262 CRM_Core_DAO
::executeQuery($insertSQL);
267 * @param string $tableName
269 public function populateCache($tableName) {
270 if (isset($this->_lookupCache
[$tableName])) {
274 $this->_lookupCache
[$tableName] = [];
275 $this->_saveMapping
[$tableName] = FALSE;
277 $query = "SELECT master_id, slave_id
278 FROM civicrm_migration_mapping
279 WHERE entity_table = '{$tableName}'
282 $dao = CRM_Core_DAO
::executeQuery($query);
283 while ($dao->fetch()) {
284 $this->_lookupCache
[$dao->slave_id
] = $dao->master_id
;