Skip to content

Commit

Permalink
Merge pull request markpasc#1 from CNG/master
Browse files Browse the repository at this point in the history
Add OrderDateHeader and OrderDateFooter tags.
  • Loading branch information
markpasc committed Oct 11, 2011
2 parents 1ffd723 + bd5cde9 commit 5d3c642
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 25 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Order 1.1 for Movable Type
Order 1.2 for Movable Type

Copyright 2008-2009 Six Apart, Ltd.
All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use ExtUtils::MakeMaker;

WriteMakefile(
NAME => 'Order',
VERSION => '1.1',
VERSION => '1.2',
DISTNAME => 'Order',
);
70 changes: 54 additions & 16 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Order 1.1 for Movable Type #
# Order 1.2 for Movable Type #

Collect sets of template output to order by a particular datum.

Expand All @@ -11,32 +11,45 @@ Unarchive into your Movable Type directory.
# Usage #

Use the provided template tags to collect and reorder template content. For
example:
example, to show the last 30 unique entries and ActionStreams items:

<mt:Order>
<mt:Order limit="30">

<mt:OrderHeader>
<div class="site-activity">
</mt:OrderHeader>

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

<mt:OrderDateFooter>
</div>
</mt:OrderDateFooter>

<mt:Entries lastn="30">
<mt:OrderItem>
<mt:setvarblock name="order_by">
<mt:setvarblock name="order_by" strip_linefeeds="1" trim="1">
<mt:EntryDate utc="1" format="%Y%m%d%H%M%S">
</mt:setvarblock>
<mt:Include module="Entry">
</mt:OrderItem>
</mt:Entries>

<mt:Comments>
<mt:OrderItem>
<mt:setvarblock name="order_by">
<mt:CommentDate utc="1" format="%Y%m%d%H%M%S">
</mt:setvarblock>
<mt:Include module="Comment">
</mt:OrderItem>
</mt:Comments>

<mt:ActionStreams limit="30">
<mt:setvarblock name="actionurl"><mt:StreamActionURL escape="html"></mt:setvarblock>
<mt:If name="actionurl" like="mysite.com">
<mt:Else>
<mt:OrderItem>
<mt:setvarblock name="order_by" strip_linefeeds="1" trim="1">
<$mt:StreamActionDate format="%Y%m%d%H%M%S"$>
</mt:setvarblock>
<p><a href="<mt:StreamActionURL escape="html">"><img src="/img/icons/<mt:var name="service_type">_16.png" width="12" height="12"></a> <a href="<mt:StreamActionURL escape="html">" class="actionlink"><mt:StreamActionTitle></a></p>
</mt:OrderItem>
</mt:If>
</mt:ActionStreams>

<mt:OrderFooter>
</div>
</mt:OrderFooter>
Expand Down Expand Up @@ -157,13 +170,38 @@ even an `mt:OrderItem` pinned to the front with the `pin="0"` attribute.
Contains template content that is displayed at the end of the `mt:Order` loop,
as long as there are `mt:OrderItem`s to display.

Content from an `mt:OrderFooter` is shown after the last `mt:OrderItem`, or
Content from an `mt:OrderFooter` is shown after the last `mt:OrderItem`,
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
`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
`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. Does not take a `utc` attribute.


# Changes #

## 1.1 in development ##
## 1.2 10 October 2011 ##

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

## 1.1 10 June 2010 ##

* Added `mt:OrderHeader` and `mt:OrderFooter` tags.
* Added `shuffle` ordering option.
Expand Down
6 changes: 5 additions & 1 deletion plugins/Order/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ description: Collect sets of template output to order by a particular datum.
author_name: Mark Paschal
author_link: http://markpasc.org/mark/
plugin_link: http://plugins.movabletype.org/order/
version: 1.1
version: 1.2
tags:
block:
Order: $Order::Order::Plugin::tag_order
OrderItem: $Order::Order::Plugin::tag_order_item
OrderHeader: $Order::Order::Plugin::tag_order_header
OrderFooter: $Order::Order::Plugin::tag_order_footer
OrderDateHeader: $Order::Order::Plugin::tag_order_date_header
OrderDateFooter: $Order::Order::Plugin::tag_order_date_footer
function:
OrderDate: $Order::Order::Plugin::_hdlr_order_date
67 changes: 61 additions & 6 deletions plugins/Order/lib/Order/Plugin.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package Order::Plugin;

sub _natural_sort {
Expand Down Expand Up @@ -85,9 +84,6 @@ sub tag_order {
}
}

# Collapse the transform.
@objs = map { $_->[1] } @objs;

if (my $offset = $args->{offset}) {
# Delete the first $offset items.
splice @objs, 0, $offset;
Expand All @@ -100,6 +96,43 @@ sub tag_order {
}
}

if ($ctx->stash('order_date_header') || $ctx->stash('order_date_footer')) {
# loop over items in @objs adding headers and footers where necessary
my $builder = $ctx->stash('builder');
my ($yesterday, $tomorrow) = ('00000000')x2;
my $i = 0;
for my $o (@objs) {
my $today = substr $o->[0], 0, 8;
my $tomorrow = $today;
my $footer = 0;
if (defined $objs[$i+1]) {
$tomorrow = substr($objs[$i+1]->[0], 0, 8);
$footer = $today ne $tomorrow;
} else {
$footer++;
}
my $header = $today ne $yesterday;
local $ctx->{current_timestamp} = $o->[0];
my ($h_html, $f_html) = ('')x2;
if ($header && $ctx->stash('order_date_header')) {
my $result = $builder->build($ctx, $ctx->stash('order_date_header'), {});
return $ctx->error( $builder->errstr ) unless defined $result;
$h_html = $result;
}
if ($footer && $ctx->stash('order_date_footer')) {
my $result = $builder->build($ctx, $ctx->stash('order_date_footer'), {});
return $ctx->error( $builder->errstr ) unless defined $result;
$f_html = $result;
}
$objs[$i][1] = $h_html.$o->[1].$f_html;
$yesterday = $today;
$i++;
}
}

# Collapse the transform.
@objs = map { $_->[1] } @objs;

return q{} if !@objs;
return join q{}, $ctx->stash('order_header'), @objs,
$ctx->stash('order_footer');
Expand All @@ -121,6 +154,18 @@ sub tag_order_footer {
return q{};
}

sub tag_order_date_header {
my ($ctx, $args, $cond) = @_;
$ctx->stash('order_date_header', $ctx->stash('tokens'));
return q{};
}

sub tag_order_date_footer {
my ($ctx, $args, $cond) = @_;
$ctx->stash('order_date_footer', $ctx->stash('tokens'));
return q{};
}

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

Expand All @@ -136,8 +181,18 @@ sub tag_order_item {

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

1;
sub _hdlr_order_date {
my ($ctx, $args) = @_;
# Order dates are already UTC (or at least shouldn't be messed with after ordering).
if ($args->{utc}) {
my $tag = $ctx->stash('tag');
return $ctx->error(qq{The mt:$tag doesn't support a utc attribute: items were already ordered by these dates, so can't readjust them for UTC after the fact.});
}
return $ctx->_hdlr_date($args);
}


1;

0 comments on commit 5d3c642

Please sign in to comment.