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

(-)a/C4/Search.pm (-92 / +91 lines)
Lines 1231-1255 sub buildQuery { Link Here
1231
        $query = "ccl=$query" if $cclq;
1231
        $query = "ccl=$query" if $cclq;
1232
    }
1232
    }
1233
1233
1234
    # add limits
1235
    my %group_OR_limits;
1236
    my $availability_limit;
1237
    foreach my $this_limit (@limits) {
1238
        next unless $this_limit;
1239
        if ( $this_limit =~ /available/ ) {
1240
#
1241
## 'available' is defined as (items.onloan is NULL) and (items.itemlost = 0)
1242
## In English:
1243
## all records not indexed in the onloan register (zebra) and all records with a value of lost equal to 0
1244
            $availability_limit .=
1245
"( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) )";
1246
            $limit_cgi  .= "&limit=available";
1247
            $limit_desc .= "";
1248
        }
1249
1250
        # group_OR_limits, prefixed by mc-
1251
        # OR every member of the group
1252
        elsif ( $this_limit =~ /mc/ ) {
1253
            my ($k,$v) = split(/:/, $this_limit,2);
1254
            if ( $k !~ /mc-i(tem)?type/ ) {
1255
                # in case the mc-ccode value has complicating chars like ()'s inside it we wrap in quotes
1256
                $this_limit =~ tr/"//d;
1257
                $this_limit = $k.':"'.$v.'"';
1258
            }
1259
1260
            $group_OR_limits{$k} .= " or " if $group_OR_limits{$k};
1261
            $limit_desc      .= " or " if $group_OR_limits{$k};
1262
            $group_OR_limits{$k} .= "$this_limit";
1263
            $limit_cgi       .= "&limit=" . uri_escape_utf8($this_limit);
1264
            $limit_desc      .= " $this_limit";
1265
        }
1266
        elsif ( $this_limit =~ '^multibranchlimit:|^branch:' ) {
1267
            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1268
            $limit .= " and " if $limit || $query;
1269
            my $branchfield  = C4::Context->preference('SearchLimitLibrary');
1270
            my @branchcodes;
1271
            if(  $this_limit =~ '^multibranchlimit:' ){
1272
                my ($group_id) = ( $this_limit =~ /^multibranchlimit:(.*)$/ );
1273
                my $search_group = Koha::Library::Groups->find( $group_id );
1274
                @branchcodes  = map { $_->branchcode } $search_group->all_libraries;
1275
                @branchcodes = sort { $a cmp $b } @branchcodes;
1276
            } else {
1277
                @branchcodes = ( $this_limit =~ /^branch:(.*)$/ );
1278
            }
1279
1280
            if (@branchcodes) {
1281
                if ( $branchfield eq "homebranch" ) {
1282
                    $this_limit = sprintf "(%s)", join " or ", map { 'homebranch: ' . $_ } @branchcodes;
1283
                }
1284
                elsif ( $branchfield eq "holdingbranch" ) {
1285
                    $this_limit = sprintf "(%s)", join " or ", map { 'holdingbranch: ' . $_ } @branchcodes;
1286
                }
1287
                else {
1288
                    $this_limit =  sprintf "(%s or %s)",
1289
                      join( " or ", map { 'homebranch: ' . $_ } @branchcodes ),
1290
                      join( " or ", map { 'holdingbranch: ' . $_ } @branchcodes );
1291
                }
1292
            }
1293
            $limit .= "$this_limit";
1294
            $limit_desc .= " $this_limit";
1295
        } elsif ( $this_limit =~ '^search_filter:' ) {
1296
            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1297
            my ($filter_id) = ( $this_limit =~ /^search_filter:(.*)$/ );
1298
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1299
            next unless $search_filter;
1300
            my $expanded = $search_filter->expand_filter;
1301
            my ( $error, undef, undef, undef, undef, $fixed_limit, undef, undef, undef ) = buildQuery ( undef, undef, undef, $expanded, undef, undef, $lang);
1302
            $limit .= " and " if $limit || $query;
1303
            $limit .= "$fixed_limit";
1304
            $limit_desc .= " $limit";
1305
        }
1306
1307
        # Regular old limits
1308
        else {
1309
            $limit .= " and " if $limit || $query;
1310
            $limit      .= "$this_limit";
1311
            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1312
            $limit_desc .= " $this_limit";
1313
        }
1314
    }
1315
    foreach my $k (keys (%group_OR_limits)) {
1316
        $limit .= " and " if ( $query || $limit );
1317
        $limit .= "($group_OR_limits{$k})";
1318
    }
1319
    if ($availability_limit) {
1320
        $limit .= " and " if ( $query || $limit );
1321
        $limit .= "($availability_limit)";
1322
    }
1323
1234
# for handling ccl, cql, pqf queries in diagnostic mode, skip the rest of the steps
1324
# for handling ccl, cql, pqf queries in diagnostic mode, skip the rest of the steps
1235
# DIAGNOSTIC ONLY!!
1325
# DIAGNOSTIC ONLY!!
1236
    if ( $query =~ /^ccl=/ ) {
1326
    if ( $query =~ /^ccl=/ ) {
1237
        my $q=$';
1327
        my $q=$';
1238
        # This is needed otherwise ccl= and &limit won't work together, and
1328
        # This is needed otherwise ccl= and &limit won't work together, and
1239
        # this happens when selecting a subject on the opac-detail page
1329
        # this happens when selecting a subject on the opac-detail page
1240
        @limits = grep {!/^$/} @limits;
1241
        my $original_q = $q; # without available part
1330
        my $original_q = $q; # without available part
1242
        unless ( grep { $_ eq 'available' } @limits ) {
1331
        $q .= $limit;
1243
            $q =~ s| and \( \(allrecords,AlwaysMatches=''\) and \(not-onloan-count,st-numeric >= 1\) and \(lost,st-numeric=0\) \)||;
1244
            $original_q = $q;
1245
        }
1246
        if ( @limits ) {
1247
            if ( grep { $_ eq 'available' } @limits ) {
1248
                $q .= q| and ( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) )|;
1249
                @limits = grep {!/^available$/} @limits;
1250
            }
1251
            $q .= ' and '.join(' and ', @limits) if @limits;
1252
        }
1253
        return ( undef, $q, $q, "q=ccl=".uri_escape_utf8($q), $original_q, '', '', '', 'ccl' );
1332
        return ( undef, $q, $q, "q=ccl=".uri_escape_utf8($q), $original_q, '', '', '', 'ccl' );
1254
    }
1333
    }
1255
    if ( $query =~ /^cql=/ ) {
1334
    if ( $query =~ /^cql=/ ) {
Lines 1446-1530 sub buildQuery { Link Here
1446
    }
1525
    }
1447
    Koha::Logger->get->debug("QUERY BEFORE LIMITS: >$query<");
1526
    Koha::Logger->get->debug("QUERY BEFORE LIMITS: >$query<");
1448
1527
1449
    # add limits
1450
    my %group_OR_limits;
1451
    my $availability_limit;
1452
    foreach my $this_limit (@limits) {
1453
        next unless $this_limit;
1454
        if ( $this_limit =~ /available/ ) {
1455
#
1456
## 'available' is defined as (items.onloan is NULL) and (items.itemlost = 0)
1457
## In English:
1458
## all records not indexed in the onloan register (zebra) and all records with a value of lost equal to 0
1459
            $availability_limit .=
1460
"( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) )";
1461
            $limit_cgi  .= "&limit=available";
1462
            $limit_desc .= "";
1463
        }
1464
1465
        # group_OR_limits, prefixed by mc-
1466
        # OR every member of the group
1467
        elsif ( $this_limit =~ /mc/ ) {
1468
            my ($k,$v) = split(/:/, $this_limit,2);
1469
            if ( $k !~ /mc-i(tem)?type/ ) {
1470
                # in case the mc-ccode value has complicating chars like ()'s inside it we wrap in quotes
1471
                $this_limit =~ tr/"//d;
1472
                $this_limit = $k.':"'.$v.'"';
1473
            }
1474
1475
            $group_OR_limits{$k} .= " or " if $group_OR_limits{$k};
1476
            $limit_desc      .= " or " if $group_OR_limits{$k};
1477
            $group_OR_limits{$k} .= "$this_limit";
1478
            $limit_cgi       .= "&limit=" . uri_escape_utf8($this_limit);
1479
            $limit_desc      .= " $this_limit";
1480
        }
1481
        elsif ( $this_limit =~ '^multibranchlimit:|^branch:' ) {
1482
            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1483
            $limit .= " and " if $limit || $query;
1484
            my $branchfield  = C4::Context->preference('SearchLimitLibrary');
1485
            my @branchcodes;
1486
            if(  $this_limit =~ '^multibranchlimit:' ){
1487
                my ($group_id) = ( $this_limit =~ /^multibranchlimit:(.*)$/ );
1488
                my $search_group = Koha::Library::Groups->find( $group_id );
1489
                @branchcodes  = map { $_->branchcode } $search_group->all_libraries;
1490
                @branchcodes = sort { $a cmp $b } @branchcodes;
1491
            } else {
1492
                @branchcodes = ( $this_limit =~ /^branch:(.*)$/ );
1493
            }
1494
1495
            if (@branchcodes) {
1496
                if ( $branchfield eq "homebranch" ) {
1497
                    $this_limit = sprintf "(%s)", join " or ", map { 'homebranch: ' . $_ } @branchcodes;
1498
                }
1499
                elsif ( $branchfield eq "holdingbranch" ) {
1500
                    $this_limit = sprintf "(%s)", join " or ", map { 'holdingbranch: ' . $_ } @branchcodes;
1501
                }
1502
                else {
1503
                    $this_limit =  sprintf "(%s or %s)",
1504
                      join( " or ", map { 'homebranch: ' . $_ } @branchcodes ),
1505
                      join( " or ", map { 'holdingbranch: ' . $_ } @branchcodes );
1506
                }
1507
            }
1508
            $limit .= "$this_limit";
1509
            $limit_desc .= " $this_limit";
1510
        }
1511
1512
        # Regular old limits
1513
        else {
1514
            $limit .= " and " if $limit || $query;
1515
            $limit      .= "$this_limit";
1516
            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1517
            $limit_desc .= " $this_limit";
1518
        }
1519
    }
1520
    foreach my $k (keys (%group_OR_limits)) {
1521
        $limit .= " and " if ( $query || $limit );
1522
        $limit .= "($group_OR_limits{$k})";
1523
    }
1524
    if ($availability_limit) {
1525
        $limit .= " and " if ( $query || $limit );
1526
        $limit .= "($availability_limit)";
1527
    }
1528
1528
1529
    # Normalize the query and limit strings
1529
    # Normalize the query and limit strings
1530
    # This is flawed , means we can't search anything with : in it
1530
    # This is flawed , means we can't search anything with : in it
1531
- 

Return to bug 30528