Bug 16045 - Use Font Awesome icons in OAI sets administration
[koha.git] / C4 / Branch.pm
blob9f8263826f893e59a8e46ba60dcb103f2be5e681
1 package C4::Branch;
3 # This file is part of Koha.
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.
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.
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>.
19 use strict;
20 #use warnings; FIXME - Bug 2505
21 require Exporter;
22 use C4::Context;
23 use Koha::LibraryCategories;
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
27 BEGIN {
28 # set the version for version checking
29 $VERSION = 3.07.00.049;
30 @ISA = qw(Exporter);
31 @EXPORT = qw(
32 &GetBranchName
33 &GetBranch
34 &GetBranches
35 &GetBranchesLoop
36 &mybranch
38 @EXPORT_OK = qw( &onlymine &mybranch );
41 =head1 NAME
43 C4::Branch - Koha branch module
45 =head1 SYNOPSIS
47 use C4::Branch;
49 =head1 DESCRIPTION
51 The functions in this module deal with branches.
53 =head1 FUNCTIONS
55 =head2 GetBranches
57 $branches = &GetBranches();
59 Returns informations about ALL branches, IndependentBranches Insensitive.
61 Create a branch selector with the following code.
63 =head3 in PERL SCRIPT
65 my $branches = GetBranches;
66 my @branchloop;
67 foreach my $thisbranch (sort keys %$branches) {
68 my $selected = 1 if $thisbranch eq $branch;
69 my %row =(value => $thisbranch,
70 selected => $selected,
71 branchname => $branches->{$thisbranch}->{branchname},
73 push @branchloop, \%row;
76 =head3 in TEMPLATE
78 <select name="branch" id="branch">
79 <option value=""></option>
80 [% FOREACH branchloo IN branchloop %]
81 [% IF ( branchloo.selected ) %]
82 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
83 [% ELSE %]
84 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
85 [% END %]
86 [% END %]
87 </select>
89 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
91 =cut
93 sub GetBranches {
94 my ($onlymine) = @_;
96 # returns a reference to a hash of references to ALL branches...
97 my %branches;
98 my $dbh = C4::Context->dbh;
99 my $sth;
100 my $query = "SELECT * FROM branches";
101 my @bind_parameters;
102 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
103 $query .= ' WHERE branchcode = ? ';
104 push @bind_parameters, C4::Context->userenv->{branch};
106 $query .= " ORDER BY branchname";
107 $sth = $dbh->prepare($query);
108 $sth->execute(@bind_parameters);
110 my $relations_sth =
111 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
112 $relations_sth->execute();
113 my %relations;
114 while ( my $rel = $relations_sth->fetchrow_hashref ) {
115 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
118 while ( my $branch = $sth->fetchrow_hashref ) {
119 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
120 $branch->{category}{$cat} = 1;
122 $branches{ $branch->{'branchcode'} } = $branch;
124 return ( \%branches );
127 sub onlymine {
128 return
129 C4::Context->preference('IndependentBranches')
130 && C4::Context->userenv
131 && !C4::Context->IsSuperLibrarian()
132 && C4::Context->userenv->{branch};
135 # always returns a string for OK comparison via "eq" or "ne"
136 sub mybranch {
137 C4::Context->userenv or return '';
138 return C4::Context->userenv->{branch} || '';
141 sub GetBranchesLoop { # since this is what most pages want anyway
142 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
143 my $onlymine = @_ ? shift : onlymine();
144 my $branches = GetBranches($onlymine);
145 my @loop;
146 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
147 push @loop, {
148 value => $branchcode,
149 branchcode => $branchcode,
150 selected => ($branchcode eq $branch) ? 1 : 0,
151 branchname => $branches->{$branchcode}->{branchname},
154 return \@loop;
157 =head2 GetBranchName
159 =cut
161 sub GetBranchName {
162 my ($branchcode) = @_;
163 my $dbh = C4::Context->dbh;
164 my $sth;
165 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
166 $sth->execute($branchcode);
167 my $branchname = $sth->fetchrow_array;
168 return ($branchname);
171 =head2 GetBranch
173 $branch = GetBranch( $query, $branches );
175 =cut
177 sub GetBranch {
178 my ( $query, $branches ) = @_; # get branch for this query from branches
179 my $branch = $query->param('branch');
180 my %cookie = $query->cookie('userenv');
181 ($branch) || ($branch = $cookie{'branchname'});
182 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
183 return $branch;
187 __END__
189 =head1 AUTHOR
191 Koha Development Team <http://koha-community.org/>
193 =cut