Skip to content

Commit

Permalink
Merge pull request civicrm#9847 from eileenmcnaughton/cust_date_birth
Browse files Browse the repository at this point in the history
CRM-19124 update birth_date to be consistent at point of hook (use da…
  • Loading branch information
eileenmcnaughton authored Mar 2, 2017
2 parents 7012939 + 9f2b179 commit 667ade8
Show file tree
Hide file tree
Showing 20 changed files with 153 additions and 143 deletions.
9 changes: 1 addition & 8 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,7 @@ public static function getValues(&$params, &$values) {
),
);

// @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;
Expand All @@ -2540,14 +2541,6 @@ public static function getValues(&$params, &$values) {
$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;
Expand Down
4 changes: 2 additions & 2 deletions CRM/Contact/Form/Edit/Demographics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions CRM/Contact/Page/Inline/Demographics.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ public function run() {
$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);

Expand Down
5 changes: 1 addition & 4 deletions CRM/Contact/Page/View/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ public function 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;
Expand Down Expand Up @@ -259,9 +259,6 @@ public function 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;
Expand Down
4 changes: 2 additions & 2 deletions CRM/Contact/Page/View/Vcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public function run() {
$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'])) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ private static function formatDisplayValue($value, $field, $entityId = NULL) {
// 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) {
Expand Down
53 changes: 28 additions & 25 deletions CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,8 @@ protected static function formatUFField(
if (CRM_Utils_Array::value('html_type', $formattedField) == 'Select Date') {
$formattedField['date_format'] = $customFields[$field->field_name]['date_format'];
$formattedField['time_format'] = $customFields[$field->field_name]['time_format'];
$formattedField['php_datetime_format'] = CRM_Utils_Date::getPhpDateFormatFromInputStyleDateFormat($customFields[$field->field_name]['date_format']);
if ($formattedField['time_format']) {
$formattedField['php_datetime_format'] .= ' H-i-s';
}
$formattedField['is_datetime_field'] = TRUE;
$formattedField['smarty_view_format'] = CRM_Utils_Date::getDateFieldViewFormat($formattedField['date_format']);
}

$formattedField['is_multi_summary'] = $field->is_multi_summary;
Expand Down Expand Up @@ -2276,6 +2273,11 @@ public static function buildProfile(
if (substr($fieldName, 0, 3) === 'is_' or substr($fieldName, 0, 7) === 'do_not_') {
$form->add('advcheckbox', $name, $title, $attributes, $required);
}
elseif (CRM_Utils_Array::value('html_type', $field) === 'Select Date') {
$extra = isset($field['datepicker']) ? $field['datepicker']['extra'] : CRM_Utils_Date::getDatePickerExtra($field);
$attributes = isset($field['datepicker']) ? $field['datepicker']['attributes'] : CRM_Utils_Date::getDatePickerAttributes($field);
$form->add('datepicker', $name, $title, $attributes, $required, $extra);
}
else {
$form->add('text', $name, $title, $attributes, $required);
}
Expand Down Expand Up @@ -2328,8 +2330,6 @@ public static function buildProfile(
*/
static public function getNonUpgradedDateFields() {
return array(
'birth_date' => 'birth',
'deceased_date' => 'birth',
'membership_start_date' => 'activityDate',
'membership_end_date' => 'activityDate',
'join_date' => 'activityDate',
Expand Down Expand Up @@ -2392,10 +2392,7 @@ public static function setProfileDefaults(
if (!empty($details[$name]) || isset($details[$name])) {
//to handle custom data (checkbox) to be written
// to handle birth/deceased date, greeting_type and few other fields
if (($name == 'birth_date') || ($name == 'deceased_date')) {
list($defaults[$fldName]) = CRM_Utils_Date::setDateDefaults($details[$name], 'birth');
}
elseif (in_array($name, CRM_Contact_BAO_Contact::$_greetingTypes)) {
if (in_array($name, CRM_Contact_BAO_Contact::$_greetingTypes)) {
$defaults[$fldName] = $details[$name . '_id'];
$defaults[$name . '_custom'] = $details[$name . '_custom'];
}
Expand Down Expand Up @@ -2451,24 +2448,30 @@ public static function setProfileDefaults(
break;

case 'Select Date':
// CRM-6681, set defult values according to date and time format (if any).
$dateFormat = NULL;
if (!empty($customFields[$customFieldId]['date_format'])) {
$dateFormat = $customFields[$customFieldId]['date_format'];
}

if (empty($customFields[$customFieldId]['time_format'])) {
list($defaults[$fldName]) = CRM_Utils_Date::setDateDefaults($details[$name], NULL,
$dateFormat
);
if (!in_array($name, array_keys(self::getNonUpgradedDateFields()))) {
$defaults[$fldName] = $details[$name];
}
else {
$timeElement = $fldName . '_time';
if (substr($fldName, -1) == ']') {
$timeElement = substr($fldName, 0, -1) . '_time]';
// Do legacy handling.
// CRM-6681, set defult values according to date and time format (if any).
$dateFormat = NULL;
if (!empty($customFields[$customFieldId]['date_format'])) {
$dateFormat = $customFields[$customFieldId]['date_format'];
}

if (empty($customFields[$customFieldId]['time_format'])) {
list($defaults[$fldName]) = CRM_Utils_Date::setDateDefaults($details[$name], NULL,
$dateFormat
);
}
else {
$timeElement = $fldName . '_time';
if (substr($fldName, -1) == ']') {
$timeElement = substr($fldName, 0, -1) . '_time]';
}
list($defaults[$fldName], $defaults[$timeElement]) = CRM_Utils_Date::setDateDefaults($details[$name],
NULL, $dateFormat, $customFields[$customFieldId]['time_format']);
}
list($defaults[$fldName], $defaults[$timeElement]) = CRM_Utils_Date::setDateDefaults($details[$name],
NULL, $dateFormat, $customFields[$customFieldId]['time_format']);
}
break;

Expand Down
24 changes: 19 additions & 5 deletions CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1366,11 +1366,16 @@ public function addSelect($name, $props = array(), $required = FALSE) {
* - 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();
Expand Down Expand Up @@ -1455,10 +1460,19 @@ public function addField($name, $props = array(), $required = FALSE) {
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;
Expand Down
24 changes: 24 additions & 0 deletions CRM/Core/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,28 @@ public function setVar($name, $value) {
$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);
}

}
29 changes: 2 additions & 27 deletions CRM/Core/SelectValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,14 @@ public static function date($type = NULL, $format = NULL, $minOffset = NULL, $ma
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['smarty_view_format'] = CRM_Utils_Date::getDateFieldViewFormat($date['format']);
if (!isset($date['time'])) {
$date['time'] = FALSE;
}
Expand Down Expand Up @@ -762,32 +763,6 @@ public static function getDatePluginInputFormats() {
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.
*
Expand Down
4 changes: 0 additions & 4 deletions CRM/Core/Smarty/plugins/modifier.crmDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ function smarty_modifier_crmDate($dateString, $dateFormat = NULL, $onlyTime = FA
$config = CRM_Core_Config::singleton();
$dateFormat = $config->dateformatTime;
}
// Handle possibility we only have a date function style date format.
if ($dateFormat && !stristr($dateFormat, '%')) {
return date($dateFormat, strtotime($dateString));
}

return CRM_Utils_Date::customFormat($dateString, $dateFormat);
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Profile/Page/MultipleRecordFieldsListing.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function browse() {
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);
}
Expand Down
Loading

0 comments on commit 667ade8

Please sign in to comment.