From 7f0141d888065881f63fbe7f3c78164cfbb6adad Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sat, 29 Aug 2015 11:49:13 -0700 Subject: [PATCH] CRM-13422 - Add MappingInterface::validateSchedule(). Also: In CRM_Admin_Form_ScheduleReminders, extract parseActionSchedule() from postProcess(). Now we can call parseActionSchedule() from both postProcess() and formRule(). --- CRM/Admin/Form/ScheduleReminders.php | 79 +++++++++++++++--------- CRM/Contribute/ActionMapping/ByPage.php | 13 ++++ CRM/Contribute/ActionMapping/ByType.php | 13 ++++ CRM/Core/BAO/ActionSchedule.php | 9 +++ Civi/ActionSchedule/Mapping.php | 13 ++++ Civi/ActionSchedule/MappingInterface.php | 11 ++++ 6 files changed, 108 insertions(+), 30 deletions(-) diff --git a/CRM/Admin/Form/ScheduleReminders.php b/CRM/Admin/Form/ScheduleReminders.php index 013cb63e20..3c7a451f0f 100644 --- a/CRM/Admin/Form/ScheduleReminders.php +++ b/CRM/Admin/Form/ScheduleReminders.php @@ -286,6 +286,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { * * @param array $fields * The input form values. + * @param CRM_Admin_Form_ScheduleReminders $self * * @return bool|array * true if no errors, else array of errors @@ -338,6 +339,12 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { $errors[$recipientKind[$fields['recipient']]['target_id']] = ts('If "Also include" or "Limit to" are selected, you must specify at least one %1', array(1 => $recipientKind[$fields['recipient']]['name'])); } + $actionSchedule = $self->parseActionSchedule($fields); + if ($actionSchedule->mapping_id) { + $mapping = CRM_Core_BAO_ActionSchedule::getMapping($actionSchedule->mapping_id); + CRM_Utils_Array::extend($errors, $mapping->validateSchedule($actionSchedule)); + } + if (!empty($errors)) { return $errors; } @@ -414,6 +421,39 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { return; } $values = $this->controller->exportValues($this->getName()); + $bao = $this->parseActionSchedule($values)->save(); + + // we need to set this on the form so that hooks can identify the created entity + $this->set('id', $bao->id); + $bao->free(); + + $status = ts("Your new Reminder titled %1 has been saved.", + array(1 => "{$values['title']}") + ); + + if ($this->_action) { + if ($this->_action & CRM_Core_Action::UPDATE) { + $status = ts("Your Reminder titled %1 has been updated.", + array(1 => "{$values['title']}") + ); + } + + if ($this->_context == 'event' && $this->_compId) { + $url = CRM_Utils_System::url('civicrm/event/manage/reminder', "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1"); + $session = CRM_Core_Session::singleton(); + $session->pushUserContext($url); + } + } + CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); + } + + /** + * @param array $values + * The submitted form values. + * @return CRM_Core_DAO_ActionSchedule + */ + public function parseActionSchedule($values) { + $params = array(); $keys = array( 'title', @@ -472,7 +512,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { $params['group_id'] = $values['group_id']; $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null'; } - elseif (!CRM_Utils_System::isNull($values['recipient_listing']) && !CRM_Utils_System::isNull($values['limit_to'])) { + elseif (isset($values['recipient_listing']) && isset($values['limit_to']) && !CRM_Utils_System::isNull($values['recipient_listing']) && !CRM_Utils_System::isNull($values['limit_to'])) { $params['recipient'] = CRM_Utils_Array::value('recipient', $values); $params['recipient_listing'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value('recipient_listing', $values) @@ -491,15 +531,14 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { } else { $params['mapping_id'] = $values['entity'][0]; - $entity_value = $values['entity'][1]; - $entity_status = $values['entity'][2]; if ($params['mapping_id'] == 1) { $params['limit_to'] = 1; } - foreach (array('entity_value', 'entity_status') as $key) { - $params[$key] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $$key); - } + $entity_value = CRM_Utils_Array::value(1, $values['entity'], array()); + $entity_status = CRM_Utils_Array::value(2, $values['entity'], array()); + $params['entity_value'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $entity_value); + $params['entity_status'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $entity_status); } $params['is_active'] = CRM_Utils_Array::value('is_active', $values, 0); @@ -514,7 +553,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { } // multilingual options - $params['filter_contact_language'] = CRM_Utils_Array::value('filter_contact_language', $values, NULL); + $params['filter_contact_language'] = CRM_Utils_Array::value('filter_contact_language', $values, array()); $params['filter_contact_language'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['filter_contact_language']); $params['communication_language'] = CRM_Utils_Array::value('communication_language', $values, NULL); @@ -613,29 +652,9 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { } } - $bao = CRM_Core_BAO_ActionSchedule::add($params); - // we need to set this on the form so that hooks can identify the created entity - $this->set('id', $bao->id); - $bao->free(); - - $status = ts("Your new Reminder titled %1 has been saved.", - array(1 => "{$values['title']}") - ); - - if ($this->_action) { - if ($this->_action & CRM_Core_Action::UPDATE) { - $status = ts("Your Reminder titled %1 has been updated.", - array(1 => "{$values['title']}") - ); - } - - if ($this->_context == 'event' && $this->_compId) { - $url = CRM_Utils_System::url('civicrm/event/manage/reminder', "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1"); - $session = CRM_Core_Session::singleton(); - $session->pushUserContext($url); - } - } - CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); + $actionSchedule = new CRM_Core_DAO_ActionSchedule(); + $actionSchedule->copyValues($params); + return $actionSchedule; } /** diff --git a/CRM/Contribute/ActionMapping/ByPage.php b/CRM/Contribute/ActionMapping/ByPage.php index 5d13360b35..aa10c4ed06 100644 --- a/CRM/Contribute/ActionMapping/ByPage.php +++ b/CRM/Contribute/ActionMapping/ByPage.php @@ -161,6 +161,19 @@ class CRM_Contribute_ActionMapping_ByPage implements \Civi\ActionSchedule\Mappin return array(); } + /** + * Determine whether a schedule based on this mapping is sufficiently + * complete. + * + * @param \CRM_Core_DAO_ActionSchedule $schedule + * @return array + * Array (string $code => string $message). + * List of error messages. + */ + public function validateSchedule($schedule) { + return array(); + } + /** * Generate a query to locate contacts who match the given * schedule. diff --git a/CRM/Contribute/ActionMapping/ByType.php b/CRM/Contribute/ActionMapping/ByType.php index abb2c322e9..1cd68ef0c3 100644 --- a/CRM/Contribute/ActionMapping/ByType.php +++ b/CRM/Contribute/ActionMapping/ByType.php @@ -161,6 +161,19 @@ class CRM_Contribute_ActionMapping_ByType implements \Civi\ActionSchedule\Mappin return array(); } + /** + * Determine whether a schedule based on this mapping is sufficiently + * complete. + * + * @param \CRM_Core_DAO_ActionSchedule $schedule + * @return array + * Array (string $code => string $message). + * List of error messages. + */ + public function validateSchedule($schedule) { + return array(); + } + /** * Generate a query to locate contacts who match the given * schedule. diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 14783381a6..7de987c02b 100755 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -67,6 +67,15 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { } } + /** + * @param string|int $id + * @return \Civi\ActionSchedule\Mapping|NULL + */ + public static function getMapping($id) { + $mappings = self::getMappings(); + return isset($mappings[$id]) ? $mappings[$id] : NULL; + } + /** * Retrieve list of selections/drop downs for Scheduled Reminder form * diff --git a/Civi/ActionSchedule/Mapping.php b/Civi/ActionSchedule/Mapping.php index ad1321528d..0d4377bbb8 100644 --- a/Civi/ActionSchedule/Mapping.php +++ b/Civi/ActionSchedule/Mapping.php @@ -329,6 +329,19 @@ abstract class Mapping implements MappingInterface { return $valueLabelMap[$name]; } + /** + * Determine whether a schedule based on this mapping is sufficiently + * complete. + * + * @param \CRM_Core_DAO_ActionSchedule $schedule + * @return array + * Array (string $code => string $message). + * List of error messages. + */ + public function validateSchedule($schedule) { + return array(); + } + /** * Generate a query to locate contacts who match the given * schedule. diff --git a/Civi/ActionSchedule/MappingInterface.php b/Civi/ActionSchedule/MappingInterface.php index 7abea4c582..b3a1703066 100644 --- a/Civi/ActionSchedule/MappingInterface.php +++ b/Civi/ActionSchedule/MappingInterface.php @@ -126,6 +126,17 @@ interface MappingInterface { */ public function getRecipientTypes($noThanksJustKidding = FALSE); + /** + * Determine whether a schedule based on this mapping is sufficiently + * complete. + * + * @param \CRM_Core_DAO_ActionSchedule $schedule + * @return array + * Array (string $code => string $message). + * List of error messages. + */ + public function validateSchedule($schedule); + /** * Generate a query to locate contacts who match the given * schedule. -- 2.25.1