-
Notifications
You must be signed in to change notification settings - Fork 8
/
Base.pm
1229 lines (1026 loc) · 36.1 KB
/
Base.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Koha::Illbackends::FreeForm::Base;
# Copyright PTFS Europe 2014, 2018
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use Modern::Perl;
use DateTime;
use File::Basename qw( dirname );
use C4::Installer;
use Koha::Illrequests;
use Koha::Illrequestattribute;
use C4::Biblio qw( AddBiblio );
use C4::Charset qw( MarcToUTF8Record );
=head1 NAME
Koha::Illrequest::Backend::FreeForm::Base - Koha ILL Backend: FreeForm
=head1 SYNOPSIS
Koha ILL implementation for the "FreeForm" backend.
=head1 DESCRIPTION
=head2 Overview
We will be providing the Abstract interface which requires we implement the
following methods:
- create -> initial placement of the request for an ILL order
- confirm -> confirm placement of the ILL order (No-op in FreeForm)
- cancel -> request an already 'confirm'ed ILL order be cancelled
- status_graph -> return a hashref of additional statuses
- name -> return the name of this backend
- metadata -> return mapping of fields from requestattributes
=head2 On the FreeForm backend
The FreeForm backend is a simple backend that is supposed to act as a
fallback. It provides the end user with some mandatory fields in a form as
well as the option to enter additional fields with arbitrary names & values.
=head1 API
=head2 Class Methods
=cut
=head3 new
my $backend = Koha::Illrequest::Backend::FreeForm->new;
=cut
sub new {
# -> instantiate the backend
my ($class, $other) = @_;
my $framework =
defined $other->{config}->{configuration}->{raw_config}->{framework} ?
$other->{config}->{configuration}->{raw_config}->{framework} :
'FA';
my $self = {
framework => $framework
};
bless( $self, $class );
return $self;
}
=head3 name
Return the name of this backend.
=cut
sub name {
return "FreeForm";
}
=head3 capabilities
$capability = $backend->capabilities($name);
Return the sub implementing a capability selected by NAME, or 0 if that
capability is not implemented.
=cut
sub capabilities {
my ( $self, $name ) = @_;
my ($query) = @_;
my $capabilities = {
# Get the requested partner email address(es)
get_requested_partners => sub { _get_requested_partners(@_); },
# Set the requested partner email address(es)
set_requested_partners => sub { _set_requested_partners(@_); },
# Migrate
migrate => sub { $self->migrate(@_); },
# Return whether we can create the request
# i.e. the create form has been submitted
can_create_request => sub { _can_create_request(@_) },
# This is required for compatibility
# with Koha versions prior to bug 33716
should_display_availability => sub { _can_create_request(@_) },
# View and manage a request
illview => sub { illview(@_); },
provides_batch_requests => sub { return 1; },
# We can create ILL requests with data passed from the API
create_api => sub { $self->create_api(@_) }
};
return $capabilities->{$name};
}
=head3 metadata
Return a hashref containing canonical values from the key/value
illrequestattributes store. We may want to ignore certain values
that we do not consider to be metadata
=cut
sub metadata {
my ( $self, $request ) = @_;
my $attrs = $request->illrequestattributes;
my $metadata = {};
my @ignore = ('requested_partners', 'type', 'type_disclaimer_value', 'type_disclaimer_date');
my $core_fields = _get_core_fields();
while ( my $attr = $attrs->next ) {
my $type = $attr->type;
if ( !grep { $_ eq $type } @ignore ) {
my $name;
$name = $core_fields->{$type} || ucfirst($type);
$metadata->{$name} = $attr->value;
}
}
return $metadata;
}
=head3 status_graph
This backend provides no additional actions on top of the core_status_graph.
=cut
sub status_graph {
return {
MIG => {
prev_actions =>
[ 'NEW', 'REQ', 'GENREQ', 'REQREV', 'QUEUED', 'CANCREQ', ],
id => 'MIG',
name => 'Switched provider',
ui_method_name => 'Switch provider',
method => 'migrate',
next_actions => [],
ui_method_icon => 'fa-search',
},
EDITITEM => {
prev_actions => [ 'NEW' ],
id => 'EDITITEM',
name => 'Edited item metadata',
ui_method_name => 'Edit item metadata',
method => 'edititem',
next_actions => [],
ui_method_icon => 'fa-edit',
},
};
}
=head3 create
my $response = $backend->create({ params => $params });
We just want to generate a form that allows the end-user to associate key
value pairs in the database.
=cut
sub create {
my ( $self, $params ) = @_;
my $other = $params->{other};
my $stage = $other->{stage};
my $core_fields = _get_core_string();
if ( !$stage || $stage eq 'init' ) {
# First thing we want to do, is check if we're receiving
# an OpenURL and transform it into something we can
# understand
if ($other->{openurl}) {
# We only want to transform once
delete $other->{openurl};
$params = _openurl_to_ill($params);
}
# We simply need our template .INC to produce a form.
return {
cwd => dirname(__FILE__),
error => 0,
status => '',
message => '',
method => 'create',
stage => 'form',
value => $params,
core => $core_fields
};
}
elsif ( $stage eq 'form' ) {
# We may be recieving a submitted form due to an additional
# custom field being added or deleted, or the material type
# having been changed, so check for these things
if (!_can_create_request($other)) {
if ( defined $other->{'add_new_custom'} ) {
my ( $custom_keys, $custom_vals ) =
_get_custom( $other->{'custom_key'}, $other->{'custom_value'} );
push @{$custom_keys}, '---';
push @{$custom_vals}, '---';
$other->{'custom_key'} = join "\0", @{$custom_keys};
$other->{'custom_value'} = join "\0", @{$custom_vals};
}
elsif ( defined $other->{'custom_delete'} ) {
my $delete_idx = $other->{'custom_delete'};
my ( $custom_keys, $custom_vals ) =
_get_custom( $other->{'custom_key'}, $other->{'custom_value'} );
splice @{$custom_keys}, $delete_idx, 1;
splice @{$custom_vals}, $delete_idx, 1;
$other->{'custom_key'} = join "\0", @{$custom_keys};
$other->{'custom_value'} = join "\0", @{$custom_vals};
}
elsif ( defined $other->{'change_type'} ) {
# We may be receiving a submitted form due to the user having
# changed request material type, so we just need to go straight
# back to the form, the type has been changed in the params
delete $other->{'change_type'};
}
return {
cwd => dirname(__FILE__),
status => "",
message => "",
error => 0,
value => $params,
method => "create",
stage => "form",
core => $core_fields
};
}
# Received completed details of form. Validate and create request.
## Validate
my ( $brw_count, $brw ) =
_validate_borrower( $other->{'cardnumber'} );
my $result = {
cwd => dirname(__FILE__),
status => "",
message => "",
error => 1,
value => {},
method => "create",
stage => "form",
core => $core_fields
};
my $failed = 0;
if ( !$other->{'type'} ) {
$result->{status} = "missing_type";
$result->{value} = $params;
$failed = 1;
}
elsif ( !$other->{'branchcode'} ) {
$result->{status} = "missing_branch";
$result->{value} = $params;
$failed = 1;
}
elsif ( !Koha::Libraries->find( $other->{'branchcode'} ) ) {
$result->{status} = "invalid_branch";
$result->{value} = $params;
$failed = 1;
}
elsif ( $brw_count == 0 ) {
$result->{status} = "invalid_borrower";
$result->{value} = $params;
$failed = 1;
}
elsif ( $brw_count > 1 ) {
# We must select a specific borrower out of our options.
$params->{brw} = $brw;
$result->{value} = $params;
$result->{stage} = "borrowers";
$result->{error} = 0;
$failed = 1;
}
return $result if $failed;
$self->add_request( { request => $params->{request}, other => $other } );
my $request_details = _get_request_details( $params, $other );
## -> create response.
return {
cwd => dirname(__FILE__),
error => 0,
status => '',
message => '',
method => 'create',
stage => 'commit',
next => 'illview',
value => $request_details,
core => $core_fields
};
}
else {
# Invalid stage, return error.
return {
cwd => dirname(__FILE__),
error => 1,
status => 'unknown_stage',
message => '',
method => 'create',
stage => $params->{stage},
value => {},
};
}
}
=head3 edititem
=cut
sub edititem {
my ( $self, $params ) = @_;
my $core = _get_core_fields();
my $core_fields = _get_core_string();
# Don't allow editing of submitted requests
return { method => 'illlist' } if $params->{request}->status ne 'NEW';
my $other = $params->{other};
my $stage = $other->{stage};
if ( !$stage || $stage eq 'init' ) {
my $attrs = $params->{request}->illrequestattributes->unblessed;
# We need to identify which parameters are custom, and pass them
# to the template in a predefined form
my $custom_keys = [];
my $custom_vals = [];
foreach my $attr(@{$attrs}) {
if (!$core->{$attr->{type}}) {
push @{$custom_keys}, $attr->{type};
push @{$custom_vals}, $attr->{value};
} else {
$other->{$attr->{type}} = $attr->{value};
}
}
$other->{'custom_key'} = join "\0", @{$custom_keys};
$other->{'custom_value'} = join "\0", @{$custom_vals};
# Pass everything back to the template
return {
cwd => dirname(__FILE__),
error => 0,
status => '',
message => '',
method => 'edititem',
stage => 'form',
value => $params,
core => $core_fields
};
}
elsif ( $stage eq 'form' ) {
# We may be recieving a submitted form due to an additional
# custom field being added or deleted, or the material type
# having been changed, so check for these things
if (
defined $other->{'add_new_custom'} ||
defined $other->{'custom_delete'} ||
defined $other->{'change_type'}
) {
if ( defined $other->{'add_new_custom'} ) {
my ( $custom_keys, $custom_vals ) =
_get_custom( $other->{'custom_key'}, $other->{'custom_value'} );
push @{$custom_keys}, '---';
push @{$custom_vals}, '---';
$other->{'custom_key'} = join "\0", @{$custom_keys};
$other->{'custom_value'} = join "\0", @{$custom_vals};
}
elsif ( defined $other->{'custom_delete'} ) {
my $delete_idx = $other->{'custom_delete'};
my ( $custom_keys, $custom_vals ) =
_get_custom( $other->{'custom_key'}, $other->{'custom_value'} );
splice @{$custom_keys}, $delete_idx, 1;
splice @{$custom_vals}, $delete_idx, 1;
$other->{'custom_key'} = join "\0", @{$custom_keys};
$other->{'custom_value'} = join "\0", @{$custom_vals};
}
elsif ( defined $other->{'change_type'} ) {
# We may be receiving a submitted form due to the user having
# changed request material type, so we just need to go straight
# back to the form, the type has been changed in the params
delete $other->{'change_type'};
}
return {
cwd => dirname(__FILE__),
status => "",
message => "",
error => 0,
value => $params,
method => "edititem",
stage => "form",
core => $core_fields
};
}
# We don't want the request ID param getting any further
delete $other->{illrequest_id};
my $result = {
cwd => dirname(__FILE__),
status => "",
message => "",
error => 1,
value => {},
method => "edititem",
stage => "form",
core => $core_fields
};
# Received completed details of form. Validate and create request.
## Validate
my $failed = 0;
if ( !$other->{'type'} ) {
$result->{status} = "missing_type";
$result->{value} = $params;
$failed = 1;
}
return $result if $failed;
## Update request
# ...Update Illrequest
my $request = $params->{request};
$request->updated( DateTime->now );
$request->store;
# ...Populate Illrequestattributes
# generate $request_details
my $request_details = _get_request_details($params, $other);
# We do this with a 'dump all and repopulate approach' inside
# a transaction, easier than catering for create, update & delete
my $dbh = C4::Context->dbh;
my $schema = Koha::Database->new->schema;
$schema->txn_do(
sub{
# Delete all existing attributes for this request
$dbh->do( q|
DELETE FROM illrequestattributes WHERE illrequest_id=?
|, undef, $request->id);
# Insert all current attributes for this request
foreach my $attr( keys %{$request_details}) {
my $value = $request_details->{$attr};
if ($value && length $value > 0){
if(column_exists( 'illrequestattributes', 'backend' ) ){
my @bind = ($request->id, 'FreeForm', $attr, $value, 0);
$dbh->do ( q|
INSERT INTO illrequestattributes
(illrequest_id, backend, type, value, readonly) VALUES
(?, ?, ?, ?, ?)
|, undef, @bind);
}else{
my @bind = ($request->id, $attr, $value, 0);
$dbh->do ( q|
INSERT INTO illrequestattributes
(illrequest_id, type, value, readonly) VALUES
(?, ?, ?, ?)
|, undef, @bind);
}
}
}
}
);
## -> create response.
return {
error => 0,
status => '',
message => '',
method => 'create',
stage => 'commit',
next => 'illview',
value => $request_details,
core => $core_fields
};
}
else {
# Invalid stage, return error.
return {
error => 1,
status => 'unknown_stage',
message => '',
method => 'create',
stage => $params->{stage},
value => {},
};
}
}
=head3 confirm
my $response = $backend->confirm({ params => $params });
Confirm the placement of the previously "selected" request (by using the
'create' method).
In the FreeForm backend we only want to display a bit of text to let staff
confirm that they have taken the steps they need to take to "confirm" the
request.
=cut
sub confirm {
my ( $self, $params ) = @_;
my $stage = $params->{other}->{stage};
if ( !$stage || $stage eq 'init' ) {
# We simply need our template .INC to produce a text block.
return {
method => 'confirm',
stage => 'confirm',
value => $params,
};
}
elsif ( $stage eq 'confirm' ) {
my $request = $params->{request};
$request->orderid( $request->illrequest_id );
$request->status("REQ");
$request->store;
# ...then return our result:
return {
method => 'confirm',
stage => 'commit',
next => 'illview',
value => {},
};
}
else {
# Invalid stage, return error.
return {
error => 1,
status => 'unknown_stage',
message => '',
method => 'confirm',
stage => $params->{stage},
value => {},
};
}
}
=head3 cancel
my $response = $backend->cancel({ params => $params });
We will attempt to cancel a request that was confirmed.
In the FreeForm backend this simply means displaying text to the librarian
asking them to confirm they have taken all steps needed to cancel a confirmed
request.
=cut
sub cancel {
my ( $self, $params ) = @_;
my $stage = $params->{other}->{stage};
if ( !$stage || $stage eq 'init' ) {
# We simply need our template .INC to produce a text block.
return {
method => 'cancel',
stage => 'confirm',
value => $params,
};
}
elsif ( $stage eq 'confirm' ) {
$params->{request}->status("REQREV");
$params->{request}->orderid(undef);
$params->{request}->store;
return {
method => 'cancel',
stage => 'commit',
next => 'illview',
value => $params,
};
}
else {
# Invalid stage, return error.
return {
error => 1,
status => 'unknown_stage',
message => '',
method => 'cancel',
stage => $params->{stage},
value => {},
};
}
}
=head3 migrate
Migrate a request into or out of this backend.
=cut
sub migrate {
my ( $self, $params ) = @_;
my $other = $params->{other};
my $stage = $other->{stage};
my $step = $other->{step};
my $core_fields = _get_core_string();
# We may be recieving a submitted form due to an additional
# custom field being added or deleted, or the material type
# having been changed, so check for these things
if (
defined $other->{'add_new_custom'} ||
defined $other->{'custom_delete'} ||
defined $other->{'change_type'}
) {
if ( defined $other->{'add_new_custom'} ) {
my ( $custom_keys, $custom_vals ) =
_get_custom( $other->{'custom_key'}, $other->{'custom_value'} );
push @{$custom_keys}, '---';
push @{$custom_vals}, '---';
$other->{'custom_key'} = join "\0", @{$custom_keys};
$other->{'custom_value'} = join "\0", @{$custom_vals};
}
elsif ( defined $other->{'custom_delete'} ) {
my $delete_idx = $other->{'custom_delete'};
my ( $custom_keys, $custom_vals ) =
_get_custom( $other->{'custom_key'}, $other->{'custom_value'} );
splice @{$custom_keys}, $delete_idx, 1;
splice @{$custom_vals}, $delete_idx, 1;
$other->{'custom_key'} = join "\0", @{$custom_keys};
$other->{'custom_value'} = join "\0", @{$custom_vals};
}
elsif ( defined $other->{'change_type'} ) {
# We may be receiving a submitted form due to the user having
# changed request material type, so we just need to go straight
# back to the form, the type has been changed in the params
delete $other->{'change_type'};
}
return {
cwd => dirname(__FILE__),
status => "",
message => "",
error => 0,
value => $params,
method => "create",
stage => "form",
core => $core_fields
};
}
# Recieve a new request from another backend and suppliment it with
# anything we require specifically for this backend.
if ( !$stage || $stage eq 'immigrate' ) {
my $original_request =
Koha::Illrequests->find( $other->{illrequest_id} );
my $new_request = $params->{request};
$new_request->borrowernumber( $original_request->borrowernumber );
$new_request->branchcode( $original_request->branchcode );
$new_request->status('NEW');
$new_request->backend( $self->name );
$new_request->placed( DateTime->now );
$new_request->updated( DateTime->now );
$new_request->store;
my @default_attributes = (
qw/title type author year volume isbn issn article_title article_author pages/
);
my $original_attributes =
$original_request->illrequestattributes->search(
{ type => { '-in' => \@default_attributes } } );
my $request_details =
{ map { $_->type => $_->value } ( $original_attributes->as_list ) };
$request_details->{migrated_from} = $original_request->illrequest_id;
while ( my ( $type, $value ) = each %{$request_details} ) {
Koha::Illrequestattribute->new(
{
illrequest_id => $new_request->illrequest_id,
column_exists( 'illrequestattributes', 'backend' ) ? (backend =>"FreeForm") : (),
type => $type,
value => $value,
}
)->store;
}
return {
error => 0,
status => '',
message => '',
method => 'migrate',
stage => 'commit',
next => 'emigrate',
value => $params,
core => $core_fields
};
}
# Cleanup any outstanding work, close the request.
elsif ( $stage eq 'emigrate' ) {
my $new_request = $params->{request};
my $from_id = $new_request->illrequestattributes->find(
{ type => 'migrated_from' } )->value;
my $request = Koha::Illrequests->find($from_id);
# Just cancel the original request now it's been migrated away
$request->status("REQREV");
$request->orderid(undef);
$request->store;
return {
error => 0,
status => '',
message => '',
method => 'migrate',
stage => 'commit',
next => 'illview',
value => $params,
core => $core_fields
};
}
}
=head3 illview
View and manage an ILL request
=cut
sub illview {
my ($self, $params) = @_;
return {
method => "illview"
};
}
## Helpers
=head3 _get_requested_partners
=cut
sub _get_requested_partners {
# Take a request and retrieve an Illrequestattribute with
# the type 'requested_partners'.
my ($args) = @_;
my $where = {
illrequest_id => $args->{request}->id,
type => 'requested_partners'
};
my $res = Koha::Illrequestattributes->find($where);
return ($res) ? $res->value : undef;
}
=head3 _set_requested_partners
=cut
sub _set_requested_partners {
# Take a request and set an Illrequestattribute on it
# detailing the email address(es) of the requested
# partner(s). We replace any existing value since, by
# the time we get to this stage, any previous request
# from partners would have had to be cancelled
my ($args) = @_;
my $where = {
illrequest_id => $args->{request}->id,
type => 'requested_partners'
};
Koha::Illrequestattributes->search($where)->delete();
Koha::Illrequestattribute->new(
{
illrequest_id => $args->{request}->id,
column_exists( 'illrequestattributes', 'backend' ) ? (backend =>"FreeForm") : (),
type => 'requested_partners',
value => $args->{to}
}
)->store;
}
=head3 _validate_borrower
=cut
sub _validate_borrower {
# Perform cardnumber search. If no results, perform surname search.
# Return ( 0, undef ), ( 1, $brw ) or ( n, $brws )
my ($input) = @_;
my $patrons = Koha::Patrons->new;
my ( $count, $brw );
my $query = { cardnumber => $input };
my $brws = $patrons->search($query);
$count = $brws->count;
my @criteria = qw/ surname userid firstname end /;
while ( $count == 0 ) {
my $criterium = shift @criteria;
return ( 0, undef ) if ( "end" eq $criterium );
$brws = $patrons->search( { $criterium => $input } );
$count = $brws->count;
}
if ( $count == 1 ) {
$brw = $brws->next;
}
else {
$brw = $brws; # found multiple results
}
return ( $count, $brw );
}
=head3 _get_custom
=cut
sub _get_custom {
# Take an string of custom keys and an string
# of custom values, both delimited by \0 (by CGI)
# and return an arrayref of each
my ( $keys, $values ) = @_;
my @k = defined $keys ? split( "\0", $keys ) : ();
my @v = defined $values ? split( "\0", $values ) : ();
return ( \@k, \@v );
}
=head3 _prepare_custom
=cut
sub _prepare_custom {
# Take an arrayref of custom keys and an arrayref
# of custom values, return a hashref of them
my ( $keys, $values ) = @_;
my %out = ();
if ($keys) {
my @k = split( "\0", $keys );
my @v = split( "\0", $values );
%out = map { $k[$_] => $v[$_] } 0 .. $#k;
}
return \%out;
}
=head3 _get_request_details
my $request_details = _get_request_details($params, $other);
Return the illrequestattributes for a given request
=cut
sub _get_request_details {
my ($params, $other) = @_;
# Get custom key / values we've been passed
# Prepare them for addition into the Illrequestattribute object
my $custom =
_prepare_custom( $other->{'custom_key'}, $other->{'custom_value'} );
my $return = {
%$custom
};
my $core = _get_core_fields();
foreach my $key( keys %{$core}) {
$return->{$key} = $params->{other}->{$key};
}
return $return;
}
=head3 _get_core_string
Return a comma delimited, quoted, string of core field keys
=cut
sub _get_core_string {
my $core = _get_core_fields();
return join(",", map { '"'.$_.'"' } keys %{$core});
}
=head3 _get_core_fields
Return a hashref of core fields
=cut
sub _get_core_fields {
return {
article_author => 'Article author',
article_title => 'Article title',
associated_id => 'Associated ID',
author => 'Author',
chapter_author => 'Chapter author',
chapter => 'Chapter',
conference_date => 'Conference date',
doi => 'DOI',
editor => 'Editor',
institution => 'Institution',
isbn => 'ISBN',
issn => 'ISSN',
issue => 'Issue',
item_date => 'Date',
pages => 'Pages',
pagination => 'Pagination',
paper_author => 'Paper author',
paper_title => 'Paper title',
part_edition => 'Part / Edition',
publication => 'Publication',
published_date => 'Publication date',
published_place => 'Place of publication',
publisher => 'Publisher',
pubmedid => 'PubMed ID',
sponsor => 'Sponsor',
title => 'Title',
type => 'Type',
venue => 'Venue',
volume => 'Volume',
year => 'Year'
};
}
=head3 add_request
Add an ILL request
=cut
sub add_request {
my ( $self, $params ) = @_;
# ...Populate Illrequestattributes
# generate $request_details
my $request_details = _get_request_details( $params, $params->{other} );
my ( $brw_count, $brw ) =
_validate_borrower( $params->{other}->{'cardnumber'} );
## Create request
# Create bib record
my $biblionumber = $self->_freeform2biblio($request_details);
# ...Populate Illrequest
my $request = $params->{request};
$request->biblio_id($biblionumber) unless $biblionumber == 0;
$request->borrowernumber( $brw->borrowernumber );
$request->branchcode( $params->{other}->{branchcode} );
$request->status('NEW');
$request->backend( $params->{other}->{backend} );
$request->placed( DateTime->now );
$request->updated( DateTime->now );
$request->batch_id(
$params->{other}->{ill_batch_id} ? $params->{other}->{ill_batch_id} : $params->{other}->{batch_id} )
if column_exists( 'illrequests', 'batch_id' );
$request->store;
while ( my ( $type, $value ) = each %{$request_details} ) {
if ( $value && length $value > 0 ) {
Koha::Illrequestattribute->new(
{