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;
41 &GetBranchesInCategory
42 &ModBranchCategoryInfo
46 @EXPORT_OK = qw( &onlymine &mybranch );
51 C4::Branch - Koha branch module
59 The functions in this module deal with branches.
65 $branches = &GetBranches();
67 Returns informations about ALL branches, IndependentBranches Insensitive.
68 GetBranchInfo() returns the same information.
70 Create a branch selector with the following code.
74 my $branches = GetBranches;
76 foreach my $thisbranch (sort keys %$branches) {
77 my $selected = 1 if $thisbranch eq $branch;
78 my %row =(value => $thisbranch,
79 selected => $selected,
80 branchname => $branches->{$thisbranch}->{branchname},
82 push @branchloop, \%row;
87 <select name="branch" id="branch">
88 <option value=""></option>
89 [% FOREACH branchloo IN branchloop %]
90 [% IF ( branchloo.selected ) %]
91 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
93 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
98 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
105 # returns a reference to a hash of references to ALL branches...
107 my $dbh = C4::Context->dbh;
109 my $query = "SELECT * FROM branches";
111 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
112 $query .= ' WHERE branchcode = ? ';
113 push @bind_parameters, C4::Context->userenv->{branch};
115 $query .= " ORDER BY branchname";
116 $sth = $dbh->prepare($query);
117 $sth->execute(@bind_parameters);
120 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
121 $relations_sth->execute();
123 while ( my $rel = $relations_sth->fetchrow_hashref ) {
124 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
127 while ( my $branch = $sth->fetchrow_hashref ) {
128 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
129 $branch->{category}{$cat} = 1;
131 $branches{ $branch->{'branchcode'} } = $branch;
133 return ( \%branches );
138 C4::Context->preference('IndependentBranches')
139 && C4::Context->userenv
140 && !C4::Context->IsSuperLibrarian()
141 && C4::Context->userenv->{branch};
144 # always returns a string for OK comparison via "eq" or "ne"
146 C4::Context->userenv or return '';
147 return C4::Context->userenv->{branch} || '';
150 sub GetBranchesLoop { # since this is what most pages want anyway
151 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
152 my $onlymine = @_ ? shift : onlymine();
153 my $branches = GetBranches($onlymine);
155 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
157 value => $branchcode,
158 branchcode => $branchcode,
159 selected => ($branchcode eq $branch) ? 1 : 0,
160 branchname => $branches->{$branchcode}->{branchname},
171 my ($branchcode) = @_;
172 my $dbh = C4::Context->dbh;
174 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
175 $sth->execute($branchcode);
176 my $branchname = $sth->fetchrow_array;
177 return ($branchname);
182 $error = &ModBranch($newvalue);
184 This function modifies an existing branch
186 C<$newvalue> is a ref to an array which contains all the columns from branches table.
193 my $dbh = C4::Context->dbh;
197 (branchcode,branchname,branchaddress1,
198 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
199 branchcountry,branchphone,branchfax,branchemail,
200 branchurl,branchip,branchprinter,branchnotes,opac_info,
201 branchreplyto, branchreturnpath)
202 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
204 my $sth = $dbh->prepare($query);
206 $data->{'branchcode'}, $data->{'branchname'},
207 $data->{'branchaddress1'}, $data->{'branchaddress2'},
208 $data->{'branchaddress3'}, $data->{'branchzip'},
209 $data->{'branchcity'}, $data->{'branchstate'},
210 $data->{'branchcountry'},
211 $data->{'branchphone'}, $data->{'branchfax'},
212 $data->{'branchemail'}, $data->{'branchurl'},
213 $data->{'branchip'}, $data->{'branchprinter'},
214 $data->{'branchnotes'}, $data->{opac_info},
215 $data->{'branchreplyto'}, $data->{'branchreturnpath'}
217 return 1 if $dbh->err;
221 SET branchname=?,branchaddress1=?,
222 branchaddress2=?,branchaddress3=?,branchzip=?,
223 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
224 branchfax=?,branchemail=?,branchurl=?,branchip=?,
225 branchprinter=?,branchnotes=?,opac_info=?,
226 branchreplyto=?, branchreturnpath=?
229 my $sth = $dbh->prepare($query);
231 $data->{'branchname'},
232 $data->{'branchaddress1'}, $data->{'branchaddress2'},
233 $data->{'branchaddress3'}, $data->{'branchzip'},
234 $data->{'branchcity'}, $data->{'branchstate'},
235 $data->{'branchcountry'},
236 $data->{'branchphone'}, $data->{'branchfax'},
237 $data->{'branchemail'}, $data->{'branchurl'},
238 $data->{'branchip'}, $data->{'branchprinter'},
239 $data->{'branchnotes'}, $data->{opac_info},
240 $data->{'branchreplyto'}, $data->{'branchreturnpath'},
241 $data->{'branchcode'},
244 # sort out the categories....
246 my @cats = Koha::LibraryCategories->search;
247 foreach my $cat (@cats) {
248 my $code = $cat->categorycode;
249 if ( $data->{$code} ) {
250 push( @checkedcats, $code );
253 my $branchcode = uc( $data->{'branchcode'} );
254 my $branch = GetBranchInfo($branchcode);
255 $branch = $branch->[0];
256 my $branchcats = $branch->{'categories'};
259 foreach my $bcat (@$branchcats) {
261 unless ( grep { /^$bcat$/ } @checkedcats ) {
262 push( @removecats, $bcat );
265 foreach my $ccat (@checkedcats) {
266 unless ( grep { /^$ccat$/ } @$branchcats ) {
267 push( @addcats, $ccat );
270 foreach my $cat (@addcats) {
273 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
275 $sth->execute( $branchcode, $cat );
277 foreach my $cat (@removecats) {
280 "delete from branchrelations where branchcode=? and categorycode=?"
282 $sth->execute( $branchcode, $cat );
286 =head2 GetCategoryTypes
288 $categorytypes = GetCategoryTypes;
289 returns a list of category types.
290 Currently these types are HARDCODED.
291 type: 'searchdomain' defines a group of agencies that the calling library may search in.
292 Other usage of agency categories falls under type: 'properties'.
293 to allow for other uses of categories.
294 The searchdomain bit may be better implemented as a separate module, but
295 the categories were already here, and minimally used.
299 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
300 sub GetCategoryTypes {
301 return ( 'searchdomain','properties');
306 $branch = GetBranch( $query, $branches );
311 my ( $query, $branches ) = @_; # get branch for this query from branches
312 my $branch = $query->param('branch');
313 my %cookie = $query->cookie('userenv');
314 ($branch) || ($branch = $cookie{'branchname'});
315 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
319 =head2 GetBranchDetail
321 $branch = &GetBranchDetail($branchcode);
323 Given the branch code, the function returns a
324 hashref for the corresponding row in the branches table.
328 sub GetBranchDetail {
329 my ($branchcode) = shift or return;
330 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
331 $sth->execute($branchcode);
332 return $sth->fetchrow_hashref();
335 =head2 GetBranchesInCategory
337 my $branches = GetBranchesInCategory($categorycode);
339 Returns a href: keys %$branches eq (branchcode,branchname) .
343 sub GetBranchesInCategory {
344 my ($categorycode) = @_;
346 my $dbh = C4::Context->dbh();
347 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
348 where r.branchcode=b.branchcode and r.categorycode=?");
349 $sth->execute($categorycode);
350 while (my $branch = $sth->fetchrow) {
351 push @branches, $branch;
353 return( \@branches );
358 $results = GetBranchInfo($branchcode);
360 returns C<$results>, a reference to an array of hashes containing branches.
361 if $branchcode, just this branch, with associated categories.
362 if ! $branchcode && $categorytype, all branches in the category.
367 my ($branchcode,$categorytype) = @_;
368 my $dbh = C4::Context->dbh;
375 "Select * from branches where branchcode = ? order by branchcode");
376 $sth->execute($branchcode);
379 $sth = $dbh->prepare("Select * from branches order by branchcode");
383 while ( my $data = $sth->fetchrow_hashref ) {
384 my @bind = ($data->{'branchcode'});
385 my $query= "select r.categorycode from branchrelations r";
386 $query .= ", branchcategories c " if($categorytype);
387 $query .= " where branchcode=? ";
389 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
390 push @bind, $categorytype;
392 my $nsth=$dbh->prepare($query);
393 $nsth->execute( @bind );
395 while ( my ($cat) = $nsth->fetchrow_array ) {
398 $data->{'categories'} = \@cats;
399 push( @results, $data );
404 =head2 ModBranchCategoryInfo
406 &ModBranchCategoryInfo($data);
407 sets the data from the editbranch form, and writes to the database...
411 sub ModBranchCategoryInfo {
413 my $dbh = C4::Context->dbh;
415 # we are doing an insert
416 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)");
417 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} );
421 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?");
422 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) );
425 sub GetBranchesCount {
426 my $dbh = C4::Context->dbh();
427 my $query = "SELECT COUNT(*) AS branches_count FROM branches";
428 my $sth = $dbh->prepare( $query );
430 my $row = $sth->fetchrow_hashref();
431 return $row->{'branches_count'};
439 Koha Development Team <http://koha-community.org/>