Note: This is the last occurrence where we use DBI to perform a CRUD
operation. In this case a delete from uploaded_files.
We now call Koha::UploadedFile[s]->delete to only delete the record
from the table. A next step will be moving the additional functionality
of removing the file(s) too.
Test plan:
[1] Run t/db_dependent/Upload.t
[2] Delete an upload from tools/upload.pl
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
sub delete {
my ( $self, $params ) = @_;
sub delete {
my ( $self, $params ) = @_;
- return if !$params->{id};
- my @res;
- my $temp = $self->_lookup({ id => $params->{id} });
- foreach( @$temp ) {
- my $d = $self->_delete( $_ );
- push @res, $d if $d;
- }
- return if !@res;
- return @res;
+ return $self->_delete( $params->{id} );
- my ( $self, $rec ) = @_;
- my $dbh = C4::Context->dbh;
- my $sql = 'DELETE FROM uploaded_files WHERE id=?';
- my $file = $self->_full_fname($rec);
+ my ( $self, $id ) = @_;
+ my $rec = Koha::UploadedFiles->find($id) || return;
+ my $filename = $rec->filename;
+ my $file = $self->_full_fname({
+ permanent => $rec->permanent,
+ dir => $rec->dir,
+ hashvalue => $rec->hashvalue,
+ filename => $filename,
+ });
+
if( !-e $file ) { # we will just delete the record
# TODO Should we add a trace here for the missing file?
if( !-e $file ) { # we will just delete the record
# TODO Should we add a trace here for the missing file?
- $dbh->do( $sql, undef, ( $rec->{id} ) );
- return $rec->{filename};
+ $rec->delete;
+ return $filename;
} elsif( unlink($file) ) {
} elsif( unlink($file) ) {
- $dbh->do( $sql, undef, ( $rec->{id} ) );
- return $rec->{filename};
+ $rec->delete;
+ return $filename;
}
$self->{files}->{ $rec->{filename} }->{errcode} = 7;
#NOTE: errcode=6 is used to report successful delete (see template)
}
$self->{files}->{ $rec->{filename} }->{errcode} = 7;
#NOTE: errcode=6 is used to report successful delete (see template)