3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
33 class CRM_Utils_Migrate_ImportJSON
{
35 protected $_lookupCache;
37 protected $_saveMapping;
39 public function __construct() {
40 $this->_lookupCache
= array();
41 $this->_saveMapping
= array();
47 public function run($file) {
48 $json = file_get_contents($file);
50 $decodedContacts = json_decode($json);
52 // migrate contact data
53 $this->contact($decodedContacts->civicrm_contact
);
54 $this->email($decodedContacts->civicrm_email
);
55 $this->phone($decodedContacts->civicrm_phone
);
56 $this->address($decodedContacts->civicrm_address
);
57 $this->note($decodedContacts->civicrm_note
);
58 $this->relationship($decodedContacts->civicrm_relationship
);
59 $this->activity($decodedContacts->civicrm_activity
,
60 $decodedContacts->civicrm_activity_contact
62 $this->group($decodedContacts->civicrm_group
,
63 $decodedContacts->civicrm_group_contact
65 $this->tag($decodedContacts->civicrm_tag
,
66 $decodedContacts->civicrm_entity_tag
69 // clean up all caches etc
70 CRM_Core_Config
::clearDBCache();
76 public function contact(&$contact) {
77 $this->restore($contact,
78 'CRM_Contact_DAO_Contact',
79 array('id' => 'civicrm_contact'),
80 array('birth_date', 'deceased_date', 'created_date', 'modified_date')
87 public function email(&$email) {
88 $this->restore($email,
90 array('contact_id' => 'civicrm_contact')
97 public function phone(&$phone) {
98 $this->restore($phone,
100 array('contact_id' => 'civicrm_contact')
107 public function address(&$address) {
108 $this->restore($address,
109 'CRM_Core_DAO_Address',
110 array('contact_id' => 'civicrm_contact')
117 public function note(&$note) {
118 $this->restore($note,
120 array('contact_id' => 'civicrm_contact'),
121 array('modified_date')
126 * @param $relationship
128 public function relationship(&$relationship) {
129 $this->restore($relationship,
130 'CRM_Contact_DAO_Relationship',
132 'contact_id_a' => 'civicrm_contact',
133 'contact_id_b' => 'civicrm_contact',
140 * @param $activityContacts
142 public function activity($activity, $activityContacts) {
143 $this->restore($activity,
144 'CRM_Activity_DAO_Activity',
146 array('activity_date_time')
149 $this->restore($activityContacts,
150 'CRM_Activity_DAO_ActivityContact',
152 'contact_id' => 'civicrm_contact',
153 'activity_id' => 'civicrm_activity',
160 * @param $groupContact
162 public function group($group, $groupContact) {
163 $this->restore($group,
164 'CRM_Contact_DAO_Group',
166 array('cache_date', 'refresh_date')
169 $this->restore($groupContact,
170 'CRM_Contact_DAO_GroupContact',
172 'group_id' => 'civicrm_group',
173 'contact_id' => 'civicrm_contact',
182 public function tag($tag, $entityTag) {
186 'created_id' => 'civicrm_contact',
187 'parent_id' => 'civicrm_tag',
191 $this->restore($entityTag,
192 'CRM_Core_DAO_EntityTag',
194 'entity_id' => 'civicrm_contact',
195 'tag_id' => 'civicrm_tag',
202 * @param string $daoName
203 * @param null $lookUpMapping
204 * @param null $dateFields
206 public function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) {
207 $object = new $daoName();
208 $tableName = $object->__table
;
210 if (is_array($lookUpMapping)) {
211 $lookUpMapping['id'] = $tableName;
214 $lookUpMapping = array('id' => $tableName);
217 foreach ($lookUpMapping as $columnName => $tableName) {
218 $this->populateCache($tableName);
221 $saveMapping = FALSE;
222 $columns = $chunk[0];
223 foreach ($chunk as $key => $value) {
225 $object = new $daoName();
226 foreach ($columns as $k => $column) {
227 if ($column == 'id') {
228 $childID = $value[$k];
229 $masterID = CRM_Utils_Array
::value($value[$k],
230 $this->_lookupCache
[$tableName],
234 $object->id
= $masterID;
238 if (array_key_exists($column, $lookUpMapping)) {
239 $object->$column = $this->_lookupCache
[$lookUpMapping[$column]][$value[$k]];
241 elseif (!empty($dateFields) && in_array($column, $dateFields)) {
242 $object->$column = CRM_Utils_Date
::isoToMysql($value[$k]);
245 $object->$column = $value[$k];
252 $this->_lookupCache
[$tableName][$childID] = $object->id
;
253 $this->_saveMapping
[$tableName] = TRUE;
259 public function saveCache() {
260 $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES ";
262 foreach ($this->_lookupCache
as $tableName => & $values) {
263 if (!$this->_saveMapping
[$tableName]) {
267 $mapValues = array();
268 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_migration_mapping where entity_table = '$tableName'");
269 foreach ($values as $childID => $masterID) {
270 $mapValues[] = "($masterID,$childID,'$tableName' )";
272 $insertSQL = $sql . implode(",\n", $mapValues);
273 CRM_Core_DAO
::executeQuery($insertSQL);
278 * @param string $tableName
280 public function populateCache($tableName) {
281 if (isset($this->_lookupCache
[$tableName])) {
285 $this->_lookupCache
[$tableName] = array();
286 $this->_saveMapping
[$tableName] = FALSE;
288 $query = "SELECT master_id, slave_id
289 FROM civicrm_migration_mapping
290 WHERE entity_table = '{$tableName}'
293 $dao = CRM_Core_DAO
::executeQuery($query);
294 while ($dao->fetch()) {
295 $this->_lookupCache
[$dao->slave_id
] = $dao->master_id
;