CRM-19124 update birth_date to be consistent at point of hook (use datepicker)
authoreileen <emcnaughton@wikimedia.org>
Tue, 14 Feb 2017 12:38:59 +0000 (01:38 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 27 Feb 2017 02:38:55 +0000 (15:38 +1300)
14 files changed:
CRM/Contact/BAO/Contact.php
CRM/Contact/Form/Edit/Demographics.php
CRM/Contact/Page/Inline/Demographics.php
CRM/Contact/Page/View/Summary.php
CRM/Contact/Page/View/Vcard.php
CRM/Core/BAO/CustomField.php
CRM/Core/Form.php
CRM/Core/Page.php
CRM/Core/SelectValues.php
CRM/Profile/Page/MultipleRecordFieldsListing.php
CRM/Utils/Date.php
templates/CRM/Contact/Form/Edit/Demographics.tpl
templates/CRM/Contact/Form/Inline/Demographics.tpl
templates/CRM/Contact/Page/Inline/Demographics.tpl

index e1f299cc63019348822013852926e8200d7904d5..7f834eb93fab5ffe969a5e087cd8c8853433c33d 100644 (file)
@@ -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;
index d01495d597a31ad5913e09ff11ba5847de8e7528..e96acc7e2246ad780c26a1d2b3ab7a68f2d0beb7 100644 (file)
@@ -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);
   }
 
   /**
index 33a43adbb4cc2dcc958999e88dce7cd6cbda4eb1..373ff401883c249aa843482dacc86fc3e0c1dad8 100644 (file)
@@ -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);
 
index 7a633d1e49cee1d7687c3130843729f8392c74b5..2e9344224cfcaa6f58ff9f0b39612fa956ae22e7 100644 (file)
@@ -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;
index 100278ef2f7824cb41e61fe31a1f8ac8b3bd1bc9..0a7b2cce4f162bd22b5577dfe81e051c09e6193d 100644 (file)
@@ -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'])) {
index 82bfee3e06f85f7b84da22756e56324374af02d2..b5dff555e58838ba52eb27834c87f0d3060c1c01 100644 (file)
@@ -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) {
index 68d4ceabea4575ac43618b789175d68075324b29..12c864b158b8a206d2ffd30a3b8f38329045344e 100644 (file)
@@ -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;
index 4167e81023662aa9cc69b70955be211cf9233706..fe0f2b0732727357f70dfae760109868e3fa81cd 100644 (file)
@@ -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);
+  }
+
 }
index 344219ef5aba0beb9c0b28f5e5179868253bd737..49e5f4f44651d583e22ee3454eb480e9fb912c79 100644 (file)
@@ -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.
    *
index 3e29e7f7d83de85091dc9cc5d1d6934bc6790d8d..49d3a214bf290ea9f128bb1e0de7748efb7a658c 100644 (file)
@@ -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);
         }
index 3d8ed11f939a527e421dbc5f150a0a95c8d846e5..a7c37d5dfdb59aab26f30f47357e5b427e02c106 100644 (file)
@@ -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';
           }
index 4a563b2fec48883b7cb8af27d36634883d59cffa..1b6113be306913c8353466b832105a7ef19ca164 100644 (file)
@@ -37,7 +37,7 @@
   </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}
@@ -45,7 +45,7 @@
   </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 -->
index 315884de9739d8bc25ef12ca255fe9b6f4ce830a..dbc3b9dbc14c1f648512c47bb2ed04681d3ebd73 100644 (file)
@@ -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">
@@ -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>
index fc9b0f0ae128988a381b034dc8d61ae1bbf2b0cf..a2dabf1fba5cb1f4ec5609f747af3bee8e85144a 100644 (file)
       <div class="crm-label">{ts}Gender{/ts}</div>
       <div class="crm-content crm-contact-gender_display">{$gender_display}</div>
     </div>
+
     <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}
+         {assign var="date_format" value = $fields.birth_date.smarty_view_format}
+         {$birth_date|crmDate:$date_format}
           &nbsp;
       </div>
     </div>
           <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}