Merge pull request #4372 from davecivicrm/CRM-15467
[civicrm-core.git] / CRM / Utils / Migrate / Import.php
index f12776a1cff620985938836c505ec1705c537417..124a58c5360cbedff590f190c5c01b1ac8d5b534 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
 class CRM_Utils_Migrate_Import {
+  /**
+   *
+   */
   function __construct() {
   }
 
+  /**
+   * Import custom-data from an XML file
+   *
+   * @param string $file path to an XML file
+   * @throws CRM_Core_Exception
+   * @return void;
+   */
   function run($file) {
-
     // read xml file
-    $dom = DomDocument::load($file);
+    $dom = new DomDocument();
+    if (! $dom->load($file)) {
+      throw new CRM_Core_Exception("Failed to parse XML file \"$file\"");
+    }
     $dom->xinclude();
     $xml = simplexml_import_dom($dom);
+    return $this->runXmlElement($xml);
+  }
 
+  /**
+   * Import custom-data from an XML element
+   *
+   * @param SimpleXMLElement $xml
+   * @return void
+   */
+  function runXmlElement($xml) {
     $idMap = array(
       'custom_group' => array(),
       'option_group' => array(),
@@ -71,6 +92,14 @@ class CRM_Utils_Migrate_Import {
     CRM_Core_Config::clearDBCache();
   }
 
+  /**
+   * @param $dao
+   * @param $xml
+   * @param bool $save
+   * @param null $keyName
+   *
+   * @return bool
+   */
   function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) {
     if ($keyName) {
       if (isset($xml->$keyName)) {
@@ -92,7 +121,7 @@ class CRM_Utils_Migrate_Import {
     foreach ($fields as $name => $dontCare) {
       if (isset($xml->$name)) {
         $value = (string ) $xml->$name;
-        $value = str_replace(":;:;:;",
+        $value = str_replace(CRM_Utils_Migrate_Export::XML_VALUE_SEPARATOR,
           CRM_Core_DAO::VALUE_SEPARATOR,
           $value
         );
@@ -105,6 +134,10 @@ class CRM_Utils_Migrate_Import {
     return TRUE;
   }
 
+  /**
+   * @param $xml
+   * @param $idMap
+   */
   function optionGroups(&$xml, &$idMap) {
     foreach ($xml->OptionGroups as $optionGroupsXML) {
       foreach ($optionGroupsXML->OptionGroup as $optionGroupXML) {
@@ -115,6 +148,10 @@ class CRM_Utils_Migrate_Import {
     }
   }
 
+  /**
+   * @param $xml
+   * @param $idMap
+   */
   function optionValues(&$xml, &$idMap) {
     foreach ($xml->OptionValues as $optionValuesXML) {
       foreach ($optionValuesXML->OptionValue as $optionValueXML) {
@@ -135,6 +172,9 @@ WHERE      v.option_group_id = %1
     }
   }
 
+  /**
+   * @param $xml
+   */
   function relationshipTypes(&$xml) {
 
     foreach ($xml->RelationshipTypes as $relationshipTypesXML) {
@@ -145,6 +185,9 @@ WHERE      v.option_group_id = %1
     }
   }
 
+  /**
+   * @param $xml
+   */
   function contributionTypes(&$xml) {
 
     foreach ($xml->ContributionTypes as $contributionTypesXML) {
@@ -155,6 +198,10 @@ WHERE      v.option_group_id = %1
     }
   }
 
+  /**
+   * @param $xml
+   * @param $idMap
+   */
   function customGroups(&$xml, &$idMap) {
     foreach ($xml->CustomGroups as $customGroupsXML) {
       foreach ($customGroupsXML->CustomGroup as $customGroupXML) {
@@ -176,36 +223,47 @@ WHERE      v.option_group_id = %1
 
         // fix extends stuff if it exists
         if (isset($customGroupXML->extends_entity_column_value_option_group) &&
-          isset($customGroupXML->extends_entity_column_value_option_value)
-        ) {
-          $optValues = explode(",", $customGroupXML->extends_entity_column_value_option_value);
-          $optValues = implode("','", $optValues);
+          isset($customGroupXML->extends_entity_column_value)) {
+          $valueIDs = array();
+          $optionValues = explode(",", $customGroupXML->extends_entity_column_value);
+          $optValues = implode("','", $optionValues);
           if (trim($customGroup->extends) != 'Participant') {
-            $sql = "
+            if ($customGroup->extends == 'Relationship') {
+              foreach ($optionValues as $key => $value) {
+                $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_RelationshipType', $value, 'id', 'name_a_b');
+                $valueIDs[] = $relTypeId;
+              }
+            }
+            elseif (in_array($customGroup->extends, array('Individual', 'Organization', 'Household'))) {
+              $valueIDs = $optionValues;
+            }
+            else {
+              $sql = "
 SELECT     v.value
 FROM       civicrm_option_value v
 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
 WHERE      g.name = %1
-AND        v.name IN (%2)
+AND        v.name IN ('$optValues')
 ";
-            $params = array(
-              1 => array(
-                (string ) $customGroupXML->extends_entity_column_value_option_group,
-                'String',
-              ),
-              2 => array((string ) $optValues, 'String'),
-            );
-            $dao = & CRM_Core_DAO::executeQuery($sql, $params);
-
-            $valueIDs = array();
-            while ($dao->fetch()) {
-              $valueIDs[] = $dao->value;
+              $params = array(
+                1 => array(
+                  (string ) $customGroupXML->extends_entity_column_value_option_group,
+                  'String',
+                ),
+              );
+              $dao = & CRM_Core_DAO::executeQuery($sql, $params);
+
+              while ($dao->fetch()) {
+                $valueIDs[] = $dao->value;
+              }
             }
             if (!empty($valueIDs)) {
               $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
                 $valueIDs
               ) . CRM_Core_DAO::VALUE_SEPARATOR;
 
+              unset($valueIDs);
+
               // Note: No need to set extends_entity_column_id here.
 
               $saveAgain = TRUE;
@@ -272,6 +330,10 @@ AND        v.name = %1
     }
   }
 
+  /**
+   * @param $xml
+   * @param $idMap
+   */
   function customFields(&$xml, &$idMap) {
     // Re-index by group id so we can build out the custom fields one table
     // at a time, and then rebuild the table triggers at the end, rather than
@@ -319,6 +381,10 @@ AND        v.name = %1
     }
   }
 
+  /**
+   * @param $xml
+   * @param $idMap
+   */
   function dbTemplateString(&$xml, &$idMap) {
     foreach ($xml->Persistent as $persistentXML) {
       foreach ($persistentXML->Persistent as $persistent) {
@@ -332,6 +398,10 @@ AND        v.name = %1
     }
   }
 
+  /**
+   * @param $xml
+   * @param $idMap
+   */
   function profileGroups(&$xml, &$idMap) {
     foreach ($xml->ProfileGroups as $profileGroupsXML) {
       foreach ($profileGroupsXML->ProfileGroup as $profileGroupXML) {
@@ -343,6 +413,12 @@ AND        v.name = %1
     }
   }
 
+  /**
+   * @param $xml
+   * @param $idMap
+   *
+   * @throws Exception
+   */
   function profileFields(&$xml, &$idMap) {
     foreach ($xml->ProfileFields as $profileFieldsXML) {
       foreach ($profileFieldsXML->ProfileField as $profileFieldXML) {
@@ -381,6 +457,10 @@ AND        f.column_name = %2
     }
   }
 
+  /**
+   * @param $xml
+   * @param $idMap
+   */
   function profileJoins(&$xml, &$idMap) {
     foreach ($xml->ProfileJoins as $profileJoinsXML) {
       foreach ($profileJoinsXML->ProfileJoin as $profileJoinXML) {