1 package SL::Template::Plugin::MultiColumnIterator;
3 use base 'Template::Plugin';
4 use Template::Constants;
5 use Template::Exception;
6 use Template::Iterator;
15 $main::lxdebug->enter_sub();
18 my $data = shift || [ ];
24 push @ISA, "Template::Iterator";
26 if (ref $data eq 'HASH') {
27 # map a hash into a list of { key => ???, value => ??? } hashes,
28 # one for each key, sorted by keys
29 $data = [ map { { key => $_, value => $data->{ $_ } } }
32 elsif (UNIVERSAL::can($data, 'as_list')) {
33 $data = $data->as_list();
35 elsif (ref $data ne 'ARRAY') {
36 # coerce any non-list data into an array reference
40 $main::lxdebug->leave_sub();
51 $main::lxdebug->enter_sub();
53 my $data = $self->{ _DATA };
54 my $dim = $self->{ _DIM };
56 $self->{ _DATASET } = $self->{ _DATA };
57 my $size = int ((scalar @$data - 1) / $dim) + 1;
60 return (undef, Template::Constants::STATUS_DONE) unless $size;
62 # initialise various counters, flags, etc.
63 @$self{ qw( SIZE MAX INDEX COUNT FIRST LAST ) } = ( $size, $size - 1, $index, 1, 1, $size > 1 ? 0 : 1, undef );
64 @$self{ qw( PREV ) } = ( undef );
65 $$self{ qw( NEXT ) } = [ @{ $self->{ _DATASET } }[ map { $index + 1 + $_ * $size } 0 .. ($dim - 1) ] ];
67 $main::lxdebug->leave_sub();
68 return [ @{ $self->{ _DATASET } }[ map { $index + $_ * $size } 0 .. ($dim - 1) ] ];
72 $main::lxdebug->enter_sub();
74 my ($max, $index) = @$self{ qw( MAX INDEX ) };
75 my $data = $self->{ _DATASET };
76 my $dim = $self->{ _DIM };
77 my $size = $self->{ SIZE };
79 # warn about incorrect usage
80 unless (defined $index) {
81 my ($pack, $file, $line) = caller();
82 warn("iterator get_next() called before get_first() at $file line $line\n");
83 return (undef, Template::Constants::STATUS_DONE); ## RETURN ##
86 # if there's still some data to go...
88 # update counters and flags
90 @$self{ qw( INDEX COUNT FIRST LAST ) } = ( $index, $index + 1, 0, $index == $max ? 1 : 0 );
91 $$self{ qw( PREV ) } = [ @{ $self->{ _DATASET } }[ map { $index - 1 + $_ * $size } 0 .. ($dim - 1) ] ];
92 $$self{ qw( NEXT ) } = [ @{ $self->{ _DATASET } }[ map { $index + 1 + $_ * $size } 0 .. ($dim - 1) ] ];
93 $main::lxdebug->leave_sub();
94 return [ @{ $self->{ _DATASET } }[ map { $index + $_ * $size } 0 .. ($dim - 1) ] ];
97 $main::lxdebug->leave_sub();
98 return (undef, Template::Constants::STATUS_DONE); ## RETURN ##
104 my ($max, $index, $dim, $size) = @$self{ qw( MAX INDEX _DIM SIZE) };
107 # if there's still some data to go...
110 @data = map do{ !($i = $_) || +[ @{ $self->{ _DATASET } }[ map { $i + $_ * $size } 0 .. ($dim - 1) ] ] }, $index .. $max;
111 # update counters and flags
112 @$self{ qw( INDEX COUNT FIRST LAST ) } = ( $max, $max + 1, 0, 1 );
113 $main::lxdebug->leave_sub();
114 return \@data; ## RETURN ##
117 $main::lxdebug->leave_sub();
118 return (undef, Template::Constants::STATUS_DONE); ## RETURN ##
124 my $item = $AUTOLOAD;
126 return if $item eq 'DESTROY';
128 # alias NUMBER to COUNT for backwards compatability
129 $item = 'COUNT' if $item =~ /NUMBER/i;
131 return $self->{ uc $item };
135 $main::lxdebug->enter_sub();
137 $main::lxdebug->leave_sub();
140 " Data: ", Dumper($self->{ _DATA }), "\n",
141 " Index: ", $self->{ INDEX }, "\n",
142 "Number: ", $self->{ NUMBER }, "\n",
143 " Max: ", $self->{ MAX }, "\n",
144 " Size: ", $self->{ SIZE }, "\n",
145 " First: ", $self->{ FIRST }, "\n",
146 " Last: ", $self->{ LAST }, "\n",
153 $main::lxdebug->enter_sub();
155 $main::lxdebug->leave_sub();
156 return $self->{ INDEX };
160 $main::lxdebug->enter_sub();
162 $main::lxdebug->leave_sub();
163 return $self->{ NUMBER };
167 $main::lxdebug->enter_sub();
169 $main::lxdebug->leave_sub();
170 return $self->{ COUNT };
173 $main::lxdebug->enter_sub();
175 $main::lxdebug->leave_sub();
176 return $self->{ MAX };
180 $main::lxdebug->enter_sub();
182 $main::lxdebug->leave_sub();
183 return $self->{ SIZE };
187 $main::lxdebug->enter_sub();
189 $main::lxdebug->leave_sub();
190 return $self->{ FIRST };
194 $main::lxdebug->enter_sub();
196 $main::lxdebug->leave_sub();
197 return $self->{ LAST};