Attached Files |
date_and_time_in_single_field.patch [^] (7,246 bytes) 2011-09-20 04:01
[Show Content]
Index: admin_templates/incs/form_blocks.tpl
===================================================================
--- admin_templates/incs/form_blocks.tpl (revision 14544)
+++ admin_templates/incs/form_blocks.tpl (working copy)
@@ -381,6 +381,35 @@
</inp2:m_RenderElement>
</inp2:m_DefineElement>
+<inp2:m_DefineElement name="inp_edit_date_time_combined" class="">
+ <inp2:m_RenderElement design="form_row" pass_params="1">
+ <td class="control-cell">
+ <input type="text" name="<inp2:{$prefix}_InputName field='$field'/>" id="<inp2:{$prefix}_InputName field='$field'/>" value="<inp2:{$prefix}_Field field='$field' format='_input_'/>" tabindex="<inp2:m_get param='tab_index'/>" size="<inp2:{$prefix}_Format field='$field' input_format='1' edit_size='edit_size'/>" class="<inp2:m_param name='class'/>" datepickerIcon="<inp2:m_ProjectBase/>core/admin_templates/img/calendar_icon.gif"/>
+
+ <img src="img/calendar_icon.gif" id="cal_img_<inp2:{$prefix}_InputName field="{$field}"/>"
+ style="cursor: pointer; margin-right: 5px"
+ title="Date selector"
+ />
+ <span class="small">(<inp2:{$prefix}_Format field="$field" input_format="1" human="true"/>)</span>
+
+ <script type="text/javascript">
+ Calendar.setup({
+ inputField : "<inp2:{$prefix}_InputName field='{$field}'/>",
+ ifFormat : Calendar.phpDateFormat("<inp2:{$prefix}_Format field='{$field}' input_format='1'/>"),
+ button : "cal_img_<inp2:{$prefix}_InputName field='{$field}'/>",
+ align : "br",
+ singleClick : true,
+ showsTime : true,
+ weekNumbers : false,
+ firstDay : <inp2:m_GetConfig var="FirstDayOfWeek"/>
+ });
+ </script>
+
+ <input type="hidden" name="<inp2:{$prefix}_InputName field='{$field}_combined'/>" value="1"/>
+ </td>
+ </inp2:m_RenderElement>
+</inp2:m_DefineElement>
+
<inp2:m_DefineElement name="textarea_field_caption_element">
<inp2:m_RenderElement name="default_field_caption_element" pass_params="1"/>
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 '';
date_and_time_in_single_field_520.patch [^] (8,261 bytes) 2011-10-04 04:07
[Show Content]
Index: admin_templates/incs/form_blocks.tpl
===================================================================
--- admin_templates/incs/form_blocks.tpl (revision 14590)
+++ admin_templates/incs/form_blocks.tpl (working copy)
@@ -381,6 +381,35 @@
</inp2:m_RenderElement>
</inp2:m_DefineElement>
+<inp2:m_DefineElement name="inp_edit_date_time_combined" class="">
+ <inp2:m_RenderElement design="form_row" pass_params="1">
+ <td class="control-cell">
+ <input type="text" name="<inp2:{$prefix}_InputName field='$field'/>" id="<inp2:{$prefix}_InputName field='$field'/>" value="<inp2:{$prefix}_Field field='$field' format='_input_'/>" tabindex="<inp2:m_get param='tab_index'/>" size="<inp2:{$prefix}_Format field='$field' input_format='1' edit_size='edit_size'/>" class="<inp2:m_param name='class'/>" datepickerIcon="<inp2:m_ProjectBase/>core/admin_templates/img/calendar_icon.gif"/>
+
+ <img src="img/calendar_icon.gif" id="cal_img_<inp2:{$prefix}_InputName field="{$field}"/>"
+ style="cursor: pointer; margin-right: 5px"
+ title="Date selector"
+ />
+ <span class="small">(<inp2:{$prefix}_Format field="$field" input_format="1" human="true"/>)</span>
+
+ <script type="text/javascript">
+ Calendar.setup({
+ inputField : "<inp2:{$prefix}_InputName field='{$field}'/>",
+ ifFormat : Calendar.phpDateFormat("<inp2:{$prefix}_Format field='{$field}' input_format='1'/>"),
+ button : "cal_img_<inp2:{$prefix}_InputName field='{$field}'/>",
+ align : "br",
+ singleClick : true,
+ showsTime : true,
+ weekNumbers : false,
+ firstDay : <inp2:m_GetConfig var="FirstDayOfWeek"/>
+ });
+ </script>
+
+ <input type="hidden" name="<inp2:{$prefix}_InputName field='{$field}_combined'/>" value="1"/>
+ </td>
+ </inp2:m_RenderElement>
+</inp2:m_DefineElement>
+
<inp2:m_DefineElement name="textarea_field_caption_element">
<inp2:m_RenderElement name="default_field_caption_element" pass_params="1"/>
Index: kernel/utility/formatters/date_formatter.php
===================================================================
--- kernel/utility/formatters/date_formatter.php (revision 14596)
+++ kernel/utility/formatters/date_formatter.php (working copy)
@@ -43,7 +43,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';
}
@@ -101,7 +101,7 @@
}
/**
- * The method is supposed to alter config options or cofigure object in some way based on its usage of formatters
+ * 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
*
@@ -119,37 +119,39 @@
$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 ('type', 'default', 'required', 'use_timezone', 'error_msgs');
foreach ($copy_options as $copy_option) {
- if (array_key_exists($copy_option, $field_options) ) {
+ if ( array_key_exists($copy_option, $field_options) ) {
$opts[$copy_option] = $field_options[$copy_option];
}
}
- $add_fields[$field_name.'_date'] = $opts;
+ $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;
+ $add_fields[$field_name . '_time'] = $opts;
$filter_type = getArrayValue($field_options, 'filter_type');
- if($filter_type == 'range')
- {
+ if ( $filter_type == 'range' ) {
$opts['format'] = $field_options['format'];
- $add_fields[$field_name.'_rangefrom'] = $opts;
- $add_fields[$field_name.'_rangeto'] = $opts;
+ $add_fields[$field_name . '_rangefrom'] = $opts;
+ $add_fields[$field_name . '_rangeto'] = $opts;
}
if ( !$object->isVirtualField($field_name) ) {
- // adding caluclated field to format date directly in the query
- $object->addCalculatedField($field_name.'_date', '%1$s.'.$field_name);
- $object->addCalculatedField($field_name.'_time', '%1$s.'.$field_name);
+ // adding calculated field to format date directly in the query
+ $object->addCalculatedField($field_name . '_date', '%1$s.' . $field_name);
+ $object->addCalculatedField($field_name . '_time', '%1$s.' . $field_name);
}
$virtual_fields = $object->getVirtualFields();
@@ -191,20 +193,28 @@
*/
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
- // 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');
- if ($empty_time === false) {
+ $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 ( $object->GetDBField($sub_fields['date']) != '' && $object->GetDBField($sub_fields['time']) == '' ) {
+ // when time is not supplied, then use "midnight" (or unit config override)
+ $empty_time = getArrayValue($options, 'empty_time');
+ if ( $empty_time === false ) {
$empty_time = adodb_mktime(0, 0, 0);
}
$object->SetDBField($sub_fields['time'], $empty_time);
}
- elseif ($object->GetDBField($sub_fields['time']) != '' && $object->GetDBField($sub_fields['date']) == '') {
- $empty_date = getArrayValue($options,'empty_date');
- if ($empty_date === false) {
+ elseif ( $object->GetDBField($sub_fields['time']) != '' && $object->GetDBField($sub_fields['date']) == '' ) {
+ // when date is not supplied, then use "1970-01-01 00:00:00" instead (or unit config override)
+ $empty_date = getArrayValue($options, 'empty_date');
+ if ( $empty_date === false ) {
$empty_date = adodb_mktime(0, 0, 0, 1, 1, 1970);
}
$object->SetDBField($sub_fields['date'], $empty_date);
@@ -213,13 +223,8 @@
$input_format['date'] = $object->GetFieldOption($sub_fields['date'], 'input_format');
$input_format['time'] = $object->GetFieldOption($sub_fields['time'], 'input_format');
- $object->SetField($field, $object->GetField($sub_fields['date'], $input_format['date']).$options['date_time_separator'].$object->GetField($sub_fields['time'], $input_format['time']));
+ $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);
- }
}
/**
|