3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Utils_Migrate_ImportJSON
{
35 protected $_lookupCache;
37 protected $_saveMapping;
42 public function __construct() {
43 $this->_lookupCache
= array();
44 $this->_saveMapping
= array();
52 public function run($file) {
53 $json = file_get_contents($file);
55 $decodedContacts = json_decode($json);
57 // migrate contact data
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
,
65 $decodedContacts->civicrm_activity_contact
67 $this->group($decodedContacts->civicrm_group
,
68 $decodedContacts->civicrm_group_contact
70 $this->tag($decodedContacts->civicrm_tag
,
71 $decodedContacts->civicrm_entity_tag
74 // clean up all caches etc
75 CRM_Core_Config
::clearDBCache();
81 public function contact(&$contact) {
82 $this->restore($contact,
83 'CRM_Contact_DAO_Contact',
84 array('id' => 'civicrm_contact'),
85 array('birth_date', 'deceased_date', 'created_date', 'modified_date')
92 public function email(&$email) {
93 $this->restore($email,
95 array('contact_id' => 'civicrm_contact')
102 public function phone(&$phone) {
103 $this->restore($phone,
104 'CRM_Core_DAO_Phone',
105 array('contact_id' => 'civicrm_contact')
112 public function address(&$address) {
113 $this->restore($address,
114 'CRM_Core_DAO_Address',
115 array('contact_id' => 'civicrm_contact')
122 public function note(&$note) {
123 $this->restore($note,
125 array('contact_id' => 'civicrm_contact'),
126 array('modified_date')
131 * @param $relationship
133 public function relationship(&$relationship) {
134 $this->restore($relationship,
135 'CRM_Contact_DAO_Relationship',
137 'contact_id_a' => 'civicrm_contact',
138 'contact_id_b' => 'civicrm_contact',
145 * @param $activityContacts
147 public function activity($activity, $activityContacts) {
148 $this->restore($activity,
149 'CRM_Activity_DAO_Activity',
151 array('activity_date_time')
154 $this->restore($activityContacts,
155 'CRM_Activity_DAO_ActivityContact',
157 'contact_id' => 'civicrm_contact',
158 'activity_id' => 'civicrm_activity',
165 * @param $groupContact
167 public function group($group, $groupContact) {
168 $this->restore($group,
169 'CRM_Contact_DAO_Group',
171 array('cache_date', 'refresh_date')
174 $this->restore($groupContact,
175 'CRM_Contact_DAO_GroupContact',
177 'group_id' => 'civicrm_group',
178 'contact_id' => 'civicrm_contact',
187 public function tag($tag, $entityTag) {
191 'created_id' => 'civicrm_contact',
192 'parent_id' => 'civicrm_tag',
196 $this->restore($entityTag,
197 'CRM_Core_DAO_EntityTag',
199 'entity_id' => 'civicrm_contact',
200 'tag_id' => 'civicrm_tag',
207 * @param string $daoName
208 * @param null $lookUpMapping
209 * @param null $dateFields
211 public function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) {
212 $object = new $daoName();
213 $tableName = $object->__table
;
215 if (is_array($lookUpMapping)) {
216 $lookUpMapping['id'] = $tableName;
219 $lookUpMapping = array('id' => $tableName);
222 foreach ($lookUpMapping as $columnName => $tableName) {
223 $this->populateCache($tableName);
226 $saveMapping = FALSE;
227 $columns = $chunk[0];
228 foreach ($chunk as $key => $value) {
230 $object = new $daoName();
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],
239 $object->id
= $masterID;
243 if (array_key_exists($column, $lookUpMapping)) {
244 $object->$column = $this->_lookupCache
[$lookUpMapping[$column]][$value[$k]];
246 elseif (!empty($dateFields) && in_array($column, $dateFields)) {
247 $object->$column = CRM_Utils_Date
::isoToMysql($value[$k]);
250 $object->$column = $value[$k];
257 $this->_lookupCache
[$tableName][$childID] = $object->id
;
258 $this->_saveMapping
[$tableName] = TRUE;
264 public function saveCache() {
265 $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES ";
267 foreach ($this->_lookupCache
as $tableName => & $values) {
268 if (!$this->_saveMapping
[$tableName]) {
272 $mapValues = array();
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' )";
277 $insertSQL = $sql . implode(",\n", $mapValues);
278 CRM_Core_DAO
::executeQuery($insertSQL);
283 * @param string $tableName
285 public function populateCache($tableName) {
286 if (isset($this->_lookupCache
[$tableName])) {
290 $this->_lookupCache
[$tableName] = array();
291 $this->_saveMapping
[$tableName] = FALSE;
293 $query = "SELECT master_id, slave_id
294 FROM civicrm_migration_mapping
295 WHERE entity_table = '{$tableName}'
298 $dao = CRM_Core_DAO
::executeQuery($query);
299 while ($dao->fetch()) {
300 $this->_lookupCache
[$dao->slave_id
] = $dao->master_id
;