From d270be1cdf239c896a9aebd3f36526039cf8c672 Mon Sep 17 00:00:00 2001 From: colemanw Date: Tue, 18 Jul 2023 10:46:14 -0400 Subject: [PATCH] ActionMapping - Clean up obtuse methods of fetching a MappingInterface class Changed calls to getMappings to use getMapping if they only needed a single value. In some cases they could be entirely removed because they weren't using the retrieved class for anything. Deprecated the use of 'filters' param in getMappings as it was redundant with simply calling getMapping with an id. --- CRM/Admin/Form/ScheduleReminders.php | 16 +---- CRM/Core/BAO/ActionSchedule.php | 67 ++++++------------- CRM/Event/BAO/Event.php | 13 +--- .../Form/ManageEvent/ScheduleReminders.php | 4 +- CRM/Event/Form/ManageEvent/TabHeader.php | 5 +- CRM/Event/Page/ManageEvent.php | 5 +- 6 files changed, 28 insertions(+), 82 deletions(-) diff --git a/CRM/Admin/Form/ScheduleReminders.php b/CRM/Admin/Form/ScheduleReminders.php index d673912f28..9ea51ea2f2 100644 --- a/CRM/Admin/Form/ScheduleReminders.php +++ b/CRM/Admin/Form/ScheduleReminders.php @@ -78,15 +78,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { } if ($isEvent) { $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->getComponentID(), 'is_template'); - $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([ - 'id' => $isTemplate ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID, - ])); - if ($mapping) { - $this->_mappingID = $mapping->getId(); - } - else { - throw new CRM_Core_Exception('Could not find mapping for event scheduled reminders.'); - } + $this->_mappingID = $isTemplate ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID; } if (!empty($_POST) && !empty($_POST['entity']) && empty($this->getContext())) { @@ -222,12 +214,6 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { $recipientListingOptions = []; - if ($mappingID) { - $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([ - 'id' => $mappingID, - ])); - } - $limitOptions = ['' => '-neither-', 1 => ts('Limit to'), 0 => ts('Also include')]; $recipientLabels = ['activity' => ts('Recipients'), 'other' => ts('Limit or Add Recipients')]; diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 74ded9e794..065c60464e 100644 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -24,41 +24,32 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { /** * @param array $filters - * Filter by property (e.g. 'id'). + * Deprecated get-by-id - use getMapping instead * - * @return array - * Array(scalar $id => Mapping $mapping). - * - * @throws \CRM_Core_Exception + * @return \Civi\ActionSchedule\MappingInterface[] */ - public static function getMappings($filters = NULL) { - static $_action_mapping; - - if ($_action_mapping === NULL) { + public static function getMappings($filters = NULL): array { + if (!isset(Civi::$statics[__CLASS__]['mappings'])) { $event = \Civi::dispatcher() ->dispatch('civi.actionSchedule.getMappings', new MappingRegisterEvent()); - $_action_mapping = $event->getMappings(); - } - - if (empty($filters)) { - return $_action_mapping; + Civi::$statics[__CLASS__]['mappings'] = $event->getMappings(); } if (isset($filters['id'])) { - return [$filters['id'] => $_action_mapping[$filters['id']]]; + CRM_Core_Error::deprecatedWarning('Use "getMapping" to retrieve a single mapping by id instead of passing a filter to "GetMappings".'); + return [$filters['id'] => Civi::$statics[__CLASS__]['mappings'][$filters['id']]]; } - throw new CRM_Core_Exception("getMappings() called with unsupported filter: " . implode(', ', array_keys($filters))); + return Civi::$statics[__CLASS__]['mappings']; } /** - * @param string|int $id + * @param string|int $identifier + * Name of the mapping e.g. 'contribpage' or CRM_Contact_ActionMapping::CONTACT_MAPPING_ID * - * @return \Civi\ActionSchedule\Mapping|NULL - * @throws \CRM_Core_Exception + * @return \Civi\ActionSchedule\MappingInterface|NULL */ - public static function getMapping($id) { - $mappings = self::getMappings(); - return $mappings[$id] ?? NULL; + public static function getMapping($identifier) { + return self::getMappings()[$identifier] ?? NULL; } /** @@ -71,7 +62,6 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { public static function getAllEntityValueLabels() { $entityValueLabels = []; foreach (CRM_Core_BAO_ActionSchedule::getMappings() as $mapping) { - /** @var \Civi\ActionSchedule\Mapping $mapping */ $entityValueLabels[$mapping->getId()] = $mapping->getValueLabels(); $valueLabel = ['- ' . strtolower($mapping->getValueHeader()) . ' -']; $entityValueLabels[$mapping->getId()] = $valueLabel + $entityValueLabels[$mapping->getId()]; @@ -89,7 +79,6 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { $entityValueLabels = self::getAllEntityValueLabels(); $entityStatusLabels = []; foreach (CRM_Core_BAO_ActionSchedule::getMappings() as $mapping) { - /** @var \Civi\ActionSchedule\Mapping $mapping */ $statusLabel = ['- ' . strtolower($mapping->getStatusHeader()) . ' -']; $entityStatusLabels[$mapping->getId()] = $entityValueLabels[$mapping->getId()]; foreach ($entityStatusLabels[$mapping->getId()] as $kkey => & $vval) { @@ -141,10 +130,7 @@ FROM civicrm_action_schedule cas $query .= $where; $dao = CRM_Core_DAO::executeQuery($query, $queryParams); while ($dao->fetch()) { - /** @var Civi\ActionSchedule\Mapping $filterMapping */ - $filterMapping = CRM_Utils_Array::first(self::getMappings([ - 'id' => $dao->mapping_id, - ])); + $filterMapping = self::getMapping($dao->mapping_id); $list[$dao->id]['id'] = $dao->id; $list[$dao->id]['title'] = $dao->title; $list[$dao->id]['start_action_offset'] = $dao->start_action_offset; @@ -233,9 +219,7 @@ FROM civicrm_action_schedule cas * @throws CRM_Core_Exception */ public static function sendMailings($mappingID, $now) { - $mapping = CRM_Utils_Array::first(self::getMappings([ - 'id' => $mappingID, - ])); + $mapping = self::getMapping($mappingID); $actionSchedule = new CRM_Core_DAO_ActionSchedule(); $actionSchedule->mapping_id = $mappingID; @@ -315,9 +299,7 @@ FROM civicrm_action_schedule cas * Build a list of the contacts to send to. * * @param string $mappingID - * Value from the mapping_id field in the civicrm_action_schedule able. It might be a string like - * 'contribpage' for an older class like CRM_Contribute_ActionMapping_ByPage of for ones following - * more recent patterns, an integer. + * Identifier of a concrete implementation of MappingInterface * @param string $now * @param array $params * @@ -334,10 +316,7 @@ FROM civicrm_action_schedule cas $actionSchedule->find(); while ($actionSchedule->fetch()) { - /** @var \Civi\ActionSchedule\Mapping $mapping */ - $mapping = CRM_Utils_Array::first(self::getMappings([ - 'id' => $mappingID, - ])); + $mapping = self::getMapping($mappingID); $builder = new \Civi\ActionSchedule\RecipientBuilder($now, $actionSchedule, $mapping); $builder->build(); } @@ -386,14 +365,10 @@ FROM civicrm_action_schedule cas * @return array */ public static function getRecipientListing($mappingID, $recipientType) { - if (!$mappingID) { + $mapping = CRM_Core_BAO_ActionSchedule::getMapping($mappingID); + if (!$mapping) { return []; } - - /** @var \Civi\ActionSchedule\Mapping $mapping */ - $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([ - 'id' => $mappingID, - ])); return $mapping->getRecipientListing($recipientType); } @@ -433,7 +408,7 @@ FROM civicrm_action_schedule cas * sending the message and pass in the fully rendered text of the message. * * @param object $tokenRow - * @param Civi\ActionSchedule\Mapping $mapping + * @param Civi\ActionSchedule\MappingInterface $mapping * @param int $contactID * @param int $entityID * @param int|null $caseID @@ -625,7 +600,7 @@ FROM civicrm_action_schedule cas /** * @param CRM_Core_DAO_ActionSchedule $schedule - * @param \Civi\ActionSchedule\Mapping $mapping + * @param \Civi\ActionSchedule\MappingInterface $mapping * @return \Civi\Token\TokenProcessor */ protected static function createTokenProcessor($schedule, $mapping) { diff --git a/CRM/Event/BAO/Event.php b/CRM/Event/BAO/Event.php index 884d9cd4b6..3875bc5e04 100644 --- a/CRM/Event/BAO/Event.php +++ b/CRM/Event/BAO/Event.php @@ -449,9 +449,6 @@ $event_summary_limit ]; $params = [1 => [$optionGroupId, 'Integer']]; - $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([ - 'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID, - ])); $dao = CRM_Core_DAO::executeQuery($query, $params); while ($dao->fetch()) { foreach ($properties as $property => $name) { @@ -556,7 +553,7 @@ $event_summary_limit $eventSummary['events'][$dao->id]['is_show_location'] = $dao->is_show_location; $eventSummary['events'][$dao->id]['is_subevent'] = $dao->slot_label_id; $eventSummary['events'][$dao->id]['is_pcp_enabled'] = $dao->is_pcp_enabled; - $eventSummary['events'][$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, $mapping->getId()); + $eventSummary['events'][$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID); $eventSummary['events'][$dao->id]['is_repeating_event'] = $dao->is_repeating_event; $statusTypes = CRM_Event_PseudoConstant::participantStatus(); @@ -997,12 +994,8 @@ WHERE civicrm_event.is_active = 1 ['replace' => ['target_entity_id' => $copyEvent->id]] ); - $oldMapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([ - 'id' => ($eventValues['is_template'] ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID), - ])); - $copyMapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([ - 'id' => ($copyEvent->is_template == 1 ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID), - ])); + $oldMapping = CRM_Core_BAO_ActionSchedule::getMapping($eventValues['is_template'] ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID); + $copyMapping = CRM_Core_BAO_ActionSchedule::getMapping($copyEvent->is_template == 1 ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID); CRM_Core_DAO::copyGeneric('CRM_Core_DAO_ActionSchedule', ['entity_value' => $id, 'mapping_id' => $oldMapping->getId()], ['entity_value' => $copyEvent->id, 'mapping_id' => $copyMapping->getId()] diff --git a/CRM/Event/Form/ManageEvent/ScheduleReminders.php b/CRM/Event/Form/ManageEvent/ScheduleReminders.php index 7e1144afc2..1af6314f12 100644 --- a/CRM/Event/Form/ManageEvent/ScheduleReminders.php +++ b/CRM/Event/Form/ManageEvent/ScheduleReminders.php @@ -32,9 +32,7 @@ class CRM_Event_Form_ManageEvent_ScheduleReminders extends CRM_Event_Form_Manage $this->setSelectedChild('reminder'); $setTab = CRM_Utils_Request::retrieve('setTab', 'Int', $this, FALSE, 0); - $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([ - 'id' => ($this->_isTemplate ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID), - ])); + $mapping = CRM_Core_BAO_ActionSchedule::getMapping($this->_isTemplate ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID); $reminderList = CRM_Core_BAO_ActionSchedule::getList($mapping, $this->_id); // Add action links to each of the reminders foreach ($reminderList as & $format) { diff --git a/CRM/Event/Form/ManageEvent/TabHeader.php b/CRM/Event/Form/ManageEvent/TabHeader.php index d940c5951f..af32097768 100644 --- a/CRM/Event/Form/ManageEvent/TabHeader.php +++ b/CRM/Event/Form/ManageEvent/TabHeader.php @@ -95,9 +95,6 @@ class CRM_Event_Form_ManageEvent_TabHeader { $eventID = $form->getVar('_id'); if ($eventID) { // disable tabs based on their configuration status - $eventNameMapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([ - 'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID, - ])); $sql = " SELECT e.loc_block_id as is_location, e.is_online_registration, e.is_monetary, taf.is_active, pcp.is_active as is_pcp, sch.id as is_reminder, re.id as is_repeating_event FROM civicrm_event e @@ -111,7 +108,7 @@ WHERE e.id = %1 CRM_Core_BAO_RecurringEntity::getParentFor($eventID, 'civicrm_event'); $params = [ 1 => [$eventID, 'Integer'], - 2 => [$eventNameMapping->getId(), 'Integer'], + 2 => [CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID, 'Integer'], ]; $dao = CRM_Core_DAO::executeQuery($sql, $params); if (!$dao->fetch()) { diff --git a/CRM/Event/Page/ManageEvent.php b/CRM/Event/Page/ManageEvent.php index 4a91db3640..1561f9f390 100644 --- a/CRM/Event/Page/ManageEvent.php +++ b/CRM/Event/Page/ManageEvent.php @@ -344,9 +344,6 @@ ORDER BY start_date desc while ($pcpDao->fetch()) { $eventPCPS[$pcpDao->entity_id] = $pcpDao->entity_id; } - $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([ - 'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID, - ])); $eventType = CRM_Core_OptionGroup::values('event_type'); while ($dao->fetch()) { if (in_array($dao->id, $permittedEventsByAction[CRM_Core_Permission::VIEW])) { @@ -425,7 +422,7 @@ ORDER BY start_date desc //show campaigns on selector. $manageEvent[$dao->id]['campaign'] = $allCampaigns[$dao->campaign_id] ?? NULL; - $manageEvent[$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, $mapping->getId()); + $manageEvent[$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID); $manageEvent[$dao->id]['is_pcp_enabled'] = $eventPCPS[$dao->id] ?? NULL; $manageEvent[$dao->id]['event_type'] = $eventType[$manageEvent[$dao->id]['event_type_id']] ?? NULL; $manageEvent[$dao->id]['is_repeating_event'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_RecurringEntity', $dao->id, 'parent_id', 'entity_id'); -- 2.25.1