+
+
+
Index: kernel/utility/formatters/date_formatter.php
===================================================================
--- kernel/utility/formatters/date_formatter.php (revision 14544)
+++ kernel/utility/formatters/date_formatter.php (working copy)
@@ -37,7 +37,7 @@
function SetMixedFormat(&$field_options, &$format, $type)
{
if (!isset($field_options[$type])) {
- // default value is date+sepatator+time
+ // default value is date+separator+time
$field_options[$type] = '_regional_DateTimeFormat';
}
@@ -94,6 +94,15 @@
}
}
+ /**
+ * The method is supposed to alter config options or configure object in some way based on its usage of formatters
+ * The methods is called for every field with formatter defined when configuring item.
+ * Could be used for adding additional VirtualFields to an object required by some special Formatter
+ *
+ * @param string $field_name
+ * @param array $field_options
+ * @param kDBBase $object
+ */
function PrepareOptions($field_name, &$field_options, &$object)
{
list ($display_format, $input_format) = $this->GetSeparateFormats($field_options, 'mixed');
@@ -104,9 +113,12 @@
$field_options['use_timezone'] = true;
}
- $add_fields = Array();
+ // 1. add field to indicate, that date is already combined into one field
+ $add_fields = Array(
+ $field_name . '_combined' => Array ('type' => 'int', 'default' => 0),
+ );
- // 1. add DATE virtual field
+ // 2. add DATE virtual field
$opts = Array('master_field' => $field_name, 'formatter' => 'kDateFormatter', 'format' => $display_format['date'], 'input_format' => $input_format['date']);
$copy_options = Array ('default', 'required', 'use_timezone', 'error_msgs');
@@ -118,7 +130,7 @@
$add_fields[$field_name.'_date'] = $opts;
- // 2. add TIME virtual field
+ // 3. add TIME virtual field
$opts['format'] = $display_format['time'];
$opts['input_format'] = $input_format['time'];
$add_fields[$field_name.'_time'] = $opts;
@@ -132,7 +144,7 @@
}
if ( !isset($object->VirtualFields[$field_name]) ) {
- // adding caluclated field to format date directly in the query
+ // adding calculated field to format date directly in the query
if ( !isset($object->CalculatedFields) || !is_array($object->CalculatedFields) ) {
$object->CalculatedFields = Array();
}
@@ -149,6 +161,15 @@
$object->setVirtualFields($add_fields);
}
+ /**
+ * Used for split fields like timestamp -> date, time
+ * Called from DBItem to update sub fields values after loading item
+ *
+ * @param string $field
+ * @param string $value
+ * @param Array $options
+ * @param kDBItem $object
+ */
function UpdateSubFields($field, $value, &$options, &$object)
{
if ( $sub_fields = getArrayValue($options, 'sub_fields') ) {
@@ -160,11 +181,29 @@
}
}
+ /**
+ * Used for split fields like timestamp -> date, time
+ * Called from DBItem Validate (before validation) to get back master field value from its sub_fields
+ *
+ * @param string $field
+ * @param mixed $value
+ * @param Array $options
+ * @param kDBItem $object
+ */
function UpdateMasterFields($field, $value, &$options, &$object)
{
- // when in master field - set own value from sub_fields
- if ( $sub_fields = getArrayValue($options, 'sub_fields') ) {
- // if date is not empty, but time is empty - set time to 0, otherwise master field fomratter will complain
+ $sub_fields = getArrayValue($options, 'sub_fields');
+ $master_field = getArrayValue($options, 'master_field');
+
+ if ( $master_field ) {
+ // when in one of sub_fields - call update for master_field to update its value from sub_fields [are you following ? :) ]
+ $opt = $object->GetFieldOptions($master_field);
+ $this->UpdateMasterFields($master_field, null, $opt, $object);
+ }
+ elseif ( $sub_fields && !$object->GetDBField($field . '_combined') ) {
+ // when in master field - set own value from sub_fields
+
+ // if date is not empty, but time is empty - set time to 0, otherwise master field formatter will complain
// when we have only date field on form, we need time hidden field always empty, don't ask me why!
if ($object->GetDBField($sub_fields['date']) != '' && $object->GetDBField($sub_fields['time']) == '') {
$empty_time = getArrayValue($options,'empty_time');
@@ -186,14 +225,17 @@
$object->SetField($field, $object->GetField($sub_fields['date'], $input_format['date']).$options['date_time_separator'].$object->GetField($sub_fields['time'], $input_format['time']));
}
- // when in one of sub_fields - call update for master_field to update its value from sub_fields [are you following ? :) ]
- elseif ($master_field = getArrayValue($options, 'master_field') ) {
- $opt = $object->GetFieldOptions($master_field);
- $this->UpdateMasterFields($master_field, null, $opt, $object);
- }
}
-//function Format($value, $options, &$errors)
+ /**
+ * Formats given field value
+ *
+ * @param string $value
+ * @param string $field_name
+ * @param kDBItem $object
+ * @param string $format
+ * @return string
+ */
function Format($value, $field_name, &$object, $format=null)
{
if ( is_null($value) ) return '';