Bug 15572: Follow-up to fix error on authority creation
[koha.git] / C4 / Branch.pm
blob7e8b1871c890bc90b2b0d7e3ed4db087ee4b85e3
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 &GetBranchDetail
37 &get_branchinfos_of
38 &ModBranch
39 &GetBranchInfo
40 &GetBranchesInCategory
41 &mybranch
43 @EXPORT_OK = qw( &onlymine &mybranch );
46 =head1 NAME
48 C4::Branch - Koha branch module
50 =head1 SYNOPSIS
52 use C4::Branch;
54 =head1 DESCRIPTION
56 The functions in this module deal with branches.
58 =head1 FUNCTIONS
60 =head2 GetBranches
62 $branches = &GetBranches();
64 Returns informations about ALL branches, IndependentBranches Insensitive.
65 GetBranchInfo() returns the same information.
67 Create a branch selector with the following code.
69 =head3 in PERL SCRIPT
71 my $branches = GetBranches;
72 my @branchloop;
73 foreach my $thisbranch (sort keys %$branches) {
74 my $selected = 1 if $thisbranch eq $branch;
75 my %row =(value => $thisbranch,
76 selected => $selected,
77 branchname => $branches->{$thisbranch}->{branchname},
79 push @branchloop, \%row;
82 =head3 in TEMPLATE
84 <select name="branch" id="branch">
85 <option value=""></option>
86 [% FOREACH branchloo IN branchloop %]
87 [% IF ( branchloo.selected ) %]
88 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
89 [% ELSE %]
90 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
91 [% END %]
92 [% END %]
93 </select>
95 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
97 =cut
99 sub GetBranches {
100 my ($onlymine) = @_;
102 # returns a reference to a hash of references to ALL branches...
103 my %branches;
104 my $dbh = C4::Context->dbh;
105 my $sth;
106 my $query = "SELECT * FROM branches";
107 my @bind_parameters;
108 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
109 $query .= ' WHERE branchcode = ? ';
110 push @bind_parameters, C4::Context->userenv->{branch};
112 $query .= " ORDER BY branchname";
113 $sth = $dbh->prepare($query);
114 $sth->execute(@bind_parameters);
116 my $relations_sth =
117 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
118 $relations_sth->execute();
119 my %relations;
120 while ( my $rel = $relations_sth->fetchrow_hashref ) {
121 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
124 while ( my $branch = $sth->fetchrow_hashref ) {
125 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
126 $branch->{category}{$cat} = 1;
128 $branches{ $branch->{'branchcode'} } = $branch;
130 return ( \%branches );
133 sub onlymine {
134 return
135 C4::Context->preference('IndependentBranches')
136 && C4::Context->userenv
137 && !C4::Context->IsSuperLibrarian()
138 && C4::Context->userenv->{branch};
141 # always returns a string for OK comparison via "eq" or "ne"
142 sub mybranch {
143 C4::Context->userenv or return '';
144 return C4::Context->userenv->{branch} || '';
147 sub GetBranchesLoop { # since this is what most pages want anyway
148 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
149 my $onlymine = @_ ? shift : onlymine();
150 my $branches = GetBranches($onlymine);
151 my @loop;
152 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
153 push @loop, {
154 value => $branchcode,
155 branchcode => $branchcode,
156 selected => ($branchcode eq $branch) ? 1 : 0,
157 branchname => $branches->{$branchcode}->{branchname},
160 return \@loop;
163 =head2 GetBranchName
165 =cut
167 sub GetBranchName {
168 my ($branchcode) = @_;
169 my $dbh = C4::Context->dbh;
170 my $sth;
171 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
172 $sth->execute($branchcode);
173 my $branchname = $sth->fetchrow_array;
174 return ($branchname);
177 =head2 ModBranch
179 $error = &ModBranch($newvalue);
181 This function modifies an existing branch
183 C<$newvalue> is a ref to an array which contains all the columns from branches table.
185 =cut
187 sub ModBranch {
188 my ($data) = @_;
190 my $dbh = C4::Context->dbh;
191 if ($data->{add}) {
192 my $query = "
193 INSERT INTO branches
194 (branchcode,branchname,branchaddress1,
195 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
196 branchcountry,branchphone,branchfax,branchemail,
197 branchurl,branchip,branchprinter,branchnotes,opac_info,
198 branchreplyto, branchreturnpath)
199 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
201 my $sth = $dbh->prepare($query);
202 $sth->execute(
203 $data->{'branchcode'}, $data->{'branchname'},
204 $data->{'branchaddress1'}, $data->{'branchaddress2'},
205 $data->{'branchaddress3'}, $data->{'branchzip'},
206 $data->{'branchcity'}, $data->{'branchstate'},
207 $data->{'branchcountry'},
208 $data->{'branchphone'}, $data->{'branchfax'},
209 $data->{'branchemail'}, $data->{'branchurl'},
210 $data->{'branchip'}, $data->{'branchprinter'},
211 $data->{'branchnotes'}, $data->{opac_info},
212 $data->{'branchreplyto'}, $data->{'branchreturnpath'}
214 return 1 if $dbh->err;
215 } else {
216 my $query = "
217 UPDATE branches
218 SET branchname=?,branchaddress1=?,
219 branchaddress2=?,branchaddress3=?,branchzip=?,
220 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
221 branchfax=?,branchemail=?,branchurl=?,branchip=?,
222 branchprinter=?,branchnotes=?,opac_info=?,
223 branchreplyto=?, branchreturnpath=?
224 WHERE branchcode=?
226 my $sth = $dbh->prepare($query);
227 $sth->execute(
228 $data->{'branchname'},
229 $data->{'branchaddress1'}, $data->{'branchaddress2'},
230 $data->{'branchaddress3'}, $data->{'branchzip'},
231 $data->{'branchcity'}, $data->{'branchstate'},
232 $data->{'branchcountry'},
233 $data->{'branchphone'}, $data->{'branchfax'},
234 $data->{'branchemail'}, $data->{'branchurl'},
235 $data->{'branchip'}, $data->{'branchprinter'},
236 $data->{'branchnotes'}, $data->{opac_info},
237 $data->{'branchreplyto'}, $data->{'branchreturnpath'},
238 $data->{'branchcode'},
241 # sort out the categories....
242 my @checkedcats;
243 my @cats = Koha::LibraryCategories->search;
244 foreach my $cat (@cats) {
245 my $code = $cat->categorycode;
246 if ( $data->{$code} ) {
247 push( @checkedcats, $code );
250 my $branchcode = uc( $data->{'branchcode'} );
251 my $branch = GetBranchInfo($branchcode);
252 $branch = $branch->[0];
253 my $branchcats = $branch->{'categories'};
254 my @addcats;
255 my @removecats;
256 foreach my $bcat (@$branchcats) {
258 unless ( grep { /^$bcat$/ } @checkedcats ) {
259 push( @removecats, $bcat );
262 foreach my $ccat (@checkedcats) {
263 unless ( grep { /^$ccat$/ } @$branchcats ) {
264 push( @addcats, $ccat );
267 foreach my $cat (@addcats) {
268 my $sth =
269 $dbh->prepare(
270 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
272 $sth->execute( $branchcode, $cat );
274 foreach my $cat (@removecats) {
275 my $sth =
276 $dbh->prepare(
277 "delete from branchrelations where branchcode=? and categorycode=?"
279 $sth->execute( $branchcode, $cat );
283 =head2 GetBranch
285 $branch = GetBranch( $query, $branches );
287 =cut
289 sub GetBranch {
290 my ( $query, $branches ) = @_; # get branch for this query from branches
291 my $branch = $query->param('branch');
292 my %cookie = $query->cookie('userenv');
293 ($branch) || ($branch = $cookie{'branchname'});
294 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
295 return $branch;
298 =head2 GetBranchDetail
300 $branch = &GetBranchDetail($branchcode);
302 Given the branch code, the function returns a
303 hashref for the corresponding row in the branches table.
305 =cut
307 sub GetBranchDetail {
308 my ($branchcode) = shift or return;
309 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
310 $sth->execute($branchcode);
311 return $sth->fetchrow_hashref();
314 =head2 GetBranchesInCategory
316 my $branches = GetBranchesInCategory($categorycode);
318 Returns a href: keys %$branches eq (branchcode,branchname) .
320 =cut
322 sub GetBranchesInCategory {
323 my ($categorycode) = @_;
324 my @branches;
325 my $dbh = C4::Context->dbh();
326 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
327 where r.branchcode=b.branchcode and r.categorycode=?");
328 $sth->execute($categorycode);
329 while (my $branch = $sth->fetchrow) {
330 push @branches, $branch;
332 return( \@branches );
335 =head2 GetBranchInfo
337 $results = GetBranchInfo($branchcode);
339 returns C<$results>, a reference to an array of hashes containing branches.
340 if $branchcode, just this branch, with associated categories.
341 if ! $branchcode && $categorytype, all branches in the category.
343 =cut
345 sub GetBranchInfo {
346 my ($branchcode,$categorytype) = @_;
347 my $dbh = C4::Context->dbh;
348 my $sth;
351 if ($branchcode) {
352 $sth =
353 $dbh->prepare(
354 "Select * from branches where branchcode = ? order by branchcode");
355 $sth->execute($branchcode);
357 else {
358 $sth = $dbh->prepare("Select * from branches order by branchcode");
359 $sth->execute();
361 my @results;
362 while ( my $data = $sth->fetchrow_hashref ) {
363 my @bind = ($data->{'branchcode'});
364 my $query= "select r.categorycode from branchrelations r";
365 $query .= ", branchcategories c " if($categorytype);
366 $query .= " where branchcode=? ";
367 if($categorytype) {
368 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
369 push @bind, $categorytype;
371 my $nsth=$dbh->prepare($query);
372 $nsth->execute( @bind );
373 my @cats = ();
374 while ( my ($cat) = $nsth->fetchrow_array ) {
375 push( @cats, $cat );
377 $data->{'categories'} = \@cats;
378 push( @results, $data );
380 return \@results;
384 __END__
386 =head1 AUTHOR
388 Koha Development Team <http://koha-community.org/>
390 =cut