From edffa1324065ad130e63143e660be0430e28f9fc Mon Sep 17 00:00:00 2001 From: colemanw Date: Tue, 18 Jul 2023 20:30:12 -0400 Subject: [PATCH] ScheduleReminders - Cleanup awful form code Lots more refactoring to do, this fixes the easy-to-fix things that were obviously wrong. --- CRM/Admin/Form/ScheduleReminders.php | 42 ++++++++++++++-------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/CRM/Admin/Form/ScheduleReminders.php b/CRM/Admin/Form/ScheduleReminders.php index 9ea51ea2f2..b910ff8d23 100644 --- a/CRM/Admin/Form/ScheduleReminders.php +++ b/CRM/Admin/Form/ScheduleReminders.php @@ -28,10 +28,18 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { */ public $_id; - public $_freqUnits; - protected $_compId; + /** + * @var CRM_Core_DAO_ActionSchedule + */ + private $_actionSchedule; + + /** + * @var int|string|null + */ + private $_mappingID; + /** * @return mixed */ @@ -103,7 +111,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { $selectedMapping = $mappings[$mappingID ?: 1]; $entityRecipientLabels = $selectedMapping->getRecipientTypes() + CRM_Core_BAO_ActionSchedule::getAdditionalRecipients(); $this->assign('entityMapping', json_encode( - CRM_Utils_Array::collectMethod('getEntity', $mappings) + CRM_Utils_Array::collectMethod('getEntityTable', $mappings) )); $this->assign('recipientMapping', json_encode( array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels)) @@ -138,17 +146,12 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { } } else { - // Dig deeper - this code is sublimely stupid. - $allEntityStatusLabels = CRM_Core_BAO_ActionSchedule::getAllEntityStatusLabels(); - $options = $allEntityStatusLabels[$this->_mappingID][0]; - $attributes = ['multiple' => 'multiple', 'class' => 'crm-select2 huge', 'placeholder' => $options[0]]; - unset($options[0]); + $mapping = CRM_Core_BAO_ActionSchedule::getMapping($this->_mappingID); + $options = $mapping->getStatusLabels($this->getComponentID()); + $attributes = ['multiple' => TRUE, 'class' => 'crm-select2 huge', 'placeholder' => $mapping->getStatusHeader()]; $this->add('select', 'entity', ts('Recipient(s)'), $options, TRUE, $attributes); - $this->assign('context', $this->getContext()); } - - //get the frequency units. - $this->_freqUnits = CRM_Core_SelectValues::getRecurringFrequencyUnits(); + $this->assign('context', $this->getContext()); //reminder_interval $this->add('number', 'start_action_offset', ts('When (trigger date)'), ['class' => 'six', 'min' => 0]); @@ -171,7 +174,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { $this->add('select', 'sms_provider_id', ts('SMS Provider'), $providerSelect, TRUE); } - foreach ($this->_freqUnits as $val => $label) { + foreach (CRM_Core_SelectValues::getRecurringFrequencyUnits() as $val => $label) { $freqUnitsDisplay[$val] = ts('%1(s)', [1 => $label]); } @@ -273,7 +276,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { $this->add('checkbox', 'is_active', $isActive); - $this->addFormRule(['CRM_Admin_Form_ScheduleReminders', 'formRule'], $this); + $this->addFormRule([__CLASS__, 'formRule'], $this); $this->setPageTitle(ts('Scheduled Reminder')); } @@ -299,7 +302,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { $errors['entity'] = ts('Please select appropriate value'); } - $mode = CRM_Utils_Array::value('mode', $fields, FALSE); + $mode = $fields['mode'] ?? FALSE; if (!empty($fields['is_active']) && CRM_Utils_System::isNull($fields['subject']) && (!$mode || $mode !== 'SMS') ) { @@ -364,10 +367,6 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { CRM_Utils_Array::extend($errors, $mapping->validateSchedule($self->_actionSchedule)); } - if (!empty($errors)) { - return $errors; - } - return empty($errors) ? TRUE : $errors; } @@ -424,7 +423,6 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { */ public function postProcess() { if ($this->_action & CRM_Core_Action::DELETE) { - // delete reminder CRM_Core_BAO_ActionSchedule::deleteRecord(['id' => $this->_id]); CRM_Core_Session::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success'); if ($this->getContext() === 'event' && $this->getComponentID()) { @@ -468,8 +466,10 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { } /** + * FIXME: This function shouldn't exist. It takes an overcomplicated form + * and maps the wonky form values to the bao object to be saved. + * * @param array $values - * The submitted form values. * * @return CRM_Core_DAO_ActionSchedule * @throws \CRM_Core_Exception -- 2.25.1