From 50a2375554b2a33cacebbe3822ff3c84f60fb9e9 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 27 Jul 2015 00:24:52 -0700 Subject: [PATCH] CRM-13244 - ActionSchedule - Extract entity-specific logic for tokens and dropdowns. The `ActionSchedule` BAO had a ton of duplicate, helter-skelter code for looking up specific details about specific entities and mappings. The mapping class (`CRM_Core_DAO_ActionMapping`) had an anemic model. The mapping records could be modified in the DB, but in practice this was pointless because they depended on so many hard-coded lookups. And there were some confusing variable names (sel1, sel2, sel3, etc) and variable-shuffling. This revision moves several entity-specific bits into new classes (`Civi\ActionSchedule\Mapping` and `CRM_Core_ActionScheduleTmp`), abandons the pretense of DB editing, centralizes mapping-related lookups, and replaces various references to `CRM_Core_DAO_ActionMapping`. This is envisioned as an intermediate step; the two classes are still a bit heavy (mixing together logic for unrelated entities) and should be split further. --- CRM/Admin/Form/ScheduleReminders.php | 21 +- CRM/Core/ActionScheduleTmp.php | 171 ++++++ CRM/Core/BAO/ActionSchedule.php | 566 ++++++-------------- CRM/Event/BAO/Event.php | 32 +- CRM/Event/Form/ManageEvent/TabHeader.php | 11 +- CRM/Event/Page/ManageEvent.php | 6 +- Civi/ActionSchedule/Mapping.php | 210 ++++++++ Civi/Core/Container.php | 5 + tests/phpunit/api/v3/ActionScheduleTest.php | 12 +- 9 files changed, 598 insertions(+), 436 deletions(-) create mode 100644 CRM/Core/ActionScheduleTmp.php create mode 100644 Civi/ActionSchedule/Mapping.php diff --git a/CRM/Admin/Form/ScheduleReminders.php b/CRM/Admin/Form/ScheduleReminders.php index 10412f4433..8d59efb64c 100644 --- a/CRM/Admin/Form/ScheduleReminders.php +++ b/CRM/Admin/Form/ScheduleReminders.php @@ -87,8 +87,13 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { if ($isTemplate) { $field = 'event_template'; } - $this->_mappingID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value'); - if (!$this->_mappingID) { + $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array( + 'entity_value' => $field, + ))); + if ($mapping) { + $this->_mappingID = $mapping->id; + } + else { CRM_Core_Error::fatal('Could not find mapping for event scheduled reminders.'); } } @@ -154,7 +159,9 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { $this->_freqUnits = CRM_Core_SelectValues::getRecurringFrequencyUnits(); //pass the mapping ID in UPDATE mode - $mappings = CRM_Core_BAO_ActionSchedule::getMapping($mappingID); + $mappings = CRM_Core_BAO_ActionSchedule::getMappings(array( + 'id' => $mappingID, + )); $numericOptions = CRM_Core_SelectValues::getNumericOptions(0, 30); @@ -216,10 +223,10 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { $recipientListingOptions = array(); if ($mappingID) { - $recipient = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', - $mappingID, - 'entity_recipient' - ); + $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array( + 'id' => $mappingID, + ))); + $recipient = $mapping->entity_recipient; } $limitOptions = array('' => '-neither-', 1 => ts('Limit to'), 0 => ts('Also include')); diff --git a/CRM/Core/ActionScheduleTmp.php b/CRM/Core/ActionScheduleTmp.php new file mode 100644 index 0000000000..6f4489df79 --- /dev/null +++ b/CRM/Core/ActionScheduleTmp.php @@ -0,0 +1,171 @@ + 'onRegister', + \Civi\Token\Events::TOKEN_EVALUATE => 'onEvaluate', + ); + } + + /** + * @param \Civi\ActionSchedule\Mapping $mapping + * @return array + * Ex: $result['event']['start_date'] === 'Event Start Date'. + */ + protected static function listMailingTokens($mapping) { + $tokenEntity = NULL; + + $tokens = array( + 'civicrm_activity' => array( + 'activity' => array( + 'activity_id' => ts('Activity ID'), + 'activity_type' => ts('Activity Type'), + 'subject' => ts('Activity Subject'), + 'details' => ts('Activity Details'), + 'activity_date_time' => ts('Activity Date-Time'), + ), + ), + 'civicrm_participant' => array( + 'event' => array( + 'event_type' => ts('Event Type'), + 'title' => ts('Event Title'), + 'event_id' => ts('Event ID'), + 'start_date' => ts('Event Start Date'), + 'end_date' => ts('Event End Date'), + 'summary' => ts('Event Summary'), + 'description' => ts('Event Description'), + 'location' => ts('Event Location'), + 'info_url' => ts('Event Info URL'), + 'registration_url' => ts('Event Registration URL'), + 'fee_amount' => ts('Event Fee'), + 'contact_email' => ts('Event Contact (Email)'), + 'contact_phone' => ts('Event Contact (Phone)'), + 'balance' => ts('Event Balance'), + ), + ), + 'civicrm_membership' => array( + 'membership' => array( + 'fee' => ts('Membership Fee'), + 'id' => ts('Membership ID'), + 'join_date' => ts('Membership Join Date'), + 'start_date' => ts('Membership Start Date'), + 'end_date' => ts('Membership End Date'), + 'status' => ts('Membership Status'), + 'type' => ts('Membership Type'), + ), + ), + //'civicrm_contact' => array( + // 'contact' => array( + // 'birth_date' => ts('Birth Date'), + // 'last_name' => ts('Last Name'), + // ) + //), + ); + + return isset($tokens[$mapping->entity]) ? $tokens[$mapping->entity] : array(); + } + + /** + * Declare available tokens. + * + * @param TokenRegisterEvent $e + */ + public function onRegister(TokenRegisterEvent $e) { + if (!isset($e->getTokenProcessor()->context['actionMapping'])) { + return; + } + + $allTokenFields = self::listMailingTokens($e->getTokenProcessor()->context['actionMapping']); + foreach ($allTokenFields as $tokenEntity => $tokenFields) { + foreach ($tokenFields as $field => $label) { + $e->register($field, $label); + } + } + } + + /** + * Load token data. + * + * @param TokenValueEvent $e + * @throws TokenException + */ + public function onEvaluate(TokenValueEvent $e) { + foreach ($e->getRows() as $row) { + /** @var \Civi\Token\TokenRow $row */ + if (!isset($row->context['actionSearchResult'])) { + continue; + } + $row->tokens(self::prepareMailingTokens($row->context['actionMapping'], $row->context['actionSearchResult'])); + } + } + + /** + * @param \Civi\ActionSchedule\Mapping $mapping + * @param $dao + * @return array + * Ex: array('activity' => array('subject' => 'Hello world)). + */ + protected static function prepareMailingTokens($mapping, $dao) { + $allTokenFields = self::listMailingTokens($mapping); + + $entityTokenParams = array(); + foreach ($allTokenFields as $tokenEntity => $tokenFields) { + foreach ($tokenFields as $field => $fieldLabel) { + if ($field == 'location') { + $loc = array(); + $stateProvince = \CRM_Core_PseudoConstant::stateProvince(); + $loc['street_address'] = $dao->street_address; + $loc['city'] = $dao->city; + $loc['state_province'] = \CRM_Utils_Array::value($dao->state_province_id, $stateProvince); + $loc['postal_code'] = $dao->postal_code; + $entityTokenParams[$tokenEntity][$field] = \CRM_Utils_Address::format($loc); + } + elseif ($field == 'info_url') { + $entityTokenParams[$tokenEntity][$field] = \CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $dao->event_id, TRUE, NULL, FALSE); + } + elseif ($field == 'registration_url') { + $entityTokenParams[$tokenEntity][$field] = \CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $dao->event_id, TRUE, NULL, FALSE); + } + elseif (in_array($field, array('start_date', 'end_date', 'join_date', 'activity_date_time'))) { + $entityTokenParams[$tokenEntity][$field] = \CRM_Utils_Date::customFormat($dao->$field); + } + elseif ($field == 'balance') { + if ($dao->entityTable == 'civicrm_contact') { + $balancePay = 'N/A'; + } + elseif (!empty($dao->entityID)) { + $info = \CRM_Contribute_BAO_Contribution::getPaymentInfo($dao->entityID, 'event'); + $balancePay = \CRM_Utils_Array::value('balance', $info); + $balancePay = \CRM_Utils_Money::format($balancePay); + } + $entityTokenParams[$tokenEntity][$field] = $balancePay; + } + elseif ($field == 'fee_amount') { + $entityTokenParams[$tokenEntity][$field] = CRM_Utils_Money::format($dao->$field); + } + else { + $entityTokenParams[$tokenEntity][$field] = $dao->$field; + } + } + } + return $entityTokenParams; + } + +} diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 701145ad43..2d92f083a8 100755 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -39,50 +39,104 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { /** - * @param int $id - * + * @param array $filters + * Filter by property (e.g. 'id', 'entity_value'). * @return array + * Array(scalar $id => Mapping $mapping). */ - public static function getMapping($id = NULL) { + public static function getMappings($filters = NULL) { static $_action_mapping; - if ($id && !is_null($_action_mapping) && isset($_action_mapping[$id])) { - return $_action_mapping[$id]; + if ($_action_mapping === NULL) { + $_action_mapping = array( + 1 => \Civi\ActionSchedule\Mapping::create(array( + 'id' => 1, + 'entity' => 'civicrm_activity', + 'entity_label' => ts('Activity'), + 'entity_value' => 'activity_type', + 'entity_value_label' => 'Activity Type', + 'entity_status' => 'activity_status', + 'entity_status_label' => 'Activity Status', + 'entity_date_start' => 'activity_date_time', + 'entity_recipient' => 'activity_contacts', + )), + 2 => \Civi\ActionSchedule\Mapping::create(array( + 'id' => 2, + 'entity' => 'civicrm_participant', + 'entity_label' => ts('Event Type'), + 'entity_value' => 'event_type', + 'entity_value_label' => 'Event Type', + 'entity_status' => 'civicrm_participant_status_type', + 'entity_status_label' => 'Participant Status', + 'entity_date_start' => 'event_start_date', + 'entity_date_end' => 'event_end_date', + 'entity_recipient' => 'event_contacts', + )), + 3 => \Civi\ActionSchedule\Mapping::create(array( + 'id' => 3, + 'entity' => 'civicrm_participant', + 'entity_label' => ts('Event Name'), + 'entity_value' => 'civicrm_event', + 'entity_value_label' => 'Event Name', + 'entity_status' => 'civicrm_participant_status_type', + 'entity_status_label' => 'Participant Status', + 'entity_date_start' => 'event_start_date', + 'entity_date_end' => 'event_end_date', + 'entity_recipient' => 'event_contacts', + )), + 4 => \Civi\ActionSchedule\Mapping::create(array( + 'id' => 4, + 'entity' => 'civicrm_membership', + 'entity_label' => ts('Membership'), + 'entity_value' => 'civicrm_membership_type', + 'entity_value_label' => 'Membership Type', + 'entity_status' => 'auto_renew_options', + 'entity_status_label' => 'Auto Renew Options', + 'entity_date_start' => 'membership_join_date', + 'entity_date_end' => 'membership_end_date', + )), + 5 => \Civi\ActionSchedule\Mapping::create(array( + 'id' => 5, + 'entity' => 'civicrm_participant', + 'entity_label' => ts('Event Template'), + 'entity_value' => 'event_template', + 'entity_value_label' => 'Event Template', + 'entity_status' => 'civicrm_participant_status_type', + 'entity_status_label' => 'Participant Status', + 'entity_date_start' => 'event_start_date', + 'entity_date_end' => 'event_end_date', + 'entity_recipient' => 'event_contacts', + )), + 6 => \Civi\ActionSchedule\Mapping::create(array( + 'id' => 6, + 'entity' => 'civicrm_contact', + 'entity_label' => ts('Contact'), + 'entity_value' => 'civicrm_contact', + 'entity_value_label' => 'Date Field', + 'entity_status' => 'contact_date_reminder_options', + 'entity_status_label' => 'Annual Options', + 'entity_date_start' => 'date_field', + )), + ); } - $dao = new CRM_Core_DAO_ActionMapping(); - if ($id) { - $dao->id = $id; + if ($filters === NULL) { + return $_action_mapping; } - $dao->find(); - $mapping = array(); - while ($dao->fetch()) { - $defaults = array(); - CRM_Core_DAO::storeValues($dao, $defaults); - $mapping[$dao->id] = $defaults; - } - $_action_mapping = $mapping; - - return $mapping; - } - - /** - * Get all fields of the type Date. - */ - public static function getDateFields() { - $allFields = CRM_Core_BAO_CustomField::getFields(''); - $dateFields = array( - 'birth_date' => ts('Birth Date'), - 'created_date' => ts('Created Date'), - 'modified_date' => ts('Modified Date'), - ); - foreach ($allFields as $fieldID => $field) { - if ($field['data_type'] == 'Date') { - $dateFields["custom_$fieldID"] = $field['label']; + $result = array(); + foreach ($_action_mapping as $mappingId => $mapping) { + $match = TRUE; + foreach ($filters as $filterField => $filterValue) { + if ($mapping->{$filterField} != $filterValue) { + $match = FALSE; + } + } + if ($match) { + $result[$mappingId] = $mapping; } } - return $dateFields; + return $result; } /** @@ -95,227 +149,71 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { * associated array of all the drop downs in the form */ public static function getSelection($id = NULL) { - $mapping = CRM_Core_BAO_ActionSchedule::getMapping(); - $activityStatus = CRM_Core_PseudoConstant::activityStatus(); - $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE); - - $participantStatus = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'); - $event = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); - $eventType = CRM_Event_PseudoConstant::eventType(); - $eventTemplate = CRM_Event_PseudoConstant::eventTemplates(); - $autoRenew = CRM_Core_OptionGroup::values('auto_renew_options'); - $membershipType = CRM_Member_PseudoConstant::membershipType(); - $dateFieldParams = array('data_type' => 'Date'); - $dateFields = CRM_Core_BAO_ActionSchedule::getDateFields(); - $contactOptions = CRM_Core_OptionGroup::values('contact_date_reminder_options'); - - asort($activityType); - - $sel1 = $sel2 = $sel3 = $sel4 = $sel5 = array(); - $options = array( - 'manual' => ts('Choose Recipient(s)'), - 'group' => ts('Select Group'), - ); + $mappings = CRM_Core_BAO_ActionSchedule::getMappings(); - $entityMapping = array(); - $recipientMapping = array_combine(array_keys($options), array_keys($options)); + $entityValueLabels = $entityStatusLabels = $dateFieldLabels = array(); + $entityRecipientLabels = $entityRecipientNames = array(); if (!$id) { $id = 1; } - foreach ($mapping as $value) { - $entityValue = CRM_Utils_Array::value('entity_value', $value); - $entityStatus = CRM_Utils_Array::value('entity_status', $value); - $entityRecipient = CRM_Utils_Array::value('entity_recipient', $value); - $valueLabel = array('- ' . strtolower(CRM_Utils_Array::value('entity_value_label', $value)) . ' -'); - $key = CRM_Utils_Array::value('id', $value); - $entityMapping[$key] = CRM_Utils_Array::value('entity', $value); - - $sel1Val = NULL; - switch ($entityValue) { - case 'activity_type': - if ($value['entity'] == 'civicrm_activity') { - $sel1Val = ts('Activity'); - } - $sel2[$key] = $valueLabel + $activityType; - break; - - case 'event_type': - if ($value['entity'] == 'civicrm_participant') { - $sel1Val = ts('Event Type'); - } - $sel2[$key] = $valueLabel + $eventType; - break; - - case 'event_template': - if ($value['entity'] == 'civicrm_participant') { - $sel1Val = ts('Event Template'); - } - $sel2[$key] = $valueLabel + $eventTemplate; - break; - - case 'civicrm_event': - if ($value['entity'] == 'civicrm_participant') { - $sel1Val = ts('Event Name'); - } - $sel2[$key] = $valueLabel + $event; - break; - - case 'civicrm_membership_type': - if ($value['entity'] == 'civicrm_membership') { - $sel1Val = ts('Membership'); - } - $sel2[$key] = $valueLabel + $membershipType; - break; + foreach ($mappings as $mapping) { + /** @var \Civi\ActionSchedule\Mapping $mapping */ - case 'civicrm_contact': - if ($value['entity'] == 'civicrm_contact') { - $sel1Val = ts('Contact'); - } - $sel2[$key] = $dateFields; - break; + $entityValueLabels[$mapping->id] = $mapping->getValueLabels(); + // Not sure why: everything *except* contact-dates have a $valueLabel. + if ($mapping->entity_value !== 'civicrm_contact') { + $valueLabel = array('- ' . strtolower($mapping->entity_value_label) . ' -'); + $entityValueLabels[$mapping->id] = $valueLabel + $entityValueLabels[$mapping->id]; } - $sel1[$key] = $sel1Val; - - if ($key == $id) { - if ($startDate = CRM_Utils_Array::value('entity_date_start', $value)) { - $sel4[$startDate] = ucwords(str_replace('_', ' ', $startDate)); - } - if ($endDate = CRM_Utils_Array::value('entity_date_end', $value)) { - $sel4[$endDate] = ucwords(str_replace('_', ' ', $endDate)); - } - switch ($entityRecipient) { - case 'activity_contacts': - $activityContacts = CRM_Core_OptionGroup::values('activity_contacts'); - $sel5[$entityRecipient] = $activityContacts + $options; - $recipientMapping += CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); - break; - - case 'event_contacts': - $eventContacts = CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name'); - $sel5[$entityRecipient] = $eventContacts + $options; - $recipientMapping += CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, 'name', TRUE, FALSE, 'name'); - break; - - case NULL: - $sel5[$entityRecipient] = $options; - break; - } + if ($mapping->id == $id) { + $dateFieldLabels = $mapping->getDateFields(); + $entityRecipientLabels = array($mapping->entity_recipient => $mapping->getRecipientTypes()); + $entityRecipientNames = array_combine(array_keys($entityRecipientLabels[$mapping->entity_recipient]), array_keys($entityRecipientLabels[$mapping->entity_recipient])); } - } - $sel3 = $sel2; - - foreach ($mapping as $value) { - $entityStatus = CRM_Utils_Array::value('entity_status', $value); - $statusLabel = array('- ' . strtolower(CRM_Utils_Array::value('entity_status_label', $value)) . ' -'); - $id = CRM_Utils_Array::value('id', $value); - - switch ($entityStatus) { - case 'activity_status': - foreach ($sel3[$id] as $kkey => & $vval) { - $vval = $statusLabel + $activityStatus; - } - break; - - case 'civicrm_participant_status_type': - foreach ($sel3[$id] as $kkey => & $vval) { - $vval = $statusLabel + $participantStatus; - } - break; - - case 'auto_renew_options': - foreach ($sel3[$id] as $kkey => & $vval) { - $auto = 0; - if ($kkey) { - $auto = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $kkey, 'auto_renew'); - } - if ($auto) { - $vval = $statusLabel + $autoRenew; - } - else { - $vval = $statusLabel; - } - } - break; - - case 'contact_date_reminder_options': - foreach ($sel3[$id] as $kkey => & $vval) { - $vval = $contactOptions; - } - break; - - case '': - $sel3[$id] = ''; - break; + $statusLabel = array('- ' . strtolower($mapping->entity_status_label) . ' -'); + $entityStatusLabels[$mapping->id] = $entityValueLabels[$mapping->id]; + foreach ($entityStatusLabels[$mapping->id] as $kkey => & $vval) { + $vval = $statusLabel + $mapping->getStatusLabels($kkey); } } + return array( - 'sel1' => $sel1, - 'sel2' => $sel2, - 'sel3' => $sel3, - 'sel4' => $sel4, - 'sel5' => $sel5, - 'entityMapping' => $entityMapping, - 'recipientMapping' => $recipientMapping, + 'sel1' => CRM_Utils_Array::collect('entity_label', $mappings), + 'sel2' => $entityValueLabels, + 'sel3' => $entityStatusLabels, + 'sel4' => $dateFieldLabels, + 'sel5' => $entityRecipientLabels, + 'entityMapping' => CRM_Utils_Array::collect('entity', $mappings), + 'recipientMapping' => $entityRecipientNames, ); } /** - * @param int $id + * @param int $mappingId * @param int $isLimit * * @return array */ - public static function getSelection1($id = NULL, $isLimit = NULL) { - $mapping = CRM_Core_BAO_ActionSchedule::getMapping($id); - $sel4 = $sel5 = array(); - $options = array( - 'manual' => ts('Choose Recipient(s)'), - 'group' => ts('Select Group'), - ); - - $recipientMapping = array_combine(array_keys($options), array_keys($options)); - - foreach ($mapping as $value) { - $entityRecipient = CRM_Utils_Array::value('entity_recipient', $value); - $key = CRM_Utils_Array::value('id', $value); - - if ($startDate = CRM_Utils_Array::value('entity_date_start', $value)) { - $sel4[$startDate] = ucwords(str_replace('_', ' ', $startDate)); - } - if ($endDate = CRM_Utils_Array::value('entity_date_end', $value)) { - $sel4[$endDate] = ucwords(str_replace('_', ' ', $endDate)); - } - - switch ($entityRecipient) { - case 'activity_contacts': - $activityContacts = CRM_Core_OptionGroup::values('activity_contacts'); - $sel5[$id] = $activityContacts + $options; - $recipientMapping += CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); - break; - - case 'event_contacts': - //CRM-15536, don't provide participant_role option on choosing 'Also Include' for Event entity - if ($isLimit == 1) { - $options += CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name'); - } - $sel5[$id] = $options; - $recipientMapping += CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, 'name', TRUE, FALSE, 'name'); - break; + public static function getSelection1($mappingId = NULL, $isLimit = NULL) { + $mappings = CRM_Core_BAO_ActionSchedule::getMappings(array( + 'id' => $mappingId, + )); + $dateFieldLabels = $entityRecipientLabels = array(); - case NULL: - $sel5[$id] = $options; - break; - } + foreach ($mappings as $mapping) { + /** @var \Civi\ActionSchedule\Mapping $mapping */ + $dateFieldLabels = $mapping->getDateFields(); + $entityRecipientLabels = $mapping->getRecipientTypes(!$isLimit); } return array( - 'sel4' => $sel4, - 'sel5' => $sel5[$id], - 'recipientMapping' => $recipientMapping, + 'sel4' => $dateFieldLabels, + 'sel5' => $entityRecipientLabels, + 'recipientMapping' => array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels)), ); } @@ -332,35 +230,12 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { * (reference) reminder list */ public static function &getList($namesOnly = FALSE, $entityValue = NULL, $id = NULL) { - // These variables may appear unused, but there's some $$ stuff going on. - $activity_type = CRM_Core_PseudoConstant::activityType(TRUE, TRUE); - $activity_status = CRM_Core_PseudoConstant::activityStatus(); - - $event_type = CRM_Event_PseudoConstant::eventType(); - $civicrm_event = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); - $civicrm_participant_status_type = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'); - $event_template = CRM_Event_PseudoConstant::eventTemplates(); - $civicrm_contact = self::getDateFields(); - - $auto_renew_options = CRM_Core_OptionGroup::values('auto_renew_options'); - $contact_date_reminder_options = CRM_Core_OptionGroup::values('contact_date_reminder_options'); - $civicrm_membership_type = CRM_Member_PseudoConstant::membershipType(); - - $entity = array( - 'civicrm_activity' => 'Activity', - 'civicrm_participant' => 'Event', - 'civicrm_membership' => 'Member', - 'civicrm_contact' => 'Contact', - ); - $query = " SELECT title, - cam.entity, cas.id as id, - cam.entity_value as entityValue, + cas.mapping_id, cas.entity_value as entityValueIds, - cam.entity_status as entityStatus, cas.entity_status as entityStatusIds, cas.start_action_date as entityDate, cas.start_action_offset, @@ -371,24 +246,25 @@ SELECT is_active FROM civicrm_action_schedule cas -LEFT JOIN civicrm_action_mapping cam ON (cam.id = cas.mapping_id) "; - $params = CRM_Core_DAO::$_nullArray; + $queryParams = array(); $where = " WHERE 1 "; if ($entityValue and $id) { - $where .= " -AND cas.entity_value = $id AND - cam.entity_value = '$entityValue'"; - - $params = array( - 1 => array($id, 'Integer'), - 2 => array($entityValue, 'String'), - ); + $mappings = self::getMappings(array( + 'entity_value' => $entityValue, + )); + $mappingIds = implode(',', array_keys($mappings)); + $where .= " AND cas.entity_value = %1 AND cas.mapping_id IN ($mappingIds)"; + $queryParams[1] = array($id, 'Integer'); } $where .= " AND cas.used_for IS NULL"; $query .= $where; - $dao = CRM_Core_DAO::executeQuery($query); + $dao = CRM_Core_DAO::executeQuery($query, $queryParams); while ($dao->fetch()) { + /** @var Civi\ActionSchedule\Mapping $mapping */ + $mapping = CRM_Utils_Array::first(self::getMappings(array( + 'id' => $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; @@ -396,23 +272,15 @@ AND cas.entity_value = $id AND $list[$dao->id]['start_action_condition'] = $dao->start_action_condition; $list[$dao->id]['entityDate'] = ucwords(str_replace('_', ' ', $dao->entityDate)); $list[$dao->id]['absolute_date'] = $dao->absolute_date; - - $status = $dao->entityStatus; - $statusArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->entityStatusIds); - foreach ($statusArray as & $s) { - $s = CRM_Utils_Array::value($s, $$status); - } - $statusIds = implode(', ', $statusArray); - - $value = $dao->entityValue; - $valueArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->entityValueIds); - foreach ($valueArray as & $v) { - $v = CRM_Utils_Array::value($v, $$value); - } - $valueIds = implode(', ', $valueArray); - $list[$dao->id]['entity'] = $entity[$dao->entity]; - $list[$dao->id]['value'] = $valueIds; - $list[$dao->id]['status'] = $statusIds; + $list[$dao->id]['entity'] = $mapping->entity_label; + $list[$dao->id]['value'] = implode(', ', CRM_Utils_Array::subset( + $mapping->getValueLabels(), + explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->entityValueIds) + )); + $list[$dao->id]['status'] = implode(', ', CRM_Utils_Array::subset( + $mapping->getStatusLabels($dao->entityValueIds), + explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->entityStatusIds) + )); $list[$dao->id]['is_repeat'] = $dao->is_repeat; $list[$dao->id]['is_active'] = $dao->is_active; } @@ -509,9 +377,9 @@ AND cas.entity_value = $id AND * @throws CRM_Core_Exception */ public static function sendMailings($mappingID, $now) { - $mapping = new CRM_Core_DAO_ActionMapping(); - $mapping->id = $mappingID; - $mapping->find(TRUE); + $mapping = CRM_Utils_Array::first(self::getMappings(array( + 'id' => $mappingID, + ))); $actionSchedule = new CRM_Core_DAO_ActionSchedule(); $actionSchedule->mapping_id = $mappingID; @@ -534,10 +402,10 @@ AND cas.entity_value = $id AND $errors = array(); try { - $tokenProcessor = self::createTokenProcessor($actionSchedule); + $tokenProcessor = self::createTokenProcessor($actionSchedule, $mapping); $tokenProcessor->addRow() ->context('contactId', $dao->contactID) - ->tokens(CRM_Core_BAO_ActionSchedule::prepareMailingTokens($mapping, $dao)); + ->context('actionSearchResult', (object) $dao->toArray()); foreach ($tokenProcessor->evaluate()->getRows() as $tokenRow) { if ($actionSchedule->mode == 'SMS' or $actionSchedule->mode == 'User_Preference') { CRM_Utils_Array::extend($errors, self::sendReminderSms($tokenRow, $actionSchedule, $dao->contactID)); @@ -589,9 +457,9 @@ AND cas.entity_value = $id AND $actionSchedule->find(); while ($actionSchedule->fetch()) { - $mapping = new CRM_Core_DAO_ActionMapping(); - $mapping->id = $mappingID; - $mapping->find(TRUE); + $mapping = CRM_Utils_Array::first(self::getMappings(array( + 'id' => $mappingID, + ))); // note: $where - this filtering applies for both // 'limit to' and 'addition to' options @@ -1119,7 +987,7 @@ WHERE m.owner_membership_id IS NOT NULL AND public static function processQueue($now = NULL, $params = array()) { $now = $now ? CRM_Utils_Time::setTime($now) : CRM_Utils_Time::getTime(); - $mappings = CRM_Core_BAO_ActionSchedule::getMapping(); + $mappings = CRM_Core_BAO_ActionSchedule::getMappings(); foreach ($mappings as $mappingID => $mapping) { CRM_Core_BAO_ActionSchedule::buildRecipientContacts($mappingID, $now, $params); CRM_Core_BAO_ActionSchedule::sendMailings($mappingID, $now); @@ -1162,9 +1030,11 @@ WHERE m.owner_membership_id IS NOT NULL AND return $options; } - $mapping = CRM_Core_BAO_ActionSchedule::getMapping($mappingID); + $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array( + 'id' => $mappingID, + ))); - switch ($mapping['entity']) { + switch ($mapping->entity) { case 'civicrm_participant': $eventContacts = CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, 'name', TRUE, FALSE, 'name'); if (empty($eventContacts[$recipientType])) { @@ -1215,7 +1085,7 @@ WHERE m.owner_membership_id IS NOT NULL AND * sending the message and pass in the fully rendered text of the message. * * @param CRM_Core_DAO_ActionSchedule $actionSchedule - * @param CRM_Core_DAO_ActionMapping $mapping + * @param Civi\ActionSchedule\Mapping $mapping * @param int $contactID * @param int $entityID * @param int|NULL $caseID @@ -1254,56 +1124,6 @@ WHERE m.owner_membership_id IS NOT NULL AND } } - /** - * @param CRM_Core_DAO_ActionMapping $mapping - * @param $dao - * @return array - * Ex: array('activity' => array('subject' => 'Hello world)). - */ - protected static function prepareMailingTokens($mapping, $dao) { - list($tokenEntity, $tokenFields) = CRM_Core_BAO_ActionSchedule::listMailingTokens($mapping); - - $entityTokenParams = array(); - foreach ($tokenFields as $field) { - if ($field == 'location') { - $loc = array(); - $stateProvince = CRM_Core_PseudoConstant::stateProvince(); - $loc['street_address'] = $dao->street_address; - $loc['city'] = $dao->city; - $loc['state_province'] = CRM_Utils_Array::value($dao->state_province_id, $stateProvince); - $loc['postal_code'] = $dao->postal_code; - $entityTokenParams[$tokenEntity][$field] = CRM_Utils_Address::format($loc); - } - elseif ($field == 'info_url') { - $entityTokenParams[$tokenEntity][$field] = CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $dao->event_id, TRUE, NULL, FALSE); - } - elseif ($field == 'registration_url') { - $entityTokenParams[$tokenEntity][$field] = CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $dao->event_id, TRUE, NULL, FALSE); - } - elseif (in_array($field, array('start_date', 'end_date', 'join_date', 'activity_date_time'))) { - $entityTokenParams[$tokenEntity][$field] = CRM_Utils_Date::customFormat($dao->$field); - } - elseif ($field == 'balance') { - if ($dao->entityTable == 'civicrm_contact') { - $balancePay = 'N/A'; - } - elseif (!empty($dao->entityID)) { - $info = CRM_Contribute_BAO_Contribution::getPaymentInfo($dao->entityID, 'event'); - $balancePay = CRM_Utils_Array::value('balance', $info); - $balancePay = CRM_Utils_Money::format($balancePay); - } - $entityTokenParams[$tokenEntity][$field] = $balancePay; - } - elseif ($field == 'fee_amount') { - $entityTokenParams[$tokenEntity][$field] = CRM_Utils_Money::format($dao->$field); - } - else { - $entityTokenParams[$tokenEntity][$field] = $dao->$field; - } - } - return $entityTokenParams; - } - /** * @param $mapping * @param $actionSchedule @@ -1389,62 +1209,6 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL return $query; } - /** - * @param $mapping - * @return array - */ - protected static function listMailingTokens($mapping) { - $tokenEntity = NULL; - $tokenFields = array(); - - if ($mapping->entity == 'civicrm_activity') { - $tokenEntity = 'activity'; - $tokenFields = array('activity_id', 'activity_type', 'subject', 'details', 'activity_date_time'); - } - - if ($mapping->entity == 'civicrm_participant') { - $tokenEntity = 'event'; - $tokenFields = array( - 'event_type', - 'title', - 'event_id', - 'start_date', - 'end_date', - 'summary', - 'description', - 'location', - 'info_url', - 'registration_url', - 'fee_amount', - 'contact_email', - 'contact_phone', - 'balance', - ); - } - - if ($mapping->entity == 'civicrm_membership') { - $tokenEntity = 'membership'; - $tokenFields = array( - 'fee', - 'id', - 'join_date', - 'start_date', - 'end_date', - 'status', - 'type', - ); - } - - if ($mapping->entity == 'civicrm_contact') { - $tokenEntity = 'contact'; - //TODO: get full list somewhere! - $tokenFields = array('birth_date', 'last_name'); - //TODO: is there anything to add here? - } - - return array($tokenEntity, $tokenFields); - } - /** * @param TokenRow $tokenRow * @param CRM_Core_DAO_ActionSchedule $schedule @@ -1561,12 +1325,14 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL /** * @param CRM_Core_DAO_ActionSchedule $schedule + * @param \Civi\ActionSchedule\Mapping $mapping * @return \Civi\Token\TokenProcessor */ - protected static function createTokenProcessor($schedule) { + protected static function createTokenProcessor($schedule, $mapping) { $tp = new \Civi\Token\TokenProcessor(\Civi\Core\Container::singleton()->get('dispatcher'), array( 'controller' => __CLASS__, 'actionSchedule' => $schedule, + 'actionMapping' => $mapping, 'smarty' => TRUE, )); $tp->addMessage('body_text', $schedule->body_text, 'text/plain'); diff --git a/CRM/Event/BAO/Event.php b/CRM/Event/BAO/Event.php index 946b51a734..bf5bbd84af 100644 --- a/CRM/Event/BAO/Event.php +++ b/CRM/Event/BAO/Event.php @@ -440,7 +440,9 @@ $event_summary_limit ); $params = array(1 => array($optionGroupId, 'Integer')); - $mappingID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'civicrm_event', 'id', 'entity_value'); + $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array( + 'entity_value' => 'civicrm_event', + ))); $dao = CRM_Core_DAO::executeQuery($query, $params); while ($dao->fetch()) { foreach ($properties as $property => $name) { @@ -544,7 +546,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, $mappingID); + $eventSummary['events'][$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, $mapping->id); $eventSummary['events'][$dao->id]['is_repeating_event'] = $dao->is_repeating_event; $statusTypes = CRM_Event_PseudoConstant::participantStatus(); @@ -983,25 +985,15 @@ WHERE civicrm_event.is_active = 1 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); + $oldMapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array( + 'entity_value' => ($eventValues['is_template'] ? 'event_template' : 'civicrm_event'), + ))); + $copyMapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array( + 'entity_value' => ($copyEvent->is_template == 1 ? 'event_template' : 'civicrm_event'), + ))); $copyReminder = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_ActionSchedule', - $oldData, - $newData + array('entity_value' => $id, 'mapping_id' => $oldMapping->id), + array('entity_value' => $copyEvent->id, 'mapping_id' => $copyMapping->id) ); if (!$afterCreate) { diff --git a/CRM/Event/Form/ManageEvent/TabHeader.php b/CRM/Event/Form/ManageEvent/TabHeader.php index 5e19cba4ee..7a32fd3bec 100644 --- a/CRM/Event/Form/ManageEvent/TabHeader.php +++ b/CRM/Event/Form/ManageEvent/TabHeader.php @@ -109,19 +109,24 @@ 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(array( + 'entity_value' => 'civicrm_event', + ))); $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 LEFT JOIN civicrm_tell_friend taf ON ( taf.entity_table = 'civicrm_event' AND taf.entity_id = e.id ) LEFT JOIN civicrm_pcp_block pcp ON ( pcp.entity_table = 'civicrm_event' AND pcp.entity_id = e.id ) -LEFT JOIN civicrm_action_mapping map ON ( map.entity_value = 'civicrm_event' ) -LEFT JOIN civicrm_action_schedule sch ON ( sch.mapping_id = map.id AND sch.entity_value = %1 ) +LEFT JOIN civicrm_action_schedule sch ON ( sch.mapping_id = %2 AND sch.entity_value = %1 ) LEFT JOIN civicrm_recurring_entity re ON ( e.id = re.entity_id AND re.entity_table = 'civicrm_event' ) WHERE e.id = %1 "; //Check if repeat is configured $eventHasParent = CRM_Core_BAO_RecurringEntity::getParentFor($eventID, 'civicrm_event'); - $params = array(1 => array($eventID, 'Integer')); + $params = array( + 1 => array($eventID, 'Integer'), + 2 => array($eventNameMapping->id, 'Integer'), + ); $dao = CRM_Core_DAO::executeQuery($sql, $params); if (!$dao->fetch()) { CRM_Core_Error::fatal(); diff --git a/CRM/Event/Page/ManageEvent.php b/CRM/Event/Page/ManageEvent.php index 3e61997e4f..75b1d17184 100644 --- a/CRM/Event/Page/ManageEvent.php +++ b/CRM/Event/Page/ManageEvent.php @@ -313,7 +313,9 @@ ORDER BY start_date desc 'enable_cart' ); $this->assign('eventCartEnabled', $enableCart); - $mappingID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'civicrm_event', 'id', 'entity_value'); + $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array( + 'entity_value' => 'civicrm_event', + ))); $eventType = CRM_Core_OptionGroup::values('event_type'); while ($dao->fetch()) { if (in_array($dao->id, $permissions[CRM_Core_Permission::VIEW])) { @@ -371,7 +373,7 @@ ORDER BY start_date desc //show campaigns on selector. $manageEvent[$dao->id]['campaign'] = CRM_Utils_Array::value($dao->campaign_id, $allCampaigns); - $manageEvent[$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, $mappingID); + $manageEvent[$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, $mapping->id); $manageEvent[$dao->id]['is_pcp_enabled'] = CRM_Utils_Array::value($dao->id, $eventPCPS); $manageEvent[$dao->id]['event_type'] = CRM_Utils_Array::value($manageEvent[$dao->id]['event_type_id'], $eventType); $manageEvent[$dao->id]['is_repeating_event'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_RecurringEntity', $dao->id, 'parent_id', 'entity_id'); diff --git a/Civi/ActionSchedule/Mapping.php b/Civi/ActionSchedule/Mapping.php new file mode 100644 index 0000000000..894a562ac6 --- /dev/null +++ b/Civi/ActionSchedule/Mapping.php @@ -0,0 +1,210 @@ +{$field} = $params[$field]; + } + } + } + + public $id; + + /** + * The basic entity to query (table name). + * + * @var string + * Ex: 'civicrm_activity', 'civicrm_event'. + */ + public $entity; + + /** + * The basic entity to query (label). + * + * @var + * Ex: 'Activity', 'Event' + */ + public $entity_label; + + /** + * Level 1 filter -- the field/option-list to filter on. + * + * @var string + * Ex: 'activity_type', 'civicrm_event', 'event_template'. + */ + public $entity_value; + + /** + * Level 1 filter -- The field label. + * + * @var string + * Ex: 'Activity Type', 'Event Name', 'Event Template'. + */ + public $entity_value_label; + + /** + * Level 2 filter -- the field/option-list to filter on. + * @var string + * Ex: 'activity_status, 'civicrm_participant_status_type', 'auto_renew_options'. + */ + public $entity_status; + + /** + * Level 2 filter -- the field label. + * @var string + * Ex: 'Activity Status', 'Participant Status', 'Auto Rewnewal Options'. + */ + public $entity_status_label; + + /** + * Date filter -- the field name. + * @var string|NULL + * Ex: 'event_start_date' + */ + public $entity_date_start; + + /** + * Date filter -- the field name. + * @var string|NULL + * Ex: 'event_end_date'. + */ + public $entity_date_end; + + /** + * Contact selector -- The field/relationship/option-group name. + * @var string|NULL + * Ex: 'activity_contacts', 'event_contacts'. + */ + public $entity_recipient; + + /** + * Get a list of value options. + * + * @return array + * Array(string $value => string $label). + * Ex: array(123 => 'Phone Call', 456 => 'Meeting'). + */ + public function getValueLabels() { + return self::getValueLabelMap($this->entity_value); + } + + /** + * Get a list of status options. + * + * @param string|int $value + * The list of status options may be contingent upon the selected filter value. + * This is the selected filter value. + * @return array + * Array(string $value => string $label). + * Ex: Array(123 => 'Completed', 456 => 'Scheduled'). + */ + public function getStatusLabels($value) { + if ($this->entity_status === 'auto_renew_options') { + if ($value && \CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $value, 'auto_renew')) { + return \CRM_Core_OptionGroup::values('auto_renew_options'); + } + else { + return array(); + } + } + return self::getValueLabelMap($this->entity_status); + } + + /** + * @return array + * Array(string $fieldName => string $fieldLabel). + */ + public function getDateFields() { + $dateFieldLabels = array(); + if (!empty($this->entity_date_start)) { + $dateFieldLabels[$this->entity_date_start] = ucwords(str_replace('_', ' ', $this->entity_date_start)); + } + if (!empty($this->entity_date_end)) { + $dateFieldLabels[$this->entity_date_end] = ucwords(str_replace('_', ' ', $this->entity_date_end)); + } + return $dateFieldLabels; + } + + /** + * @param bool|NULL $noThanksJustKidding + * This is ridiculous and should not exist. + * If true, don't do our main job. + * @return array + * array(string $value => string $label). + * Ex: array('assignee' => 'Activity Assignee'). + */ + public function getRecipientTypes($noThanksJustKidding = FALSE) { + $entityRecipientLabels = array(); + switch ($this->entity_recipient) { + case 'activity_contacts': + $entityRecipientLabels = \CRM_Core_OptionGroup::values('activity_contacts'); + break; + + case 'event_contacts': + if (!$noThanksJustKidding) { + $entityRecipientLabels = \CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name'); + } + break; + + default: + } + $entityRecipientLabels += array( + 'manual' => ts('Choose Recipient(s)'), + 'group' => ts('Select Group'), + ); + return $entityRecipientLabels; + } + + protected static function getValueLabelMap($name) { + static $valueLabelMap = NULL; + if ($valueLabelMap === NULL) { + $valueLabelMap['activity_type'] = \CRM_Core_PseudoConstant::activityType(TRUE, TRUE); + asort($valueLabelMap['activity_type']); + + $valueLabelMap['activity_status'] = \CRM_Core_PseudoConstant::activityStatus(); + $valueLabelMap['event_type'] = \CRM_Event_PseudoConstant::eventType(); + $valueLabelMap['civicrm_event'] = \CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); + $valueLabelMap['civicrm_participant_status_type'] = \CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'); + $valueLabelMap['event_template'] = \CRM_Event_PseudoConstant::eventTemplates(); + $valueLabelMap['auto_renew_options'] = \CRM_Core_OptionGroup::values('auto_renew_options'); + $valueLabelMap['contact_date_reminder_options'] = \CRM_Core_OptionGroup::values('contact_date_reminder_options'); + $valueLabelMap['civicrm_membership_type'] = \CRM_Member_PseudoConstant::membershipType(); + + $allCustomFields = \CRM_Core_BAO_CustomField::getFields(''); + $dateFields = array( + 'birth_date' => ts('Birth Date'), + 'created_date' => ts('Created Date'), + 'modified_date' => ts('Modified Date'), + ); + foreach ($allCustomFields as $fieldID => $field) { + if ($field['data_type'] == 'Date') { + $dateFields["custom_$fieldID"] = $field['label']; + } + } + $valueLabelMap['civicrm_contact'] = $dateFields; + } + + return $valueLabelMap[$name]; + } + +} diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index d1f6b17276..fa7fe74994 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -199,6 +199,11 @@ class Container { array() ))->addTag('kernel.event_subscriber'); + $container->setDefinition('actionscheduletmp', new Definition( + 'CRM_Core_ActionScheduleTmp', + array() + ))->addTag('kernel.event_subscriber'); + \CRM_Utils_Hook::container($container); return $container; diff --git a/tests/phpunit/api/v3/ActionScheduleTest.php b/tests/phpunit/api/v3/ActionScheduleTest.php index 7fca8f8abb..0c7da114d6 100644 --- a/tests/phpunit/api/v3/ActionScheduleTest.php +++ b/tests/phpunit/api/v3/ActionScheduleTest.php @@ -54,7 +54,9 @@ class api_v3_ActionScheduleTest extends CiviUnitTestCase { $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name'); - $mappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'activity_type', 'id', 'entity_value'); + $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array( + 'entity_value' => 'activity_type', + ))); $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name'); $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7); $params = array( @@ -66,7 +68,7 @@ class api_v3_ActionScheduleTest extends CiviUnitTestCase { 'is_active' => 1, 'record_activity' => 1, 'start_action_date' => 'activity_date_time', - 'mapping_id' => $mappingId, + 'mapping_id' => $mapping->id, ); $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params); $this->assertTrue(is_numeric($actionSchedule['id'])); @@ -94,7 +96,9 @@ class api_v3_ActionScheduleTest extends CiviUnitTestCase { $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name'); - $mappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'activity_type', 'id', 'entity_value'); + $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array( + 'entity_value' => 'activity_type', + ))); $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name'); $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7); $params = array( @@ -105,7 +109,7 @@ class api_v3_ActionScheduleTest extends CiviUnitTestCase { 'entity_status' => $scheduledStatus, 'is_active' => 1, 'record_activity' => 1, - 'mapping_id' => $mappingId, + 'mapping_id' => $mapping->id, 'start_action_offset' => 3, 'start_action_unit' => 'day', 'start_action_condition' => 'before', -- 2.25.1