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>.
20 #use warnings; FIXME - Bug 2505
23 use Koha
::LibraryCategories
;
25 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28 # set the version for version checking
29 $VERSION = 3.07.00.049;
40 &GetBranchesInCategory
43 @EXPORT_OK = qw( &onlymine &mybranch );
48 C4::Branch - Koha branch module
56 The functions in this module deal with branches.
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.
71 my $branches = GetBranches;
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;
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>
90 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
95 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
102 # returns a reference to a hash of references to ALL branches...
104 my $dbh = C4
::Context
->dbh;
106 my $query = "SELECT * FROM branches";
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);
117 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
118 $relations_sth->execute();
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 );
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"
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);
152 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname
}) cmp uc($branches->{$b}->{branchname
}) } keys %$branches ) {
154 value
=> $branchcode,
155 branchcode
=> $branchcode,
156 selected
=> ($branchcode eq $branch) ?
1 : 0,
157 branchname
=> $branches->{$branchcode}->{branchname
},
168 my ($branchcode) = @_;
169 my $dbh = C4
::Context
->dbh;
171 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
172 $sth->execute($branchcode);
173 my $branchname = $sth->fetchrow_array;
174 return ($branchname);
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.
190 my $dbh = C4
::Context
->dbh;
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);
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;
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=?
226 my $sth = $dbh->prepare($query);
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....
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'};
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) {
270 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
272 $sth->execute( $branchcode, $cat );
274 foreach my $cat (@removecats) {
277 "delete from branchrelations where branchcode=? and categorycode=?"
279 $sth->execute( $branchcode, $cat );
285 $branch = GetBranch( $query, $branches );
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] );
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.
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) .
322 sub GetBranchesInCategory
{
323 my ($categorycode) = @_;
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 );
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.
346 my ($branchcode,$categorytype) = @_;
347 my $dbh = C4
::Context
->dbh;
354 "Select * from branches where branchcode = ? order by branchcode");
355 $sth->execute($branchcode);
358 $sth = $dbh->prepare("Select * from branches order by branchcode");
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=? ";
368 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
369 push @bind, $categorytype;
371 my $nsth=$dbh->prepare($query);
372 $nsth->execute( @bind );
374 while ( my ($cat) = $nsth->fetchrow_array ) {
377 $data->{'categories'} = \
@cats;
378 push( @results, $data );
388 Koha Development Team <http://koha-community.org/>