Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add image_by_name and image_by_index methods

  • Loading branch information...
commit fb400436f390d1662786e5c0df272c030fe8f297 1 parent 1f3d3c2
@tonycoz authored
Showing with 75 additions and 6 deletions.
  1. +61 −4 site/cgi-bin/modules/BSE/TB/SiteCommon.pm
  2. +14 −2 t/050-local/010-api.t
View
65 site/cgi-bin/modules/BSE/TB/SiteCommon.pm
@@ -2,7 +2,7 @@ package BSE::TB::SiteCommon;
use strict;
use Carp qw(confess);
-our $VERSION = "1.008";
+our $VERSION = "1.009";
=head1 NAME
@@ -150,11 +150,68 @@ sub all_menu_kids {
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 {
View
16 t/050-local/010-api.t
@@ -1,7 +1,7 @@
#!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);
@@ -37,6 +37,9 @@ ok($child->is_descendant_of($art->id), "check decendant by id");
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"
@@ -222,7 +225,8 @@ undef $art;
);
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;
@@ -232,6 +236,14 @@ undef $art;
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");
Please sign in to comment.
Something went wrong with that request. Please try again.