Bug 30928: Add interface to statistics
[koha.git] / Koha / ERM / Documents.pm
1 package Koha::ERM::Documents;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use MIME::Base64 qw( decode_base64 );
21 use MIME::Types;
22
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::ERM::Document;
26
27 use base qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::ERM::Documents- Koha ERM Document Object set class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =cut
38
39 =head3 replace_with
40
41 Replaces ERM Agreement or ERM License documents with new updated ones
42
43 =cut
44
45 sub replace_with {
46     my ( $self, $documents, $obj ) = @_;
47     my $schema = $obj->_result->result_source->schema;
48     $schema->txn_do(
49         sub {
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(
58                 {
59                     @$document_ids_after_update
60                     ? (
61                         document_id => {
62                             '-not_in' => $document_ids_after_update
63                         }
64                         )
65                     : ()
66                 }
67             )->delete;
68
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;
74
75                 if ( $document->{document_id} ) {
76                     # The document already exists in DB
77                     $existing_documents->find( $document->{document_id} )
78                         ->set(
79                         {
80                             #Check if existing document had its file changed
81                             length $file_content
82                             ? (
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,
87                             )
88                             : (),
89                             file_description  => $document->{file_description},
90                             physical_location => $document->{physical_location},
91                             uri               => $document->{uri},
92                             notes             => $document->{notes},
93                         }
94                     )->store;
95                 }
96                 else {
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);
102                 }
103             }
104         }
105     );
106 }
107
108 =head3 type
109
110 =cut
111
112 sub _type {
113     return 'ErmDocument';
114 }
115
116 =head3 object_class
117
118 =cut
119
120 sub object_class {
121     return 'Koha::ERM::Document';
122 }
123
124 1;