Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 349 lines (306 sloc) 10.859 kb
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
#!/usr/bin/env perl

# Copyright 2009 PTFS Inc.
# 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 2 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., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA

use strict;
use warnings;

use CGI;
use C4::Auth;
use Koha;
use C4::Context;
use C4::Items;
use C4::Output;
use C4::ItemDeleteList;

my $query = new CGI;

# # editable fields
my %ifields_map = (
    homebranch => 'items.homebranch',
    notforloan => 'items.notforloan',
    damaged => 'items.damaged',
    itemlost => 'items.itemlost',
    wthdrawn => 'items.wthdrawn',
    holdingbranch => 'items.holdingbranch',
    location => 'items.location',
    permanent_location => 'items.permanent_location',
    itype => 'items.itype',
    restricted => 'items.restricted',
    ccode => 'items.ccode',
);
my @ifields = keys %ifields_map;

#itemnumber biblionumber biblioitemnumber barcode dateaccessioned booksellerid homebranch price replacementprice
#replacementpricedate datelastborrowed datelastseen stack notforloan damaged itemlost wthdrawn itemcallnumber
#issues renewals reserves restricted itemnotes holdingbranch paidfor timestamp location permanent_location
#onloan cn_source cn_sort ccode materials uri itype more_subfields_xml enumchron copynumber);

my ( $template, $user, $cookie ) = get_template_and_user(
    { template_name => 'tools/batchitemedit.tmpl',
        query => $query,
        type => 'intranet',
        authnotrequired => 0,
        flagsrequired => { tools => q|batch_edit_items|, },
    }
);

my $op = $query->param('confirm');

$op ||= q{}; # quieten warnings

my $dbh = C4::Context->dbh;

if ( $op eq 'Proceed' ) {
    my $reading_from_file = 0;
    my $uploadbarcodes = $query->param('uploadbarcodes');
    if ( $uploadbarcodes && length $uploadbarcodes > 0 ) {
        $reading_from_file = 1;
    }

    # perform updates on item list
    my $processed_items = [];
    my $updated = 0;
    my $columns_to_select = get_columns_to_select($reading_from_file);
    my $columns_to_edit = get_columns_to_edit();
    if ( $columns_to_edit && $columns_to_select ) {
        my $items = get_itemnumbers($columns_to_select);
        foreach my $item ( @{$items} ) {
            my $rep;
            $rep->{barcode} = $item->{barcode};
            ModItem( $columns_to_edit, $item->{bnum}, $item->{inum} );
            ++$updated;
            $rep->{updated} =
              1; # None of the update routines return success/failure
            push @{$processed_items}, $rep;
        }
    } elsif ( $columns_to_edit && $reading_from_file ) {
        while ( my $barcode = <$uploadbarcodes> ) {
            chomp $barcode;
            $barcode =~ s/\r//g;
            my $rep = { barcode => $barcode, };
            my $item = GetItem( "", $barcode );
            if ($item) {
                ModItem( $columns_to_edit, $item->{biblionumber},
                    $item->{itemnumber} );
                ++$updated;
                $rep->{updated} = 1;
            } else {
                $rep->{error} = 'No matching item found';
            }
            push @{$processed_items}, $rep;
        }
    }

    $template->param(
        completed => 1,
        edit_complete => 1,
        items_updated => $updated,
        itemsloop => $processed_items,
    );
} elsif ( $op eq 'Delete' ) {

    # delete the items in the list
    my $reading_from_file = 0;
    my $uploadbarcodes = $query->param('uploadbarcodes');
    if ( $uploadbarcodes && length $uploadbarcodes > 0 ) {
        $reading_from_file = 1;
    }
    my $columns_to_select = get_columns_to_select($reading_from_file);
    my $idl = C4::ItemDeleteList->new();
    if ($reading_from_file) {
        while ( my $barcode = <$uploadbarcodes> ) {
            chomp $barcode;
            $barcode =~ s/\r//g;
            my $rep = { barcode => $barcode, };
            my $item = GetItem( q{}, $barcode );
            $idl->append(
                { itemnumber => $item->{itemnumber},
                    biblionumber => $item->{biblionumber},
                }
            );
        }
    } elsif ($columns_to_select) {
        my $items = get_itemnumbers($columns_to_select);
        foreach my $item ( @{$items} ) {
            $idl->append(
                { itemnumber => $item->{inum},
                    biblionumber => $item->{bnum},
                }
            );
        }
    }
    $template->param(
        completed => 1,
        delete_complete => 1,
        items_updated => $idl->rowcount(),
        itemsloop => $idl->item_barcodes(),
        delete_list_id => $idl->list_id(),
    );

} else { # Generate the edit form
    my $branchloop = get_branches();
    my $withdrawnloop = get_withdrawn();
    my $lostloop = get_lost_statuses();
    my $damagedloop = get_damaged();
    my $userestrictionloop = get_userestrict();
    my $nflloop = get_notforloan();
    my $collectioncodeloop = get_ccodes();
    my $shelvinglocationloop = get_shelving_locations();
    my $itemtypeloop = get_itypes();

    # pass_selections ???
    $template->param(
        branchloop => $branchloop,
        withdrawnloop => $withdrawnloop,
        lostloop => $lostloop,
        damagedloop => $damagedloop,
        userestrictionloop => $userestrictionloop,
        nflloop => $nflloop,
        collectioncodeloop => $collectioncodeloop,
        shelvinglocationloop => $shelvinglocationloop,
        itemtypeloop => $itemtypeloop,

    );
}

#my %item;
# # editable fields
#my @ifields = qw(
#itemnumber biblionumber biblioitemnumber barcode dateaccessioned booksellerid homebranch price replacementprice
#replacementpricedate datelastborrowed datelastseen stack notforloan damaged itemlost wthdrawn itemcallnumber
#issues renewals reserves restricted itemnotes holdingbranch paidfor timestamp location permanent_location
#onloan cn_source cn_sort ccode materials uri itype more_subfields_xml enumchron copynumber);

output_html_with_http_headers $query, $cookie, $template->output;

sub get_authorised_values {
    my $cat = shift;
    my $loop = [];
    my $sql_query =
q{SELECT lib, authorised_value FROM authorised_values WHERE category = ? ORDER BY lib};
    my @tuples =
      @{ C4::Context->dbh->selectall_arrayref( $sql_query, { Slice => {} }, $cat ) };
    for my $t (@tuples) {
        push @{$loop},
          { value => $t->{authorised_value},
            description => $t->{lib},
            selected => undef,
          };
    }
    return $loop;
}

sub get_branches {
    my $loop;

    my $sql_query =
      q{SELECT branchcode, branchname FROM branches ORDER BY branchname};
    my @tuples = @{ C4::Context->dbh->selectall_arrayref( $sql_query, { Slice => {} } ) };
    for my $t (@tuples) {
        push @{$loop},
          { value => $t->{branchcode},
            description => $t->{branchname},
            selected => undef,
          };
    }
    return $loop;
}

sub get_withdrawn {
    return get_authorised_values('WITHDRAWN');
}

sub get_lost_statuses {
    return get_authorised_values('LOST');
}

sub get_damaged {
    return get_authorised_values('DAMAGED');
}

sub get_userestrict {
    return get_authorised_values('RESTRICTED');
}

sub get_notforloan {
    return get_authorised_values('NOT_LOAN');
}

sub get_ccodes {
    return get_authorised_values('CCODE');
}

sub get_shelving_locations {
    return get_authorised_values('LOC');
}

sub get_itypes {
    my $loop;

    my $sql_query =
      'SELECT itemtype, description FROM itemtypes ORDER BY description';
    my @types = @{ C4::Context->dbh->selectall_arrayref( $sql_query, { Slice => {} } ) };
    for my $it (@types) {
        push @{$loop},
          { value => $it->{itemtype},
            description => $it->{description},
            selected => 0,
          };
    }
    return $loop;
}

sub get_itemnumbers {
    my $sel = shift;
    my ( @where, @bind_parameters );

    # Get a list of matching item numbers
    # similar to C4/Items/GetItemsForInventory
    my $select =
      'SELECT items.itemnumber as inum, barcode, itemcallnumber, title, author,'
      . ' biblio.biblionumber as bnum, datelastseen FROM items '
      . 'LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber '
      . 'LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber';

    for my $field ( keys %{$sel} ) {
        push @where, " $ifields_map{$field} = ? ";
        push @bind_parameters, $sel->{$field};
    }

    if (@where) {
        $select .= ' where ';
        $select .= join ' and ', @where;
    } else {
        return; # Don't do all items with no selection
    }
    my $result_set =
      $dbh->selectall_arrayref( $select, { Slice => {} }, @bind_parameters );
    return $result_set;
}

sub get_columns_to_select {
    my $upload_barcodes = shift;
    if ($upload_barcodes) {
        return;
    }
    my $select_hash = {};
    my @fields_to_update;
    for my $field (@ifields) {
        my $value = $query->param("select_$field");
        if (defined($value)) {
            push @fields_to_update, $field;
        }
    }
    for my $field (@fields_to_update) {
        my $value = $query->param($field);
        if (defined($value)) {
            $select_hash->{$field} = $value;
        }
    }

    if ( keys %{$select_hash} ) {
        return $select_hash;
    }

    # no selection criteria requested return undef
    return;
}

sub get_columns_to_edit {

    # created a hash of item fields to change
    my $edit_hash = {};
    my @fields_to_update;
    for my $field (@ifields) {
        my $value = $query->param("change_$field");
        if (defined($value)) {
            push @fields_to_update, $field;
        }
    }
    for my $field (@fields_to_update) {
        my $value = $query->param("new_$field");
        if (defined($value)) {
            $edit_hash->{$field} = $value;
        }
    }

    if ( keys %{$edit_hash} ) {
        return $edit_hash;
    }

    # no change requested return undef
    return;
}
Something went wrong with that request. Please try again.