add image_by_name and image_by_index methods
authorTony Cook <tony@develop-help.com>
Tue, 19 Mar 2013 08:24:57 +0000 (19:24 +1100)
committerTony Cook <tony@develop-help.com>
Tue, 19 Mar 2013 08:24:57 +0000 (19:24 +1100)
site/cgi-bin/modules/BSE/TB/SiteCommon.pm
t/050-local/010-api.t

index 3f4e691076eae66b267d45761916b390f8a235b2..62300cd267308c9df730e590139c4227738a5838 100644 (file)
@@ -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 {
index bd25f8ff3add905b801338265f99485d70fd831f..3d282565ca2616364fc099ded83ecb214a295c92 100644 (file)
@@ -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");