diff --git a/date.install b/date.install index 9a9b6d3..aa71008 100644 --- a/date.install +++ b/date.install @@ -51,6 +51,19 @@ function date_field_schema($field) { // We don't want Field API to create additional columns, just the first. // We modify them our own way in views data. $db_columns['value2']['views'] = FALSE; + + // If the end date is optional, we need an extra field to know when the end + // date is really filled. + if ($field['settings']['todate'] === 'optional') { + $db_columns['has_todate'] = array( + 'type' => 'int', + 'size' => 'tiny', + 'not null' => TRUE, + 'default' => 0, + 'sortable' => TRUE, + 'views' => TRUE, + ); + } } // Timezone and offset columns are used only if date-specific dates are used. if (isset($field['settings']['tz_handling']) && $field['settings']['tz_handling'] == 'date') { @@ -204,3 +217,50 @@ function date_update_7004() { field_cache_clear(); drupal_set_message(t('Date text widgets have been updated to use an increment of 1.')); } + +/** + * Add the has_todate column to all fields that have optional end date. + */ +function date_update_7005() { + $tables = array(); + $column = array( + 'type' => 'int', + 'size' => 'tiny', + 'not null' => TRUE, + 'default' => 0, + 'sortable' => TRUE, + 'views' => TRUE, + ); + + foreach (field_info_fields() as $field) { + if ($field['module'] === 'date') { + if (!empty($field['settings']['todate']) && $field['settings']['todate'] === 'optional') { + $has_todate = _field_sql_storage_columnname($field['field_name'], 'has_todate'); + $tables[$field['field_name']] = array( + _field_sql_storage_tablename($field), + _field_sql_storage_revision_tablename($field), + ); + + foreach ($tables[$field['field_name']] as $table) { + db_add_field($table, $has_todate, $column); + } + } + } + } + + field_cache_clear(); + drupal_get_schema(NULL, TRUE); + + // Now it is time to update the values of the new column. + foreach ($tables as $field_name => $ts) { + foreach ($ts as $table) { + $value = _field_sql_storage_columnname($field_name, 'value'); + $value2 = _field_sql_storage_columnname($field_name, 'value2'); + $has_todate = _field_sql_storage_columnname($field_name, 'has_todate'); + + // Update the value of this new column. Make use of db_query to take + // care of large sites that makes a huge usage of date fields. + db_query("UPDATE {{$table}} SET `$has_todate` = :to_date WHERE `$value` != `$value2`", array(':to_date' => 1)); + } + } +} diff --git a/date_elements.inc b/date_elements.inc index b99f2c3..61cd7b7 100644 --- a/date_elements.inc +++ b/date_elements.inc @@ -532,11 +532,16 @@ function date_combo_validate($element, &$form_state) { } } - // If the user chose the option to not show the end date, just swap in the - // start date as that value so the start and end dates are the same. - if ($field['settings']['todate'] == 'optional' && empty($item['show_todate'])) { - $item[$to_field] = $item[$from_field]; - $posted[$to_field] = $posted[$from_field]; + if ($field['settings']['todate'] == 'optional') { + $item['has_todate'] = 1; + + // If the user chose the option to not show the end date, just swap in the + // start date as that value so the start and end dates are the same. + if (empty($item['show_todate'])) { + $item[$to_field] = $item[$from_field]; + $posted[$to_field] = $posted[$from_field]; + $item['has_todate'] = 0; + } } if ($empty) {