),
);
+ // @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;
$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;
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);
}
/**
$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);
$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;
$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;
$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'])) {
// 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) {
* - 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();
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;
$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);
+ }
+
}
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;
}
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.
*
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);
}
}
/**
- * 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',
'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;
}
/**
* 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];
}
$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';
}
</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}
</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 -->
<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">
<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>
<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}
</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}