Bug 14168 - enhance streaming cataloging to include youtube
[koha.git] / 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($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
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
37         );
38     @EXPORT_OK = qw( &onlymine &mybranch );
39 }
40
41 =head1 NAME
42
43 C4::Branch - Koha branch module
44
45 =head1 SYNOPSIS
46
47 use C4::Branch;
48
49 =head1 DESCRIPTION
50
51 The functions in this module deal with branches.
52
53 =head1 FUNCTIONS
54
55 =head2 GetBranches
56
57   $branches = &GetBranches();
58
59 Returns informations about ALL branches, IndependentBranches Insensitive.
60
61 Create a branch selector with the following code.
62
63 =head3 in PERL SCRIPT
64
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},
72                 );
73         push @branchloop, \%row;
74     }
75
76 =head3 in TEMPLATE
77
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>
88
89 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
90
91 =cut
92
93 sub GetBranches {
94     my ($onlymine) = @_;
95
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};
105     }
106     $query .= " ORDER BY branchname";
107     $sth = $dbh->prepare($query);
108     $sth->execute(@bind_parameters);
109
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};
116     }
117
118     while ( my $branch = $sth->fetchrow_hashref ) {
119         foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
120             $branch->{category}{$cat} = 1;
121         }
122         $branches{ $branch->{'branchcode'} } = $branch;
123     }
124     return ( \%branches );
125 }
126
127 sub onlymine {
128     return
129          C4::Context->preference('IndependentBranches')
130       && C4::Context->userenv
131       && !C4::Context->IsSuperLibrarian()
132       && C4::Context->userenv->{branch};
133 }
134
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} || '';
139 }
140
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},
152         };
153     }
154     return \@loop;
155 }
156
157 =head2 GetBranchName
158
159 =cut
160
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);
169 }
170
171 =head2 GetBranch
172
173 $branch = GetBranch( $query, $branches );
174
175 =cut
176
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;
184 }
185
186 1;
187 __END__
188
189 =head1 AUTHOR
190
191 Koha Development Team <http://koha-community.org/>
192
193 =cut