Merge pull request #1939 from kurund/CRM-13327
[civicrm-core.git] / CRM / Utils / Migrate / Export.php
index f713554357e7d1e19d145d9c62b459e8415eb0f0..235a513ab72871314e822e4f4546c89d8389390f 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -175,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');
@@ -251,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');
@@ -263,6 +263,96 @@ 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);
+  }
+
+  /**
+   * @param array $ufGroupIds list of custom groups to export
+   * @return void
+   */
+  function buildUFGroups($ufGroupIds) {
+    $ufGroupIdsSql = implode(',', array_filter($ufGroupIds, 'is_numeric'));
+    if (empty($ufGroupIdsSql)) {
+      return;
+    }
+
+    $sql = "
+      SELECT cg.*
+      FROM   civicrm_uf_group cg
+      WHERE  cg.id IN ($ufGroupIdsSql)
+
+    ";
+    $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup', $sql);
+
+    $sql = "
+      SELECT f.*
+      FROM   civicrm_uf_field f,
+             civicrm_uf_group cg
+      WHERE  f.uf_group_id = cg.id
+      AND    cg.id IN ($ufGroupIdsSql)
+    ";
+    $this->fetch('profileField', 'CRM_Core_DAO_UFField', $sql);
+
+    $sql = "
+      SELECT *
+      FROM   civicrm_uf_join
+      WHERE  entity_table IS NULL
+      AND    entity_id    IS NULL
+      AND    uf_group_id  IN ($ufGroupIdsSql)
+    ";
+    $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
+  }
+
   /**
    * Render the in-memory representation as XML
    *
@@ -287,6 +377,21 @@ 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) {
     $idNameFields = isset($this->_xml[$groupName]['idNameFields']) ? $this->_xml[$groupName]['idNameFields'] : NULL;
     $mappedFields = isset($this->_xml[$groupName]['mappedFields']) ? $this->_xml[$groupName]['mappedFields'] : NULL;
@@ -300,7 +405,7 @@ AND    entity_id    IS NULL
     }
 
     while ($dao->fetch()) {
-      $this->_xml[$groupName]['data'][] = $this->exportDAO($this->_xml[$groupName]['name'], $dao, $mappedFields);
+      $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])) {
@@ -356,10 +461,7 @@ AND    entity_id    IS NULL
       if (isset($object->$name) && $object->$name !== NULL) {
         // hack for extends_entity_column_value
         if ($name == 'extends_entity_column_value') {
-          if ($object->extends == 'Event' ||
-            $object->extends == 'Activity' ||
-            $object->extends == 'Relationship'
-          ) {
+          if (in_array($object->extends, array('Event', 'Activity', 'Relationship', 'Individual', 'Organization', 'Household'))) {
             if ($object->extends == 'Event') {
               $key = 'event_type';
             }
@@ -369,14 +471,32 @@ AND    entity_id    IS NULL
             elseif ($object->extends == 'Relationship') {
               $key = 'relationship_type';
             }
-            $keyValues['extends_entity_column_value_option_group'] = $key;
+            elseif($object->extends == 'Case') {
+              $key = 'case_type';
+            }
             $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($object->$name, 1, -1));
             $values = array();
-            foreach ($types as $type) {
-              $values[] = $this->_xml['optionValue']['idNameMap']["$key.{$type}"];
+            if (in_array($object->extends, array('Individual', 'Organization', 'Household'))) {
+              $key = 'contact_type';
+              $values = $types;
+            }
+            else {
+              foreach ($types as $type) {
+                if (in_array($key, array('activity_type', 'event_type'))) {
+                  $ogID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
+                  $ovParams = array('option_group_id' => $ogID, 'value' => $type);
+                  CRM_Core_BAO_OptionValue::retrieve($ovParams, $oValue);
+                  $values[] = $oValue['name'];
+                }
+                else {
+                  $relTypeName = CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_RelationshipType', $type, 'name_a_b', 'id');
+                  $values[] = $relTypeName;
+                }
+              }
             }
+            $keyValues['extends_entity_column_value_option_group'] = $key;
             $value = implode(',', $values);
-            $keyValues['extends_entity_column_value_option_value'] = $value;
+            $object->extends_entity_column_value = $value;
           }
           else {
             echo "This extension: {$object->extends} is not yet handled";