From d2a4c29bcd25e3c6a8b4196ace43b5277029f314 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 2 Mar 2015 15:37:56 -0500 Subject: [PATCH] CRM-15932 - Fix recurring entity form validation --- CRM/Activity/Form/Activity.php | 5 +- CRM/Core/Form/RecurringEntity.php | 21 ++- CRM/Event/Form/ManageEvent/Repeat.php | 5 +- templates/CRM/Activity/Form/Activity.tpl | 33 +--- templates/CRM/Core/Form/RecurringEntity.tpl | 165 ++++++------------ .../CRM/Event/Form/ManageEvent/Repeat.tpl | 2 +- 6 files changed, 66 insertions(+), 165 deletions(-) diff --git a/CRM/Activity/Form/Activity.php b/CRM/Activity/Form/Activity.php index afba759e10..14db8696de 100644 --- a/CRM/Activity/Form/Activity.php +++ b/CRM/Activity/Form/Activity.php @@ -524,7 +524,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { return CRM_Custom_Form_CustomData::setDefaultValues($this); } - $defaults = $this->_values; + $defaults = $this->_values + CRM_Core_Form_RecurringEntity::setDefaultValues(); // if we're editing... if (isset($this->_activityId)) { if (empty($defaults['activity_date_time'])) { @@ -536,9 +536,6 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task { $defaults['activity_date_time_time'] ) = CRM_Utils_Date::setDateDefaults($defaults['activity_date_time'], 'activityDateTime'); list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['activity_date_time'], 'activityDateTime'); - $recurringEntityDefaults = array(); - $recurringEntityDefaults = CRM_Core_Form_RecurringEntity::setDefaultValues(); - $defaults = array_merge($defaults, $recurringEntityDefaults); } if ($this->_context != 'standalone') { diff --git a/CRM/Core/Form/RecurringEntity.php b/CRM/Core/Form/RecurringEntity.php index babbb9ec96..7df05aa487 100644 --- a/CRM/Core/Form/RecurringEntity.php +++ b/CRM/Core/Form/RecurringEntity.php @@ -123,7 +123,12 @@ class CRM_Core_Form_RecurringEntity { * @return array */ public static function setDefaultValues() { - $defaults = array(); + // Defaults for new entity + $defaults = array( + 'repetition_frequency_unit' => 'week', + ); + + // Default for existing entity if (self::$_scheduleReminderID) { $defaults['repetition_frequency_unit'] = self::$_scheduleReminderDetails->repetition_frequency_unit; $defaults['repetition_frequency_interval'] = self::$_scheduleReminderDetails->repetition_frequency_interval; @@ -144,7 +149,6 @@ class CRM_Core_Form_RecurringEntity { if (self::$_scheduleReminderDetails->limit_to) { $defaults['repeats_by'] = 1; } - $explodeStartActionCondition = array(); if (self::$_scheduleReminderDetails->entity_status) { $explodeStartActionCondition = explode(" ", self::$_scheduleReminderDetails->entity_status); $defaults['entity_status_1'] = $explodeStartActionCondition[0]; @@ -180,9 +184,9 @@ class CRM_Core_Form_RecurringEntity { 'saturday', ); $dayOfTheWeek = array_combine($dayOfTheWeek, CRM_Utils_Date::getAbbrWeekdayNames()); - $form->add('select', 'repetition_frequency_unit', ts('Repeats every'), $freqUnitsDisplay); + $form->add('select', 'repetition_frequency_unit', ts('Repeats every'), $freqUnitsDisplay, FALSE, array('class' => 'required')); $numericOptions = CRM_Core_SelectValues::getNumericOptions(1, 30); - $form->add('select', 'repetition_frequency_interval', NULL, $numericOptions); + $form->add('select', 'repetition_frequency_interval', NULL, $numericOptions, FALSE, array('class' => 'required')); $form->addDateTime('repetition_start_date', ts('Repetition Start Date'), FALSE, array('formatType' => 'activityDateTime')); foreach ($dayOfTheWeek as $key => $val) { $startActionCondition[] = $form->createElement('checkbox', $key, NULL, $val); @@ -192,9 +196,8 @@ class CRM_Core_Form_RecurringEntity { '1' => ts('day of the month'), '2' => ts('day of the week'), ); - $form->addRadio('repeats_by', ts("Repeats By"), $roptionTypes, array(), NULL); - $getMonths = CRM_Core_SelectValues::getNumericOptions(1, 31); - $form->add('select', 'limit_to', '', $getMonths); + $form->addRadio('repeats_by', ts("Repeats by"), $roptionTypes, array('required' => TRUE), NULL); + $form->add('select', 'limit_to', '', CRM_Core_SelectValues::getNumericOptions(1, 31)); $dayOfTheWeekNo = array( 'first' => ts('First'), 'second' => ts('Second'), @@ -208,8 +211,8 @@ class CRM_Core_Form_RecurringEntity { '1' => ts('After'), '2' => ts('On'), ); - $form->addRadio('ends', ts("Ends"), $eoptionTypes, array(), NULL); - $form->add('text', 'start_action_offset', '', array('size' => 3, 'maxlength' => 2)); + $form->addRadio('ends', ts("Ends"), $eoptionTypes, array('class' => 'required'), NULL); + $form->add('select', 'start_action_offset', NULL, CRM_Core_SelectValues::getNumericOptions(1, 30), FALSE); $form->addFormRule(array('CRM_Core_Form_RecurringEntity', 'formRule')); $form->addDate('repeat_absolute_date', ts('On'), FALSE, array('formatType' => 'mailing')); $form->add('text', 'exclude_date_list', ts('Exclude Dates'), array( diff --git a/CRM/Event/Form/ManageEvent/Repeat.php b/CRM/Event/Form/ManageEvent/Repeat.php index c6d4f57f91..2d9919092b 100644 --- a/CRM/Event/Form/ManageEvent/Repeat.php +++ b/CRM/Event/Form/ManageEvent/Repeat.php @@ -75,7 +75,7 @@ class CRM_Event_Form_ManageEvent_Repeat extends CRM_Event_Form_ManageEvent { * the default values are retrieved from the database * * - * @return void + * @return array */ public function setDefaultValues() { $defaults = array(); @@ -84,8 +84,7 @@ class CRM_Event_Form_ManageEvent_Repeat extends CRM_Event_Form_ManageEvent { $currentEventStartDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'start_date', 'id'); list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date::setDateDefaults($currentEventStartDate, 'activityDateTime'); $recurringEntityDefaults = CRM_Core_Form_RecurringEntity::setDefaultValues(); - $defaults = array_merge($defaults, $recurringEntityDefaults); - return $defaults; + return array_merge($defaults, $recurringEntityDefaults); } public function buildQuickForm() { diff --git a/templates/CRM/Activity/Form/Activity.tpl b/templates/CRM/Activity/Form/Activity.tpl index 096e6aa537..77e8a3e7d4 100644 --- a/templates/CRM/Activity/Form/Activity.tpl +++ b/templates/CRM/Activity/Form/Activity.tpl @@ -211,38 +211,7 @@ {if $action eq 2 OR $action eq 1} - {include file="CRM/Core/Form/RecurringEntity.tpl"} - {literal} - - {/literal} - {if $action eq 1} - {literal} - - {/literal} - {/if} + {include file="CRM/Core/Form/RecurringEntity.tpl" recurringFormIsEmbedded=true} {/if} diff --git a/templates/CRM/Core/Form/RecurringEntity.tpl b/templates/CRM/Core/Form/RecurringEntity.tpl index d1580a003e..8623cfaa2f 100644 --- a/templates/CRM/Core/Form/RecurringEntity.tpl +++ b/templates/CRM/Core/Form/RecurringEntity.tpl @@ -24,14 +24,16 @@ +--------------------------------------------------------------------+ *} -
+
{ts 1=$recurringEntityType}Repeat %1{/ts}
-
- {include file="CRM/common/formButtons.tpl" location="top"} -
+ {if !$recurringFormIsEmbedded} +
+ {include file="CRM/common/formButtons.tpl" location="top"} +
+ {/if} @@ -60,7 +62,7 @@ - + @@ -73,9 +75,11 @@
{$form.repetition_start_date.label}
{$form.ends.label} * {help id="id-ends-after" entityType=$recurringEntityType file="CRM/Core/Form/RecurringEntity.hlp"}{$form.ends.label} * {help id="id-ends-after" entityType=$recurringEntityType file="CRM/Core/Form/RecurringEntity.hlp"} {$form.ends.1.html} {$form.start_action_offset.html} {ts}occurrences{/ts}
{$form.exclude_date_list.html}
-
- {include file="CRM/common/formButtons.tpl" location="bottom"} -
+ {if !$recurringFormIsEmbedded} +
+ {include file="CRM/common/formButtons.tpl" location="bottom"} +
+ {/if}
{literal} @@ -83,126 +87,51 @@ CRM.$(function($) { var $form = $('form.{/literal}{$form.formClass}{literal}'); - $('#repetition_start_date_display').closest("tr").hide(); - /****** On load "Repeats By" and "Repeats On" blocks should be hidden if dropdown value is not week or month****** (Edit Mode)***/ - switch ($('#repetition_frequency_unit').val()) { - case 'week': - $('.crm-core-form-recurringentity-block-start_action_condition').show(); - $('.crm-core-form-recurringentity-block-repeats_by td').hide(); - break; - case 'month': - $('.crm-core-form-recurringentity-block-repeats_by td').show(); - $('.crm-core-form-recurringentity-block-start_action_condition').hide(); - break; - default: - $('.crm-core-form-recurringentity-block-start_action_condition').hide(); - $('.crm-core-form-recurringentity-block-repeats_by td').hide(); - break; - } - - /***********On Load Set Ends Value (Edit Mode) **********/ - switch ($('input:radio[name=ends]:checked').val()) { - case '1': - $('#start_action_offset').prop('disabled', false); - $('#repeat_absolute_date_display').prop('disabled', true).val(''); - break; - case '2': - $('#repeat_absolute_date_display').prop('disabled', false); - $('#start_action_offset').prop('disabled', true).val(''); - break; - default: - $('#start_action_offset').prop('disabled', true); - $('#repeat_absolute_date_display').prop('disabled', true); - break; - } + // Prevent html5 errors + $form.attr('novalidate', 'novalidate'); - /******On Load set Repeats by section******************/ - switch ($('input:radio[name=repeats_by]:checked').val()) { - case '1': - $('#limit_to').prop('disabled', false); - $('#entity_status_1, #entity_status_2').prop('disabled', true); - break; - case '2': - $('#entity_status_1, #entity_status_2').prop('disabled', false); - $('#limit_to').prop('disabled', true); - break; - default: - //Just in-case block shows up, disable it - $('#limit_to, #entity_status_1, #entity_status_2').prop('disabled', true); - break; - } - - $('#repetition_frequency_unit').change(function () { + function changeFrequencyUnit() { switch ($(this).val()) { - case 'hour': - $('.crm-core-form-recurringentity-block-start_action_condition').hide(); - $('.crm-core-form-recurringentity-block-repeats_by td').hide(); - break; - case 'day': - $('.crm-core-form-recurringentity-block-start_action_condition').hide(); - $('.crm-core-form-recurringentity-block-repeats_by td').hide(); - break; case 'week': //Show "Repeats On" block when week is selected - $('.crm-core-form-recurringentity-block-start_action_condition').show(); - $('.crm-core-form-recurringentity-block-repeats_by td').hide(); + $('.crm-core-form-recurringentity-block-start_action_condition', $form).show(); + $('.crm-core-form-recurringentity-block-repeats_by td', $form).hide(); break; case 'month': - $('.crm-core-form-recurringentity-block-start_action_condition').hide(); //Show "Repeats By" block when month is selected - $('.crm-core-form-recurringentity-block-repeats_by td').show(); - break; - case 'year': - $('.crm-core-form-recurringentity-block-start_action_condition').hide(); - $('.crm-core-form-recurringentity-block-repeats_by td').hide(); - break; - } - }); - - // For "Ends" block - $('input:radio[name=ends]').click(function() { - switch ($(this).val()) { - case '1': - $('#start_action_offset').prop('disabled', false); - $('#repeat_absolute_date_display').val(''); - break; - case '2': - $('#repeat_absolute_date_display').prop('disabled', false); - $('#start_action_offset').val(''); + $('.crm-core-form-recurringentity-block-start_action_condition', $form).hide(); + $('.crm-core-form-recurringentity-block-repeats_by td', $form).show(); break; default: - $('#repeat_absolute_date_display').prop('disabled', true); - break; + $('.crm-core-form-recurringentity-block-start_action_condition', $form).hide(); + $('.crm-core-form-recurringentity-block-repeats_by td', $form).hide(); } - }); + } + $('#repetition_frequency_unit', $form).each(changeFrequencyUnit).change(changeFrequencyUnit); - //For "Repeats By" block - $('input:radio[name=repeats_by]').click(function() { - $('#limit_to').prop('disabled', $(this).val() != 1); - $('#entity_status_1, #entity_status_2').prop('disabled', $(this).val() != 2); - }); + function disableUnselected() { + $('input:radio[name=ends], input[name=repeats_by]', $form).not(':checked').siblings(':input').prop('disabled', true); + } + disableUnselected(); - $form.submit(function() { - //Check form for values submitted - if ($('input[name=ends]:checked').val() == 1) { - if ($('#start_action_offset').val() == "") { - $('#start_action_offset').crmError(); - return false; - } - } else if ($('input[name=ends]:checked').val() == 2) { - if ($('#repeat_absolute_date_display').val() == "") { - $('#repeat_absolute_date_display').crmError(); - return false; - } - } + $('input:radio[name=ends], input[name=repeats_by]', $form).click(function() { + $(this).siblings(':input').prop('disabled', false).filter(':visible').addClass('required').focus(); + disableUnselected(); }); + function validate() { + var valid = $(':input', '#recurring-entity-block').valid(), + modified = CRM.utils.initialValueChanged('#recurring-entity-block'); + $('#allowRepeatConfigToSubmit', $form).val(valid && modified ? '1' : '0'); + return valid; + } + function getDisplayDate(date) { return $.datepicker.formatDate(CRM.config.dateInputFormat, $.datepicker.parseDate('yy-mm-dd', date)); } // Combine select2 and datepicker into a multi-select-date widget - $('#exclude_date_list').crmSelect2({ + $('#exclude_date_list', $form).crmSelect2({ multiple: true, data: [], initSelection: function(element, callback) { @@ -216,12 +145,12 @@ callback(values); } }) - // Prevent select2 from opening and show a datepicker instead .on('select2-opening', function(e) { var $el = $(this); + // Prevent select2 from opening and show a datepicker instead e.preventDefault(); $('.select2-search-field input', $el.select2('container')) - .datepicker({dateFormat: CRM.config.dateInputFormat}) + .datepicker() .datepicker('show') .off('.crmDate') .on('change.crmDate', function() { @@ -240,9 +169,9 @@ //If there are changes in repeat configuration, enable save button //Dialog for preview repeat Configuration dates function previewDialog() { - $('#allowRepeatConfigToSubmit').val(CRM.utils.initialValueChanged('.crm-core-form-recurringentity-block') ? '1' : '0'); var payload = $form.serialize() + '&entity_table={/literal}{$entityTable}{literal}&entity_id={/literal}{$currentEntityId}{literal}', settings = CRM.utils.adjustDialogDefaults({ + width: '50%', url: CRM.url("civicrm/recurringentity/preview", payload) }); CRM.confirm(settings) @@ -252,14 +181,18 @@ } $('#_qf_Repeat_submit-top, #_qf_Repeat_submit-bottom').click(function (e) { - previewDialog(); + if (validate()) { + previewDialog(); + } e.preventDefault(); }); - $('#_qf_Activity_upload-top, #_qf_Activity_upload-bottom').click( function (e) { - if (CRM.utils.initialValueChanged('.crm-core-form-recurringentity-block')) { + $('#_qf_Activity_upload-top, #_qf_Activity_upload-bottom').click(function (e) { + if (CRM.utils.initialValueChanged('#recurring-entity-block')) { e.preventDefault(); - previewDialog(); + if (validate()) { + previewDialog(); + } } }); diff --git a/templates/CRM/Event/Form/ManageEvent/Repeat.tpl b/templates/CRM/Event/Form/ManageEvent/Repeat.tpl index 8cd3c500a9..e5187ca670 100644 --- a/templates/CRM/Event/Form/ManageEvent/Repeat.tpl +++ b/templates/CRM/Event/Form/ManageEvent/Repeat.tpl @@ -24,7 +24,7 @@ +--------------------------------------------------------------------+ *}
-{include file="CRM/Core/Form/RecurringEntity.tpl"} +{include file="CRM/Core/Form/RecurringEntity.tpl" recurringFormIsEmbedded=false} {if $rows}
{ts}Connected Repeating Events{/ts}
-- 2.25.1