Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Migrate_Import { | |
5bc392e6 | 36 | /** |
5bc392e6 | 37 | */ |
00be9182 | 38 | public function __construct() { |
3302658e | 39 | } |
6a488035 | 40 | |
32963c28 TO |
41 | /** |
42 | * Import custom-data from an XML file | |
43 | * | |
77855840 TO |
44 | * @param string $file |
45 | * Path to an XML file. | |
32963c28 TO |
46 | * @throws CRM_Core_Exception |
47 | * @return void; | |
48 | */ | |
00be9182 | 49 | public function run($file) { |
6a488035 | 50 | // read xml file |
8080d1e0 | 51 | $dom = new DomDocument(); |
353ffa53 | 52 | if (!$dom->load($file)) { |
8080d1e0 TO |
53 | throw new CRM_Core_Exception("Failed to parse XML file \"$file\""); |
54 | } | |
6a488035 TO |
55 | $dom->xinclude(); |
56 | $xml = simplexml_import_dom($dom); | |
32963c28 TO |
57 | return $this->runXmlElement($xml); |
58 | } | |
6a488035 | 59 | |
32963c28 TO |
60 | /** |
61 | * Import custom-data from an XML element | |
62 | * | |
63 | * @param SimpleXMLElement $xml | |
64 | * @return void | |
65 | */ | |
00be9182 | 66 | public function runXmlElement($xml) { |
3302658e TO |
67 | $idMap = array( |
68 | 'custom_group' => array(), | |
6a488035 TO |
69 | 'option_group' => array(), |
70 | ); | |
71 | ||
72 | // first create option groups and values if any | |
73 | $this->optionGroups($xml, $idMap); | |
74 | $this->optionValues($xml, $idMap); | |
75 | ||
76 | $this->relationshipTypes($xml); | |
77 | $this->contributionTypes($xml); | |
78 | ||
79 | // now create custom groups | |
80 | $this->customGroups($xml, $idMap); | |
81 | $this->customFields($xml, $idMap); | |
82 | ||
83 | // now create profile groups | |
84 | $this->profileGroups($xml, $idMap); | |
85 | $this->profileFields($xml, $idMap); | |
86 | $this->profileJoins($xml, $idMap); | |
87 | ||
88 | //create DB Template String sample data | |
89 | $this->dbTemplateString($xml, $idMap); | |
90 | ||
91 | // clean up all caches etc | |
92 | CRM_Core_Config::clearDBCache(); | |
93 | } | |
94 | ||
5bc392e6 | 95 | /** |
c490a46a | 96 | * @param CRM_Core_DAO $dao |
5bc392e6 EM |
97 | * @param $xml |
98 | * @param bool $save | |
99 | * @param null $keyName | |
100 | * | |
101 | * @return bool | |
102 | */ | |
00be9182 | 103 | public function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) { |
6a488035 TO |
104 | if ($keyName) { |
105 | if (isset($xml->$keyName)) { | |
106 | $dao->$keyName = (string ) $xml->$keyName; | |
107 | if ($dao->find(TRUE)) { | |
108 | CRM_Core_Session::setStatus(ts("Found %1, %2, %3", | |
3302658e TO |
109 | array( |
110 | 1 => $keyName, | |
111 | 2 => $dao->$keyName, | |
21dfd5f5 | 112 | 3 => $dao->__table, |
3302658e TO |
113 | ) |
114 | ), '', 'info'); | |
6a488035 TO |
115 | return FALSE; |
116 | } | |
117 | } | |
118 | } | |
119 | ||
353ffa53 | 120 | $fields = &$dao->fields(); |
6a488035 TO |
121 | foreach ($fields as $name => $dontCare) { |
122 | if (isset($xml->$name)) { | |
123 | $value = (string ) $xml->$name; | |
4f652853 | 124 | $value = str_replace(CRM_Utils_Migrate_Export::XML_VALUE_SEPARATOR, |
6a488035 TO |
125 | CRM_Core_DAO::VALUE_SEPARATOR, |
126 | $value | |
127 | ); | |
128 | $dao->$name = $value; | |
129 | } | |
130 | } | |
131 | if ($save) { | |
132 | $dao->save(); | |
133 | } | |
134 | return TRUE; | |
135 | } | |
136 | ||
5bc392e6 EM |
137 | /** |
138 | * @param $xml | |
139 | * @param $idMap | |
140 | */ | |
00be9182 | 141 | public function optionGroups(&$xml, &$idMap) { |
6a488035 TO |
142 | foreach ($xml->OptionGroups as $optionGroupsXML) { |
143 | foreach ($optionGroupsXML->OptionGroup as $optionGroupXML) { | |
144 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
145 | $this->copyData($optionGroup, $optionGroupXML, TRUE, 'name'); | |
146 | $idMap['option_group'][$optionGroup->name] = $optionGroup->id; | |
147 | } | |
148 | } | |
149 | } | |
150 | ||
5bc392e6 EM |
151 | /** |
152 | * @param $xml | |
153 | * @param $idMap | |
154 | */ | |
00be9182 | 155 | public function optionValues(&$xml, &$idMap) { |
6a488035 TO |
156 | foreach ($xml->OptionValues as $optionValuesXML) { |
157 | foreach ($optionValuesXML->OptionValue as $optionValueXML) { | |
158 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
159 | $optionValue->option_group_id = $idMap['option_group'][(string ) $optionValueXML->option_group_name]; | |
160 | $this->copyData($optionValue, $optionValueXML, FALSE, 'label'); | |
161 | if (!isset($optionValue->value)) { | |
162 | $sql = " | |
163 | SELECT MAX(ROUND(v.value)) + 1 | |
164 | FROM civicrm_option_value v | |
165 | WHERE v.option_group_id = %1 | |
166 | "; | |
167 | $params = array(1 => array($optionValue->option_group_id, 'Integer')); | |
168 | $optionValue->value = CRM_Core_DAO::singleValueQuery($sql, $params); | |
169 | } | |
170 | $optionValue->save(); | |
171 | } | |
172 | } | |
173 | } | |
174 | ||
5bc392e6 EM |
175 | /** |
176 | * @param $xml | |
177 | */ | |
00be9182 | 178 | public function relationshipTypes(&$xml) { |
6a488035 TO |
179 | |
180 | foreach ($xml->RelationshipTypes as $relationshipTypesXML) { | |
181 | foreach ($relationshipTypesXML->RelationshipType as $relationshipTypeXML) { | |
182 | $relationshipType = new CRM_Contact_DAO_RelationshipType(); | |
183 | $this->copyData($relationshipType, $relationshipTypeXML, TRUE, 'name_a_b'); | |
184 | } | |
185 | } | |
186 | } | |
187 | ||
5bc392e6 EM |
188 | /** |
189 | * @param $xml | |
190 | */ | |
00be9182 | 191 | public function contributionTypes(&$xml) { |
6a488035 TO |
192 | |
193 | foreach ($xml->ContributionTypes as $contributionTypesXML) { | |
194 | foreach ($contributionTypesXML->ContributionType as $contributionTypeXML) { | |
3302658e | 195 | $contributionType = new CRM_Financial_DAO_FinancialType(); |
6a488035 TO |
196 | $this->copyData($contributionType, $contributionTypeXML, TRUE, 'name'); |
197 | } | |
198 | } | |
199 | } | |
200 | ||
5bc392e6 EM |
201 | /** |
202 | * @param $xml | |
203 | * @param $idMap | |
204 | */ | |
00be9182 | 205 | public function customGroups(&$xml, &$idMap) { |
6a488035 TO |
206 | foreach ($xml->CustomGroups as $customGroupsXML) { |
207 | foreach ($customGroupsXML->CustomGroup as $customGroupXML) { | |
208 | $customGroup = new CRM_Core_DAO_CustomGroup(); | |
209 | if (!$this->copyData($customGroup, $customGroupXML, TRUE, 'name')) { | |
210 | $idMap['custom_group'][$customGroup->name] = $customGroup->id; | |
211 | continue; | |
212 | } | |
213 | ||
214 | $saveAgain = FALSE; | |
215 | if (!isset($customGroup->table_name) || | |
216 | empty($customGroup->table_name) | |
217 | ) { | |
218 | // fix table name | |
219 | $customGroup->table_name = "civicrm_value_" . strtolower(CRM_Utils_String::munge($customGroup->title, '_', 32)) . "_{$customGroup->id}"; | |
220 | ||
221 | $saveAgain = TRUE; | |
222 | } | |
223 | ||
224 | // fix extends stuff if it exists | |
225 | if (isset($customGroupXML->extends_entity_column_value_option_group) && | |
353ffa53 TO |
226 | isset($customGroupXML->extends_entity_column_value) |
227 | ) { | |
9d1c4909 | 228 | $valueIDs = array(); |
0f2ea47d RN |
229 | $optionValues = explode(",", $customGroupXML->extends_entity_column_value); |
230 | $optValues = implode("','", $optionValues); | |
6a488035 | 231 | if (trim($customGroup->extends) != 'Participant') { |
0f2ea47d RN |
232 | if ($customGroup->extends == 'Relationship') { |
233 | foreach ($optionValues as $key => $value) { | |
234 | $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_RelationshipType', $value, 'id', 'name_a_b'); | |
235 | $valueIDs[] = $relTypeId; | |
236 | } | |
237 | } | |
9d1c4909 RN |
238 | elseif (in_array($customGroup->extends, array('Individual', 'Organization', 'Household'))) { |
239 | $valueIDs = $optionValues; | |
240 | } | |
0f2ea47d RN |
241 | else { |
242 | $sql = " | |
6a488035 TO |
243 | SELECT v.value |
244 | FROM civicrm_option_value v | |
245 | INNER JOIN civicrm_option_group g ON g.id = v.option_group_id | |
246 | WHERE g.name = %1 | |
0f2ea47d | 247 | AND v.name IN ('$optValues') |
6a488035 | 248 | "; |
0f2ea47d RN |
249 | $params = array( |
250 | 1 => array( | |
251 | (string ) $customGroupXML->extends_entity_column_value_option_group, | |
252 | 'String', | |
253 | ), | |
254 | ); | |
353ffa53 | 255 | $dao = &CRM_Core_DAO::executeQuery($sql, $params); |
6a488035 | 256 | |
0f2ea47d RN |
257 | while ($dao->fetch()) { |
258 | $valueIDs[] = $dao->value; | |
259 | } | |
6a488035 TO |
260 | } |
261 | if (!empty($valueIDs)) { | |
262 | $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
263 | $valueIDs |
264 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 | 265 | |
0f2ea47d RN |
266 | unset($valueIDs); |
267 | ||
6a488035 TO |
268 | // Note: No need to set extends_entity_column_id here. |
269 | ||
270 | $saveAgain = TRUE; | |
271 | } | |
272 | } | |
273 | else { | |
274 | // when custom group extends 'Participant' | |
275 | $sql = " | |
276 | SELECT v.value | |
277 | FROM civicrm_option_value v | |
278 | INNER JOIN civicrm_option_group g ON g.id = v.option_group_id | |
279 | WHERE g.name = 'custom_data_type' | |
280 | AND v.name = %1 | |
281 | "; | |
282 | $params = array( | |
3302658e TO |
283 | 1 => array( |
284 | (string ) $customGroupXML->extends_entity_column_value_option_group, | |
6a488035 | 285 | 'String', |
21dfd5f5 | 286 | ), |
3302658e | 287 | ); |
6a488035 TO |
288 | $valueID = (int ) CRM_Core_DAO::singleValueQuery($sql, $params); |
289 | if ($valueID) { | |
290 | $customGroup->extends_entity_column_id = $valueID; | |
291 | } | |
292 | ||
293 | $optionIDs = array(); | |
294 | switch ($valueID) { | |
295 | case 1: | |
296 | // ParticipantRole | |
297 | $condition = "AND v.name IN ( '{$optValues}' )"; | |
298 | $optionIDs = CRM_Core_OptionGroup::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name'); | |
299 | break; | |
300 | ||
301 | case 2: | |
302 | // ParticipantEventName | |
303 | $condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )"; | |
304 | $optionIDs = CRM_Event_PseudoConstant::event(NULL, FALSE, $condition); | |
305 | break; | |
306 | ||
307 | case 3: | |
308 | // ParticipantEventType | |
309 | $condition = "AND v.name IN ( '{$optValues}' )"; | |
310 | $optionIDs = CRM_Core_OptionGroup::values('event_type', FALSE, FALSE, FALSE, $condition, 'name'); | |
311 | break; | |
312 | } | |
313 | ||
314 | if (is_array($optionIDs) && !empty($optionIDs)) { | |
315 | $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
316 | array_keys($optionIDs) |
317 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
318 | |
319 | $saveAgain = TRUE; | |
320 | } | |
321 | } | |
322 | } | |
323 | ||
324 | if ($saveAgain) { | |
325 | $customGroup->save(); | |
326 | } | |
327 | ||
328 | CRM_Core_BAO_CustomGroup::createTable($customGroup); | |
329 | $idMap['custom_group'][$customGroup->name] = $customGroup->id; | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
5bc392e6 EM |
334 | /** |
335 | * @param $xml | |
336 | * @param $idMap | |
337 | */ | |
00be9182 | 338 | public function customFields(&$xml, &$idMap) { |
6a488035 TO |
339 | // Re-index by group id so we can build out the custom fields one table |
340 | // at a time, and then rebuild the table triggers at the end, rather than | |
341 | // rebuilding the table triggers after each field is added (which is | |
342 | // painfully slow). | |
343 | $fields_indexed_by_group_id = array(); | |
344 | foreach ($xml->CustomFields as $customFieldsXML) { | |
345 | $total = count($customFieldsXML->CustomField); | |
346 | foreach ($customFieldsXML->CustomField as $customFieldXML) { | |
347 | $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name]; | |
348 | $fields_indexed_by_group_id[$id][] = $customFieldXML; | |
349 | } | |
350 | } | |
3302658e | 351 | while (list($group_id, $fields) = each($fields_indexed_by_group_id)) { |
6a488035 TO |
352 | $total = count($fields); |
353 | $count = 0; | |
3302658e | 354 | while (list(, $customFieldXML) = each($fields)) { |
6a488035 TO |
355 | $count++; |
356 | $customField = new CRM_Core_DAO_CustomField(); | |
357 | $customField->custom_group_id = $group_id; | |
358 | $skipStore = FALSE; | |
359 | if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) { | |
360 | $skipStore = TRUE; | |
361 | } | |
362 | ||
363 | if (empty($customField->option_group_id) && | |
364 | isset($customFieldXML->option_group_name) | |
365 | ) { | |
366 | $customField->option_group_id = $idMap['option_group'][(string ) $customFieldXML->option_group_name]; | |
367 | } | |
368 | if ($skipStore) { | |
369 | continue; | |
370 | } | |
371 | $customField->save(); | |
372 | ||
373 | // Only rebuild the table's trigger on the last field added to avoid un-necessary | |
374 | // and slow rebuilds when adding many fields at the same time. | |
375 | $triggerRebuild = FALSE; | |
3302658e | 376 | if ($count == $total) { |
6a488035 | 377 | $triggerRebuild = TRUE; |
3302658e | 378 | } |
6a488035 TO |
379 | $indexExist = FALSE; |
380 | CRM_Core_BAO_CustomField::createField($customField, 'add', $indexExist, $triggerRebuild); | |
381 | } | |
382 | } | |
383 | } | |
384 | ||
5bc392e6 EM |
385 | /** |
386 | * @param $xml | |
387 | * @param $idMap | |
388 | */ | |
00be9182 | 389 | public function dbTemplateString(&$xml, &$idMap) { |
6a488035 TO |
390 | foreach ($xml->Persistent as $persistentXML) { |
391 | foreach ($persistentXML->Persistent as $persistent) { | |
392 | $persistentObj = new CRM_Core_DAO_Persistent(); | |
393 | ||
394 | if ($persistent->is_config == 1) { | |
395 | $persistent->data = serialize(explode(',', $persistent->data)); | |
396 | } | |
397 | $this->copyData($persistentObj, $persistent, TRUE, 'context'); | |
398 | } | |
399 | } | |
400 | } | |
401 | ||
5bc392e6 EM |
402 | /** |
403 | * @param $xml | |
404 | * @param $idMap | |
405 | */ | |
00be9182 | 406 | public function profileGroups(&$xml, &$idMap) { |
6a488035 TO |
407 | foreach ($xml->ProfileGroups as $profileGroupsXML) { |
408 | foreach ($profileGroupsXML->ProfileGroup as $profileGroupXML) { | |
409 | $profileGroup = new CRM_Core_DAO_UFGroup(); | |
410 | $this->copyData($profileGroup, $profileGroupXML, TRUE, 'title'); | |
411 | $idMap['profile_group'][$profileGroup->name] = $profileGroup->id; | |
412 | $idMap['profile_group'][$profileGroup->title] = $profileGroup->id; | |
413 | } | |
414 | } | |
415 | } | |
416 | ||
5bc392e6 EM |
417 | /** |
418 | * @param $xml | |
419 | * @param $idMap | |
420 | * | |
421 | * @throws Exception | |
422 | */ | |
00be9182 | 423 | public function profileFields(&$xml, &$idMap) { |
6a488035 TO |
424 | foreach ($xml->ProfileFields as $profileFieldsXML) { |
425 | foreach ($profileFieldsXML->ProfileField as $profileFieldXML) { | |
426 | $profileField = new CRM_Core_DAO_UFField(); | |
427 | $profileField->uf_group_id = $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name]; | |
428 | $this->copyData($profileField, $profileFieldXML, FALSE, 'field_name'); | |
429 | ||
430 | // fix field name | |
431 | if (substr($profileField->field_name, 0, 7) == 'custom.') { | |
432 | list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name); | |
433 | $sql = " | |
434 | SELECT f.id | |
435 | FROM civicrm_custom_field f | |
436 | INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id | |
437 | WHERE g.table_name = %1 | |
438 | AND f.column_name = %2 | |
439 | "; | |
3302658e TO |
440 | $params = array( |
441 | 1 => array($tableName, 'String'), | |
6a488035 TO |
442 | 2 => array($columnName, 'String'), |
443 | ); | |
444 | $cfID = CRM_Core_DAO::singleValueQuery($sql, $params); | |
445 | if (!$cfID) { | |
446 | CRM_Core_Error::fatal(ts("Could not find custom field for %1, %2, %3", | |
353ffa53 TO |
447 | array( |
448 | 1 => $profileField->field_name, | |
449 | 2 => $tableName, | |
450 | 3 => $columnName, | |
451 | ) | |
452 | ) . "<br />"); | |
6a488035 TO |
453 | } |
454 | $profileField->field_name = "custom_{$cfID}"; | |
455 | } | |
456 | $profileField->save(); | |
457 | } | |
458 | } | |
459 | } | |
460 | ||
5bc392e6 EM |
461 | /** |
462 | * @param $xml | |
463 | * @param $idMap | |
464 | */ | |
00be9182 | 465 | public function profileJoins(&$xml, &$idMap) { |
6a488035 TO |
466 | foreach ($xml->ProfileJoins as $profileJoinsXML) { |
467 | foreach ($profileJoinsXML->ProfileJoin as $profileJoinXML) { | |
468 | $profileJoin = new CRM_Core_DAO_UFJoin(); | |
469 | $profileJoin->uf_group_id = $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name]; | |
470 | $this->copyData($profileJoin, $profileJoinXML, FALSE, 'module'); | |
471 | $profileJoin->save(); | |
472 | } | |
473 | } | |
474 | } | |
96025800 | 475 | |
6a488035 | 476 | } |