Skip to content

Commit

Permalink
CRM-19124 update birth_date to be consistent at point of hook (use da…
Browse files Browse the repository at this point in the history
…tepicker)
  • Loading branch information
eileenmcnaughton committed Feb 16, 2017
1 parent ae08115 commit aa24aae
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 39 deletions.
26 changes: 17 additions & 9 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,15 +719,15 @@ public static function resolveDefaults(&$defaults, $reverse = FALSE) {
*
* @return CRM_Contact_BAO_Contact
*/
public static function &retrieve(&$params, &$defaults, $microformat = FALSE) {
public static function &retrieve(&$params, &$defaults, $microformat = FALSE, $mode = CRM_Core_Action::UPDATE) {
if (array_key_exists('contact_id', $params)) {
$params['id'] = $params['contact_id'];
}
elseif (array_key_exists('id', $params)) {
$params['contact_id'] = $params['id'];
}

$contact = self::getValues($params, $defaults);
$contact = self::getValues($params, $defaults, $mode);

unset($params['id']);

Expand Down Expand Up @@ -2476,16 +2476,19 @@ public static function getPrimaryOpenId($contactID) {
* Input parameters to find object.
* @param array $values
* Output values of the object.
* @param int $mode
*
* @return CRM_Contact_BAO_Contact|null
* The found object or null
*/
public static function getValues(&$params, &$values) {
public static function getValues(&$params, &$values, $mode = CRM_Core_Action::UPDATE) {
$contact = new CRM_Contact_BAO_Contact();

$contact->copyValues($params);

if ($contact->find(TRUE)) {
$fields = civicrm_api3('Contact', 'getfields', array());
$fields = $fields['values'];

CRM_Core_DAO::storeValues($contact, $values);

Expand Down Expand Up @@ -2517,6 +2520,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 +2544,18 @@ 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;
foreach ($fields as $key => $fieldSpec) {
if (!empty($contact->$key) && isset($fieldSpec['html']['type']) && $fieldSpec['html']['type'] == 'Select Date') {
if ($mode == CRM_Core_Action::VIEW) {
$fieldSpec = CRM_Utils_Date::addDateMetadataToField($fieldSpec, $fieldSpec);
$values[$key] = date($fieldSpec['php_datetime_format'], strtotime($contact->$key));
}
else {
$values[$key] = $contact->$key;
}
}
}

$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
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function setDefaultValues() {
$defaults = $params = array();
$params['id'] = $this->_contactId;

CRM_Contact_BAO_Contact::getValues($params, $defaults);
CRM_Contact_BAO_Contact::getValues($params, $defaults, $this->getAction());

return $defaults;
}
Expand Down
5 changes: 1 addition & 4 deletions CRM/Contact/Page/Inline/Demographics.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function run() {
$params = array('id' => $contactId);

$defaults = array();
CRM_Contact_BAO_Contact::getValues($params, $defaults);
CRM_Contact_BAO_Contact::getValues($params, $defaults, CRM_Core_Action::VIEW);

if (!empty($defaults['gender_id'])) {
$gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
Expand All @@ -58,9 +58,6 @@ public function run() {
$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 @@ -137,7 +137,7 @@ public function view() {

$params['id'] = $params['contact_id'] = $this->_contactId;
$params['noRelationships'] = $params['noNotes'] = $params['noGroups'] = TRUE;
$contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults, TRUE);
$contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults, FALSE, CRM_Core_Action::VIEW);
// Let summary page know if outbound mail is disabled so email links can be built conditionally
$mailingBackend = Civi::settings()->get('mailing_backend');
$this->assign('mailingOutboundOption', $mailingBackend['outBound_option']);
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
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
1 change: 1 addition & 0 deletions CRM/Core/SelectValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@ public static function datePluginToPHPFormats() {
"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',
Expand Down
4 changes: 2 additions & 2 deletions templates/CRM/Contact/Form/Edit/Demographics.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
</div>
<div class="form-item">
<span class="label">{$form.birth_date.label}</span>
<span class="value">{include file="CRM/common/jcalendar.tpl" elementName=birth_date}</span>
<span class="fields">{$form.birth_date.html}</span>
</div>
<div class="form-item">
{$form.is_deceased.html}
{$form.is_deceased.label}
</div>
<div id="showDeceasedDate" class="form-item">
<span class="label">{$form.deceased_date.label}</span>
<span class="value">{include file="CRM/common/jcalendar.tpl" elementName=deceased_date}</span>
<span class="fields">{$form.deceased_date.html}</span>
</div>
</div><!-- /.crm-accordion-body -->
</div><!-- /.crm-accordion-wrapper -->
Expand Down
4 changes: 2 additions & 2 deletions templates/CRM/Contact/Form/Inline/Demographics.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<div class="crm-summary-row">
<div class="crm-label">{$form.birth_date.label}</div>
<div class="crm-content">
{include file="CRM/common/jcalendar.tpl" elementName=birth_date}
{$form.birth_date.html}
</div>
</div>
<div class="crm-summary-row">
Expand All @@ -50,7 +50,7 @@
<div class="crm-summary-row">
<div class="crm-label crm-deceased-date">{$form.deceased_date.label}</div>
<div class="crm-content crm-deceased-date">
{include file="CRM/common/jcalendar.tpl" elementName=deceased_date}
{$form.deceased_date.html}
</div>
</div>
</div>
Expand Down
12 changes: 2 additions & 10 deletions templates/CRM/Contact/Page/Inline/Demographics.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@
<div class="crm-summary-row">
<div class="crm-label">{ts}Date of Birth{/ts}</div>
<div class="crm-content crm-contact-birth_date_display">
{if $birthDateViewFormat}
{$birth_date_display|crmDate:$birthDateViewFormat}
{else}
{$birth_date_display|crmDate}
{/if}
{$birth_date}
&nbsp;
</div>
</div>
Expand All @@ -50,11 +46,7 @@
<div class="crm-summary-row">
<div class="crm-label">{ts}Date Deceased{/ts}</div>
<div class="crm-content crm-contact-deceased_date_display">
{if $birthDateViewFormat}
{$deceased_date_display|crmDate:$birthDateViewFormat}
{else}
{$deceased_date_display|crmDate}
{/if}
{$deceased_date}
</div>
</div>
{else}
Expand Down

0 comments on commit aa24aae

Please sign in to comment.