Skip to content

Commit

Permalink
[ref] Move copyCustomFields function from Event to Core_DAO for re-us…
Browse files Browse the repository at this point in the history
…ablibilty
  • Loading branch information
eileenmcnaughton committed May 1, 2019
1 parent 0908f97 commit 7ce3f19
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 51 deletions.
51 changes: 51 additions & 0 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,57 @@ public static function &copyGeneric($daoName, $criteria, $newData = NULL, $field
return $newObject;
}

/**
* Method that copies custom fields values from an old event to a new one. Fixes bug CRM-19302,
* where if a custom field of File type was present, left both events using the same file,
* breaking download URL's for the old event.
*
* @param int $oldEventID
* @param int $newCopyID
*/
public static function copyCustomFields($oldEventID, $newCopyID) {
// Obtain custom values for old event
$customParams = $htmlType = [];
$customValues = CRM_Core_BAO_CustomValueTable::getEntityValues($oldEventID, 'Event');

// If custom values present, we copy them
if (!empty($customValues)) {
// Get Field ID's and identify File type attributes, to handle file copying.
$fieldIds = implode(', ', array_keys($customValues));
$sql = "SELECT id FROM civicrm_custom_field WHERE html_type = 'File' AND id IN ( {$fieldIds} )";
$result = CRM_Core_DAO::executeQuery($sql);

// Build array of File type fields
while ($result->fetch()) {
$htmlType[] = $result->id;
}

// Build params array of custom values
foreach ($customValues as $field => $value) {
if ($value !== NULL) {
// Handle File type attributes
if (in_array($field, $htmlType)) {
$fileValues = CRM_Core_BAO_File::path($value, $oldEventID);
$customParams["custom_{$field}_-1"] = [
'name' => CRM_Utils_File::duplicate($fileValues[0]),
'type' => $fileValues[1],
];
}
// Handle other types
else {
$customParams["custom_{$field}_-1"] = $value;
}
}
}

// Save Custom Fields for new Event
CRM_Core_BAO_CustomValueTable::postProcess($customParams, 'civicrm_event', $newCopyID, 'Event');
}

// copy activity attachments ( if any )
CRM_Core_BAO_File::copyEntityFile('civicrm_event', $oldEventID, 'civicrm_event', $newCopyID);
}

/**
* Cascade update through related entities.
*
Expand Down
51 changes: 0 additions & 51 deletions CRM/Event/BAO/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -996,57 +996,6 @@ public static function copy($id, $params = []) {
return $copyEvent;
}

/**
* Method that copies custom fields values from an old event to a new one. Fixes bug CRM-19302,
* where if a custom field of File type was present, left both events using the same file,
* breaking download URL's for the old event.
*
* @param int $oldEventID
* @param int $newCopyID
*/
public static function copyCustomFields($oldEventID, $newCopyID) {
// Obtain custom values for old event
$customParams = $htmlType = [];
$customValues = CRM_Core_BAO_CustomValueTable::getEntityValues($oldEventID, 'Event');

// If custom values present, we copy them
if (!empty($customValues)) {
// Get Field ID's and identify File type attributes, to handle file copying.
$fieldIds = implode(', ', array_keys($customValues));
$sql = "SELECT id FROM civicrm_custom_field WHERE html_type = 'File' AND id IN ( {$fieldIds} )";
$result = CRM_Core_DAO::executeQuery($sql);

// Build array of File type fields
while ($result->fetch()) {
$htmlType[] = $result->id;
}

// Build params array of custom values
foreach ($customValues as $field => $value) {
if ($value !== NULL) {
// Handle File type attributes
if (in_array($field, $htmlType)) {
$fileValues = CRM_Core_BAO_File::path($value, $oldEventID);
$customParams["custom_{$field}_-1"] = [
'name' => CRM_Utils_File::duplicate($fileValues[0]),
'type' => $fileValues[1],
];
}
// Handle other types
else {
$customParams["custom_{$field}_-1"] = $value;
}
}
}

// Save Custom Fields for new Event
CRM_Core_BAO_CustomValueTable::postProcess($customParams, 'civicrm_event', $newCopyID, 'Event');
}

// copy activity attachments ( if any )
CRM_Core_BAO_File::copyEntityFile('civicrm_event', $oldEventID, 'civicrm_event', $newCopyID);
}

/**
* This is sometimes called in a loop (during event search).
*
Expand Down

0 comments on commit 7ce3f19

Please sign in to comment.