CRM_Utils_Migrate_Import - Fix static warning
[civicrm-core.git] / CRM / Utils / Migrate / Export.php
index 32c66f7eb8cf1929b3f4ff9543d37a8e9f22e0fb..9045c95d89038d0017a8ed8e54ec6996101c3635 100644 (file)
@@ -36,110 +36,124 @@ class CRM_Utils_Migrate_Export {
 
   const XML_VALUE_SEPARATOR = ":;:;:;";
 
+  /**
+   * @var array description of export field mapping
+   *
+   * @code
+   * 'exampleEntityMappingName' => array(
+   *   'data' => array(),                     // placeholder; this will get filled-in during execution
+   *   'name' => 'CustomGroup',               // per-item XML tag name
+   *   'scope' => 'CustomGroups',             // container XML tag name
+   *   'required' => FALSE,                   // whether we *must* find records of this type
+   *   'idNameFields' => array('id', 'name'), // name of the (local/autogenerated) "id" and (portable) "name" columns
+   *   'idNameMap' => array(),                // placeholder; this will get filled-in during execution
+   * ),
+   * @endcode
+   */
   protected $_xml;
 
   function __construct() {
     $this->_xml = array(
       'customGroup' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'CustomGroup',
         'scope' => 'CustomGroups',
         'required' => FALSE,
         'idNameFields' => array('id', 'name'),
-        'map' => array(),
+        'idNameMap' => array(),
       ),
       'customField' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'CustomField',
         'scope' => 'CustomFields',
         'required' => FALSE,
         'idNameFields' => array('id', 'column_name'),
-        'map' => array(),
+        'idNameMap' => array(),
         'mappedFields' => array(
           array('optionGroup', 'option_group_id', 'option_group_name'),
           array('customGroup', 'custom_group_id', 'custom_group_name'),
         )
       ),
       'optionGroup' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'OptionGroup',
         'scope' => 'OptionGroups',
         'required' => FALSE,
-        'map' => array(),
+        'idNameMap' => array(),
         'idNameFields' => array('id', 'name'),
       ),
       'relationshipType' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'RelationshipType',
         'scope' => 'RelationshipTypes',
         'required' => FALSE,
         'idNameFields' => array('id', 'name_a_b'),
-        'map' => array(),
+        'idNameMap' => array(),
       ),
       'locationType' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'LocationType',
         'scope' => 'LocationTypes',
         'required' => FALSE,
         'idNameFields' => array('id', 'name'),
-        'map' => array(),
+        'idNameMap' => array(),
       ),
       'optionValue' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'OptionValue',
         'scope' => 'OptionValues',
         'required' => FALSE,
-        'map' => array(),
+        'idNameMap' => array(),
         'idNameFields' => array('value', 'name', 'prefix'),
         'mappedFields' => array(
           array('optionGroup', 'option_group_id', 'option_group_name'),
         ),
       ),
       'profileGroup' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'ProfileGroup',
         'scope' => 'ProfileGroups',
         'required' => FALSE,
         'idNameFields' => array('id', 'title'),
-        'map' => array(),
+        'idNameMap' => array(),
       ),
       'profileField' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'ProfileField',
         'scope' => 'ProfileFields',
         'required' => FALSE,
-        'map' => array(),
+        'idNameMap' => array(),
         'mappedFields' => array(
           array('profileGroup', 'uf_group_id', 'profile_group_name')
         ),
       ),
       'profileJoin' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'ProfileJoin',
         'scope' => 'ProfileJoins',
         'required' => FALSE,
-        'map' => array(),
+        'idNameMap' => array(),
         'mappedFields' => array(
           array('profileGroup', 'uf_group_id', 'profile_group_name')
         ),
       ),
       'mappingGroup' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'MappingGroup',
         'scope' => 'MappingGroups',
         'required' => FALSE,
         'idNameFields' => array('id', 'name'),
-        'map' => array(),
+        'idNameMap' => array(),
         'mappedFields' => array(
           array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'),
         )
       ),
       'mappingField' => array(
-        'data' => NULL,
+        'data' => array(),
         'name' => 'MappingField',
         'scope' => 'MappingFields',
         'required' => FALSE,
-        'map' => array(),
+        'idNameMap' => array(),
         'mappedFields' => array(
           array('mappingGroup', 'mapping_id', 'mapping_group_name'),
           array('locationType', 'location_type_id', 'location_type_name'),
@@ -161,75 +175,75 @@ class CRM_Utils_Migrate_Export {
     $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
 
     $sql = "
-SELECT distinct(g.id), g.*
-FROM   civicrm_option_group g
-WHERE  g.name IN $optionGroups
-";
+      SELECT distinct(g.id), g.*
+      FROM   civicrm_option_group g
+      WHERE  g.name IN $optionGroups
+    ";
     $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
 
     $sql = "
-SELECT distinct(g.id), g.*
-FROM   civicrm_option_group g,
-       civicrm_custom_field f,
-       civicrm_custom_group cg
-WHERE  f.option_group_id = g.id
-AND    f.custom_group_id = cg.id
-AND    cg.is_active = 1
-";
+      SELECT distinct(g.id), g.*
+      FROM   civicrm_option_group g,
+             civicrm_custom_field f,
+             civicrm_custom_group cg
+      WHERE  f.option_group_id = g.id
+      AND    f.custom_group_id = cg.id
+      AND    cg.is_active = 1
+    ";
     $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
 
     $sql = "
-SELECT v.*, g.name as prefix
-FROM   civicrm_option_value v,
-       civicrm_option_group g
-WHERE  v.option_group_id = g.id
-AND    g.name IN $optionGroups
-";
+      SELECT v.*, g.name as prefix
+      FROM   civicrm_option_value v,
+             civicrm_option_group g
+      WHERE  v.option_group_id = g.id
+      AND    g.name IN $optionGroups
+    ";
 
     $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
 
     $sql = "
-SELECT distinct(v.id), v.*, g.name as prefix
-FROM   civicrm_option_value v,
-       civicrm_option_group g,
-       civicrm_custom_field f,
-       civicrm_custom_group cg
-WHERE  v.option_group_id = g.id
-AND    f.option_group_id = g.id
-AND    f.custom_group_id = cg.id
-AND    cg.is_active = 1
-";
+      SELECT distinct(v.id), v.*, g.name as prefix
+      FROM   civicrm_option_value v,
+             civicrm_option_group g,
+             civicrm_custom_field f,
+             civicrm_custom_group cg
+      WHERE  v.option_group_id = g.id
+      AND    f.option_group_id = g.id
+      AND    f.custom_group_id = cg.id
+      AND    cg.is_active = 1
+    ";
 
     $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
 
     $sql = "
-SELECT rt.*
-FROM   civicrm_relationship_type rt
-WHERE  rt.is_active = 1
-";
+      SELECT rt.*
+      FROM   civicrm_relationship_type rt
+      WHERE  rt.is_active = 1
+    ";
     $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
 
     $sql = "
-SELECT lt.*
-FROM   civicrm_location_type lt
-WHERE  lt.is_active = 1
-";
+      SELECT lt.*
+      FROM   civicrm_location_type lt
+      WHERE  lt.is_active = 1
+    ";
     $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
 
     $sql = "
-SELECT cg.*
-FROM   civicrm_custom_group cg
-WHERE  cg.is_active = 1
-";
+      SELECT cg.*
+      FROM   civicrm_custom_group cg
+      WHERE  cg.is_active = 1
+    ";
     $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
 
     $sql = "
-SELECT f.*
-FROM   civicrm_custom_field f,
-       civicrm_custom_group cg
-WHERE  f.custom_group_id = cg.id
-AND    cg.is_active = 1
-";
+      SELECT f.*
+      FROM   civicrm_custom_field f,
+             civicrm_custom_group cg
+      WHERE  f.custom_group_id = cg.id
+      AND    cg.is_active = 1
+    ";
     $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
 
     $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
@@ -237,11 +251,11 @@ AND    cg.is_active = 1
     $this->fetch('profileField', 'CRM_Core_DAO_UFField');
 
     $sql = "
-SELECT *
-FROM   civicrm_uf_join
-WHERE  entity_table IS NULL
-AND    entity_id    IS NULL
-";
+      SELECT *
+      FROM   civicrm_uf_join
+      WHERE  entity_table IS NULL
+      AND    entity_id    IS NULL
+    ";
     $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
 
     $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
@@ -249,6 +263,59 @@ AND    entity_id    IS NULL
     $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
   }
 
+  /**
+   * @param array $customGroupIds list of custom groups to export
+   * @return void
+   */
+  function buildCustomGroups($customGroupIds) {
+    $customGroupIdsSql = implode(',', array_filter($customGroupIds, 'is_numeric'));
+    if (empty($customGroupIdsSql)) {
+      return;
+    }
+
+    $sql = "
+      SELECT distinct(g.id), g.*
+      FROM   civicrm_option_group g,
+             civicrm_custom_field f,
+             civicrm_custom_group cg
+      WHERE  f.option_group_id = g.id
+      AND    f.custom_group_id = cg.id
+      AND    cg.id in ($customGroupIdsSql)
+    ";
+    $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
+
+    $sql = "
+      SELECT distinct(v.id), v.*, g.name as prefix
+      FROM   civicrm_option_value v,
+             civicrm_option_group g,
+             civicrm_custom_field f,
+             civicrm_custom_group cg
+      WHERE  v.option_group_id = g.id
+      AND    f.option_group_id = g.id
+      AND    f.custom_group_id = cg.id
+      AND    cg.id in ($customGroupIdsSql)
+    ";
+
+    $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
+
+    $sql = "
+      SELECT cg.*
+      FROM   civicrm_custom_group cg
+      WHERE  cg.id in ($customGroupIdsSql)
+
+    ";
+    $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
+
+    $sql = "
+      SELECT f.*
+      FROM   civicrm_custom_field f,
+             civicrm_custom_group cg
+      WHERE  f.custom_group_id = cg.id
+      AND    cg.id in ($customGroupIdsSql)
+    ";
+    $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
+  }
+
   /**
    * Render the in-memory representation as XML
    *
@@ -259,7 +326,11 @@ AND    entity_id    IS NULL
     $buffer .= "\n\n<CustomData>\n";
     foreach (array_keys($this->_xml) as $key) {
       if (!empty($this->_xml[$key]['data'])) {
-        $buffer .= "  <{$this->_xml[$key]['scope']}>\n{$this->_xml[$key]['data']}  </{$this->_xml[$key]['scope']}>\n";
+        $buffer .= "  <{$this->_xml[$key]['scope']}>\n";
+        foreach ($this->_xml[$key]['data'] as $item) {
+          $buffer .= $this->renderKeyValueXML($this->_xml[$key]['name'], $item);
+        }
+        $buffer .= "  </{$this->_xml[$key]['scope']}>\n";
       }
       elseif ($this->_xml[$key]['required']) {
         CRM_Core_Error::fatal("No records in DB for $key");
@@ -269,9 +340,24 @@ AND    entity_id    IS NULL
     return $buffer;
   }
 
+  /**
+   * Generate an array-tree representation of the exported elements.
+   *
+   * @return array
+   */
+  function toArray() {
+    $result = array();
+    foreach (array_keys($this->_xml) as $key) {
+      if (!empty($this->_xml[$key]['data'])) {
+        $result[ $this->_xml[$key]['name'] ] = array_values($this->_xml[$key]['data']);
+      }
+    }
+    return $result;
+  }
+
   function fetch($groupName, $daoName, $sql = NULL) {
-    $map = isset($this->_xml[$groupName]['idNameFields']) ? $this->_xml[$groupName]['idNameFields'] : NULL;
-    $add = isset($this->_xml[$groupName]['mappedFields']) ? $this->_xml[$groupName]['mappedFields'] : NULL;
+    $idNameFields = isset($this->_xml[$groupName]['idNameFields']) ? $this->_xml[$groupName]['idNameFields'] : NULL;
+    $mappedFields = isset($this->_xml[$groupName]['mappedFields']) ? $this->_xml[$groupName]['mappedFields'] : NULL;
 
     $dao = new $daoName();
     if ($sql) {
@@ -282,17 +368,14 @@ AND    entity_id    IS NULL
     }
 
     while ($dao->fetch()) {
-      $keyValues = $this->exportDAO($this->_xml[$groupName]['name'], $dao, $add);
-      $this->_xml[$groupName]['data'] .= $this->renderKeyValueXML(
-        $this->_xml[$groupName]['name'],
-        $keyValues
-      );
-      if ($map) {
-        if (isset($map[2])) {
-          $this->_xml[$groupName]['map'][$dao->{$map[2]} . '.' . $dao->{$map[0]}] = $dao->{$map[1]};
+      $this->_xml[$groupName]['data'][$dao->id] = $this->exportDAO($this->_xml[$groupName]['name'], $dao, $mappedFields);
+      if ($idNameFields) {
+        // index the id/name fields so that we can translate from FK ids to FK names
+        if (isset($idNameFields[2])) {
+          $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
         }
         else {
-          $this->_xml[$groupName]['map'][$dao->{$map[0]}] = $dao->{$map[1]};
+          $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
         }
       }
     }
@@ -311,10 +394,10 @@ AND    entity_id    IS NULL
       foreach ($mappedFields as $mappedField) {
         if (isset($dao->{$mappedField[1]})) {
           if (isset($mappedField[3])) {
-            $label = $this->_xml[$mappedField[0]]['map']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
+            $label = $this->_xml[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
           }
           else {
-            $label = $this->_xml[$mappedField[0]]['map'][$dao->{$mappedField[1]}];
+            $label = $this->_xml[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
           }
           $keyValues[$mappedField[2]] = $label;
         }
@@ -358,7 +441,7 @@ AND    entity_id    IS NULL
             $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($object->$name, 1, -1));
             $values = array();
             foreach ($types as $type) {
-              $values[] = $this->_xml['optionValue']['map']["$key.{$type}"];
+              $values[] = $this->_xml['optionValue']['idNameMap']["$key.{$type}"];
             }
             $value = implode(',', $values);
             $keyValues['extends_entity_column_value_option_value'] = $value;