Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Migrate_ImportJSON { | |
36 | ||
37 | protected $_lookupCache; | |
38 | ||
39 | protected $_saveMapping; | |
3302658e | 40 | |
6a488035 TO |
41 | function __construct() { |
42 | $this->_lookupCache = array(); | |
43 | $this->_saveMapping = array(); | |
44 | } | |
45 | ||
46 | function run($file) { | |
47 | $json = file_get_contents($file); | |
48 | ||
49 | $decodedContacts = json_decode($json); | |
50 | ||
51 | //migrate contact data | |
52 | $this->contact($decodedContacts->civicrm_contact); | |
53 | $this->email($decodedContacts->civicrm_email); | |
54 | $this->phone($decodedContacts->civicrm_phone); | |
55 | $this->address($decodedContacts->civicrm_address); | |
56 | $this->note($decodedContacts->civicrm_note); | |
57 | $this->relationship($decodedContacts->civicrm_relationship); | |
58 | $this->activity($decodedContacts->civicrm_activity, | |
42d58fac | 59 | $decodedContacts->civicrm_activity_contact |
6a488035 TO |
60 | ); |
61 | $this->group($decodedContacts->civicrm_group, | |
62 | $decodedContacts->civicrm_group_contact | |
63 | ); | |
64 | $this->tag($decodedContacts->civicrm_tag, | |
65 | $decodedContacts->civicrm_entity_tag | |
66 | ); | |
67 | ||
68 | // clean up all caches etc | |
69 | CRM_Core_Config::clearDBCache(); | |
70 | } | |
71 | ||
72 | function contact(&$contact) { | |
73 | $this->restore($contact, | |
74 | 'CRM_Contact_DAO_Contact', | |
42d58fac PJ |
75 | array('id' => 'civicrm_contact'), |
76 | array('birth_date', 'deceased_date', 'created_date', 'modified_date') | |
6a488035 TO |
77 | ); |
78 | } | |
79 | ||
80 | function email(&$email) { | |
81 | $this->restore($email, | |
82 | 'CRM_Core_DAO_Email', | |
83 | array('contact_id' => 'civicrm_contact') | |
84 | ); | |
85 | } | |
86 | ||
87 | function phone(&$phone) { | |
88 | $this->restore($phone, | |
89 | 'CRM_Core_DAO_Phone', | |
90 | array('contact_id' => 'civicrm_contact') | |
91 | ); | |
92 | } | |
93 | ||
94 | function address(&$address) { | |
95 | $this->restore($address, | |
96 | 'CRM_Core_DAO_Address', | |
97 | array('contact_id' => 'civicrm_contact') | |
98 | ); | |
99 | } | |
100 | ||
101 | function note(&$note) { | |
102 | $this->restore($note, | |
103 | 'CRM_Core_DAO_Note', | |
42d58fac PJ |
104 | array('contact_id' => 'civicrm_contact'), |
105 | array('modified_date') | |
6a488035 TO |
106 | ); |
107 | } | |
108 | ||
109 | function relationship(&$relationship) { | |
110 | $this->restore($relationship, | |
111 | 'CRM_Contact_DAO_Relationship', | |
112 | array( | |
113 | 'contact_id_a' => 'civicrm_contact', | |
114 | 'contact_id_b' => 'civicrm_contact', | |
115 | ) | |
116 | ); | |
117 | } | |
118 | ||
42d58fac | 119 | function activity($activity, $activityContacts) { |
6a488035 TO |
120 | $this->restore($activity, |
121 | 'CRM_Activity_DAO_Activity', | |
42d58fac PJ |
122 | NULL, |
123 | array('activity_date_time') | |
6a488035 TO |
124 | ); |
125 | ||
42d58fac PJ |
126 | $this->restore($activityContacts, |
127 | 'CRM_Activity_DAO_ActivityContact', | |
6a488035 | 128 | array( |
42d58fac | 129 | 'contact_id' => 'civicrm_contact', |
6a488035 TO |
130 | 'activity_id' => 'civicrm_activity', |
131 | ) | |
132 | ); | |
133 | } | |
134 | ||
135 | function group($group, $groupContact) { | |
136 | $this->restore($group, | |
42d58fac PJ |
137 | 'CRM_Contact_DAO_Group', |
138 | NULL, | |
139 | array('cache_date', 'refresh_date') | |
6a488035 TO |
140 | ); |
141 | ||
142 | $this->restore($groupContact, | |
143 | 'CRM_Contact_DAO_GroupContact', | |
144 | array( | |
145 | 'group_id' => 'civicrm_group', | |
146 | 'contact_id' => 'civicrm_contact', | |
147 | ) | |
148 | ); | |
149 | } | |
150 | ||
151 | function tag($tag, $entityTag) { | |
152 | $this->restore($tag, | |
153 | 'CRM_Core_DAO_Tag', | |
154 | array( | |
155 | 'created_id' => 'civicrm_contact', | |
156 | 'parent_id' => 'civicrm_tag', | |
157 | ) | |
158 | ); | |
159 | ||
160 | $this->restore($entityTag, | |
161 | 'CRM_Core_DAO_EntityTag', | |
162 | array( | |
163 | 'entity_id' => 'civicrm_contact', | |
164 | 'tag_id' => 'civicrm_tag', | |
165 | ) | |
166 | ); | |
167 | } | |
168 | ||
42d58fac | 169 | function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) { |
4d5c2eb5 | 170 | $object = new $daoName(); |
6a488035 TO |
171 | $tableName = $object->__table; |
172 | ||
173 | if (is_array($lookUpMapping)) { | |
174 | $lookUpMapping['id'] = $tableName; | |
175 | } | |
176 | else { | |
177 | $lookUpMapping = array('id' => $tableName); | |
178 | } | |
179 | ||
180 | foreach ($lookUpMapping as $columnName => $tableName) { | |
181 | $this->populateCache($tableName); | |
182 | } | |
183 | ||
184 | $saveMapping = FALSE; | |
185 | $columns = $chunk[0]; | |
186 | foreach ($chunk as $key => $value) { | |
187 | if ($key) { | |
4d5c2eb5 | 188 | $object = new $daoName(); |
6a488035 TO |
189 | foreach ($columns as $k => $column) { |
190 | if ($column == 'id') { | |
191 | $childID = $value[$k]; | |
192 | $masterID = CRM_Utils_Array::value($value[$k], | |
193 | $this->_lookupCache[$tableName], | |
194 | NULL | |
195 | ); | |
196 | if ($masterID) { | |
197 | $object->id = $masterID; | |
198 | } | |
199 | } | |
200 | else { | |
201 | if (array_key_exists($column, $lookUpMapping)) { | |
202 | $object->$column = $this->_lookupCache[$lookUpMapping[$column]][$value[$k]]; | |
203 | } | |
42d58fac PJ |
204 | elseif (!empty($dateFields) && in_array($column, $dateFields)) { |
205 | $object->$column = CRM_Utils_Date::isoToMysql($value[$k]); | |
206 | } | |
6a488035 TO |
207 | else { |
208 | $object->$column = $value[$k]; | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
213 | $object->save(); | |
214 | if (!$masterID) { | |
215 | $this->_lookupCache[$tableName][$childID] = $object->id; | |
216 | $this->_saveMapping[$tableName] = TRUE; | |
217 | } | |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | function saveCache() { | |
223 | $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES "; | |
224 | ||
225 | foreach ($this->_lookupCache as $tableName => & $values) { | |
226 | if (!$this->_saveMapping[$tableName]) { | |
227 | continue; | |
228 | } | |
229 | ||
230 | $mapValues = array(); | |
231 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_migration_mapping where entity_table = '$tableName'"); | |
232 | foreach ($values as $childID => $masterID) { | |
233 | $mapValues[] = "($masterID,$childID,'$tableName' )"; | |
234 | } | |
235 | $insertSQL = $sql . implode(",\n", $mapValues); | |
236 | CRM_Core_DAO::executeQuery($insertSQL); | |
237 | } | |
238 | } | |
239 | ||
240 | function populateCache($tableName) { | |
241 | if (isset($this->_lookupCache[$tableName])) { | |
242 | return; | |
243 | } | |
244 | ||
245 | $this->_lookupCache[$tableName] = array(); | |
246 | $this->_saveMapping[$tableName] = FALSE; | |
247 | ||
248 | $query = "SELECT master_id, slave_id | |
249 | FROM civicrm_migration_mapping | |
250 | WHERE entity_table = '{$tableName}' | |
251 | "; | |
252 | ||
253 | $dao = CRM_Core_DAO::executeQuery($query); | |
254 | while ($dao->fetch()) { | |
255 | $this->_lookupCache[$dao->slave_id] = $dao->master_id; | |
256 | } | |
257 | } | |
258 | } | |
259 |