Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

Commit

Permalink
Implemented _hdlr_order_date() to allow <mt:OrderDate> to ignore …
Browse files Browse the repository at this point in the history
…the `utc` attribute if provided, so the timestamp doesn't get double offset.

Signed-off-by: Charlie Gorichanaz <[email protected]>
  • Loading branch information
CNG committed Oct 9, 2011
1 parent ac56771 commit 679451a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
13 changes: 6 additions & 7 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ example, to show the last 30 unique entries and ActionStreams items:
</mt:OrderHeader>

<mt:OrderDateHeader>
<div id="one_day">
<p class="date"><mt:OrderDate utc="1" format="%B %e, %Y"></p>
<div id="one_day">
<p class="date"><mt:OrderDate format="%B %e, %Y"></p>
</mt:OrderDateHeader>

<mt:OrderDateFooter>
Expand Down Expand Up @@ -177,21 +177,22 @@ even an `mt:OrderItem` pinned to the end with the `pin="-1"` attribute.
## `mt:OrderDateHeader` ##

A container tag whose contents will be displayed before the `mt:OrderItem` in context
if it is the first item for a given day. Requires `order_by` variable set inside the
if it is the first item for a given day. Requires `order_by` variable set inside the
`mt:OrderItem` tag to be a timestamp formatted `%Y%m%d%H%M%S`.


## `mt:OrderDateFooter` ##

A container tag whose contents will be displayed after the `mt:OrderItem` in context
if it is the last item for a given day. Requires `order_by` variable set inside the
if it is the last item for a given day. Requires `order_by` variable set inside the
`mt:OrderItem` tag to be a timestamp formatted `%Y%m%d%H%M%S`.


## `mt:OrderDate` ##

A function tag that works like an `mt:Date` tag, for use within `mt:OrderDateHeader`
and `mt:OrderDateFooter` blocks.
and `mt:OrderDateFooter` blocks. Does not take a `utc` attribute or support the
`relative` option of the `format` attribute.


# Changes #
Expand All @@ -200,8 +201,6 @@ and `mt:OrderDateFooter` blocks.

* Added `mt:OrderDateHeader` and `mt:OrderDateFooter` tags.
* Added `mt:OrderDate` tag.
* Added `unique` ordering option.


## 1.1 10 June 2010 ##

Expand Down
2 changes: 1 addition & 1 deletion plugins/Order/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ tags:
OrderDateHeader: $Order::Order::Plugin::tag_order_date_header
OrderDateFooter: $Order::Order::Plugin::tag_order_date_footer
function:
OrderDate: $Core::MT::Template::Context::_hdlr_date
OrderDate: $Order::Order::Plugin::_hdlr_order_date
73 changes: 65 additions & 8 deletions plugins/Order/lib/Order/Plugin.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package Order::Plugin;

use MT::Util qw(format_ts); # Required by _hdlr_order_date() below

sub _natural_sort {
sort {
$a->[0] =~ m{ \A [+-]? \d+ }xms && $b->[0] =~ m{ \A [+-]? \d+ }xms
Expand Down Expand Up @@ -52,7 +53,7 @@ sub _sort_group_ids {

sub tag_order {
my ($ctx, $args, $cond) = @_;

my %groups = ( items => [] );
local $ctx->{__stash}{order_items} = \%groups;
local $ctx->{__stash}{order_by_var} = $args->{by} || 'order_by';
Expand All @@ -67,7 +68,7 @@ sub tag_order {
my $items = delete $groups{items};
my $sort = sort_function_for_args($args);
my @objs = $sort->(@$items);

# Inject the pinned groups from first place (0) to last (-1).
for my $i (sort _sort_group_ids keys %groups) {
my $items = $groups{$i};
Expand All @@ -84,7 +85,7 @@ sub tag_order {
push @objs, $sort->(@$items);
}
}

if (my $offset = $args->{offset}) {
# Delete the first $offset items.
splice @objs, 0, $offset;
Expand Down Expand Up @@ -125,7 +126,7 @@ sub tag_order {
return $ctx->error( $builder->errstr ) unless defined $result;
$f_html = $result;
}
$objs[$i][1] = $h_html.$o->[1].$f_html;
$objs[$i][1] = $h_html.$o->[1].$f_html;
$yesterday = $today;
$i++;
}
Expand Down Expand Up @@ -169,7 +170,7 @@ sub tag_order_date_footer {

sub tag_order_item {
my ($ctx, $args, $cond) = @_;

my $group_id = defined $args->{pin} ? int $args->{pin} : 'items';

my $order_var = $ctx->stash('order_by_var');
Expand All @@ -179,11 +180,67 @@ sub tag_order_item {

my $order_value = $ctx->var($order_var) || q{};
$order_value =~ s{ \A \s+ | \s+ \z }{}xmsg;

my $groups = $ctx->stash('order_items');
my $group = ($groups->{$group_id} ||= []);
push @$group, [ $order_value, $output];

}

sub _hdlr_order_date {
## This was taken from the Melody project's source code at
## https://github.com/openmelody/melody/blob/master/lib/MT/Template/ContextHandlers.pm#L11358
## Code to handle relative dates and the UTC attribute were removed
## Any bugs found in that code likely will need to be fixed here as well
my ( $ctx, $args ) = @_;
my $ts = $args->{ts} || $ctx->{current_timestamp};
my $tag = $ctx->stash('tag');
return
$ctx->error(
MT->translate(
"You used an [_1] tag without a date context set up.",
"MT$tag"
)
) unless defined $ts;
my $blog = $ctx->stash('blog');
unless ( ref $blog ) {
my $blog_id = $blog || $args->{offset_blog_id};
if ($blog_id) {
$blog = MT->model('blog')->load($blog_id);
return $ctx->error(
MT->translate( 'Can\'t load blog #[_1].', $blog_id ) )
unless $blog;
}
}
my $lang
= $args->{language}
|| $ctx->var('local_lang_id')
|| ( $blog && $blog->language );
if ( my $format = lc( $args->{format_name} || '' ) ) {
my $tz = 'Z';
my $so = ( $blog && $blog->server_offset )
|| MT->config->TimeOffset;
my $partial_hour_offset = 60 * abs( $so - int($so) );
if ( $format eq 'rfc822' ) {
$tz = sprintf( "%s%02d%02d",
$so < 0 ? '-' : '+',
abs($so), $partial_hour_offset );
}
elsif ( $format eq 'iso8601' ) {
$tz = sprintf( "%s%02d:%02d",
$so < 0 ? '-' : '+',
abs($so), $partial_hour_offset );
}
if ( $format eq 'rfc822' ) {
## RFC-822 dates must be in English.
$args->{'format'} = '%a, %d %b %Y %H:%M:%S ' . $tz;
$lang = 'en';
}
elsif ( $format eq 'iso8601' ) {
$args->{format} = '%Y-%m-%dT%H:%M:%S' . $tz;
}
} ## end if ( my $format = lc( ...))
my $mail_flag = $args->{mail} || 0;
return format_ts( $args->{'format'}, $ts, $blog, $lang, $mail_flag );
}

1;

0 comments on commit 679451a

Please sign in to comment.