1 package Koha::ERM::Documents;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use MIME::Base64 qw( decode_base64 );
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::ERM::Document;
27 use base qw(Koha::Objects);
31 Koha::ERM::Documents- Koha ERM Document Object set class
41 Replaces ERM Agreement or ERM License documents with new updated ones
46 my ( $self, $documents, $obj ) = @_;
47 my $schema = $obj->_result->result_source->schema;
50 my $existing_documents = $obj->documents;
51 my $max_allowed_packet = C4::Context->dbh->selectrow_array(q{SELECT @@max_allowed_packet});
52 # FIXME Here we are not deleting all the documents before recreating them, like we do for other related resources.
53 # As we do not want the content of the documents to transit over the network we need to use the document_id (and allow it in the API spec)
54 # to distinguish from each other
55 # Delete all the documents that are not part of the PUT request
56 my $document_ids_after_update = [ map { $_->{document_id} || () } @$documents ];
57 $obj->documents->search(
59 @$document_ids_after_update
62 '-not_in' => $document_ids_after_update
69 for my $document (@$documents) {
70 my $file_content = defined($document->{file_content}) ? decode_base64( $document->{file_content} ) : "";
71 my $mt = MIME::Types->new();
72 # Throw exception if uploaded file exceeds server limit
73 Koha::Exceptions::PayloadTooLarge->throw("File size exceeds limit defined by server") if length($file_content) > $max_allowed_packet;
75 if ( $document->{document_id} ) {
76 # The document already exists in DB
77 $existing_documents->find( $document->{document_id} )
80 #Check if existing document had its file changed
83 file_content => $file_content,
84 file_type => $mt->mimeTypeOf( $document->{file_name} ),
85 file_name => $document->{file_name},
86 uploaded_on => dt_from_string,
89 file_description => $document->{file_description},
90 physical_location => $document->{physical_location},
91 uri => $document->{uri},
92 notes => $document->{notes},
97 # Creating a whole new document
98 $document->{file_type} = $mt->mimeTypeOf( $document->{file_name} );
99 $document->{uploaded_on} //= dt_from_string;
100 $document->{file_content} = $file_content;
101 $obj->_result->add_to_erm_documents( $document);
113 return 'ErmDocument';
121 return 'Koha::ERM::Document';