From 013ac5dfe9845780c8c7268aebe1ac2828de04e6 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 20 Dec 2015 21:35:46 -0500 Subject: [PATCH] CRM-16019 - Switch custom fields to use crmDatepicker --- CRM/Contact/Form/Search/Criteria.php | 15 ++---- CRM/Core/BAO/CustomField.php | 51 ++++--------------- CRM/Core/BAO/CustomGroup.php | 20 -------- CRM/Core/Form.php | 14 +++-- js/Common.js | 6 ++- .../Contact/Form/Edit/Address/CustomField.tpl | 6 +-- templates/CRM/Custom/Form/CustomField.tpl | 7 +-- templates/CRM/Custom/Form/Preview.tpl | 6 +-- templates/CRM/Custom/Form/Search.tpl | 11 +--- templates/CRM/UF/Form/Preview.tpl | 3 +- 10 files changed, 31 insertions(+), 108 deletions(-) diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index 15163ee9cc..9c0e424ad5 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -533,16 +533,11 @@ class CRM_Contact_Form_Search_Criteria { foreach ($group['fields'] as $field) { $fieldId = $field['id']; $elementName = 'custom_' . $fieldId; - if ($field['data_type'] == 'Date' && $field['is_search_range']) { - CRM_Core_Form_Date::buildDateRange($form, $elementName, 1, '_from', '_to', ts('From:'), FALSE); - } - else { - CRM_Core_BAO_CustomField::addQuickFormElement($form, - $elementName, - $fieldId, - FALSE, FALSE, TRUE - ); - } + CRM_Core_BAO_CustomField::addQuickFormElement($form, + $elementName, + $fieldId, + FALSE, FALSE, TRUE + ); } } } diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 022a823669..b43cf5fe9d 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -835,7 +835,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $options = CRM_Utils_Array::value('values', civicrm_api3('contact', 'getoptions', array( 'field' => "custom_$fieldId", 'context' => $search ? 'search' : 'create', - ), array())); + )), array()); // Consolidate widget types to simplify the below switch statement if ($search || ($widget !== 'AdvMulti-Select' && strpos($widget, 'Select') !== FALSE)) { @@ -909,37 +909,19 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { break; case 'Select Date': + $attr = array('data-crm-custom' => $dataCrmCustomVal); + $params = array( + 'date' => $field->date_format, + 'minDate' => isset($field->start_date_years) ? (date('Y') - $field->start_date_years) . '-01-01' : NULL, + 'maxDate' => isset($field->end_date_years) ? (date('Y') + $field->end_date_years) . '-01-01' : NULL, + 'time' => $field->time_format ? $field->time_format * 12 : FALSE, + ); if ($field->is_search_range && $search) { - $qf->addDate($elementName . '_from', $label . ' - ' . ts('From'), FALSE, - array( - 'format' => $field->date_format, - 'timeFormat' => $field->time_format, - 'startOffset' => $field->start_date_years, - 'endOffset' => $field->end_date_years, - 'data-crm-custom' => $dataCrmCustomVal, - ) - ); - - $qf->addDate($elementName . '_to', ts('To'), FALSE, - array( - 'format' => $field->date_format, - 'timeFormat' => $field->time_format, - 'startOffset' => $field->start_date_years, - 'endOffset' => $field->end_date_years, - 'data-crm-custom' => $dataCrmCustomVal, - ) - ); + $qf->add('datepicker', $elementName . '_from', $label, $attr + array('placeholder' => ts('From')), FALSE, $params); + $qf->add('datepicker', $elementName . '_to', NULL, $attr + array('placeholder' => ts('To')), FALSE, $params); } else { - $required = $useRequired && !$search; - - $qf->addDate($elementName, $label, $required, array( - 'format' => $field->date_format, - 'timeFormat' => $field->time_format, - 'startOffset' => $field->start_date_years, - 'endOffset' => $field->end_date_years, - 'data-crm-custom' => $dataCrmCustomVal, - )); + $qf->add('datepicker', $elementName, $label, $attr, $useRequired && !$search, $params); } break; @@ -1370,17 +1352,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { } break; - case 'Select Date': - if ($value) { - list($defaults[$elementName], $defaults[$elementName . '_time']) = CRM_Utils_Date::setDateDefaults( - $value, - NULL, - $customField->date_format, - $customField->time_format - ); - } - break; - case 'Autocomplete-Select': if ($customField->data_type == 'ContactReference') { if (is_numeric($value)) { diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index f27a456341..dafb03aed7 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -1366,23 +1366,6 @@ ORDER BY civicrm_custom_group.weight, } break; - case 'Select Date': - if (isset($value)) { - if (empty($field['time_format'])) { - list($defaults[$elementName]) = CRM_Utils_Date::setDateDefaults($value, NULL, - $field['date_format'] - ); - } - else { - $timeElement = $elementName . '_time'; - if (substr($elementName, -1) == ']') { - $timeElement = substr($elementName, 0, -1) . '_time]'; - } - list($defaults[$elementName], $defaults[$timeElement]) = CRM_Utils_Date::setDateDefaults($value, NULL, $field['date_format'], $field['time_format']); - } - } - break; - case 'Multi-Select Country': case 'Multi-Select State/Province': if (isset($value)) { @@ -1561,9 +1544,6 @@ ORDER BY civicrm_custom_group.weight, public static function buildQuickForm(&$form, &$groupTree, $inactiveNeeded = FALSE, $prefix = '') { $form->assign_by_ref("{$prefix}groupTree", $groupTree); - // this is fix for date field - $form->assign('currentYear', date('Y')); - foreach ($groupTree as $id => $group) { CRM_Core_ShowHideBlocks::links($form, $group['title'], '', ''); foreach ($group['fields'] as $field) { diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 57619a4ce1..c387db99a6 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1699,13 +1699,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page { /** * Add date. * - * @code - * // if you need time - * $attributes = array( - * 'addTime' => true, - * 'formatType' => 'relative' or 'birth' etc check advanced date settings - * ); - * @endcode + * @deprecated + * Use $this->add('datepicker', ...) instead. * * @param string $name * Name of the element. @@ -1795,7 +1790,10 @@ class CRM_Core_Form extends HTML_QuickForm_Page { } /** - * Function that will add date and time. + * Function that will add date and time. + * + * @deprecated + * Use $this->add('datepicker', ...) instead. * * @param string $name * @param string $label diff --git a/js/Common.js b/js/Common.js index 6a35778063..2dbf522b22 100644 --- a/js/Common.js +++ b/js/Common.js @@ -643,8 +643,10 @@ if (!CRM.vars) CRM.vars = {}; copyAttributes($dataField, $dateField, ['placeholder', 'style', 'class', 'disabled']); $dateField.addClass('crm-form-text crm-form-date'); settings.date = typeof settings.date === 'string' ? settings.date : CRM.config.dateInputFormat; - settings.changeMonth = _.includes('m', settings.date); - settings.changeYear = _.includes('y', settings.date); + settings.changeMonth = _.includes(settings.date, 'm'); + settings.changeYear = _.includes(settings.date, 'y'); + settings.minDate = settings.minDate ? CRM.utils.makeDate(settings.minDate) : null; + settings.maxDate = settings.maxDate ? CRM.utils.makeDate(settings.maxDate) : null; $dateField.datepicker(settings).change(updateDataField); } // Rudimentary validation. TODO: Roll into use of jQUery validate and ui.datepicker.validation diff --git a/templates/CRM/Contact/Form/Edit/Address/CustomField.tpl b/templates/CRM/Contact/Form/Edit/Address/CustomField.tpl index 9b785e2e19..8a1bade7d3 100644 --- a/templates/CRM/Contact/Form/Edit/Address/CustomField.tpl +++ b/templates/CRM/Contact/Form/Edit/Address/CustomField.tpl @@ -69,11 +69,7 @@ {$form.address.$blockId.$element_name.label} - {if $element.data_type neq 'Date'} - {$form.address.$blockId.$element_name.html}  - {elseif $element.skip_calendar NEQ true } - {include file="CRM/common/jcalendar.tpl" blockId=$blockId blockSection='address' elementName=$element_name} - {/if} + {$form.address.$blockId.$element_name.html}  {if $element.data_type eq 'File'} {if $element.element_value.data} diff --git a/templates/CRM/Custom/Form/CustomField.tpl b/templates/CRM/Custom/Form/CustomField.tpl index 2199c2a2be..43cf47a6ab 100644 --- a/templates/CRM/Custom/Form/CustomField.tpl +++ b/templates/CRM/Custom/Form/CustomField.tpl @@ -63,12 +63,7 @@ {$form.$element_name.label}{if $element.help_post}{help id=$element.id file="CRM/Custom/Form/CustomField.hlp" title=$element.label}{/if} - {if $element.data_type neq 'Date' OR ($element.data_type eq 'Date' AND $element.is_view eq 1)} - {$form.$element_name.html}  - {elseif $element.skip_calendar NEQ true} - {include file="CRM/common/jcalendar.tpl" elementName=$element_name} - {/if} - + {$form.$element_name.html}  {if $element.data_type eq 'File'} {if $element.element_value.data}
diff --git a/templates/CRM/Custom/Form/Preview.tpl b/templates/CRM/Custom/Form/Preview.tpl index eddedc94dc..b8bcfd0a30 100644 --- a/templates/CRM/Custom/Form/Preview.tpl +++ b/templates/CRM/Custom/Form/Preview.tpl @@ -84,11 +84,7 @@ {$form.$element_name.label}{if $element.help_post}{help id=$element.id file="CRM/Custom/Form/CustomField.hlp" title=$form.$element_name.label}{/if} - {if $element.data_type neq 'Date'} - {$form.$element_name.html}  - {elseif $element.skip_calendar NEQ true } - {include file="CRM/common/jcalendar.tpl" elementName=$element_name} - {/if} + {$form.$element_name.html}  {if $element.html_type eq 'Autocomplete-Select'} {if $element.data_type eq 'ContactReference'} {include file="CRM/Custom/Form/ContactReference.tpl"} diff --git a/templates/CRM/Custom/Form/Search.tpl b/templates/CRM/Custom/Form/Search.tpl index bda7ef3bbc..0f69732785 100644 --- a/templates/CRM/Custom/Form/Search.tpl +++ b/templates/CRM/Custom/Form/Search.tpl @@ -40,21 +40,12 @@ {assign var="element_name_from" value=$element_name|cat:"_from"} {assign var="element_name_to" value=$element_name|cat:"_to"} - {if $element.data_type neq 'Date'} {$form.$element_name_from.label} {$form.$element_name_from.html|crmAddClass:six}   {$form.$element_name_to.label}  {$form.$element_name_to.html|crmAddClass:six} - {elseif $element.skip_calendar NEQ true } - - {include file="CRM/Core/DateRange.tpl" fieldName=$element_name from='_from' to='_to'} - {/if} {else} {$form.$element_name.label} - {if $element.data_type neq 'Date'} - {$form.$element_name.html} - {elseif $element.skip_calendar NEQ true } - {include file="CRM/common/jcalendar.tpl" elementName=$element_name} - {/if} + {$form.$element_name.html} {if !empty($form.$operator_name)} {$form.$operator_name.html} {assign var="add_multivalue_js" value=true} diff --git a/templates/CRM/UF/Form/Preview.tpl b/templates/CRM/UF/Form/Preview.tpl index 239b1d68f9..024fbf0273 100644 --- a/templates/CRM/UF/Form/Preview.tpl +++ b/templates/CRM/UF/Form/Preview.tpl @@ -108,8 +108,7 @@ {include file="CRM/Contact/Form/Edit/TagsAndGroups.tpl" type=$n} {elseif $n eq 'email_greeting' or $n eq 'postal_greeting' or $n eq 'addressee'} {include file="CRM/Profile/Form/GreetingType.tpl"} - {elseif ( $field.data_type eq 'Date' AND $element.skip_calendar NEQ true ) or - ( $n|substr:-5:5 eq '_date' ) or ( $field.name eq 'activity_date_time' ) } + {elseif ( $n|substr:-5:5 eq '_date' ) or ( $field.name eq 'activity_date_time' ) } {include file="CRM/common/jcalendar.tpl" elementName=$form.$n.name} {elseif $n|substr:0:5 eq 'phone'} {assign var="phone_ext_field" value=$n|replace:'phone':'phone_ext'} -- 2.25.1