7f1b713e76242abdc789214d2bd0b1dc9e7669cb
[srvgit] / C4 / Branch.pm
1 package C4::Branch;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
19 use strict;
20 #use warnings; FIXME - Bug 2505
21 require Exporter;
22 use C4::Context;
23 use Koha::LibraryCategories;
24
25 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
27 BEGIN {
28         @ISA    = qw(Exporter);
29         @EXPORT = qw(
30                 &GetBranch
31                 &GetBranches
32         );
33     @EXPORT_OK = qw( &onlymine );
34 }
35
36 =head1 NAME
37
38 C4::Branch - Koha branch module
39
40 =head1 SYNOPSIS
41
42 use C4::Branch;
43
44 =head1 DESCRIPTION
45
46 The functions in this module deal with branches.
47
48 =head1 FUNCTIONS
49
50 =head2 GetBranches
51
52   $branches = &GetBranches();
53
54 Returns informations about ALL branches, IndependentBranches Insensitive.
55
56 Create a branch selector with the following code.
57
58 =head3 in PERL SCRIPT
59
60     my $branches = GetBranches;
61     my @branchloop;
62     foreach my $thisbranch (sort keys %$branches) {
63         my $selected = 1 if $thisbranch eq $branch;
64         my %row =(value => $thisbranch,
65                     selected => $selected,
66                     branchname => $branches->{$thisbranch}->{branchname},
67                 );
68         push @branchloop, \%row;
69     }
70
71 =head3 in TEMPLATE
72
73     <select name="branch" id="branch">
74         <option value=""></option>
75             [% FOREACH branchloo IN branchloop %]
76                 [% IF ( branchloo.selected ) %]
77                     <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
78                 [% ELSE %]
79                     <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
80                 [% END %]
81             [% END %]
82     </select>
83
84 =cut
85
86 sub GetBranches {
87     my ($onlymine) = @_;
88
89     # returns a reference to a hash of references to ALL branches...
90     my %branches;
91     my $dbh = C4::Context->dbh;
92     my $sth;
93     my $query = "SELECT * FROM branches";
94     my @bind_parameters;
95     if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
96         $query .= ' WHERE branchcode = ? ';
97         push @bind_parameters, C4::Context->userenv->{branch};
98     }
99     $query .= " ORDER BY branchname";
100     $sth = $dbh->prepare($query);
101     $sth->execute(@bind_parameters);
102
103     my $relations_sth =
104       $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
105     $relations_sth->execute();
106     my %relations;
107     while ( my $rel = $relations_sth->fetchrow_hashref ) {
108         push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
109     }
110
111     while ( my $branch = $sth->fetchrow_hashref ) {
112         foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
113             $branch->{category}{$cat} = 1;
114         }
115         $branches{ $branch->{'branchcode'} } = $branch;
116     }
117     return ( \%branches );
118 }
119
120 sub onlymine {
121     return
122          C4::Context->preference('IndependentBranches')
123       && C4::Context->userenv
124       && !C4::Context->IsSuperLibrarian()
125       && C4::Context->userenv->{branch};
126 }
127
128 =head2 GetBranch
129
130 $branch = GetBranch( $query, $branches );
131
132 =cut
133
134 sub GetBranch {
135     my ( $query, $branches ) = @_;    # get branch for this query from branches
136     my $branch = $query->param('branch');
137     my %cookie = $query->cookie('userenv');
138     ($branch)                || ($branch = $cookie{'branchname'});
139     ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
140     return $branch;
141 }
142
143 1;
144 __END__
145
146 =head1 AUTHOR
147
148 Koha Development Team <http://koha-community.org/>
149
150 =cut