From aef807a937598f8f17e2344b78149eb30c635df2 Mon Sep 17 00:00:00 2001 From: Andrew Moore Date: Tue, 29 Apr 2008 18:20:24 -0500 Subject: [PATCH] bug 2047: refactoring icon manipulation logic I'm extracting some of the icon manipulation logic so that I can get to it from the authorized values pages. There should be no functionality or documentation changes with this commit. Signed-off-by: Joshua Ferraro --- C4/Koha.pm | 113 ++++++++++++++++++ admin/itemtypes.pl | 65 +--------- .../prog/en/modules/admin/itemtypes.tmpl | 4 +- 3 files changed, 117 insertions(+), 65 deletions(-) diff --git a/C4/Koha.pm b/C4/Koha.pm index 31ffa37b2c..c473644f7a 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -479,6 +479,119 @@ sub getitemtypeimagesrc { } } +=head3 _getImagesFromDirectory + + Find all of the image files in a directory in the filesystem + + parameters: + a directory name + + returns: a list of images in that directory. + + Notes: this does not traverse into subdirectories. See + _getSubdirectoryNames for help with that. + Images are assumed to be files with .gif or .png file extensions. + The image names returned do not have the directory name on them. + +=cut + +sub _getImagesFromDirectory { + my $directoryname = shift; + return unless defined $directoryname; + return unless -d $directoryname; + + if ( opendir ( my $dh, $directoryname ) ) { + my @images = grep { /\.(gif|png)$/i } readdir( $dh ); + closedir $dh; + return @images; + } else { + warn "unable to opendir $directoryname: $!"; + return; + } +} + +=head3 _getSubdirectoryNames + + Find all of the directories in a directory in the filesystem + + parameters: + a directory name + + returns: a list of subdirectories in that directory. + + Notes: this does not traverse into subdirectories. Only the first + level of subdirectories are returned. + The directory names returned don't have the parent directory name + on them. + +=cut + +sub _getSubdirectoryNames { + my $directoryname = shift; + return unless defined $directoryname; + return unless -d $directoryname; + + if ( opendir ( my $dh, $directoryname ) ) { + my @directories = grep { -d File::Spec->catfile( $directoryname, $_ ) && ! ( /^\./ ) } readdir( $dh ); + closedir $dh; + return @directories; + } else { + warn "unable to opendir $directoryname: $!"; + return; + } +} + +=head3 getImageSets + + returns: a listref of hashrefs. Each hash represents another collection of images. + { imagesetname => 'npl', # the name of the image set (npl is the original one) + images => listref of image hashrefs + } + + each image is represented by a hashref like this: + { KohaImage => 'npl/image.gif', + StaffImageUrl => '/intranet-tmpl/prog/img/itemtypeimg/npl/image.gif', + OpacImageURL => '/opac-tmpl/prog/itemtypeimg/npl/image.gif' + checked => 0 or 1: was this the image passed to this method? + Note: I'd like to remove this somehow. + } + +=cut + +sub getImageSets { + my %params = @_; + my $checked = $params{'checked'} || ''; + + my $paths = { staff => { filesystem => getitemtypeimagedir('intranet'), + url => getitemtypeimagesrc('intranet'), + }, + opac => { filesystem => getitemtypeimagedir('opac'), + url => getitemtypeimagesrc('opac'), + } + }; + + my @imagesets = (); # list of hasrefs of image set data to pass to template + my @subdirectories = _getSubdirectoryNames( $paths->{'staff'}{'filesystem'} ); + + foreach my $imagesubdir ( @subdirectories ) { + my @imagelist = (); # hashrefs of image info + my @imagenames = _getImagesFromDirectory( File::Spec->catfile( $paths->{'staff'}{'filesystem'}, $imagesubdir ) ); + foreach my $thisimage ( @imagenames ) { + push( @imagelist, + { KohaImage => "$imagesubdir/$thisimage", + StaffImageUrl => join( '/', $paths->{'staff'}{'url'}, $imagesubdir, $thisimage ), + OpacImageUrl => join( '/', $paths->{'opac'}{'url'}, $imagesubdir, $thisimage ), + checked => "$imagesubdir/$thisimage" eq $checked ? 1 : 0, + } + ); + } + push @imagesets, { imagesetname => $imagesubdir, + images => \@imagelist }; + + } + return \@imagesets; +} + =head2 GetPrinters $printers = &GetPrinters(); diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl index 5ac15f93b3..f7d8d8912b 100755 --- a/admin/itemtypes.pl +++ b/admin/itemtypes.pl @@ -64,35 +64,6 @@ sub StringSearch { # like [ fetchrow_hashref(), fetchrow_hashref() ... ] } -sub getImagesFromDirectory { - my $directoryname = shift; - return unless defined $directoryname; - return unless -d $directoryname; - - if ( opendir ( my $dh, $directoryname ) ) { - my @images = grep { /\.(gif|png)$/i } readdir( $dh ); - closedir $dh; - return @images; - } else { - warn "unable to opendir $directoryname: $!"; - return; - } -} -sub getSubdirectoryNames { - my $directoryname = shift; - return unless defined $directoryname; - return unless -d $directoryname; - - if ( opendir ( my $dh, $directoryname ) ) { - my @directories = grep { -d File::Spec->catfile( $directoryname, $_ ) && ! ( /^\./ ) } readdir( $dh ); - closedir $dh; - return @directories; - } else { - warn "unable to opendir $directoryname: $!"; - return; - } -} - my $input = new CGI; my $searchfield = $input->param('description'); my $script_name = "/cgi-bin/koha/admin/itemtypes.pl"; @@ -131,39 +102,7 @@ if ( $op eq 'add_form' ) { $data = $sth->fetchrow_hashref; } - # build list of images - my $src = "intranet"; # so that the getitemtypeimage functions know where they were called from -fbcit - my $imagedir_filesystem = getitemtypeimagedir($src); - my $imagedir_web = getitemtypeimagesrc($src); - - my @imagesets = (); # list of hasrefs of image set data to pass to template - my @subdirectories = getSubdirectoryNames( $imagedir_filesystem ); - - foreach my $imagesubdir ( @subdirectories ) { - my @imagelist = (); # hashrefs of image info - my $i = 0; # counter - my $image_per_line = 12; # max images in a line? - my @imagenames = getImagesFromDirectory( File::Spec->catfile( $imagedir_filesystem, $imagesubdir ) ); - foreach my $thisimage ( @imagenames ) { - $i++; - if ( $i == $image_per_line ) { - $i = 0; - push @imagelist, { KohaImage => '', KohaImageSrc => '' }; - } else { - push( - @imagelist, - { - KohaImage => "$imagesubdir/$thisimage", - KohaImageSrc => join( '/', $imagedir_web, $imagesubdir, $thisimage ), - checked => "$imagesubdir/$thisimage" eq $data->{imageurl} ? 1 : 0, - } - ); - } - } - push @imagesets, { imagesetname => $imagesubdir, - images => \@imagelist }; - - } + my $imagesets = C4::Koha::getImageSets( checked => $data->{'imageurl'} ); my $remote_image = undef; if ( defined $data->{imageurl} and $data->{imageurl} =~ /^http/i ) { @@ -179,7 +118,7 @@ if ( $op eq 'add_form' ) { imageurl => $data->{'imageurl'}, template => C4::Context->preference('template'), summary => $data->{summary}, - imagesets => \@imagesets, + imagesets => $imagesets, remote_image => $remote_image, ); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tmpl index 4aebb80672..89776b0276 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tmpl @@ -163,8 +163,8 @@ Item Types Administration
  • Icons from collection :
  • -