From c6914066df7816c70e40b11b489b5f9161b5f552 Mon Sep 17 00:00:00 2001
From: Pradeep Nayak <pradeep@pradeep.(none)>
Date: Mon, 20 May 2013 01:12:39 +0530
Subject: [PATCH] -- fixed for CRM-12510

----------------------------------------
* CRM-12510: Copy Contribution Page does not copy associated quick-config price set
  http://issues.civicrm.org/jira/browse/CRM-12510
---
 CRM/Contribute/BAO/ContributionPage.php  |  5 +-
 CRM/Core/BAO/OptionGroup.php             | 88 ------------------------
 CRM/Event/BAO/Event.php                  | 18 +----
 CRM/Event/Form/ManageEvent/EventInfo.php | 13 ----
 CRM/Price/BAO/Set.php                    | 46 +++++++++++++
 5 files changed, 50 insertions(+), 120 deletions(-)

diff --git a/CRM/Contribute/BAO/ContributionPage.php b/CRM/Contribute/BAO/ContributionPage.php
index 2475655358..09f6407d34 100644
--- a/CRM/Contribute/BAO/ContributionPage.php
+++ b/CRM/Contribute/BAO/ContributionPage.php
@@ -607,8 +607,9 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio
         'contribution_page_id' => $copy->id,
       ));
 
-    //copy option group and values
-    $copy->default_amount_id = CRM_Core_BAO_OptionGroup::copyValue('contribution', $id, $copy->id, CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'default_amount_id'));
+    //copy price sets
+    CRM_Price_BAO_Set::copyPriceSet('civicrm_contribution_page', $id, $copy->id);
+
     $copyTellFriend = &CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', array(
         'entity_id' => $id,
         'entity_table' => 'civicrm_contribution_page',
diff --git a/CRM/Core/BAO/OptionGroup.php b/CRM/Core/BAO/OptionGroup.php
index 872f4b79e6..315fbf06f1 100644
--- a/CRM/Core/BAO/OptionGroup.php
+++ b/CRM/Core/BAO/OptionGroup.php
@@ -144,93 +144,5 @@ class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup {
     $optionGroup->find(TRUE);
     return $optionGroup->name;
   }
-
-  /**
-   * Function to copy the option group and values
-   *
-   * @param  String $component      - component page for which custom
-   *                                  option group and values need to be copied
-   * @param  int    $fromId         - component page id on which
-   *                                  basis copy is to be made
-   * @param  int    $toId           - component page id to be copied onto
-   * @param  int    $defaultId      - default custom value id on the
-   *                                  component page
-   * @param  String $discountSuffix - discount suffix for the discounted
-   *                                  option group
-   *
-   * @return int   $id              - default custom value id for the
-   *                                 copied component page
-   *
-   * @access public
-   * @static
-   */
-  static function copyValue($component, $fromId, $toId, $defaultId = FALSE, $discountSuffix = NULL) {
-    $page = '_page';
-    if ($component == 'event') {
-      //fix for CRM-3391.
-      //as for event we remove 'page' from group name.
-      $page = NULL;
-    }
-    elseif ($component == 'price') {
-      $page = '_field';
-    }
-
-    $fromGroupName = 'civicrm_' . $component . $page . '.amount.' . $fromId . $discountSuffix;
-    $toGroupName = 'civicrm_' . $component . $page . '.amount.' . $toId . $discountSuffix;
-
-    $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
-      $fromGroupName,
-      'id',
-      'name'
-    );
-    if ($optionGroupId) {
-      $copyOptionGroup = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_OptionGroup',
-        array('name' => $fromGroupName),
-        array('name' => $toGroupName)
-      );
-
-      $copyOptionValue = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_OptionValue',
-        array('option_group_id' => $optionGroupId),
-        array('option_group_id' => $copyOptionGroup->id)
-      );
-
-      if ($discountSuffix) {
-        $copyDiscount =& CRM_Core_DAO::copyGeneric( 'CRM_Core_DAO_Discount',
-          array(
-            'entity_id' => $fromId,
-            'entity_table' => 'civicrm_' . $component,
-            'option_group_id' => $optionGroupId,
-          ),
-          array(
-            'entity_id' => $toId,
-            'option_group_id' => $copyOptionGroup->id,
-          )
-        );
-      }
-
-      if ($defaultId) {
-        $query = "
-SELECT second.id default_id
-FROM civicrm_option_value first, civicrm_option_value second
-WHERE second.option_group_id =%1
-AND first.option_group_id =%2
-AND first.weight = second.weight
-AND first.id =%3
-";
-        $params = array(1 => array($copyOptionGroup->id, 'Int'),
-          2 => array($optionGroupId, 'Int'),
-          3 => array($defaultId, 'Int'),
-        );
-
-        $dao = CRM_Core_DAO::executeQuery($query, $params);
-
-        while ($dao->fetch()) {
-          $id = $dao->default_id;
-        }
-        return $id;
-      }
-      return FALSE;
-    }
-  }
 }
 
diff --git a/CRM/Event/BAO/Event.php b/CRM/Event/BAO/Event.php
index e9c460bd32..01c04c91bc 100644
--- a/CRM/Event/BAO/Event.php
+++ b/CRM/Event/BAO/Event.php
@@ -866,23 +866,7 @@ WHERE civicrm_event.is_active = 1
          $fieldsFix
        );
     }
-    $priceSetId = CRM_Price_BAO_Set::getFor('civicrm_event', $id);
-    if ($priceSetId) {
-      $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $priceSetId, 'is_quick_config');
-      if(!$isQuickConfig) {
-        $copyPriceSet = &CRM_Price_BAO_Set::copy($priceSetId);
-        CRM_Price_BAO_Set::addTo('civicrm_event', $copyEvent->id, $copyPriceSet->id);
-      } else {
-        $copyPriceSet = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_SetEntity',
-          array(
-            'entity_id' => $id,
-            'entity_table' => 'civicrm_event',
-          ),
-          array('entity_id' => $copyEvent->id)
-        );
-      }
-    }
-
+    CRM_Price_BAO_Set::copyPriceSet('civicrm_event', $id, $copyEvent->id);
     $copyUF = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin',
       array(
         'entity_id' => $id,
diff --git a/CRM/Event/Form/ManageEvent/EventInfo.php b/CRM/Event/Form/ManageEvent/EventInfo.php
index ebccf785fb..a5f4682de8 100644
--- a/CRM/Event/Form/ManageEvent/EventInfo.php
+++ b/CRM/Event/Form/ManageEvent/EventInfo.php
@@ -334,19 +334,6 @@ class CRM_Event_Form_ManageEvent_EventInfo extends CRM_Event_Form_ManageEvent {
     // now that we have the event’s id, do some more template-based stuff
     if (CRM_Utils_Array::value('template_id', $params)) {
       CRM_Event_BAO_Event::copy($params['template_id'], $event, TRUE);
-      // copy price sets if any
-      $priceSetId = CRM_Price_BAO_Set::getFor('civicrm_event', $params['template_id']);
-      if ($priceSetId) {
-        $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set',
-          $priceSetId,
-          'is_quick_config'
-        );
-        if ($isQuickConfig) {
-          $copyPriceSet = &CRM_Price_BAO_Set::copy($priceSetId);
-          $priceSetId = $copyPriceSet->id;
-        }
-        CRM_Price_BAO_Set::addTo('civicrm_event', $event->id, $priceSetId);
-      }
     }
     
     $this->set('id', $event->id);
diff --git a/CRM/Price/BAO/Set.php b/CRM/Price/BAO/Set.php
index 2f580d7e1a..e2c9e57279 100644
--- a/CRM/Price/BAO/Set.php
+++ b/CRM/Price/BAO/Set.php
@@ -1178,5 +1178,51 @@ WHERE       ps.id = %1
     return false;
   }
 
+  /*
+   * Copy priceSet when event/contibution page is copied
+   *
+   * @params string $baoName  BAO name
+   * @params int $id old event/contribution page id
+   * @params int $newId newly created event/contribution page id
+   *
+   */
+  static function copyPriceSet($baoName, $id, $newId) {
+    $priceSetId = CRM_Price_BAO_Set::getFor($baoName, $id);
+    if ($priceSetId) {
+      $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $priceSetId, 'is_quick_config');
+      if($isQuickConfig) {
+        $copyPriceSet = &CRM_Price_BAO_Set::copy($priceSetId);
+        CRM_Price_BAO_Set::addTo($baoName, $newId, $copyPriceSet->id);
+      } 
+      else {
+        $copyPriceSet = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_SetEntity',
+          array(
+            'entity_id' => $id,
+            'entity_table' => $baoName,
+          ),
+          array('entity_id' => $newId)
+        );
+      }
+      // copy event discount
+      if ($baoName == 'civicrm_event') {
+        $discount = CRM_Core_BAO_Discount::getOptionGroup($id, 'civicrm_event');
+        foreach ($discount as $discountId => $setId) {
+
+          $copyPriceSet = &CRM_Price_BAO_Set::copy($setId);
+        
+          $copyDiscount = &CRM_Core_DAO::copyGeneric(
+            'CRM_Core_DAO_Discount',
+            array(
+              'id' => $discountId,
+            ),
+            array(
+              'entity_id' => $newId,
+              'price_set_id' => $copyPriceSet->id,
+            )
+          );
+        }
+      }
+    }
+  }
 }
 
-- 
GitLab