From 0905ee5d32a8a17bf2833d6b281ca9eacc4fa413 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sat, 29 Aug 2015 12:52:20 -0700 Subject: [PATCH] CRM-13422 - ActionSchedule - Kill getSelection() and getSelection1() These are silly functions whose job is to wrap up data in a bundle using mysterious names so that it can get immediately unwrapped. --- CRM/Admin/Form/ScheduleReminders.php | 32 ++++++++----- CRM/Admin/Page/AJAX.php | 19 +++----- CRM/Core/BAO/ActionSchedule.php | 67 ++++++++-------------------- 3 files changed, 45 insertions(+), 73 deletions(-) diff --git a/CRM/Admin/Form/ScheduleReminders.php b/CRM/Admin/Form/ScheduleReminders.php index 25f9c0c858..144cbe803c 100644 --- a/CRM/Admin/Form/ScheduleReminders.php +++ b/CRM/Admin/Form/ScheduleReminders.php @@ -110,15 +110,17 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { TRUE ); - $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection($mappingID); - $this->assign('entityMapping', json_encode($selectionOptions['entityMapping'])); - $this->assign('recipientMapping', json_encode($selectionOptions['recipientMapping'])); + $mappings = CRM_Core_BAO_ActionSchedule::getMappings(); + $selectedMapping = $mappings[$mappingID ? $mappingID : 1]; + $entityRecipientLabels = $selectedMapping->getRecipientTypes() + CRM_Core_BAO_ActionSchedule::getAdditionalRecipients(); + $this->assign('entityMapping', json_encode( + CRM_Utils_Array::collectMethod('getEntity', $mappings) + )); + $this->assign('recipientMapping', json_encode( + array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels)) + )); if (empty($this->_context)) { - if (empty($selectionOptions['sel1'])) { - CRM_Core_Error::fatal('Could not find mapping for scheduled reminders.'); - } - $sel = &$this->add( 'hierselect', 'entity', @@ -128,7 +130,11 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { 'style' => 'vertical-align: top;', ) ); - $sel->setOptions(array($selectionOptions['sel1'], $selectionOptions['sel2'], $selectionOptions['sel3'])); + $sel->setOptions(array( + CRM_Utils_Array::collectMethod('getLabel', $mappings), + CRM_Core_BAO_ActionSchedule::getAllEntityValueLabels(), + CRM_Core_BAO_ActionSchedule::getAllEntityStatusLabels(), + )); if (is_a($sel->_elements[1], 'HTML_QuickForm_select')) { // make second selector a multi-select - @@ -143,7 +149,9 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { } } else { - $options = $selectionOptions['sel3'][$this->_mappingID][0]; + // Dig deeper - this code is sublimely stupid. + $allEntityStatusLabels = CRM_Core_BAO_ActionSchedule::getAllEntityStatusLabels(); + $options = $allEntityStatusLabels[$this->_mappingID][0]; $attributes = array('multiple' => 'multiple', 'class' => 'crm-select2 huge', 'placeholder' => $options[0]); unset($options[0]); $this->add('select', 'entity', ts('Recipient(s)'), $options, TRUE, $attributes); @@ -191,7 +199,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { //reminder_action $this->add('select', 'start_action_condition', ts('Action Condition'), $condition); - $this->add('select', 'start_action_date', ts('Date Field'), $selectionOptions['sel4'], TRUE); + $this->add('select', 'start_action_date', ts('Date Field'), $selectedMapping->getDateFields(), TRUE); $this->addElement('checkbox', 'record_activity', $recordActivity); @@ -204,7 +212,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { $this->add('select', 'end_frequency_unit', ts('until'), $freqUnitsDisplay); $this->add('select', 'end_frequency_interval', ts('until'), $numericOptions); $this->add('select', 'end_action', ts('Repetition Condition'), $condition, TRUE); - $this->add('select', 'end_date', ts('Date Field'), $selectionOptions['sel4'], TRUE); + $this->add('select', 'end_date', ts('Date Field'), $selectedMapping->getDateFields(), TRUE); $this->add('text', 'from_name', ts('From Name')); $this->add('text', 'from_email', ts('From Email')); @@ -224,7 +232,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { $this->add('select', 'limit_to', ts('Limit Options'), $limitOptions, FALSE, array('onChange' => "showHideByValue('limit_to','','recipient', 'select','select',true);")); - $this->add('select', 'recipient', $recipientLabels['other'], $selectionOptions['sel5'], + $this->add('select', 'recipient', $recipientLabels['other'], $entityRecipientLabels, FALSE, array('onchange' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);") ); diff --git a/CRM/Admin/Page/AJAX.php b/CRM/Admin/Page/AJAX.php index ffa7699322..2fe81d04e8 100644 --- a/CRM/Admin/Page/AJAX.php +++ b/CRM/Admin/Page/AJAX.php @@ -303,21 +303,16 @@ LIMIT $limit"; CRM_Utils_JSON::output(array('status' => 'error', 'error_msg' => 'required params missing.')); } - $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($_GET['mappingID'], $_GET['isLimit']); + $mapping = CRM_Core_BAO_ActionSchedule::getMapping($_GET['mappingID']); + $dateFieldLabels = $mapping ? $mapping->getDateFields() : array(); + $entityRecipientLabels = $mapping ? ($mapping->getRecipientTypes(!$_GET['isLimit']) + CRM_Core_BAO_ActionSchedule::getAdditionalRecipients()) : array(); + $recipientMapping = array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels)); $output = array( - 'sel4' => array(), - 'sel5' => array(), - 'recipientMapping' => $selectionOptions['recipientMapping'], + 'sel4' => CRM_Utils_Array::toKeyValueRows($dateFieldLabels), + 'sel5' => CRM_Utils_Array::toKeyValueRows($entityRecipientLabels), + 'recipientMapping' => $recipientMapping, ); - foreach (array(4, 5) as $sel) { - foreach ($selectionOptions["sel$sel"] as $id => $name) { - $output["sel$sel"][] = array( - 'value' => $name, - 'key' => $id, - ); - } - } CRM_Utils_JSON::output($output); } diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 780da240d8..6d7c3c880d 100755 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -77,28 +77,33 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { } /** - * Retrieve list of selections/drop downs for Scheduled Reminder form - * - * @param bool $id - * Mapping id. + * For each entity, get a list of entity-value labels. * * @return array - * associated array of all the drop downs in the form + * Ex: $entityValueLabels[$mappingId][$valueId] = $valueLabel. + * @throws CRM_Core_Exception */ - public static function getSelection($id = NULL) { - $mappings = CRM_Core_BAO_ActionSchedule::getMappings(); - $selectedMapping = $mappings[$id ? $id : 1]; - + public static function getAllEntityValueLabels() { $entityValueLabels = array(); - foreach ($mappings as $mapping) { + foreach (CRM_Core_BAO_ActionSchedule::getMappings() as $mapping) { /** @var \Civi\ActionSchedule\Mapping $mapping */ $entityValueLabels[$mapping->getId()] = $mapping->getValueLabels(); $valueLabel = array('- ' . strtolower($mapping->getValueHeader()) . ' -'); $entityValueLabels[$mapping->getId()] = $valueLabel + $entityValueLabels[$mapping->getId()]; } + return $entityValueLabels; + } + /** + * For each entity, get a list of entity-status labels. + * + * @return array + * Ex: $entityValueLabels[$mappingId][$valueId][$statusId] = $statusLabel. + */ + public static function getAllEntityStatusLabels() { + $entityValueLabels = self::getAllEntityValueLabels(); $entityStatusLabels = array(); - foreach ($mappings as $mapping) { + foreach (CRM_Core_BAO_ActionSchedule::getMappings() as $mapping) { /** @var \Civi\ActionSchedule\Mapping $mapping */ $statusLabel = array('- ' . strtolower($mapping->getStatusHeader()) . ' -'); $entityStatusLabels[$mapping->getId()] = $entityValueLabels[$mapping->getId()]; @@ -106,43 +111,7 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { $vval = $statusLabel + $mapping->getStatusLabels($kkey); } } - - $entityRecipientLabels = $selectedMapping->getRecipientTypes() + self::getAdditionalRecipients(); - - return array( - 'sel1' => CRM_Utils_Array::collectMethod('getLabel', $mappings), - 'sel2' => $entityValueLabels, - 'sel3' => $entityStatusLabels, - 'sel4' => $selectedMapping->getDateFields(), - 'sel5' => $entityRecipientLabels, - 'entityMapping' => CRM_Utils_Array::collectMethod('getEntity', $mappings), - 'recipientMapping' => array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels)), - ); - } - - /** - * @param int $mappingId - * @param int $isLimit - * - * @return array - */ - public static function getSelection1($mappingId = NULL, $isLimit = NULL) { - $mappings = CRM_Core_BAO_ActionSchedule::getMappings(array( - 'id' => $mappingId, - )); - $dateFieldLabels = $entityRecipientLabels = array(); - - foreach ($mappings as $mapping) { - /** @var \Civi\ActionSchedule\Mapping $mapping */ - $dateFieldLabels = $mapping->getDateFields(); - $entityRecipientLabels = $mapping->getRecipientTypes(!$isLimit) + self::getAdditionalRecipients(); - } - - return array( - 'sel4' => $dateFieldLabels, - 'sel5' => $entityRecipientLabels, - 'recipientMapping' => array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels)), - ); + return $entityStatusLabels; } /** @@ -717,7 +686,7 @@ FROM civicrm_action_schedule cas * @return array * array(mixed $value => string $label). */ - protected static function getAdditionalRecipients() { + public static function getAdditionalRecipients() { return array( 'manual' => ts('Choose Recipient(s)'), 'group' => ts('Select Group'), -- 2.25.1