Skip to content

Commit

Permalink
Trigger event.create post hook on event copy. Simplify copy function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Oct 29, 2018
1 parent 5da52e7 commit ce57ce1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 37 deletions.
51 changes: 17 additions & 34 deletions CRM/Event/BAO/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -920,13 +920,11 @@ public static function &getCompleteInfo(
* @param int $id
* The event id to copy.
* boolean $afterCreate call to copy after the create function
* @param null $newEvent
* @param bool $afterCreate
*
* @return CRM_Event_DAO_Event
* @throws \CRM_Core_Exception
*/
public static function copy($id, $newEvent = NULL, $afterCreate = FALSE) {

public static function copy($id) {
$eventValues = array();

//get the require event values.
Expand All @@ -941,46 +939,35 @@ public static function copy($id, $newEvent = NULL, $afterCreate = FALSE) {

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') . ' '));
$fieldsFix = 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
);
}
$copyEvent = CRM_Core_DAO::copyGeneric('CRM_Event_DAO_Event',
array('id' => $id),
// since the location is sharable, lets use the same loc_block_id.
array('loc_block_id' => CRM_Utils_Array::value('loc_block_id', $eventValues)),
$fieldsFix
);
CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_event', $id, $copyEvent->id);
$copyUF = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin',
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',
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',
CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock',
array(
'entity_id' => $id,
'entity_table' => 'civicrm_event',
Expand All @@ -995,22 +982,18 @@ public static function copy($id, $newEvent = NULL, $afterCreate = FALSE) {
$copyMapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array(
'id' => ($copyEvent->is_template == 1 ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID),
)));
$copyReminder = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_ActionSchedule',
CRM_Core_DAO::copyGeneric('CRM_Core_DAO_ActionSchedule',
array('entity_value' => $id, 'mapping_id' => $oldMapping->getId()),
array('entity_value' => $copyEvent->id, 'mapping_id' => $copyMapping->getId())
);

if (!$afterCreate) {
// CRM-19302
self::copyCustomFields($id, $copyEvent->id);
}
self::copyCustomFields($id, $copyEvent->id);

$copyEvent->save();

CRM_Utils_System::flushCache();
if (!$afterCreate) {
CRM_Utils_Hook::copy('Event', $copyEvent);
}
CRM_Utils_Hook::copy('Event', $copyEvent);
CRM_Utils_Hook::post('create', 'Event', $copyEvent->id, $copyEvent);

return $copyEvent;
}

Expand Down
7 changes: 4 additions & 3 deletions CRM/Event/Form/ManageEvent/EventInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,12 @@ public function postProcess() {
$params = array_merge(CRM_Event_BAO_Event::getTemplateDefaultValues($params['template_id']), $params);
}

$event = CRM_Event_BAO_Event::create($params);

// now that we have the event’s id, do some more template-based stuff
if (!empty($params['template_id'])) {
CRM_Event_BAO_Event::copy($params['template_id'], $event, TRUE);
$event = CRM_Event_BAO_Event::copy($params['template_id']);
}
else {
$event = CRM_Event_BAO_Event::create($params);
}

$this->set('id', $event->id);
Expand Down
1 change: 1 addition & 0 deletions CRM/Event/Page/ManageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ public function browse() {
* all the fields in the event wizard
*
* @return void
* @throws \CRM_Core_Exception
*/
public function copy() {
$id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE, 0, 'GET');
Expand Down

0 comments on commit ce57ce1

Please sign in to comment.