View | Details | Raw Unified | Return to bug 18606
Collapse All | Expand All

(-)a/C4/RotatingCollections.pm (-50 lines)
Lines 48-108 BEGIN { Link Here
48
    require Exporter;
48
    require Exporter;
49
    @ISA    = qw( Exporter );
49
    @ISA    = qw( Exporter );
50
    @EXPORT = qw(
50
    @EXPORT = qw(
51
      AddItemToCollection
52
      RemoveItemFromCollection
51
      RemoveItemFromCollection
53
      TransferCollection
52
      TransferCollection
54
    );
53
    );
55
}
54
}
56
55
57
=head2 AddItemToCollection
58
59
 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
60
61
Adds an item to a rotating collection.
62
63
 Input:
64
   $colId: Collection to add the item to.
65
   $itemnumber: Item to be added to the collection
66
 Output:
67
   $success: 1 if all database operations were successful, 0 otherwise
68
   $errorCode: Code for reason of failure, good for translating errors in templates
69
   $errorMessage: English description of error
70
71
=cut
72
73
sub AddItemToCollection {
74
    my ( $colId, $itemnumber ) = @_;
75
76
    ## Check for all necessary parameters
77
    if ( !$colId ) {
78
        return ( 0, 1, "NO_ID" );
79
    }
80
    if ( !$itemnumber ) {
81
        return ( 0, 2, "NO_ITEM" );
82
    }
83
84
    if ( isItemInThisCollection( $itemnumber, $colId ) ) {
85
        return ( 0, 2, "IN_COLLECTION" );
86
    }
87
    elsif ( isItemInAnyCollection($itemnumber) ) {
88
        return ( 0, 3, "IN_COLLECTION_OTHER" );
89
    }
90
91
    my $dbh = C4::Context->dbh;
92
93
    my $sth;
94
    $sth = $dbh->prepare("
95
        INSERT INTO collections_tracking (
96
            colId,
97
            itemnumber
98
        ) VALUES ( ?, ? )
99
    ");
100
    $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
101
102
    return 1;
103
104
}
105
106
=head2  RemoveItemFromCollection
56
=head2  RemoveItemFromCollection
107
57
108
 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
58
 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
(-)a/Koha/RotatingCollection.pm (+33 lines)
Lines 22-28 use Modern::Perl; Link Here
22
use Carp;
22
use Carp;
23
23
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::Exceptions;
25
use Koha::Items;
26
use Koha::Items;
27
use Koha::RotatingCollection::Trackings;
26
28
27
use base qw(Koha::Object);
29
use base qw(Koha::Object);
28
30
Lines 54-59 sub items { Link Here
54
    return $items;
56
    return $items;
55
}
57
}
56
58
59
=head3 add_item
60
61
$collection->add_item( $item_object );
62
63
throws
64
    Koha::Exceptions::MissingParameter
65
    Koha::Exceptions::DuplicateObject
66
    Koha::Exceptions::ObjectNotFound
67
68
=cut
69
70
sub add_item {
71
    my ( $self, $item ) = @_;
72
73
    Koha::Exceptions::MissingParameter->throw if not defined $item;
74
75
    Koha::Exceptions::ObjectNotFound->throw if ref($item) ne 'Koha::Item';
76
77
    Koha::Exceptions::DuplicateObject->throw
78
        if Koha::RotatingCollection::Trackings->search( { itemnumber => $item->itemnumber } )->count;
79
80
    my $col_tracking = Koha::RotatingCollection::Tracking->new(
81
        {
82
            colId => $self->colId,
83
            itemnumber => $item->itemnumber,
84
        }
85
    )->store;
86
87
    return $col_tracking;
88
}
89
57
=head3 type
90
=head3 type
58
91
59
=cut
92
=cut
(-)a/rotating_collections/addItems.pl (-15 / +14 lines)
Lines 42-50 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
42
    }
42
    }
43
);
43
);
44
44
45
my @errors;
46
my @messages;
47
my $colId = $query->param('colId');
48
my $collection = Koha::RotatingCollections->find($colId);
49
45
if ( $query->param('action') eq 'addItem' ) {
50
if ( $query->param('action') eq 'addItem' ) {
46
    ## Add the given item to the collection
51
    ## Add the given item to the collection
47
    my $colId      = $query->param('colId');
48
    my $barcode    = $query->param('barcode');
52
    my $barcode    = $query->param('barcode');
49
    my $removeItem = $query->param('removeItem');
53
    my $removeItem = $query->param('removeItem');
50
    my $item       = Koha::Items->find({barcode => $barcode});
54
    my $item       = Koha::Items->find({barcode => $barcode});
Lines 55-74 if ( $query->param('action') eq 'addItem' ) { Link Here
55
    $template->param( barcode => $barcode );
59
    $template->param( barcode => $barcode );
56
60
57
    if ( !$removeItem ) {
61
    if ( !$removeItem ) {
58
        ( $success, $errorCode, $errorMessage ) =
62
        my $added = eval { $collection->add_item( $item ) };
59
          AddItemToCollection( $colId, $itemnumber );
63
64
        if ( $@ or not $added ) {
65
            push @errors, { code => 'error_adding_item' };
66
        } else {
67
            push @messages, { code => 'success_adding_item' };
68
        }
60
69
61
        $template->param(
70
        $template->param(
62
            previousActionAdd => 1,
71
            previousActionAdd => 1,
63
        );
72
        );
64
65
        if ($success) {
66
            $template->param( addSuccess => 1 );
67
        }
68
        else {
69
            $template->param( addFailure     => 1 );
70
            $template->param( failureMessage => $errorMessage );
71
        }
72
    }
73
    }
73
    else {
74
    else {
74
        ## Remove the given item from the collection
75
        ## Remove the given item from the collection
Lines 91-101 if ( $query->param('action') eq 'addItem' ) { Link Here
91
    }
92
    }
92
}
93
}
93
94
94
my $colId = $query->param('colId');
95
my $collection = Koha::RotatingCollections->find($colId);
96
97
$template->param(
95
$template->param(
98
    collection => $collection,
96
    collection => $collection,
97
    messages   => \@messages,
98
    errors     => \@errors,
99
);
99
);
100
100
101
output_html_with_http_headers $query, $cookie, $template->output;
101
output_html_with_http_headers $query, $cookie, $template->output;
102
- 

Return to bug 18606