From ed0ca2486df5e2d7c40e3b8ace28b066830e3961 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 15 Feb 2017 01:38:59 +1300 Subject: [PATCH] CRM-19124 update birth_date to be consistent at point of hook (use datepicker) --- CRM/Contact/BAO/Contact.php | 9 +-- CRM/Contact/Form/Edit/Demographics.php | 4 +- CRM/Contact/Page/Inline/Demographics.php | 4 +- CRM/Contact/Page/View/Summary.php | 5 +- CRM/Contact/Page/View/Vcard.php | 4 +- CRM/Core/BAO/CustomField.php | 2 +- CRM/Core/Form.php | 24 ++++-- CRM/Core/Page.php | 24 ++++++ CRM/Core/SelectValues.php | 29 +------ .../Page/MultipleRecordFieldsListing.php | 2 +- CRM/Utils/Date.php | 76 +++++++++++++++---- .../CRM/Contact/Form/Edit/Demographics.tpl | 4 +- .../CRM/Contact/Form/Inline/Demographics.tpl | 4 +- .../CRM/Contact/Page/Inline/Demographics.tpl | 14 +--- 14 files changed, 123 insertions(+), 82 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index e1f299cc63..7f834eb93f 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -2517,6 +2517,7 @@ AND civicrm_openid.is_primary = 1"; ), ); + // @todo This can be figured out from metadata & we can avoid the uncached query. CRM_Core_OptionGroup::lookupValues($temp, $names, FALSE); $values['preferred_communication_method'] = $preffComm; @@ -2540,14 +2541,6 @@ AND civicrm_openid.is_primary = 1"; $values['age']['y'] = CRM_Utils_Array::value('years', $age); $values['age']['m'] = CRM_Utils_Array::value('months', $age); } - - list($values['birth_date']) = CRM_Utils_Date::setDateDefaults($contact->birth_date, 'birth'); - $values['birth_date_display'] = $contact->birth_date; - } - - if ($contact->deceased_date) { - list($values['deceased_date']) = CRM_Utils_Date::setDateDefaults($contact->deceased_date, 'birth'); - $values['deceased_date_display'] = $contact->deceased_date; } $contact->contact_id = $contact->id; diff --git a/CRM/Contact/Form/Edit/Demographics.php b/CRM/Contact/Form/Edit/Demographics.php index d01495d597..e96acc7e22 100644 --- a/CRM/Contact/Form/Edit/Demographics.php +++ b/CRM/Contact/Form/Edit/Demographics.php @@ -45,10 +45,10 @@ class CRM_Contact_Form_Edit_Demographics { public static function buildQuickForm(&$form) { $form->addField('gender_id', array('entity' => 'contact', 'type' => 'Radio', 'allowClear' => TRUE)); - $form->addField('birth_date', array('entity' => 'contact', 'formatType' => 'birth')); + $form->addField('birth_date', array('entity' => 'contact'), FALSE, FALSE); $form->addField('is_deceased', array('entity' => 'contact', 'label' => ts('Contact is Deceased'), 'onclick' => "showDeceasedDate()")); - $form->addField('deceased_date', array('entity' => 'contact', 'formatType' => 'birth')); + $form->addField('deceased_date', array('entity' => 'contact'), FALSE, FALSE); } /** diff --git a/CRM/Contact/Page/Inline/Demographics.php b/CRM/Contact/Page/Inline/Demographics.php index 33a43adbb4..373ff40188 100644 --- a/CRM/Contact/Page/Inline/Demographics.php +++ b/CRM/Contact/Page/Inline/Demographics.php @@ -54,13 +54,11 @@ class CRM_Contact_Page_Inline_Demographics extends CRM_Core_Page { $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); $defaults['gender_display'] = $gender[CRM_Utils_Array::value('gender_id', $defaults)]; } + $this->assignFieldMetadataToTemplate('Contact'); $this->assign('contactId', $contactId); $this->assign($defaults); - //for birthdate format with respect to birth format set - $this->assign('birthDateViewFormat', CRM_Utils_Array::value('qfMapping', CRM_Utils_Date::checkBirthDateFormat())); - // check logged in user permission CRM_Contact_Page_View::checkUserPermission($this, $contactId); diff --git a/CRM/Contact/Page/View/Summary.php b/CRM/Contact/Page/View/Summary.php index 7a633d1e49..2e9344224c 100644 --- a/CRM/Contact/Page/View/Summary.php +++ b/CRM/Contact/Page/View/Summary.php @@ -130,10 +130,10 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View { $session = CRM_Core_Session::singleton(); $url = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $this->_contactId); $session->pushUserContext($url); + $this->assignFieldMetadataToTemplate('Contact'); $params = array(); $defaults = array(); - $ids = array(); $params['id'] = $params['contact_id'] = $this->_contactId; $params['noRelationships'] = $params['noNotes'] = $params['noGroups'] = TRUE; @@ -259,9 +259,6 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View { $defaults['current_employer'] = $contact->organization_name; $defaults['current_employer_id'] = $contact->employer_id; } - - //for birthdate format with respect to birth format set - $this->assign('birthDateViewFormat', CRM_Utils_Array::value('qfMapping', CRM_Utils_Date::checkBirthDateFormat())); } $defaults['external_identifier'] = $contact->external_identifier; diff --git a/CRM/Contact/Page/View/Vcard.php b/CRM/Contact/Page/View/Vcard.php index 100278ef2f..0a7b2cce4f 100644 --- a/CRM/Contact/Page/View/Vcard.php +++ b/CRM/Contact/Page/View/Vcard.php @@ -88,8 +88,8 @@ class CRM_Contact_Page_View_Vcard extends CRM_Contact_Page_View { $vcard->setTitle($defaults['job_title']); } - if (!empty($defaults['birth_date_display'])) { - $vcard->setBirthday(CRM_Utils_Array::value('birth_date_display', $defaults)); + if (!empty($defaults['birth_date'])) { + $vcard->setBirthday(CRM_Utils_Array::value('birth_date', $defaults)); } if (!empty($defaults['home_URL'])) { diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 82bfee3e06..b5dff555e5 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1218,7 +1218,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { // Or should we throw an exception here if it is? $value = is_array($value) ? CRM_Utils_Array::first($value) : $value; - $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats(); + $actualPHPFormats = CRM_Utils_Date::datePluginToPHPFormats(); $format = CRM_Utils_Array::value('date_format', $field); if ($format) { diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 68d4ceabea..12c864b158 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1366,11 +1366,16 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * - multiple - bool * - context - @see CRM_Core_DAO::buildOptionsContext * @param bool $required + * @param bool $legacyDate + * Temporary param to facilitate the conversion of fields to use the datepicker in + * a controlled way. To convert the field the jcalendar code needs to be removed from the + * tpl as well. That file is intended to be EOL. + * * @throws \CiviCRM_API3_Exception * @throws \Exception * @return HTML_QuickForm_Element */ - public function addField($name, $props = array(), $required = FALSE) { + public function addField($name, $props = array(), $required = FALSE, $legacyDate = TRUE) { // Resolve context. if (empty($props['context'])) { $props['context'] = $this->getDefaultContext(); @@ -1455,10 +1460,19 @@ class CRM_Core_Form extends HTML_QuickForm_Page { return $this->add('textarea', $name, $label, $props, $required); case 'Select Date': - //TODO: add range support - //TODO: Add date formats - //TODO: Add javascript template for dates. - return $this->addDate($name, $label, $required, $props); + // This is a white list for fields that have been tested with + // date picker. We should be able to remove the other + if ($legacyDate) { + //TODO: add range support + //TODO: Add date formats + //TODO: Add javascript template for dates. + return $this->addDate($name, $label, $required, $props); + } + else { + $fieldSpec = CRM_Utils_Date::addDateMetadataToField($fieldSpec, $fieldSpec); + $attributes = array('format' => $fieldSpec['date_format']); + return $this->add('datepicker', $name, $label, $attributes, $required, $fieldSpec['datepicker']['extra']); + } case 'Radio': $separator = isset($props['separator']) ? $props['separator'] : NULL; diff --git a/CRM/Core/Page.php b/CRM/Core/Page.php index 4167e81023..fe0f2b0732 100644 --- a/CRM/Core/Page.php +++ b/CRM/Core/Page.php @@ -406,4 +406,28 @@ class CRM_Core_Page { $this->$name = $value; } + /** + * Assign metadata about fields to the template. + * + * In order to allow the template to format fields we assign information about them to the template. + * + * At this stage only date field metadata is assigned as that is the only use-case in play and + * we don't want to assign a lot of unneeded data. + * + * @param string $entity + * The entity being queried. + * + * @throws \CiviCRM_API3_Exception + */ + protected function assignFieldMetadataToTemplate($entity) { + $fields = civicrm_api3($entity, 'getfields', array('action' => 'get')); + $dateFields = array(); + foreach ($fields['values'] as $fieldName => $fieldMetaData) { + if (isset($fieldMetaData['html']) && CRM_Utils_Array::value('type', $fieldMetaData['html']) == 'Select Date') { + $dateFields[$fieldName] = CRM_Utils_Date::addDateMetadataToField($fieldMetaData, $fieldMetaData); + } + } + $this->assign('fields', $dateFields); + } + } diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index 344219ef5a..49e5f4f446 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -343,13 +343,14 @@ class CRM_Core_SelectValues { if (empty($date['format'])) { if ($context == 'Input') { $date['format'] = Civi::settings()->get('dateInputFormat'); - $date['php_datetime_format'] = self::datePluginToPHPFormats(Civi::settings()->get('dateInputFormat')); } else { $date['format'] = 'M d'; } } } + $date['php_datetime_format'] = CRM_Utils_Date::getPhpDateFormatFromInputStyleDateFormat($date['format']); + $date['smarty_view_format'] = CRM_Utils_Date::getDateFieldViewFormat($date['format']); if (!isset($date['time'])) { $date['time'] = FALSE; } @@ -762,32 +763,6 @@ class CRM_Core_SelectValues { return $dateInputFormats; } - /** - * Map date plugin and actual format that is used by PHP. - * - * @return array - */ - public static function datePluginToPHPFormats() { - $dateInputFormats = array( - "mm/dd/yy" => 'm/d/Y', - "dd/mm/yy" => 'd/m/Y', - "yy-mm-dd" => 'Y-m-d', - "dd-mm-yy" => 'd-m-Y', - "dd.mm.yy" => 'd.m.Y', - "M d, yy" => 'M j, Y', - "d M yy" => 'j M Y', - "MM d, yy" => 'F j, Y', - "d MM yy" => 'j F Y', - "DD, d MM yy" => 'l, j F Y', - "mm/dd" => 'm/d', - "dd-mm" => 'd-m', - "yy-mm" => 'Y-m', - "M yy" => 'M Y', - "yy" => 'Y', - ); - return $dateInputFormats; - } - /** * Time formats. * diff --git a/CRM/Profile/Page/MultipleRecordFieldsListing.php b/CRM/Profile/Page/MultipleRecordFieldsListing.php index 3e29e7f7d8..49d3a214bf 100644 --- a/CRM/Profile/Page/MultipleRecordFieldsListing.php +++ b/CRM/Profile/Page/MultipleRecordFieldsListing.php @@ -234,7 +234,7 @@ class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic { CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities); if ($returnValues['data_type'] == 'Date') { $dateFields[$fieldIDs[$key]] = 1; - $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats(); + $actualPHPFormats = CRM_Utils_Date::datePluginToPHPFormats(); $dateFormat = (array) CRM_Utils_Array::value($returnValues['date_format'], $actualPHPFormats); $timeFormat = CRM_Utils_Array::value('time_format', $returnValues); } diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index 3d8ed11f93..a7c37d5dfd 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -939,21 +939,21 @@ class CRM_Utils_Date { } /** - * Check given format is valid for bith date. - * and retrun supportable birth date format w/ qf mapping. + * Get the smarty view presentation mapping for the given format. + * + * Historically it was decided that where the view format is 'dd/mm/yy' or 'mm/dd/yy' + * they should be rendered using a longer date format. This is likely as much to + * do with the earlier date widget being unable to handle some formats as usablity. + * However, we continue to respect this. * * @param $format * Given format ( eg 'M Y', 'Y M' ). - * return array of qfMapping and date parts for date format. * - * @return array|null|string + * @return string|null + * Smarty translation of the date format. Null is also valid and is translated + * according to the available parts at the smarty layer. */ - public static function &checkBirthDateFormat($format = NULL) { - $birthDateFormat = NULL; - if (!$format) { - $birthDateFormat = self::getDateFormat('birth'); - } - + public static function getDateFieldViewFormat($format) { $supportableFormats = array( 'mm/dd' => '%B %E%f', 'dd-mm' => '%E%f %B', @@ -963,11 +963,56 @@ class CRM_Utils_Date { 'dd/mm/yy' => '%E%f %B %Y', ); - if (array_key_exists($birthDateFormat, $supportableFormats)) { - $birthDateFormat = array('qfMapping' => $supportableFormats[$birthDateFormat]); + return array_key_exists($format, $supportableFormats) ? $supportableFormats[$format] : self::pickBestSmartyFormat($format); + } + + /** + * Pick the smarty format from settings that best matches the time string we have. + * + * For view purposes we historically use the setting that most closely matches the data + * in the format from our settings, as opposed to the setting configured for the field. + * + * @param $format + * @return mixed + */ + public static function pickBestSmartyFormat($format) { + if (stristr($format, 'h')) { + return Civi::settings()->get('dateformatDatetime'); + } + if (stristr($format, 'd') || stristr($format, 'j')) { + return Civi::settings()->get('dateformatFull'); + } + if (stristr($format, 'm')) { + return Civi::settings()->get('dateformatPartial'); } + return Civi::settings()->get('dateformatYear'); + } - return $birthDateFormat; + /** + * Map date plugin and actual format that is used by PHP. + * + * @return array + */ + public static function datePluginToPHPFormats() { + $dateInputFormats = array( + "mm/dd/yy" => 'm/d/Y', + "dd/mm/yy" => 'd/m/Y', + "yy-mm-dd" => 'Y-m-d', + "dd-mm-yy" => 'd-m-Y', + "dd.mm.yy" => 'd.m.Y', + "M d" => 'M j', + "M d, yy" => 'M j, Y', + "d M yy" => 'j M Y', + "MM d, yy" => 'F j, Y', + "d MM yy" => 'j F Y', + "DD, d MM yy" => 'l, j F Y', + "mm/dd" => 'm/d', + "dd-mm" => 'd-m', + "yy-mm" => 'Y-m', + "M yy" => 'M Y', + "yy" => 'Y', + ); + return $dateInputFormats; } /** @@ -1758,7 +1803,7 @@ class CRM_Utils_Date { * http://php.net/manual/en/function.strtotime.php */ public static function getPhpDateFormatFromInputStyleDateFormat($dateFormatString) { - $formats = CRM_Core_SelectValues::datePluginToPHPFormats(); + $formats = CRM_Utils_Date::datePluginToPHPFormats(); return $formats[$dateFormatString]; } @@ -1783,7 +1828,8 @@ class CRM_Utils_Date { $field['date_format'] = $dateAttributes['format']; $field['is_datetime_field'] = TRUE; $field['time_format'] = $dateAttributes['time']; - $field['php_datetime_format'] = CRM_Utils_Date::getPhpDateFormatFromInputStyleDateFormat($field['date_format']); + $field['php_datetime_format'] = $dateAttributes['php_datetime_format']; + $field['smarty_view_format'] = $dateAttributes['smarty_view_format']; if ($field['time_format']) { $field['php_datetime_format'] .= ' H-i-s'; } diff --git a/templates/CRM/Contact/Form/Edit/Demographics.tpl b/templates/CRM/Contact/Form/Edit/Demographics.tpl index 4a563b2fec..1b6113be30 100644 --- a/templates/CRM/Contact/Form/Edit/Demographics.tpl +++ b/templates/CRM/Contact/Form/Edit/Demographics.tpl @@ -37,7 +37,7 @@
{$form.birth_date.label} - {include file="CRM/common/jcalendar.tpl" elementName=birth_date} + {$form.birth_date.html}
{$form.is_deceased.html} @@ -45,7 +45,7 @@
{$form.deceased_date.label} - {include file="CRM/common/jcalendar.tpl" elementName=deceased_date} + {$form.deceased_date.html}
diff --git a/templates/CRM/Contact/Form/Inline/Demographics.tpl b/templates/CRM/Contact/Form/Inline/Demographics.tpl index 315884de97..dbc3b9dbc1 100644 --- a/templates/CRM/Contact/Form/Inline/Demographics.tpl +++ b/templates/CRM/Contact/Form/Inline/Demographics.tpl @@ -37,7 +37,7 @@
{$form.birth_date.label}
- {include file="CRM/common/jcalendar.tpl" elementName=birth_date} + {$form.birth_date.html}
@@ -50,7 +50,7 @@
{$form.deceased_date.label}
- {include file="CRM/common/jcalendar.tpl" elementName=deceased_date} + {$form.deceased_date.html}
diff --git a/templates/CRM/Contact/Page/Inline/Demographics.tpl b/templates/CRM/Contact/Page/Inline/Demographics.tpl index fc9b0f0ae1..a2dabf1fba 100644 --- a/templates/CRM/Contact/Page/Inline/Demographics.tpl +++ b/templates/CRM/Contact/Page/Inline/Demographics.tpl @@ -34,14 +34,12 @@
{ts}Gender{/ts}
{$gender_display}
+
{ts}Date of Birth{/ts}
- {if $birthDateViewFormat} - {$birth_date_display|crmDate:$birthDateViewFormat} - {else} - {$birth_date_display|crmDate} - {/if} + {assign var="date_format" value = $fields.birth_date.smarty_view_format} + {$birth_date|crmDate:$date_format}  
@@ -50,11 +48,7 @@
{ts}Date Deceased{/ts}
- {if $birthDateViewFormat} - {$deceased_date_display|crmDate:$birthDateViewFormat} - {else} - {$deceased_date_display|crmDate} - {/if} + {$deceased_date}
{else} -- 2.25.1