use strict;
use Carp qw(confess);
-our $VERSION = "1.008";
+our $VERSION = "1.009";
=head1 NAME
return grep $_->listed_in_menu, $self->all_visible_kids;
}
+=item images
+
+Return article images (or global images for the site).
+
+=cut
+
sub images {
my ($self) = @_;
- require BSE::TB::Images;
- return sort { $a->{displayOrder} <=> $b->{displayOrder} }
- BSE::TB::Images->getBy(articleId=>$self->{id});
+
+ unless ($self->{_images}) {
+ require BSE::TB::Images;
+ $self->{_images} =
+ [
+ sort { $a->{displayOrder} <=> $b->{displayOrder} }
+ BSE::TB::Images->getBy(articleId=>$self->{id})
+ ];
+ }
+
+ return @{$self->{_images}};
+}
+
+=item image_by_name
+
+Return an image from the site or article given the image name.
+
+=cut
+
+sub image_by_name {
+ my ($self, $name) = @_;
+
+ unless ($self->{_images_by_name}) {
+ $self->{_images_by_name} =
+ +{
+ map { $_->name => $_ } grep $_->name, $self->images
+ };
+ }
+
+ my $image = $self->{_images_by_name}{$name}
+ or return;
+
+ return $image;
+}
+
+=item image_by_index
+
+Return an image for an article (or the site) by index.
+
+Named images are not counted and the index starts from 1.
+
+=cut
+
+sub image_by_index {
+ my ($self, $index) = @_;
+
+ unless ($self->{_images_by_index}) {
+ $self->{_images_by_index} = [ grep !$_->name, $self->images ];
+ }
+
+ $index >= 1 && $index <= @{$self->{_images_by_index}}
+ or return;
+
+ return $self->{_images_by_index}[$index-1];
}
sub children {
#!perl -w
use strict;
use BSE::Test qw(make_ua base_url);
-use Test::More tests => 59;
+use Test::More tests => 62;
use File::Spec;
use File::Slurp;
use Carp qw(confess);
my $im1 = bse_add_image($cfg, $art, file => "t/data/t101.jpg");
ok($im1, "add an image, just a filename");
+my $byindex = $art->image_by_index(1);
+is($byindex->id, $im1->id, "check image_by_index()");
+
my $im2;
{
open my $fh, "<", "t/data/t101.jpg"
);
ok($im3, "make a global image (a)");
- my @images = bse_site()->images;
+ my $site = bse_site();
+ my @images = $site->images;
cmp_ok(@images, '>=', 3, "we have some global images");
my @mine = grep $_->name =~ /^\Q$prefix/, @images;
is($mine[1]->displayOrder, $im2->displayOrder, "middle should be middle");
is($mine[2]->displayOrder, $im3->displayOrder, "last should be last");
+ # fetch by name
+ my $named = $site->image_by_name($prefix . "a");
+ is($named->id, $im3->id, "check we got the right image by name");
+
+ # fetch by index
+ my $byindex = $site->image_by_index(1);
+ is($byindex, undef, "all images named, none available by index");
+
ok($im3->remove, "remove the global image");
undef $im3;
ok($im2->remove, "remove the global image");