From 1e5a860287b1408814a01f4eefc2db900b2344e8 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 9 May 2013 10:55:55 -0400 Subject: [PATCH] Bug 10276 - Extend IndependentBranches to support groups of libraries This patch adds the ability to have independent library groups. To enable this feature, enable the IndependentBranches system preference. If no libraries are in any independent library groups, IndependentBranches will behave as it always have. If a library is part of an independent library group with one or more libraries, that library will be able to access and modify patrons and items from any library in that group. Let's say we have 3 groups: Group1: LibA LibB LibC Group1: LibD LibE LibF Group3: LibG LibH LibA Note how LibA is in two groups ( Group1 and Group3 ). With this configuration, if IndependentBranches is enabled, libraries will be able to access and edit patrons and items in the following configuration: LibA => LibB, LibC, LibG, LibH LibB => LibA, LibC LibC => LibA, LibB LibD => LibE, LibF LibF => LibD, LibE LibG => LibH, LibA LibH => LibG, LibA Furthermore, let us assume there is a library LibI, which does not belong to any group. That library will only be able to view and edit patrons and items from it's own library. Imagine a library consortium consisting of multiple library systems. This feature would allow a consortium to group libraries by system such that those systems could work independently from one another on a single installation. Test Plan: 1) Apply this patch 2) Run updatedatabase.pl 3) Enable IndependentBranches 4) Test independent branches, no changes should be noted 5) Navigate to admin/branches.pl 6) Create a new Independent library group 7) Add your library and some other libraries to that group 8) Re-test IndependentBranches, everything should work as previously, but instead of being limited to just your library, you should have access to everything within your library group. Example: Try to edit an item. If the item is owned by a library in your group, you should be able to edit it. Signed-off-by: Joel Sasse Signed-off-by: Mark Tompsett --- C4/Acquisition.pm | 23 ++- C4/Branch.pm | 108 ++++++++++- C4/Circulation.pm | 21 ++- C4/Items.pm | 48 +++-- C4/Letters.pm | 1 + C4/Members.pm | 118 +++++++----- C4/Serials.pm | 109 +++++++++-- C4/Suggestions.pm | 51 +++-- acqui/basket.pl | 22 ++- admin/branches.pl | 52 ++---- catalogue/moredetail.pl | 15 +- cataloguing/additem.pl | 13 +- circ/circulation-home.pl | 7 +- circ/pendingreserves.pl | 5 +- circ/reserveratios.pl | 6 +- installer/data/mysql/kohastructure.sql | 2 +- installer/data/mysql/updatedatabase.pl | 18 ++- .../prog/en/modules/admin/branches.tt | 210 +++++++++++++------- members/deletemem.pl | 14 +- members/member.pl | 2 +- t/db_dependent/Branch.t | 76 +++++++- 21 files changed, 662 insertions(+), 259 deletions(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index a14ffe7..9d1ad6d 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -31,6 +31,7 @@ use C4::Debug; use C4::SQLHelper qw(InsertInTable UpdateInTable); use C4::Bookseller qw(GetBookSellerFromId); use C4::Templates qw(gettemplate); +use C4::Branch qw(GetIndependentGroupModificationRights); use Time::localtime; use HTML::Entities; @@ -1964,9 +1965,9 @@ sub GetParcel { my @query_params = ( $supplierid, $code, $datereceived ); if ( C4::Context->preference("IndependentBranches") ) { unless ( C4::Context->IsSuperLibrarian() ) { - $strsth .= " and (borrowers.branchcode = ? - or borrowers.branchcode = '')"; - push @query_params, C4::Context->userenv->{branch}; + my $branches = + GetIndependentGroupModificationRights( { stringify => 1 } ); + $strsth .= " AND ( borrowers.branchcode IN ( $branches ) OR borrowers.branchcode = '')"; } } $strsth .= " ORDER BY aqbasket.basketno"; @@ -2181,8 +2182,8 @@ sub GetLateOrders { } if (C4::Context->preference("IndependentBranches") && !C4::Context->IsSuperLibrarian() ) { - $from .= ' AND borrowers.branchcode LIKE ? '; - push @query_params, C4::Context->userenv->{branch}; + my $branches = GetIndependentGroupModificationRights( { stringify => 1 } ); + $from .= qq{ AND borrowers.branchcode IN ( $branches ) }; } $from .= " AND orderstatus <> 'cancelled' "; my $query = "$select $from $having\nORDER BY latesince, basketno, borrowers.branchcode, supplier"; @@ -2403,12 +2404,12 @@ sub GetHistory { $query .= ") "; } - - if ( C4::Context->preference("IndependentBranches") ) { - unless ( C4::Context->IsSuperLibrarian() ) { - $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) "; - push @query_params, C4::Context->userenv->{branch}; - } + if ( C4::Context->preference("IndependentBranches") + && !C4::Context->IsSuperLibrarian() ) + { + my $branches = + GetIndependentGroupModificationRights( { stringify => 1 } ); + $query .= qq{ AND ( borrowers.branchcode = ? OR borrowers.branchcode IN ( $branches ) ) }; } $query .= " ORDER BY id"; my $sth = $dbh->prepare($query); diff --git a/C4/Branch.pm b/C4/Branch.pm index 83d2010..f6c4333 100644 --- a/C4/Branch.pm +++ b/C4/Branch.pm @@ -19,6 +19,7 @@ package C4::Branch; use strict; #use warnings; FIXME - Bug 2505 require Exporter; +use Carp; use C4::Context; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); @@ -42,6 +43,7 @@ BEGIN { &GetBranchCategories &GetBranchesInCategory &ModBranchCategoryInfo + &GetIndependentGroupModificationRights &DelBranch &DelBranchCategory &CheckCategoryUnique @@ -113,9 +115,9 @@ sub GetBranches { my $sth; my $query = "SELECT * FROM branches"; my @bind_parameters; - if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) { - $query .= ' WHERE branchcode = ? '; - push @bind_parameters, C4::Context->userenv->{branch}; + if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){ + my $branches = GetIndependentGroupModificationRights({ stringify => 1 }); + $query .= qq{ WHERE branchcode IN ( $branches ) }; } $query .= " ORDER BY branchname"; $sth = $dbh->prepare($query); @@ -294,7 +296,10 @@ C<$results> is an hashref sub GetBranchCategory { my ($catcode) = @_; - return unless $catcode; + unless ( $catcode ) { + carp("No category code passed in!"); + return; + } my $dbh = C4::Context->dbh; my $sth; @@ -364,7 +369,7 @@ the categories were already here, and minimally used. #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories. sub GetCategoryTypes { - return ( 'searchdomain','properties'); + return ( 'searchdomain','independent_groups'); } =head2 GetBranch @@ -419,6 +424,99 @@ sub GetBranchesInCategory { return( \@branches ); } +=head2 GetIndependentGroupModificationRights + + GetIndependentGroupModificationRights( + { + branch => $this_branch, + for => $other_branch, + stringify => 1, + } + ); + + Returns a list of branches this branch shares a common + independent group with. + + If 'branch' is not provided, it will be looked up via + C4::Context->userenv->{branch}. + + If 'for' is provided, the lookup is limited to that branch. + + If called in a list context, returns a list of + branchcodes ( including $this_branch ). + + If called in a scalar context, it returns + a count of matching branchcodes. Returns 1 if + + If stringify param is passed, the return value will + be a string of the comma delimited branchcodes. This + is useful for "branchcode IN $branchcodes" clauses + in SQL queries. + + $this_branch and $other_branch are equal for efficiency. + + So you can write: + my @branches = GetIndependentGroupModificationRights(); + or something like: + if ( GetIndependentGroupModificationRights( { for => $other_branch } ) ) { do_stuff(); } + +=cut + +sub GetIndependentGroupModificationRights { + my ($params) = @_; + + my $this_branch = $params->{branch}; + my $other_branch = $params->{for}; + my $stringify = $params->{stringify}; + + $this_branch ||= C4::Context->userenv->{branch}; + + carp("No branch found!") unless ($this_branch); + + return 1 if ( $this_branch eq $other_branch ); + + my $sql = q{ + SELECT DISTINCT(branchcode) + FROM branchrelations + JOIN branchcategories USING ( categorycode ) + WHERE categorycode IN ( + SELECT categorycode + FROM branchrelations + WHERE branchcode = ? + ) + AND branchcategories.categorytype = 'independent_group' + }; + + my @params; + push( @params, $this_branch ); + + if ($other_branch) { + $sql .= q{ AND branchcode = ? }; + push( @params, $other_branch ); + } + + my $dbh = C4::Context->dbh; + my @branchcodes = @{ $dbh->selectcol_arrayref( $sql, {}, @params ) }; + + if ( $stringify ) { + if ( @branchcodes ) { + return join( ',', map { qq{'$_'} } @branchcodes ); + } else { + return qq{'$this_branch'}; + } + } + + if ( wantarray() ) { + if ( @branchcodes ) { + return @branchcodes; + } else { + return $this_branch; + } + } else { + return scalar(@branchcodes); + } +} + =head2 GetBranchInfo $results = GetBranchInfo($branchcode); diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 639956b..fc4f003 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -896,14 +896,25 @@ sub CanBookBeIssued { $alerts{ITEM_LOST} = $code if ( C4::Context->preference("IssueLostItem") eq 'alert' ); } if ( C4::Context->preference("IndependentBranches") ) { - my $userenv = C4::Context->userenv; unless ( C4::Context->IsSuperLibrarian() ) { - if ( $item->{C4::Context->preference("HomeOrHoldingBranch")} ne $userenv->{branch} ){ + unless ( + GetIndependentGroupModificationRights( + { + for => $item->{ C4::Context->preference( + "HomeOrHoldingBranch") } + } + ) + ) + { $issuingimpossible{ITEMNOTSAMEBRANCH} = 1; - $issuingimpossible{'itemhomebranch'} = $item->{C4::Context->preference("HomeOrHoldingBranch")}; + $issuingimpossible{'itemhomebranch'} = + $item->{ C4::Context->preference("HomeOrHoldingBranch") }; } - $needsconfirmation{BORRNOTSAMEBRANCH} = GetBranchName( $borrower->{'branchcode'} ) - if ( $borrower->{'branchcode'} ne $userenv->{branch} ); + + $needsconfirmation{BORRNOTSAMEBRANCH} = + GetBranchName( $borrower->{'branchcode'} ) + if ( + $borrower->{'branchcode'} ne C4::Context->userenv->{branch} ); } } diff --git a/C4/Items.pm b/C4/Items.pm index c183644..2233983 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -35,6 +35,7 @@ use DateTime::Format::MySQL; use Data::Dumper; # used as part of logging item record changes, not just for # debugging; so please don't remove this use Koha::DateUtils qw/dt_from_string/; +use C4::Branch qw/GetIndependentGroupModificationRights/; use vars qw($VERSION @ISA @EXPORT); @@ -1295,18 +1296,22 @@ sub GetItemsInfo { my $datedue = ''; $isth->execute( $data->{'itemnumber'} ); if ( my $idata = $isth->fetchrow_hashref ) { - $data->{borrowernumber} = $idata->{borrowernumber}; - $data->{cardnumber} = $idata->{cardnumber}; - $data->{surname} = $idata->{surname}; - $data->{firstname} = $idata->{firstname}; + $data->{borrowernumber} = $idata->{borrowernumber}; + $data->{cardnumber} = $idata->{cardnumber}; + $data->{surname} = $idata->{surname}; + $data->{firstname} = $idata->{firstname}; $data->{lastreneweddate} = $idata->{lastreneweddate}; - $datedue = $idata->{'date_due'}; - if (C4::Context->preference("IndependentBranches")){ - my $userenv = C4::Context->userenv; - unless ( C4::Context->IsSuperLibrarian() ) { - $data->{'NOTSAMEBRANCH'} = 1 if ($idata->{'bcode'} ne $userenv->{branch}); - } - } + $datedue = $idata->{'date_due'}; + + if ( C4::Context->preference("IndependentBranches") && C4::Context->userenv ) { + unless ( + C4::Context->IsSuperLibrarian() + || GetIndependentGroupModificationRights( { for => $idata->{'bcode'} } ) + ) + { + $data->{'NOTSAMEBRANCH'} = 1 if ($idata->{'bcode'} ne C4::Context->userenv->{branch}); + } + } } if ( $data->{'serial'}) { $ssth->execute($data->{'itemnumber'}) ; @@ -2262,12 +2267,18 @@ sub DelItemCheck { my $item = GetItem($itemnumber); - if ($onloan){ - $error = "book_on_loan" + if ($onloan) { + $error = "book_on_loan"; } - elsif ( !C4::Context->IsSuperLibrarian() - and C4::Context->preference("IndependentBranches") - and ( C4::Context->userenv->{branch} ne $item->{'homebranch'} ) ) + elsif ( + !C4::Context->IsSuperLibrarian() + && C4::Context->preference("IndependentBranches") + && !GetIndependentGroupModificationRights( + { + for => $item->{ C4::Context->preference("HomeOrHoldingBranch") } + } + ) + ) { $error = "not_same_branch"; } @@ -2749,8 +2760,9 @@ sub PrepareItemrecordDisplay { if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) { if ( ( C4::Context->preference("IndependentBranches") ) && !C4::Context->IsSuperLibrarian() ) { - my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname" ); - $sth->execute( C4::Context->userenv->{branch} ); + my $branches = GetIndependentGroupModificationRights( { stringify => 1 } ); + my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches WHERE branchcode IN ( $branches ) ORDER BY branchname" ); + $sth->execute(); push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} ); while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) { diff --git a/C4/Letters.pm b/C4/Letters.pm index 055c8c1..24a522e 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -126,6 +126,7 @@ sub getletter { my ( $module, $code, $branchcode, $message_transport_type ) = @_; $message_transport_type ||= 'email'; + $branchcode ||= q{}; if ( C4::Context->preference('IndependentBranches') and $branchcode diff --git a/C4/Members.pm b/C4/Members.pm index f832495..5024de7 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -25,6 +25,7 @@ use strict; use C4::Context; use C4::Dates qw(format_date_in_iso format_date); use String::Random qw( random_string ); +use Clone qw(clone); use Date::Calc qw/Today Add_Delta_YM check_date Date_to_Days/; use C4::Log; # logaction use C4::Overdues; @@ -41,7 +42,7 @@ use Koha::DateUtils; use Koha::Borrower::Debarments qw(IsDebarred); use Text::Unaccent qw( unac_string ); use Koha::AuthUtils qw(hash_password); - +use C4::Branch qw( GetIndependentGroupModificationRights ); our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,$debug); BEGIN { @@ -255,25 +256,24 @@ sub Search { # $showallbranches was not used at the time SearchMember() was mainstreamed into Search(). # Mentioning for the reference - if ( C4::Context->preference("IndependentBranches") ) { # && !$showallbranches){ - if ( my $userenv = C4::Context->userenv ) { - my $branch = $userenv->{'branch'}; - if ( !C4::Context->IsSuperLibrarian() && $branch ){ - if (my $fr = ref $filter) { - if ( $fr eq "HASH" ) { - $filter->{branchcode} = $branch; - } - else { - foreach (@$filter) { - $_ = { '' => $_ } unless ref $_; - $_->{branchcode} = $branch; - } - } + if ( C4::Context->preference("IndependentBranches") ) { + unless ( C4::Context->IsSuperLibrarian() ) { + $filter = clone($filter); # Modify a copy only + my @branches = GetIndependentGroupModificationRights(); + if ( my $fr = ref $filter ) { + if ( $fr eq "HASH" ) { + $filter->{branchcode} = \@branches; } else { - $filter = { '' => $filter, branchcode => $branch }; + foreach (@$filter) { + $_ = { '' => $_ } unless ref $_; + $_->{branchcode} = \@branches; + } } - } + } + else { + $filter = { '' => $filter, branchcode => \@branches }; + } } } @@ -1344,6 +1344,12 @@ sub checkuniquemember { ($dateofbirth) ? "SELECT borrowernumber,categorycode FROM borrowers WHERE surname=? and firstname=? and dateofbirth=?" : "SELECT borrowernumber,categorycode FROM borrowers WHERE surname=? and firstname=?"; + + if ( C4::Context->preference('IndependentBranches') ) { + my $branches = GetIndependentGroupModificationRights( { stringify => 1 } ); + $request .= " AND branchcode IN ( $branches )"; + } + my $sth = $dbh->prepare($request); if ($collectivity) { $sth->execute( uc($surname) ); @@ -1996,13 +2002,14 @@ sub GetBorrowersToExpunge { my $filterdate = $params->{'not_borrowered_since'}; my $filterexpiry = $params->{'expired_before'}; my $filtercategory = $params->{'category_code'}; - my $filterbranch = $params->{'branchcode'} || - ((C4::Context->preference('IndependentBranches') - && C4::Context->userenv - && !C4::Context->IsSuperLibrarian() - && C4::Context->userenv->{branch}) - ? C4::Context->userenv->{branch} - : ""); + my $filterbranch = $params->{'branchcode'}; + my @filterbranches = + ( C4::Context->preference('IndependentBranches') + && C4::Context->userenv + && !C4::Context->IsSuperLibrarian() + && C4::Context->userenv->{branch} ) + ? GetIndependentGroupModificationRights() + : ($filterbranch); my $dbh = C4::Context->dbh; my $query = " @@ -2017,9 +2024,10 @@ sub GetBorrowersToExpunge { AND borrowernumber NOT IN (SELECT guarantorid FROM borrowers WHERE guarantorid IS NOT NULL AND guarantorid <> 0) "; my @query_params; - if ( $filterbranch && $filterbranch ne "" ) { - $query.= " AND borrowers.branchcode = ? "; - push( @query_params, $filterbranch ); + if ( @filterbranches ) { + my $placeholders = join( ',', ('?') x @filterbranches ); + $query.= " AND borrowers.branchcode IN ( $placeholders )"; + push( @query_params, @filterbranches ); } if ( $filterexpiry ) { $query .= " AND dateexpiry < ? "; @@ -2062,13 +2070,16 @@ I<$result> is a ref to an array which all elements are a hasref. =cut sub GetBorrowersWhoHaveNeverBorrowed { - my $filterbranch = shift || - ((C4::Context->preference('IndependentBranches') - && C4::Context->userenv - && !C4::Context->IsSuperLibrarian() - && C4::Context->userenv->{branch}) - ? C4::Context->userenv->{branch} - : ""); + my $filterbranch = shift; + + my @filterbranches = + ( C4::Context->preference('IndependentBranches') + && C4::Context->userenv + && !C4::Context->IsSuperLibrarian() + && C4::Context->userenv->{branch} ) + ? GetIndependentGroupModificationRights() + : ($filterbranch); + my $dbh = C4::Context->dbh; my $query = " SELECT borrowers.borrowernumber,max(timestamp) as latestissue @@ -2076,10 +2087,12 @@ sub GetBorrowersWhoHaveNeverBorrowed { LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber WHERE issues.borrowernumber IS NULL "; + my @query_params; - if ($filterbranch && $filterbranch ne ""){ - $query.=" AND borrowers.branchcode= ?"; - push @query_params,$filterbranch; + if (@filterbranches) { + my $placeholders = join( ',', ('?') x @filterbranches ); + $query .= " AND borrowers.branchcode IN ( $placeholders ) "; + push( @query_params, @filterbranches ); } warn $query if $debug; @@ -2112,25 +2125,32 @@ This hashref is containt the number of time this borrowers has borrowed before I sub GetBorrowersWithIssuesHistoryOlderThan { my $dbh = C4::Context->dbh; my $date = shift ||POSIX::strftime("%Y-%m-%d",localtime()); - my $filterbranch = shift || - ((C4::Context->preference('IndependentBranches') - && C4::Context->userenv - && !C4::Context->IsSuperLibrarian() - && C4::Context->userenv->{branch}) - ? C4::Context->userenv->{branch} - : ""); + my $filterbranch = shift; + + my @filterbranches = + ( C4::Context->preference('IndependentBranches') + && C4::Context->userenv + && !C4::Context->IsSuperLibrarian() + && C4::Context->userenv->{branch} ) + ? GetIndependentGroupModificationRights() + : ($filterbranch); + my $query = " SELECT count(borrowernumber) as n,borrowernumber FROM old_issues WHERE returndate < ? AND borrowernumber IS NOT NULL "; + my @query_params; - push @query_params, $date; - if ($filterbranch){ - $query.=" AND branchcode = ?"; - push @query_params, $filterbranch; - } + push( @query_params, $date ); + + if (@filterbranches) { + my $placeholders = join( ',', ('?') x @filterbranches ); + $query .= " AND branchcode IN ( $placeholders ) "; + push( @query_params, @filterbranches ); + } + $query.=" GROUP BY borrowernumber "; warn $query if $debug; my $sth = $dbh->prepare($query); diff --git a/C4/Serials.pm b/C4/Serials.pm index 9657cbc..f98b095 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -31,6 +31,7 @@ use C4::Log; # logaction use C4::Debug; use C4::Serials::Frequency; use C4::Serials::Numberpattern; +use C4::Branch qw(GetIndependentGroupModificationRights); use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); @@ -231,7 +232,22 @@ sub GetSerialInformation { my ($serialid) = @_; my $dbh = C4::Context->dbh; my $query = qq| - SELECT serial.*, serial.notes as sernotes, serial.status as serstatus,subscription.*,subscription.subscriptionid as subsid + SELECT serial.*, + serial.notes as sernotes, + serial.status as serstatus, + subscription.*, + subscription.subscriptionid as subsid + |; + if ( C4::Context->preference('IndependentBranches') + && C4::Context->userenv + && C4::Context->userenv->{'flags'} % 2 != 1 + && C4::Context->userenv->{'branch'} ) { + my $branches = GetIndependentGroupModificationRights( { stringify => 1 } ); + $query .= qq| + , ( ( subscription.branchcode NOT IN ( $branches ) ) AND subscription.branchcode <> '' AND subscription.branchcode IS NOT NULL ) AS cannotedit + |; + } + $query .= qq| FROM serial LEFT JOIN subscription ON subscription.subscriptionid=serial.subscriptionid WHERE serialid = ? |; @@ -330,18 +346,35 @@ subscription, subscriptionhistory, aqbooksellers.name, biblio.title sub GetSubscription { my ($subscriptionid) = @_; my $dbh = C4::Context->dbh; - my $query = qq( + + my $query = qq| SELECT subscription.*, subscriptionhistory.*, aqbooksellers.name AS aqbooksellername, biblio.title AS bibliotitle, subscription.biblionumber as bibnum + |; + + if ( C4::Context->preference('IndependentBranches') + && C4::Context->userenv + && C4::Context->userenv->{'flags'} % 2 != 1 + && C4::Context->userenv->{'branch'} ) + { + my $branches = + GetIndependentGroupModificationRights( { stringify => 1 } ); + + $query .= qq| + , ( ( subscription.branchcode NOT IN ( $branches ) ) AND subscription.branchcode <> '' AND subscription.branchcode IS NOT NULL ) AS cannotedit + |; + } + + $query .= qq| FROM subscription LEFT JOIN subscriptionhistory ON subscription.subscriptionid=subscriptionhistory.subscriptionid LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id LEFT JOIN biblio ON biblio.biblionumber=subscription.biblionumber WHERE subscription.subscriptionid = ? - ); + |; $debug and warn "query : $query\nsubsid :$subscriptionid"; my $sth = $dbh->prepare($query); @@ -364,8 +397,10 @@ sub GetFullSubscription { return unless ($subscriptionid); my $dbh = C4::Context->dbh; - my $query = qq| - SELECT serial.serialid, + + my $query = qq| + SELECT + serial.serialid, serial.serialseq, serial.planneddate, serial.publisheddate, @@ -376,6 +411,22 @@ sub GetFullSubscription { biblio.title as bibliotitle, subscription.branchcode AS branchcode, subscription.subscriptionid AS subscriptionid + |; + + if ( C4::Context->preference('IndependentBranches') + && C4::Context->userenv + && C4::Context->userenv->{'flags'} % 2 != 1 + && C4::Context->userenv->{'branch'} ) + { + my $branches = + GetIndependentGroupModificationRights( { stringify => 1 } ); + + $query .= qq| + , ( ( subscription.branchcode NOT IN ( $branches ) ) AND subscription.branchcode <> '' AND subscription.branchcode IS NOT NULL ) AS cannotedit + |; + } + + $query .= qq| FROM serial LEFT JOIN subscription ON (serial.subscriptionid=subscription.subscriptionid ) @@ -496,6 +547,16 @@ sub GetSubscriptionsFromBiblionumber { $subs->{ "periodicity" . $subs->{periodicity} } = 1; $subs->{ "numberpattern" . $subs->{numberpattern} } = 1; $subs->{ "status" . $subs->{'status'} } = 1; + $subs->{'cannotedit'} = ( + C4::Context->preference('IndependentBranches') + && C4::Context->userenv + && !C4::Context->IsSuperLibrarian() + && C4::Context->userenv->{branch} + && $subs->{branchcode} + && GetIndependentGroupModificationRights( + { for => $subs->{branchcode} } + ) + ); if ( $subs->{enddate} eq '0000-00-00' ) { $subs->{enddate} = ''; @@ -520,8 +581,10 @@ sub GetSubscriptionsFromBiblionumber { sub GetFullSubscriptionsFromBiblionumber { my ($biblionumber) = @_; my $dbh = C4::Context->dbh; - my $query = qq| - SELECT serial.serialid, + + my $query = qq| + SELECT + serial.serialid, serial.serialseq, serial.planneddate, serial.publisheddate, @@ -531,6 +594,22 @@ sub GetFullSubscriptionsFromBiblionumber { biblio.title as bibliotitle, subscription.branchcode AS branchcode, subscription.subscriptionid AS subscriptionid + |; + + if ( C4::Context->preference('IndependentBranches') + && C4::Context->userenv + && C4::Context->userenv->{'flags'} != 1 + && C4::Context->userenv->{'branch'} ) + { + my $branches = + GetIndependentGroupModificationRights( { stringify => 1 } ); + + $query .= qq| + , ( ( subscription.branchcode NOT IN ( $branches ) ) AND subscription.branchcode <> '' AND subscription.branchcode IS NOT NULL ) AS cannotedit + |; + } + + $query .= qq| FROM serial LEFT JOIN subscription ON (serial.subscriptionid=subscription.subscriptionid) @@ -2820,7 +2899,9 @@ sub can_show_subscription { sub _can_do_on_subscription { my ( $subscription, $userid, $permission ) = @_; return 0 unless C4::Context->userenv; + my $flags = C4::Context->userenv->{flags}; + $userid ||= C4::Context->userenv->{'id'}; if ( C4::Context->preference('IndependentBranches') ) { @@ -2829,12 +2910,16 @@ sub _can_do_on_subscription { or C4::Auth::haspermission( $userid, { serials => 'superserials' } ) or ( - C4::Auth::haspermission( $userid, - { serials => $permission } ) + C4::Auth::haspermission( + $userid, { serials => $permission } + ) and ( not defined $subscription->{branchcode} or $subscription->{branchcode} eq '' or $subscription->{branchcode} eq C4::Context->userenv->{'branch'} ) + ) + or GetIndependentGroupModificationRights( + { for => $subscription->{branchcode} } ); } else { @@ -2842,10 +2927,8 @@ sub _can_do_on_subscription { if C4::Context->IsSuperLibrarian() or C4::Auth::haspermission( $userid, { serials => 'superserials' } ) - or C4::Auth::haspermission( - $userid, { serials => $permission } - ), - ; + or C4::Auth::haspermission( $userid, { serials => $permission } ) + ,; } return 0; } diff --git a/C4/Suggestions.pm b/C4/Suggestions.pm index b543293..acaaa5f 100644 --- a/C4/Suggestions.pm +++ b/C4/Suggestions.pm @@ -32,6 +32,7 @@ use C4::Letters; use List::MoreUtils qw(any); use C4::Dates qw(format_date_in_iso); use base qw(Exporter); +use C4::Branch qw(GetIndependentGroupModificationRights); our $VERSION = 3.07.00.049; our @EXPORT = qw( @@ -132,18 +133,15 @@ sub SearchSuggestion { } # filter on user branch - if ( C4::Context->preference('IndependentBranches') ) { - my $userenv = C4::Context->userenv; - if ($userenv) { - if ( !C4::Context->IsSuperLibrarian() && !$suggestion->{branchcode} ) - { - push @sql_params, $$userenv{branch}; - push @query, q{ - AND (suggestions.branchcode=? OR suggestions.branchcode='') - }; - } - } - } else { + if ( C4::Context->preference('IndependentBranches') + && !C4::Context->IsSuperLibrarian() + && !$suggestion->{branchcode} ) + { + my $branches = + GetIndependentGroupModificationRights( { stringify => 1 } ); + push( @query, qq{ AND (suggestions.branchcode IN ( $branches ) OR suggestions.branchcode='') } ); + } + else { if ( defined $suggestion->{branchcode} && $suggestion->{branchcode} ) { unless ( $suggestion->{branchcode} eq '__ANY__' ) { push @sql_params, $suggestion->{branchcode}; @@ -340,13 +338,19 @@ sub GetSuggestionByStatus { # filter on branch if ( C4::Context->preference("IndependentBranches") || $branchcode ) { - my $userenv = C4::Context->userenv; - if ($userenv) { - unless ( C4::Context->IsSuperLibrarian() ) { - push @sql_params, $userenv->{branch}; - $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') }; - } + if ( C4::Context->userenv + && C4::Context->preference("IndependentBranches") + && !C4::Context->IsSuperLibrarian() ) + { + + my $branches = + GetIndependentGroupModificationRights( { stringify => 1 } ); + + $query .= qq{ + AND (U1.branchcode IN ( $branches ) OR U1.branchcode ='') + }; } + if ($branchcode) { push @sql_params, $branchcode; $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') }; @@ -392,12 +396,19 @@ sub CountSuggestion { if ( C4::Context->preference("IndependentBranches") && !C4::Context->IsSuperLibrarian() ) { - my $query = q{ + my $branches = + GetIndependentGroupModificationRights( { stringify => 1 } ); + + my $query = qq{ SELECT count(*) FROM suggestions LEFT JOIN borrowers ON borrowers.borrowernumber=suggestions.suggestedby WHERE STATUS=? - AND (borrowers.branchcode='' OR borrowers.branchcode=?) + AND ( + borrowers.branchcode IN ( $branches ) + OR + borrowers.branchcode=? + ) }; $sth = $dbh->prepare($query); $sth->execute( $status, $userenv->{branch} ); diff --git a/acqui/basket.pl b/acqui/basket.pl index 4f2a37e..1b62b11 100755 --- a/acqui/basket.pl +++ b/acqui/basket.pl @@ -36,6 +36,7 @@ use C4::Members qw/GetMember/; #needed for permissions checking for changing ba use C4::Items; use C4::Suggestions; use Date::Calc qw/Add_Delta_Days/; +use C4::Branch qw/GetIndependentGroupModificationRights/; =head1 NAME @@ -155,10 +156,17 @@ if ( $op eq 'delete_confirm' ) { if ( C4::Context->preference("IndependentBranches") ) { my $userenv = C4::Context->userenv; unless ( C4::Context->IsSuperLibrarian() ) { - my $validtest = ( $basket->{creationdate} eq '' ) + my $validtest = + ( $basket->{creationdate} eq '' ) || ( $userenv->{branch} eq $basket->{branch} ) || ( $userenv->{branch} eq '' ) - || ( $basket->{branch} eq '' ); + || ( $basket->{branch} eq '' ) + || ( + GetIndependentGroupModificationRights( + { for => $basket->{branch} } + ) + ); + unless ($validtest) { print $query->redirect("../mainpage.pl"); exit 1; @@ -255,10 +263,16 @@ if ( $op eq 'delete_confirm' ) { if ( C4::Context->preference("IndependentBranches") ) { my $userenv = C4::Context->userenv; unless ( C4::Context->IsSuperLibrarian() ) { - my $validtest = ( $basket->{creationdate} eq '' ) + my $validtest = + ( $basket->{creationdate} eq '' ) || ( $userenv->{branch} eq $basket->{branch} ) || ( $userenv->{branch} eq '' ) - || ( $basket->{branch} eq '' ); + || ( $basket->{branch} eq '' ) + || ( + GetIndependentGroupModificationRights( + { for => $basket->{branch} } + ) + ); unless ($validtest) { print $query->redirect("../mainpage.pl"); exit 1; diff --git a/admin/branches.pl b/admin/branches.pl index 9e6b14a..29d7843 100755 --- a/admin/branches.pl +++ b/admin/branches.pl @@ -258,7 +258,7 @@ sub editbranchform { $oldprinter = $data->{'branchprinter'} || ''; _branch_to_template($data, $innertemplate); } - $innertemplate->param( categoryloop => $catinfo ); + $innertemplate->param( branch_categories => $catinfo ); foreach my $thisprinter ( keys %$printers ) { push @printerloop, { @@ -277,25 +277,8 @@ sub editbranchform { } sub editcatform { - - # prepares the edit form... - my ($categorycode,$innertemplate) = @_; - # warn "cat : $categorycode"; - my @cats; - my $data; - if ($categorycode) { - my $data = GetBranchCategory($categorycode); - $innertemplate->param( - categorycode => $data->{'categorycode'}, - categoryname => $data->{'categoryname'}, - codedescription => $data->{'codedescription'}, - show_in_pulldown => $data->{'show_in_pulldown'}, - ); - } - for my $ctype (GetCategoryTypes()) { - push @cats , { type => $ctype , selected => ($data->{'categorytype'} and $data->{'categorytype'} eq $ctype) }; - } - $innertemplate->param(categorytype => \@cats); + my ( $categorycode, $innertemplate ) = @_; + $innertemplate->param( category => GetBranchCategory($categorycode) ); } sub branchinfotable { @@ -366,25 +349,20 @@ sub branchinfotable { push @loop_data, \%row; } - my @branchcategories = (); - for my $ctype ( GetCategoryTypes() ) { - my $catinfo = GetBranchCategories($ctype); - my @categories; - foreach my $cat (@$catinfo) { - push @categories, { - categoryname => $cat->{'categoryname'}, - categorycode => $cat->{'categorycode'}, - codedescription => $cat->{'codedescription'}, - categorytype => $cat->{'categorytype'}, - }; - } - push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => ( @categories ? \@categories : undef) }; - } + + my $catinfo = GetBranchCategories(); + my $categories; + foreach my $cat (@$catinfo) { + $categories->{ $cat->{categorytype} }->{ $cat->{'categorycode'} } = { + categoryname => $cat->{'categoryname'}, + codedescription => $cat->{'codedescription'}, + }; + } + $innertemplate->param( - branches => \@loop_data, - branchcategories => \@branchcategories + branches => \@loop_data, + branch_categories => $categories ); - } sub _branch_to_template { diff --git a/catalogue/moredetail.pl b/catalogue/moredetail.pl index 86adb72..b820901 100755 --- a/catalogue/moredetail.pl +++ b/catalogue/moredetail.pl @@ -172,13 +172,18 @@ foreach my $item (@items){ $item->{status_advisory} = 1; } - if (C4::Context->preference("IndependentBranches")) { - #verifying rights - my $userenv = C4::Context->userenv(); - unless (C4::Context->IsSuperLibrarian() or ($userenv->{'branch'} eq $item->{'homebranch'})) { - $item->{'nomod'}=1; + if ( C4::Context->preference("IndependentBranches") ) { + unless ( + C4::Context->IsSuperLibrarian() + || GetIndependentGroupModificationRights( + { for => $item->{'homebranch'} } + ) + ) + { + $item->{'nomod'} = 1; } } + $item->{'homebranchname'} = GetBranchName($item->{'homebranch'}); $item->{'holdingbranchname'} = GetBranchName($item->{'holdingbranch'}); if ($item->{'datedue'}) { diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl index 268ef66..f5df12c 100755 --- a/cataloguing/additem.pl +++ b/cataloguing/additem.pl @@ -695,10 +695,19 @@ foreach my $field (@fields) { || $subfieldvalue; } - if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependentBranches")) { + if ( $field->tag eq $branchtagfield + && $subfieldcode eq $branchtagsubfield + && C4::Context->preference("IndependentBranches") ) + { #verifying rights my $userenv = C4::Context->userenv(); - unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $subfieldvalue))){ + unless ( + C4::Context->IsSuperLibrarian() + || GetIndependentGroupModificationRights( + { for => $subfieldvalue } + ) + ) + { $this_row{'nomod'} = 1; } } diff --git a/circ/circulation-home.pl b/circ/circulation-home.pl index d10829f..8b4996c 100755 --- a/circ/circulation-home.pl +++ b/circ/circulation-home.pl @@ -23,6 +23,7 @@ use C4::Auth; use C4::Output; use C4::Context; use C4::Koha; +use C4::Branch qw/GetIndependentGroupModificationRights/; my $query = new CGI; my ($template, $loggedinuser, $cookie, $flags) @@ -38,8 +39,10 @@ my $fa = getframeworkinfo('FA'); $template->param( fast_cataloging => 1 ) if (defined $fa); # Checking if the transfer page needs to be displayed -$template->param( display_transfer => 1 ) if ( ($flags->{'superlibrarian'} == 1) || (C4::Context->preference("IndependentBranches") == 0) ); -$template->{'VARS'}->{'AllowOfflineCirculation'} = C4::Context->preference('AllowOfflineCirculation'); +$template->param( display_transfer => 1 ) + if ( $flags->{'superlibrarian'} == 1 + || scalar GetIndependentGroupModificationRights() ); +$template->{'VARS'}->{'AllowOfflineCirculation'} = C4::Context->preference('AllowOfflineCirculation'); output_html_with_http_headers $query, $cookie, $template->output; diff --git a/circ/pendingreserves.pl b/circ/pendingreserves.pl index 7370819..5708155 100755 --- a/circ/pendingreserves.pl +++ b/circ/pendingreserves.pl @@ -36,6 +36,7 @@ use C4::Auth; use C4::Dates qw/format_date format_date_in_iso/; use C4::Debug; use Date::Calc qw/Today Add_Delta_YMD/; +use C4::Branch qw/GetIndependentGroupModificationRights/; my $input = new CGI; my $startdate=$input->param('from'); @@ -153,8 +154,8 @@ if ( $run_report ) { if (C4::Context->preference('IndependentBranches')){ - $strsth .= " AND items.holdingbranch=? "; - push @query_params, C4::Context->userenv->{'branch'}; + my $branches = GetIndependentGroupModificationRights( { stringify => 1 } ); + $strsth .= " AND items.holdingbranch IN ( $branches ) "; } $strsth .= " GROUP BY reserves.biblionumber ORDER BY biblio.title "; diff --git a/circ/reserveratios.pl b/circ/reserveratios.pl index 216fb0b..7d23db1 100755 --- a/circ/reserveratios.pl +++ b/circ/reserveratios.pl @@ -120,9 +120,9 @@ my $strsth = $sqldatewhere "; -if (C4::Context->preference('IndependentBranches')){ - $strsth .= " AND items.holdingbranch=? "; - push @query_params, C4::Context->userenv->{'branch'}; +if ( C4::Context->preference('IndependentBranches') ) { + my $branches = GetIndependentGroupModificationRights( { stringify => 1 } ); + $strsth .= " AND items.holdingbranch IN ( $branches ) "; } $strsth .= " GROUP BY reserves.biblionumber ORDER BY reservecount DESC"; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 92ab713..42561c8 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -365,7 +365,7 @@ CREATE TABLE `branchcategories` ( -- information related to library/branch group `categorycode` varchar(10) NOT NULL default '', -- unique identifier for the library/branch group `categoryname` varchar(32), -- name of the library/branch group `codedescription` mediumtext, -- longer description of the library/branch group - `categorytype` varchar(16), -- says whether this is a search group or a properties group + `categorytype` ENUM( 'searchdomain', 'independent_group' ) NULL DEFAULT NULL, -- says whether this is a search group or an independent group `show_in_pulldown` tinyint(1) NOT NULL DEFAULT '0', -- says this group should be in the opac libararies pulldown if it is enabled PRIMARY KEY (`categorycode`), KEY `show_in_pulldown` (`show_in_pulldown`) diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 421e772..1903ef6 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -6938,7 +6938,7 @@ $DBversion = "3.13.00.002"; if ( CheckVersion($DBversion) ) { $dbh->do("UPDATE systempreferences SET variable = 'IndependentBranches' WHERE variable = 'IndependantBranches'"); print "Upgrade to $DBversion done (Bug 10080 - Change system pref IndependantBranches to IndependentBranches)\n"; - SetVersion ($DBversion); + SetVersion ($DBversion); } $DBversion = '3.13.00.003'; @@ -8471,6 +8471,22 @@ if ( CheckVersion($DBversion) ) { SetVersion($DBversion); } +$DBversion = "3.17.00.XXX"; +if ( CheckVersion($DBversion) ) { + $dbh->do(q{ + DELETE FROM branchcategories WHERE categorytype = 'properties' + }); + + $dbh->do(q{ + ALTER TABLE branchcategories + CHANGE categorytype categorytype + ENUM( 'searchdomain', 'independent_group' ) + NULL DEFAULT NULL + }); + print "Upgrade to $DBversion done (Remove branch property groups, add independent groups)\n"; + SetVersion ($DBversion); +} + =head1 FUNCTIONS =head2 TableExists($table) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt index f12107f..25e28b7 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt @@ -100,20 +100,50 @@ tinyMCE.init({ - [% IF ( categoryloop ) %]
Group(s): -
    - [% FOREACH categoryloo IN categoryloop %] -
  1. - [% IF categoryloo.selected %] - - [% ELSE %] - - [% END %] - [% categoryloo.codedescription %] -
  2. - [% END %] -
-
[% END %] + + [% IF ( branch_categories ) %] +
+ Group(s): +
    +
    + Search domain + [% FOREACH bc IN branch_categories %] + [% IF bc.categorytype == "searchdomain" %] +
  1. + + [% IF ( bc.selected ) %] + + [% ELSE %] + + [% END %] + [% bc.codedescription %] +
  2. + [% END %] + [% END %] +
    +
+
    +
    + Independent library groups + [% FOREACH bc IN branch_categories %] + [% IF bc.categorytype == "independent_group" %] +
  1. + + [% IF ( bc.selected ) %] + + [% ELSE %] + + [% END %] + [% bc.codedescription %] +
  2. + [% END %] + [% END %] +
  3. If independent library groups are enabled ( via the IndependentBranches system preference ), a library may access and alter records and patrons from libraries in any group that also library belongs to. A library may belong to multiple library groups.
  4. +
    +
+
+ [% END %] +
  1. @@ -249,90 +279,122 @@ tinyMCE.init({ [% ELSE %]
    There are no libraries defined. Start defining libraries.
    [% END %] - - [% IF ( branchcategories ) %] - [% FOREACH branchcategorie IN branchcategories %] -

    Group(s): [% IF ( branchcategorie.properties ) %]Properties[% ELSE %][% IF ( branchcategorie.searchdomain ) %]Search domain[% END %][% END %]

    - [% IF ( branchcategorie.catloop ) %] - - - - - - - - - - - - [% FOREACH catloo IN branchcategorie.catloop %] - - - - - - - - [% END %] - -
    NameCodeDescription  
    [% catloo.categoryname %][% catloo.categorycode %][% catloo.codedescription %] - Edit - - Delete -
    + +

    Search domain groups

    + [% IF branch_categories.searchdomain %] + + + + + + + + + + + + [% FOREACH bc IN branch_categories.searchdomain %] + + + + + + + + [% END %] + +
    NameCodeDescription  
    [% bc.value.categoryname %][% bc.key %][% bc.value.codedescription %] + Edit + + Delete +
    + [% ELSE %] + No search domain groups defined. + [% END %] + Add a new group. + +

    Independent library groups:

    + [% IF branch_categories.independent_group %] + + + + + + + + + + + + [% FOREACH bc IN branch_categories.independent_group %] + + + + + + + + [% END %] + +
    NameCodeDescription  
    [% bc.value.categoryname %][% bc.key %][% bc.value.codedescription %] + Edit + + Delete +
    [% ELSE %] - No [% IF ( branchcategorie.properties ) %]properties[% ELSIF ( branchcategorie.searchdomain ) %]search domain[% END %] defined. Add a new group. + No independent library groups defined. [% END %] - [% END %] - [% ELSE %] -

    No groups defined.

    - [% END %] + Add a new group. [% END %] [% IF ( editcategory ) %] -

    [% IF ( categorycode ) %]Edit group [% categorycode %][% ELSE %]Add group[% END %]

    +

    [% IF ( category ) %]Edit group [% category.categorycode %][% ELSE %]Add group[% END %]

    - [% IF ( categorycode ) %] - - [% ELSE %] - - [% END %] + [% IF ( category.categorycode ) %] + + [% ELSE %] + + [% END %]
    1. - [% IF ( categorycode ) %] + [% IF ( category.categorycode ) %] Category code: - - [% categorycode %] + + [% category.categorycode %] [% ELSE %] - - + + [% END %]
    2. - +
    3. - +
    4. - [% IF ( show_in_pulldown ) %] + [% IF ( category.show_in_pulldown ) %] [% ELSE %] @@ -340,7 +402,13 @@ tinyMCE.init({
    -
    +
    + [% IF category %] + + [% ELSE %] + + [% END %] +
    [% END %] diff --git a/members/deletemem.pl b/members/deletemem.pl index a6143b1..d39f01e 100755 --- a/members/deletemem.pl +++ b/members/deletemem.pl @@ -66,11 +66,17 @@ if ($bor->{category_type} eq "S") { } } -if (C4::Context->preference("IndependentBranches")) { - my $userenv = C4::Context->userenv; +if ( C4::Context->preference("IndependentBranches") ) { if ( !C4::Context->IsSuperLibrarian() && $bor->{'branchcode'}){ - unless ($userenv->{branch} eq $bor->{'branchcode'}){ - print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY"); + unless ( + GetIndependentGroupModificationRights( + { for => $bor->{'branchcode'} } + ) + ) + { + print $input->redirect( + "/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY" + ); exit; } } diff --git a/members/member.pl b/members/member.pl index dfc7363..593753f 100755 --- a/members/member.pl +++ b/members/member.pl @@ -57,7 +57,7 @@ $input->delete( 'new_patron_list', 'borrowernumber', ); -my $patron = $input->Vars; +my $patron = { $input->Vars }; foreach (keys %$patron){ delete $$patron{$_} unless($$patron{$_}); } diff --git a/t/db_dependent/Branch.t b/t/db_dependent/Branch.t index 338ffe4..799a315 100644 --- a/t/db_dependent/Branch.t +++ b/t/db_dependent/Branch.t @@ -21,7 +21,7 @@ use Modern::Perl; use C4::Context; use Data::Dumper; -use Test::More tests => 36; +use Test::More tests => 69; use C4::Branch; @@ -172,14 +172,14 @@ my $cat1 = { categorycode => 'CAT1', categoryname => 'catname1', codedescription => 'catdesc1', - categorytype => 'cattype1', + categorytype => 'searchdomain', show_in_pulldown => 1 }; my $cat2 = { add => 1, categorycode => 'CAT2', categoryname => 'catname2', - categorytype => 'catype2', + categorytype => 'searchdomain', codedescription => 'catdesc2', show_in_pulldown => 1 }; @@ -188,7 +188,7 @@ my %new_category = ( categorycode => 'LIBCATCODE', categoryname => 'library category name', codedescription => 'library category code description', - categorytype => 'searchdomain', + categorytype => 'independent_group', show_in_pulldown => 1, ); @@ -335,7 +335,7 @@ is( CheckCategoryUnique('CAT_NO_EXISTS'), 1, 'CAT_NO_EXISTS doesnt exist' ); #Test GetCategoryTypes my @category_types = GetCategoryTypes(); -is_deeply(\@category_types, [ 'searchdomain', 'properties' ], 'received expected library category types'); +is_deeply(\@category_types, [ 'searchdomain', 'independent_groups' ], 'received expected library category types'); $categories = GetBranchCategories(undef, undef, 'LIBCATCODE'); is_deeply($categories, [ {%$cat1}, {%$cat2},{ %new_category, selected => 1 } ], 'retrieve expected, eselected library category (bug 10515)'); @@ -347,6 +347,72 @@ is_deeply($categories, [ {%$cat1}, {%$cat2},{ %new_category, selected => 1 } ], my $loop = GetBranchesLoop; is( scalar(@$loop), GetBranchesCount(), 'There is the right number of branches' ); +# Test GetIndependentGroupModificationRights +my @branches_bra = GetIndependentGroupModificationRights({ branch => 'BRA' }); +is_deeply( \@branches_bra, [ 'BRA' ], 'Library with no group only has rights for its own branch' ); + +my $string = GetIndependentGroupModificationRights({ branch => 'BRA', stringify => 1 }); +ok( $string eq q{'BRA'}, "String returns correctly" ); + +ok( GetIndependentGroupModificationRights({ branch => 'BRA', for => 'BRA' }), 'Boolean test for BRA rights to BRA returns true' ); +ok( !GetIndependentGroupModificationRights({ branch => 'BRA', for => 'BRB' }), 'Boolean test for BRA rights to BRB returns false' ); +ok( !GetIndependentGroupModificationRights({ branch => 'BRA', for => 'BRC' }), 'Boolean test for BRA rights to BRC returns false' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRB', for => 'BRB' }), 'Boolean test for BRB rights to BRB returns true' ); +ok( !GetIndependentGroupModificationRights({ branch => 'BRB', for => 'BRA' }), 'Boolean test for BRB rights to BRA returns false' ); +ok( !GetIndependentGroupModificationRights({ branch => 'BRB', for => 'BRC' }), 'Boolean test for BRB rights to BRC returns false' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRC', for => 'BRC' }), 'Boolean test for BRC rights to BRC returns true' ); +ok( !GetIndependentGroupModificationRights({ branch => 'BRC', for => 'BRA'}), 'Boolean test for BRC rights to BRA returns false' ); +ok( !GetIndependentGroupModificationRights({ branch => 'BRC', for => 'BRB'}), 'Boolean test for BRC rights to BRB returns false' ); + +ModBranch({ + branchcode => 'BRA', + branchname => 'BranchA', + LIBCATCODE => 1, +}); +ModBranch({ + branchcode => 'BRB', + branchname => 'BranchB', + LIBCATCODE => 1, +}); + +@branches_bra = GetIndependentGroupModificationRights({ branch => 'BRA' }); +is_deeply( \@branches_bra, [ 'BRA', 'BRB' ], 'Libraries in LIBCATCODE returned correctly' ); + +$string = GetIndependentGroupModificationRights({ branch => 'BRA', stringify => 1 }); +ok( $string eq q{'BRA','BRB'}, "String returns correctly" ); + +ok( GetIndependentGroupModificationRights({ branch => 'BRA', for => 'BRA' }), 'Boolean test for BRA rights to BRA returns true' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRA', for => 'BRB'}), 'Boolean test for BRA rights to BRB returns true' ); +ok( !GetIndependentGroupModificationRights({ branch => 'BRA', for => 'BRC'}), 'Boolean test for BRA rights to BRC returns false' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRB', for => 'BRB' }), 'Boolean test for BRB rights to BRB returns true' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRB', for => 'BRA'}), 'Boolean test for BRB rights to BRA returns true' ); +ok( !GetIndependentGroupModificationRights({ branch => 'BRB', for => 'BRC'}), 'Boolean test for BRB rights to BRC returns false' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRC', for => 'BRC' }), 'Boolean test for BRC rights to BRC returns true' ); +ok( !GetIndependentGroupModificationRights({ branch => 'BRC', for => 'BRA'}), 'Boolean test for BRC rights to BRA returns false' ); +ok( !GetIndependentGroupModificationRights({ branch => 'BRC', for => 'BRB'}), 'Boolean test for BRC rights to BRB returns false' ); + +ModBranch({ + branchcode => 'BRC', + branchname => 'BranchC', + LIBCATCODE => 1, +}); + +@branches_bra = GetIndependentGroupModificationRights({ branch => 'BRA' }); +is_deeply( \@branches_bra, [ 'BRA', 'BRB', 'BRC' ], 'Library with no group only has rights for its own branch' ); + +ok( GetIndependentGroupModificationRights({ branch => 'BRA', for => 'BRA' }), 'Boolean test for BRA rights to BRA returns true' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRA', for => 'BRB'}), 'Boolean test for BRA rights to BRB returns true' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRA', for => 'BRC'}), 'Boolean test for BRA rights to BRC returns true' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRB', for => 'BRB' }), 'Boolean test for BRB rights to BRB returns true' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRB', for => 'BRA'}), 'Boolean test for BRB rights to BRA returns true' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRB', for => 'BRC'}), 'Boolean test for BRB rights to BRC returns true' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRC', for => 'BRC' }), 'Boolean test for BRC rights to BRC returns true' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRC', for => 'BRA'}), 'Boolean test for BRC rights to BRA returns true' ); +ok( GetIndependentGroupModificationRights({ branch => 'BRC', for => 'BRA'}), 'Boolean test for BRC rights to BRB returns true' ); + +$string = GetIndependentGroupModificationRights({ branch => 'BRA', stringify => 1 }); +ok( $string eq q{'BRA','BRB','BRC'}, "String returns correctly" ); + # End transaction $dbh->rollback; -- 1.7.2.5