From ae70f47e19ca1ea26dcb8495dce57fd6a7062509 Mon Sep 17 00:00:00 2001 From: "Matthew Wire (MJW Consulting)" Date: Tue, 23 Oct 2018 21:01:57 +0100 Subject: [PATCH] Trigger event.create post hook on event copy. Simplify copy function --- CRM/Core/DAO.php | 2 + CRM/Event/BAO/Event.php | 50 ++++++++---------------- CRM/Event/Form/ManageEvent/EventInfo.php | 7 ++-- CRM/Event/Page/ManageEvent.php | 1 + CRM/Export/Form/Select.php | 1 + 5 files changed, 24 insertions(+), 37 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index c6a495b61db5..79a69570e31c 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -1670,7 +1670,9 @@ public static function ©Generic($daoName, $criteria, $newData = NULL, $field } } $newObject->save(); + CRM_Utils_Hook::post('create', CRM_Core_DAO_AllCoreTables::getBriefName($daoName), $newObject->id, $newObject); } + return $newObject; } diff --git a/CRM/Event/BAO/Event.php b/CRM/Event/BAO/Event.php index 2e6c5e0bd70e..78155fbc7390 100644 --- a/CRM/Event/BAO/Event.php +++ b/CRM/Event/BAO/Event.php @@ -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. @@ -941,30 +939,19 @@ 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', @@ -972,7 +959,7 @@ public static function copy($id, $newEvent = NULL, $afterCreate = FALSE) { 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', @@ -980,7 +967,7 @@ public static function copy($id, $newEvent = NULL, $afterCreate = FALSE) { 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', @@ -995,22 +982,17 @@ 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); + return $copyEvent; } diff --git a/CRM/Event/Form/ManageEvent/EventInfo.php b/CRM/Event/Form/ManageEvent/EventInfo.php index ebc68876f464..75929b13681b 100644 --- a/CRM/Event/Form/ManageEvent/EventInfo.php +++ b/CRM/Event/Form/ManageEvent/EventInfo.php @@ -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); diff --git a/CRM/Event/Page/ManageEvent.php b/CRM/Event/Page/ManageEvent.php index f9673ff04b53..dd435068bede 100644 --- a/CRM/Event/Page/ManageEvent.php +++ b/CRM/Event/Page/ManageEvent.php @@ -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'); diff --git a/CRM/Export/Form/Select.php b/CRM/Export/Form/Select.php index af478d4adfad..d26fdeaa6faa 100644 --- a/CRM/Export/Form/Select.php +++ b/CRM/Export/Form/Select.php @@ -98,6 +98,7 @@ public function preProcess() { $components = array('Contact', 'Contribute', 'Member', 'Event', 'Pledge', 'Case', 'Grant', 'Activity'); // FIXME: This should use a modified version of CRM_Contact_Form_Search::getModeValue but it doesn't have all the contexts + // FIXME: Or better still, use CRM_Core_DAO_AllCoreTables::getBriefName($daoName) to get the $entityShortName switch ($this->getQueryMode()) { case CRM_Contact_BAO_Query::MODE_CONTRIBUTE: $entityShortname = 'Contribute';