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