Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | |
5bc392e6 | 41 | /** |
5bc392e6 | 42 | */ |
00be9182 | 43 | public function __construct() { |
6a488035 TO |
44 | $this->_lookupCache = array(); |
45 | $this->_saveMapping = array(); | |
46 | } | |
47 | ||
5bc392e6 EM |
48 | /** |
49 | * @param $file | |
50 | */ | |
00be9182 | 51 | public function run($file) { |
6a488035 TO |
52 | $json = file_get_contents($file); |
53 | ||
54 | $decodedContacts = json_decode($json); | |
55 | ||
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, | |
42d58fac | 64 | $decodedContacts->civicrm_activity_contact |
6a488035 TO |
65 | ); |
66 | $this->group($decodedContacts->civicrm_group, | |
67 | $decodedContacts->civicrm_group_contact | |
68 | ); | |
69 | $this->tag($decodedContacts->civicrm_tag, | |
70 | $decodedContacts->civicrm_entity_tag | |
71 | ); | |
72 | ||
73 | // clean up all caches etc | |
74 | CRM_Core_Config::clearDBCache(); | |
75 | } | |
76 | ||
5bc392e6 EM |
77 | /** |
78 | * @param $contact | |
79 | */ | |
00be9182 | 80 | public function contact(&$contact) { |
6a488035 TO |
81 | $this->restore($contact, |
82 | 'CRM_Contact_DAO_Contact', | |
42d58fac PJ |
83 | array('id' => 'civicrm_contact'), |
84 | array('birth_date', 'deceased_date', 'created_date', 'modified_date') | |
6a488035 TO |
85 | ); |
86 | } | |
87 | ||
5bc392e6 EM |
88 | /** |
89 | * @param $email | |
90 | */ | |
00be9182 | 91 | public function email(&$email) { |
6a488035 TO |
92 | $this->restore($email, |
93 | 'CRM_Core_DAO_Email', | |
94 | array('contact_id' => 'civicrm_contact') | |
95 | ); | |
96 | } | |
97 | ||
5bc392e6 EM |
98 | /** |
99 | * @param $phone | |
100 | */ | |
00be9182 | 101 | public function phone(&$phone) { |
6a488035 TO |
102 | $this->restore($phone, |
103 | 'CRM_Core_DAO_Phone', | |
104 | array('contact_id' => 'civicrm_contact') | |
105 | ); | |
106 | } | |
107 | ||
5bc392e6 EM |
108 | /** |
109 | * @param $address | |
110 | */ | |
00be9182 | 111 | public function address(&$address) { |
6a488035 TO |
112 | $this->restore($address, |
113 | 'CRM_Core_DAO_Address', | |
114 | array('contact_id' => 'civicrm_contact') | |
115 | ); | |
116 | } | |
117 | ||
5bc392e6 EM |
118 | /** |
119 | * @param $note | |
120 | */ | |
00be9182 | 121 | public function note(&$note) { |
6a488035 TO |
122 | $this->restore($note, |
123 | 'CRM_Core_DAO_Note', | |
42d58fac PJ |
124 | array('contact_id' => 'civicrm_contact'), |
125 | array('modified_date') | |
6a488035 TO |
126 | ); |
127 | } | |
128 | ||
5bc392e6 EM |
129 | /** |
130 | * @param $relationship | |
131 | */ | |
00be9182 | 132 | public function relationship(&$relationship) { |
6a488035 TO |
133 | $this->restore($relationship, |
134 | 'CRM_Contact_DAO_Relationship', | |
135 | array( | |
136 | 'contact_id_a' => 'civicrm_contact', | |
137 | 'contact_id_b' => 'civicrm_contact', | |
138 | ) | |
139 | ); | |
140 | } | |
141 | ||
5bc392e6 EM |
142 | /** |
143 | * @param $activity | |
144 | * @param $activityContacts | |
145 | */ | |
00be9182 | 146 | public function activity($activity, $activityContacts) { |
6a488035 TO |
147 | $this->restore($activity, |
148 | 'CRM_Activity_DAO_Activity', | |
42d58fac PJ |
149 | NULL, |
150 | array('activity_date_time') | |
6a488035 TO |
151 | ); |
152 | ||
42d58fac PJ |
153 | $this->restore($activityContacts, |
154 | 'CRM_Activity_DAO_ActivityContact', | |
6a488035 | 155 | array( |
42d58fac | 156 | 'contact_id' => 'civicrm_contact', |
6a488035 TO |
157 | 'activity_id' => 'civicrm_activity', |
158 | ) | |
159 | ); | |
160 | } | |
161 | ||
5bc392e6 EM |
162 | /** |
163 | * @param $group | |
164 | * @param $groupContact | |
165 | */ | |
00be9182 | 166 | public function group($group, $groupContact) { |
6a488035 | 167 | $this->restore($group, |
42d58fac PJ |
168 | 'CRM_Contact_DAO_Group', |
169 | NULL, | |
170 | array('cache_date', 'refresh_date') | |
6a488035 TO |
171 | ); |
172 | ||
173 | $this->restore($groupContact, | |
174 | 'CRM_Contact_DAO_GroupContact', | |
175 | array( | |
176 | 'group_id' => 'civicrm_group', | |
177 | 'contact_id' => 'civicrm_contact', | |
178 | ) | |
179 | ); | |
180 | } | |
181 | ||
5bc392e6 EM |
182 | /** |
183 | * @param $tag | |
184 | * @param $entityTag | |
185 | */ | |
00be9182 | 186 | public function tag($tag, $entityTag) { |
6a488035 TO |
187 | $this->restore($tag, |
188 | 'CRM_Core_DAO_Tag', | |
189 | array( | |
190 | 'created_id' => 'civicrm_contact', | |
191 | 'parent_id' => 'civicrm_tag', | |
192 | ) | |
193 | ); | |
194 | ||
195 | $this->restore($entityTag, | |
196 | 'CRM_Core_DAO_EntityTag', | |
197 | array( | |
198 | 'entity_id' => 'civicrm_contact', | |
199 | 'tag_id' => 'civicrm_tag', | |
200 | ) | |
201 | ); | |
202 | } | |
203 | ||
5bc392e6 EM |
204 | /** |
205 | * @param $chunk | |
c490a46a | 206 | * @param string $daoName |
5bc392e6 EM |
207 | * @param null $lookUpMapping |
208 | * @param null $dateFields | |
209 | */ | |
00be9182 | 210 | public function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) { |
353ffa53 | 211 | $object = new $daoName(); |
6a488035 TO |
212 | $tableName = $object->__table; |
213 | ||
214 | if (is_array($lookUpMapping)) { | |
215 | $lookUpMapping['id'] = $tableName; | |
216 | } | |
217 | else { | |
218 | $lookUpMapping = array('id' => $tableName); | |
219 | } | |
220 | ||
221 | foreach ($lookUpMapping as $columnName => $tableName) { | |
222 | $this->populateCache($tableName); | |
223 | } | |
224 | ||
225 | $saveMapping = FALSE; | |
226 | $columns = $chunk[0]; | |
227 | foreach ($chunk as $key => $value) { | |
228 | if ($key) { | |
353ffa53 | 229 | $object = new $daoName(); |
6a488035 TO |
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], | |
235 | NULL | |
236 | ); | |
237 | if ($masterID) { | |
238 | $object->id = $masterID; | |
239 | } | |
240 | } | |
241 | else { | |
242 | if (array_key_exists($column, $lookUpMapping)) { | |
243 | $object->$column = $this->_lookupCache[$lookUpMapping[$column]][$value[$k]]; | |
244 | } | |
42d58fac PJ |
245 | elseif (!empty($dateFields) && in_array($column, $dateFields)) { |
246 | $object->$column = CRM_Utils_Date::isoToMysql($value[$k]); | |
247 | } | |
6a488035 TO |
248 | else { |
249 | $object->$column = $value[$k]; | |
250 | } | |
251 | } | |
252 | } | |
253 | ||
254 | $object->save(); | |
255 | if (!$masterID) { | |
256 | $this->_lookupCache[$tableName][$childID] = $object->id; | |
257 | $this->_saveMapping[$tableName] = TRUE; | |
258 | } | |
259 | } | |
260 | } | |
261 | } | |
262 | ||
00be9182 | 263 | public function saveCache() { |
6a488035 TO |
264 | $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES "; |
265 | ||
266 | foreach ($this->_lookupCache as $tableName => & $values) { | |
267 | if (!$this->_saveMapping[$tableName]) { | |
268 | continue; | |
269 | } | |
270 | ||
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' )"; | |
275 | } | |
276 | $insertSQL = $sql . implode(",\n", $mapValues); | |
277 | CRM_Core_DAO::executeQuery($insertSQL); | |
278 | } | |
279 | } | |
280 | ||
5bc392e6 | 281 | /** |
100fef9d | 282 | * @param string $tableName |
5bc392e6 | 283 | */ |
00be9182 | 284 | public function populateCache($tableName) { |
6a488035 TO |
285 | if (isset($this->_lookupCache[$tableName])) { |
286 | return; | |
287 | } | |
288 | ||
289 | $this->_lookupCache[$tableName] = array(); | |
290 | $this->_saveMapping[$tableName] = FALSE; | |
291 | ||
292 | $query = "SELECT master_id, slave_id | |
293 | FROM civicrm_migration_mapping | |
294 | WHERE entity_table = '{$tableName}' | |
295 | "; | |
296 | ||
297 | $dao = CRM_Core_DAO::executeQuery($query); | |
298 | while ($dao->fetch()) { | |
299 | $this->_lookupCache[$dao->slave_id] = $dao->master_id; | |
300 | } | |
301 | } | |
302 | } |