Commit d57f19a6 authored by merlinofchaos's avatar merlinofchaos

97290: New options for granularity on date sorts

parent 0222dbb4
......@@ -27,3 +27,4 @@ Views 1.1-dev
o 88849: new views_pre_query hook
o 97290: new $offset argument to views_build_view to allow views to to
skip the first N nodes [doesn't work if $ pager = true].
o 97290: New options for granularity on date sorts
......@@ -65,6 +65,8 @@ function comment_views_tables() {
'sorts' => array(
'timestamp' => array(
'name' => t('Comment: Created Time'),
'handler' => 'views_handler_sort_date',
'option' => views_handler_sort_date_options(),
'help' => t('Sort by the created time of comments.'),
),
),
......@@ -128,6 +130,8 @@ function comment_views_tables() {
'sorts' => array(
'last_comment_timestamp' => array(
'name' => t('Comment: Last Comment Date'),
'handler' => 'views_handler_sort_date',
'option' => views_handler_sort_date_options(),
'help' => t('This will allow you to sort by the date of the most recent comment on a node.')
),
'comment_count' => array(
......
......@@ -101,10 +101,14 @@ function node_views_tables() {
),
'created' => array(
'name' => t('Node: Created Time'),
'handler' => 'views_handler_sort_date',
'option' => views_handler_sort_date_options(),
'help' => t('Sort by the submission date of the node.'),
),
'changed' => array(
'name' => t('Node: Updated Time'),
'handler' => 'views_handler_sort_date',
'option' => views_handler_sort_date_options(),
'help' => t('Sort by the last update date of the node.'),
),
'sticky' => array(
......
......@@ -48,6 +48,8 @@ function statistics_views_tables() {
),
'timestamp' => array(
'name' => t('Node: Last Hit Time'),
'handler' => 'views_handler_sort_date',
'option' => views_handler_sort_date_options(),
'help' => t('This allows you to sort by the time a node was last read.'),
),
),
......
......@@ -1685,7 +1685,7 @@ function theme_views_nodate() {
* Provide a list of all standard supproted date output handlers.
*/
function views_handler_field_dates() {
return array(
return array(
'views_handler_field_date_small' => t('As Short Date'),
'views_handler_field_date' => t('As Medium Date'),
'views_handler_field_date_large' => t('As Long Date'),
......@@ -1694,6 +1694,59 @@ return array(
);
}
function views_handler_sort_date_options() {
return array(
'#type' => 'select',
'#options' => array(
'normal' => t('Normal'),
'minute' => t('Granularity: minute'),
'hour' => t('Granularity: hour'),
'day' => t('Granularity: day'),
'month' => t('Granularity: month'),
'year' => t('Granularity: year'),
),
);
}
function views_handler_sort_date($op, &$query, $sortinfo, $sort) {
switch($sort['options']) {
case 'normal':
default:
$table = $sortinfo['table'];
$field = $sortinfo['field'];
break;
case 'minute':
$field = "DATE_FORMAT(FROM_UNIXTIME($sortinfo[table].$sortinfo[field]), '%Y%m%%d%H%m')";
break;
case 'hour':
$field = "DATE_FORMAT(FROM_UNIXTIME($sortinfo[table].$sortinfo[field]), '%Y%m%%d%H')";
break;
case 'day':
$field = "DATE_FORMAT(FROM_UNIXTIME($sortinfo[table].$sortinfo[field]), '%Y%m%%d')";
break;
case 'month':
$field = "DATE_FORMAT(FROM_UNIXTIME($sortinfo[table].$sortinfo[field]), '%Y%m%)";
break;
case 'year':
$field = "DATE_FORMAT(FROM_UNIXTIME($sortinfo[table].$sortinfo[field]), '%Y%')";
break;
}
$alias = $as = $sortinfo['table'] . '_' . $sortinfo['field'];
if (!$table) {
$as .= '_orderby';
$alias = $field;
}
$query->add_field($field, $table, $as);
$query->orderby[] = "$alias $sort[sortorder]";
// $query->add_orderby($table, $field, $sort['sortorder']);
}
function views_handler_sort_date_minute($op, &$query, $sortinfo, $sort) {
$field = "DATE_FORMAT(FROM_UNIXTIME($table.$sortinfo[field]), '%Y%m%%d%H%m')";
$query->add_orderby(NULL, $field, $sort['sortorder']);
}
/**
* Format a field as an integer.
*/
......
......@@ -192,9 +192,9 @@ function _views_view_build_sorts(&$query, $view) {
$fieldbits = explode('.', $sort['field']);
$field = $fieldbits[1];
}
$table = $sortinfo['table'];
if (isset($sortinfo['handler']) && function_exists($sortinfo['handler'])) {
$sortinfo['field'] = $field;
$sortinfo['handler']('sort', $query, $sortinfo, $sort);
}
else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment