From 1c1cb10e5e373e4f0344bde5f0fbc7aa9f488685 Mon Sep 17 00:00:00 2001 From: Chris Nighswonger Date: Sat, 23 Feb 2008 06:28:01 -0500 Subject: [PATCH] First installment on moving patronimages into the database. Signed-off-by: Joshua Ferraro --- C4/Members.pm | 47 ++++++++++++++----- installer/data/mysql/kohastructure.sql | 13 +++++ installer/data/mysql/updatedatabase.pl | 13 +++++ .../prog/en/includes/circ-menu.inc | 2 +- tools/picture-upload.pl | 30 +++++++++--- 5 files changed, 86 insertions(+), 19 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index 96b47feca0..6e81ea3044 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -58,6 +58,7 @@ BEGIN { &GetSortDetails &GetTitles &GetPatronImage + &PutPatronImage &GetMemberAccountRecords &GetBorNotifyAcctRecord @@ -1699,23 +1700,45 @@ sub GetTitles { =head2 GetPatronImage - $patronimage = &GetPatronImage('cardnumber'); + my ($imagedata, $dberror) = GetPatronImage($cardnumber); -Returns the path/filename.jpg of the image for the patron with the supplied cardnumber. +Returns the mimetype and binary image data of the image for the patron with the supplied cardnumber. =cut sub GetPatronImage { - my $cardnumber = shift; - warn "Cardnumber passed to GetPatronImage is $cardnumber" if $debug; - my $htdocs = C4::Context->config('intrahtdocs'); - my $picture = "patronimages/" . $cardnumber . ".jpg"; - if ( -e "$htdocs/$picture" ) { - return ( "/intranet-tmpl/$picture" ); # FIXME: This is a real hack and should be handled better, but I'm in a hurry... -fbcit - } - else { - return (); - } + my ($cardnumber) = @_; + warn "Cardnumber passed to GetPatronImage is $cardnumber"; # if $debug; + my $dbh = C4::Context->dbh; + my $query = "SELECT mimetype, imagefile FROM patronimage WHERE cardnumber = ?;"; + my $sth = $dbh->prepare($query); + $sth->execute($cardnumber); + my $imagedata = $sth->fetchrow_hashref; + my $dberror = $sth->errstr; + warn "Database error!" if $sth->errstr; + $sth->finish; + return $imagedata, $dberror; +} + +=head2 PutPatronImage + + PutPatronImage($cardnumber, $mimetype, $imgfile); + +Stores patron binary image data and mimetype in database. + +=cut + +sub PutPatronImage { + my ($cardnumber, $mimetype, $imgfile) = @_; + warn "Parameters passed in: Cardnumber=$cardnumber, Mimetype=$mimetype, " . ($imgfile ? "Imagefile" : "No Imagefile") if $debug; + my $dbh = C4::Context->dbh; + my $query = "INSERT INTO patronimage (cardnumber, mimetype, imagefile) VALUES (?,?,?) ON DUPLICATE KEY UPDATE cardnumber = ?;"; + my $sth = $dbh->prepare($query); + $sth->execute($cardnumber,$mimetype,$imgfile,$cardnumber); + warn "Error returned inserting $cardnumber.$mimetype." if $sth->errstr; + my $dberror = $sth->errstr; + $sth->finish; + return $dberror; } =head2 GetRoadTypeDetails (OUEST-PROVENCE) diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 0f666514bb..17d0c6362c 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -1389,6 +1389,19 @@ CREATE TABLE `overduerules` ( PRIMARY KEY (`branchcode`,`categorycode`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +-- +-- Table structure for table `patronimage` +-- + +DROP TABLE IF EXISTS `patronimage`; +CREATE TABLE `patronimage` ( + `cardnumber` varchar(16) NOT NULL, + `mimetype` varchar(15) NOT NULL, + `imagefile` mediumblob NOT NULL, + PRIMARY KEY (`cardnumber`), + CONSTRAINT `patronimage_fk1` FOREIGN KEY (`cardnumber`) REFERENCES `borrowers` (`cardnumber`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + -- -- Table structure for table `printers` -- diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 3b4f03fdb8..7e0aa1a186 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -1037,6 +1037,19 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) { SetVersion ($DBversion); } +$DBversion = "3.00.00.057"; +if (C4::Context->preference("Version") < TransformToNum($DBversion)) { + $dbh->do("CREATE TABLE `patronimage` ( + `cardnumber` varchar(16) NOT NULL, + `mimetype` varchar(15) NOT NULL, + `imagefile` mediumblob NOT NULL, + PRIMARY KEY (`cardnumber`), + CONSTRAINT `patronimage_fk1` FOREIGN KEY (`cardnumber`) REFERENCES `borrowers` (`cardnumber`) ON DELETE CASCADE ON UPDATE CASCADE + ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + print "Upgrade to $DBversion done ( Added patronimage table. )\n"; + SetVersion ($DBversion); +} + =item DropAllForeignKeys($table) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.inc index dda89bab43..d516fcc79f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.inc @@ -1,7 +1,7 @@
()
    -
  • " alt=" ()" border="0" style="margin: .3em 0 .3em .3em; padding: .2em; border: 1px solid #CCCCCC;" width="100" />
  • +
  • " alt=" ()" border="0" style="margin: .3em 0 .3em .3em; padding: .2em; border: 1px solid #CCCCCC;" width="100" />
  • diff --git a/tools/picture-upload.pl b/tools/picture-upload.pl index b6fe1896b8..78ca35ff8f 100755 --- a/tools/picture-upload.pl +++ b/tools/picture-upload.pl @@ -6,6 +6,7 @@ use CGI; use C4::Context; use C4::Auth; use C4::Output; +use C4::Members; my $DEBUG = ($ENV{DEBUG}) ? 1 : 0; @@ -91,6 +92,13 @@ sub handle_dir { my ( %count ); my $file; $count{filenames} = (); + + my $mimemap = { + "gif" => "image/gif", + "jpg" => "image/jpeg", + "jpeg" => "image/jpeg", + "png" => "image/png" + }; opendir my $dirhandle, $dir; while ( my $filename = readdir $dirhandle ) { @@ -113,12 +121,22 @@ sub handle_dir { $filename =~ s/[\"\r\n\s]//g; warn "Cardnumber: $cardnumber Filename: $filename" if $DEBUG; if ($cardnumber && $filename) { - warn "Source: $dir/$filename Target: $destdir/$cardnumber.jpg" if $DEBUG; - my $result = move ( "$dir/$filename", "$destdir/$cardnumber.jpg" ); - if ( $result ) { - $count{count}++; - push @{ $count{filenames} }, { source => $filename, dest => $cardnumber .".jpg" }; - } + warn "Source: $dir/$filename" if $DEBUG; + open (IMG, "$dir/$filename") or warn "Could not open $dir/$filename"; + #binmode (IMG); # Not sure if we need this or not -fbcit + my $imgfile; + while () { + $imgfile .= $_; + } + my $mimetype = $mimemap->{lc ($1)} if $filename =~ m/\.([^.]+)$/i; + warn "$filename is mimetype \"$mimetype\"" if $DEBUG; + my $dberror = PutPatronImage($cardnumber,$mimetype, $imgfile) if $mimetype; +# warn "Database says: $dberror" if $dberror; + close (IMG); + unless ( $dberror || !$mimetype ) { + $count{count}++; + push @{ $count{filenames} }, { source => $filename, dest => $cardnumber }; + } } } $count{source} = $dir; -- 2.39.5