3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Utils_Migrate_ImportJSON
{
37 protected $_lookupCache;
39 protected $_saveMapping;
43 public function __construct() {
44 $this->_lookupCache
= array();
45 $this->_saveMapping
= array();
51 public function run($file) {
52 $json = file_get_contents($file);
54 $decodedContacts = json_decode($json);
56 //migrate contact data
57 $this->contact($decodedContacts->civicrm_contact
);
58 $this->email($decodedContacts->civicrm_email
);
59 $this->phone($decodedContacts->civicrm_phone
);
60 $this->address($decodedContacts->civicrm_address
);
61 $this->note($decodedContacts->civicrm_note
);
62 $this->relationship($decodedContacts->civicrm_relationship
);
63 $this->activity($decodedContacts->civicrm_activity
,
64 $decodedContacts->civicrm_activity_contact
66 $this->group($decodedContacts->civicrm_group
,
67 $decodedContacts->civicrm_group_contact
69 $this->tag($decodedContacts->civicrm_tag
,
70 $decodedContacts->civicrm_entity_tag
73 // clean up all caches etc
74 CRM_Core_Config
::clearDBCache();
80 public function contact(&$contact) {
81 $this->restore($contact,
82 'CRM_Contact_DAO_Contact',
83 array('id' => 'civicrm_contact'),
84 array('birth_date', 'deceased_date', 'created_date', 'modified_date')
91 public function email(&$email) {
92 $this->restore($email,
94 array('contact_id' => 'civicrm_contact')
101 public function phone(&$phone) {
102 $this->restore($phone,
103 'CRM_Core_DAO_Phone',
104 array('contact_id' => 'civicrm_contact')
111 public function address(&$address) {
112 $this->restore($address,
113 'CRM_Core_DAO_Address',
114 array('contact_id' => 'civicrm_contact')
121 public function note(&$note) {
122 $this->restore($note,
124 array('contact_id' => 'civicrm_contact'),
125 array('modified_date')
130 * @param $relationship
132 public function relationship(&$relationship) {
133 $this->restore($relationship,
134 'CRM_Contact_DAO_Relationship',
136 'contact_id_a' => 'civicrm_contact',
137 'contact_id_b' => 'civicrm_contact',
144 * @param $activityContacts
146 public function activity($activity, $activityContacts) {
147 $this->restore($activity,
148 'CRM_Activity_DAO_Activity',
150 array('activity_date_time')
153 $this->restore($activityContacts,
154 'CRM_Activity_DAO_ActivityContact',
156 'contact_id' => 'civicrm_contact',
157 'activity_id' => 'civicrm_activity',
164 * @param $groupContact
166 public function group($group, $groupContact) {
167 $this->restore($group,
168 'CRM_Contact_DAO_Group',
170 array('cache_date', 'refresh_date')
173 $this->restore($groupContact,
174 'CRM_Contact_DAO_GroupContact',
176 'group_id' => 'civicrm_group',
177 'contact_id' => 'civicrm_contact',
186 public function tag($tag, $entityTag) {
190 'created_id' => 'civicrm_contact',
191 'parent_id' => 'civicrm_tag',
195 $this->restore($entityTag,
196 'CRM_Core_DAO_EntityTag',
198 'entity_id' => 'civicrm_contact',
199 'tag_id' => 'civicrm_tag',
206 * @param string $daoName
207 * @param null $lookUpMapping
208 * @param null $dateFields
210 public function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) {
211 $object = new $daoName();
212 $tableName = $object->__table
;
214 if (is_array($lookUpMapping)) {
215 $lookUpMapping['id'] = $tableName;
218 $lookUpMapping = array('id' => $tableName);
221 foreach ($lookUpMapping as $columnName => $tableName) {
222 $this->populateCache($tableName);
225 $saveMapping = FALSE;
226 $columns = $chunk[0];
227 foreach ($chunk as $key => $value) {
229 $object = new $daoName();
230 foreach ($columns as $k => $column) {
231 if ($column == 'id') {
232 $childID = $value[$k];
233 $masterID = CRM_Utils_Array
::value($value[$k],
234 $this->_lookupCache
[$tableName],
238 $object->id
= $masterID;
242 if (array_key_exists($column, $lookUpMapping)) {
243 $object->$column = $this->_lookupCache
[$lookUpMapping[$column]][$value[$k]];
245 elseif (!empty($dateFields) && in_array($column, $dateFields)) {
246 $object->$column = CRM_Utils_Date
::isoToMysql($value[$k]);
249 $object->$column = $value[$k];
256 $this->_lookupCache
[$tableName][$childID] = $object->id
;
257 $this->_saveMapping
[$tableName] = TRUE;
263 public function saveCache() {
264 $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES ";
266 foreach ($this->_lookupCache
as $tableName => & $values) {
267 if (!$this->_saveMapping
[$tableName]) {
271 $mapValues = array();
272 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_migration_mapping where entity_table = '$tableName'");
273 foreach ($values as $childID => $masterID) {
274 $mapValues[] = "($masterID,$childID,'$tableName' )";
276 $insertSQL = $sql . implode(",\n", $mapValues);
277 CRM_Core_DAO
::executeQuery($insertSQL);
282 * @param string $tableName
284 public function populateCache($tableName) {
285 if (isset($this->_lookupCache
[$tableName])) {
289 $this->_lookupCache
[$tableName] = array();
290 $this->_saveMapping
[$tableName] = FALSE;
292 $query = "SELECT master_id, slave_id
293 FROM civicrm_migration_mapping
294 WHERE entity_table = '{$tableName}'
297 $dao = CRM_Core_DAO
::executeQuery($query);
298 while ($dao->fetch()) {
299 $this->_lookupCache
[$dao->slave_id
] = $dao->master_id
;