/** * This function is to make a copy of a Event, including * all the fields in the event Wizard * * @param int $id the event id to copy * obj $newEvent object of CRM_Event_DAO_Event * boolean $afterCreate call to copy after the create function * @param null $newEvent * @param bool $afterCreate * * @return void * @access public */ static function copy($id, $newEvent = NULL, $afterCreate = FALSE) { $defaults = $eventValues = array(); //get the require event values. $eventParams = array('id' => $id); $returnProperties = array('loc_block_id', 'is_show_location', 'default_fee_id', 'default_discount_fee_id', 'is_template'); CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event', $eventParams, $eventValues, $returnProperties); // since the location is sharable, lets use the same loc_block_id. $locBlockId = CRM_Utils_Array::value('loc_block_id', $eventValues); $fieldsFix = $afterCreate ? array() : array('prefix' => array('title' => ts('Copy of') . ' ')); if (empty($eventValues['is_show_location'])) { $fieldsFix['prefix']['is_show_location'] = 0; } if ($newEvent && is_a($newEvent, 'CRM_Event_DAO_Event')) { $copyEvent = $newEvent; } if (!isset($copyEvent)) { $copyEvent =& CRM_Core_DAO::copyGeneric('CRM_Event_DAO_Event', array('id' => $id), array('loc_block_id' => $locBlockId ? $locBlockId : NULL), $fieldsFix); } CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_event', $id, $copyEvent->id); $copyUF =& CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', array('entity_id' => $id, 'entity_table' => 'civicrm_event'), array('entity_id' => $copyEvent->id)); $copyTellFriend =& CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', array('entity_id' => $id, 'entity_table' => 'civicrm_event'), array('entity_id' => $copyEvent->id)); $copyPCP =& CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', array('entity_id' => $id, 'entity_table' => 'civicrm_event'), array('entity_id' => $copyEvent->id), array('replace' => array('target_entity_id' => $copyEvent->id))); if ($eventValues['is_template']) { $field = 'event_template'; } else { $field = 'civicrm_event'; } $mappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value'); $oldData = array('entity_value' => $id, 'mapping_id' => $mappingId); if ($copyEvent->is_template == 1) { $field = 'event_template'; } else { $field = 'civicrm_event'; } $copyMappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value'); $newData = array('entity_value' => $copyEvent->id, 'mapping_id' => $copyMappingId); $copyReminder =& CRM_Core_DAO::copyGeneric('CRM_Core_DAO_ActionSchedule', $oldData, $newData); if (!$afterCreate) { //copy custom data $extends = array('event'); $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends); if ($groupTree) { foreach ($groupTree as $groupID => $group) { $table[$groupTree[$groupID]['table_name']] = array('entity_id'); foreach ($group['fields'] as $fieldID => $field) { $table[$groupTree[$groupID]['table_name']][] = $groupTree[$groupID]['fields'][$fieldID]['column_name']; } } foreach ($table as $tableName => $tableColumns) { $insert = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $tableColumns) . ') '; $tableColumns[0] = $copyEvent->id; $select = 'SELECT ' . implode(', ', $tableColumns); $from = ' FROM ' . $tableName; $where = " WHERE {$tableName}.entity_id = {$id}"; $query = $insert . $select . $from . $where; $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); } } } $copyEvent->save(); CRM_Utils_System::flushCache(); if (!$afterCreate) { CRM_Utils_Hook::copy('Event', $copyEvent); } return $copyEvent; }
/** * make a copy of a contribution page, including * all the blocks in the page * * @param int $id * The contribution page id to copy. * * @return CRM_Contribute_DAO_ContributionPage */ public static function copy($id) { $fieldsFix = array('prefix' => array('title' => ts('Copy of') . ' ')); $copy =& CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', array('id' => $id), NULL, $fieldsFix); //copying all the blocks pertaining to the contribution page $copyPledgeBlock =& CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', array('entity_id' => $id, 'entity_table' => 'civicrm_contribution_page'), array('entity_id' => $copy->id)); $copyMembershipBlock =& CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', array('entity_id' => $id, 'entity_table' => 'civicrm_contribution_page'), array('entity_id' => $copy->id)); $copyUFJoin =& CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', array('entity_id' => $id, 'entity_table' => 'civicrm_contribution_page'), array('entity_id' => $copy->id)); $copyWidget =& CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', array('contribution_page_id' => $id), array('contribution_page_id' => $copy->id)); //copy price sets CRM_Price_BAO_PriceSet::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'), array('entity_id' => $copy->id)); $copyPersonalCampaignPages =& CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', array('entity_id' => $id, 'entity_table' => 'civicrm_contribution_page'), array('entity_id' => $copy->id, 'target_entity_id' => $copy->id)); $copyPremium =& CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', array('entity_id' => $id, 'entity_table' => 'civicrm_contribution_page'), array('entity_id' => $copy->id)); $premiumQuery = "\nSELECT id\nFROM civicrm_premiums\nWHERE entity_table = 'civicrm_contribution_page'\n AND entity_id ={$id}"; $premiumDao = CRM_Core_DAO::executeQuery($premiumQuery, CRM_Core_DAO::$_nullArray); while ($premiumDao->fetch()) { if ($premiumDao->id) { $copyPremiumProduct =& CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_PremiumsProduct', array('premiums_id' => $premiumDao->id), array('premiums_id' => $copyPremium->id)); } } $copy->save(); CRM_Utils_Hook::copy('ContributionPage', $copy); return $copy; }