3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
27 # set the version for version checking
28 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v).".".join( "_", map { sprintf "%03d", $_ } @v ); };
32 C4::Branch - Koha branch module
40 The functions in this module deal with branches.
55 &CheckBranchCategorycode
57 &ModBranchCategoryInfo
64 $branches = &GetBranches();
65 returns informations about ALL branches.
66 Create a branch selector with the following code
67 IndependantBranches Insensitive...
71 my $branches = GetBranches;
73 foreach my $thisbranch (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">
85 <option value="">Default</option>
86 <!-- TMPL_LOOP name="branchloop" -->
87 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
96 # returns a reference to a hash of references to ALL branches...
98 my $dbh = C4::Context->dbh;
100 my $query="SELECT * from branches";
101 if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
102 $query .= " WHERE branchcode =".$dbh->quote(C4::Context->userenv->{branch});
104 $query.=" order by branchname";
105 $sth = $dbh->prepare($query);
107 while ( my $branch = $sth->fetchrow_hashref ) {
110 "select categorycode from branchrelations where branchcode = ?");
111 $nsth->execute( $branch->{'branchcode'} );
112 while ( my ($cat) = $nsth->fetchrow_array ) {
114 # FIXME - This seems wrong. It ought to be
115 # $branch->{categorycodes}{$cat} = 1;
116 # otherwise, there's a namespace collision if there's a
117 # category with the same name as a field in the 'branches'
118 # table (i.e., don't create a category called "issuing").
119 # In addition, the current structure doesn't really allow
120 # you to list the categories that a branch belongs to:
121 # you'd have to list keys %$branch, and remove those keys
122 # that aren't fields in the "branches" table.
125 $branches{ $branch->{'branchcode'} } = $branch;
127 return ( \%branches );
135 my ($branchcode) = @_;
136 my $dbh = C4::Context->dbh;
138 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
139 $sth->execute($branchcode);
140 my $branchname = $sth->fetchrow_array;
142 return ($branchname);
147 &ModBranch($newvalue);
149 This function modify an existing branches.
151 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
158 my $dbh = C4::Context->dbh;
162 (branchcode,branchname,branchaddress1,
163 branchaddress2,branchaddress3,branchphone,
164 branchfax,branchemail,branchip,branchprinter)
165 VALUES (?,?,?,?,?,?,?,?,?,?)
167 my $sth = $dbh->prepare($query);
169 $data->{'branchcode'}, $data->{'branchname'},
170 $data->{'branchaddress1'}, $data->{'branchaddress2'},
171 $data->{'branchaddress3'}, $data->{'branchphone'},
172 $data->{'branchfax'}, $data->{'branchemail'},
173 $data->{'branchip'}, $data->{'branchprinter'},
178 SET branchname=?,branchaddress1=?,
179 branchaddress2=?,branchaddress3=?,branchphone=?,
180 branchfax=?,branchemail=?,branchip=?,branchprinter=?
183 my $sth = $dbh->prepare($query);
185 $data->{'branchname'},
186 $data->{'branchaddress1'}, $data->{'branchaddress2'},
187 $data->{'branchaddress3'}, $data->{'branchphone'},
188 $data->{'branchfax'}, $data->{'branchemail'},
189 $data->{'branchip'}, $data->{'branchprinter'},
190 $data->{'branchcode'},
193 # sort out the categories....
195 my $cats = GetBranchCategory();
196 foreach my $cat (@$cats) {
197 my $code = $cat->{'categorycode'};
198 if ( $data->{$code} ) {
199 push( @checkedcats, $code );
202 my $branchcode = uc( $data->{'branchcode'} );
203 my $branch = GetBranchInfo($branchcode);
204 $branch = $branch->[0];
205 my $branchcats = $branch->{'categories'};
208 foreach my $bcat (@$branchcats) {
210 unless ( grep { /^$bcat$/ } @checkedcats ) {
211 push( @removecats, $bcat );
214 foreach my $ccat (@checkedcats) {
215 unless ( grep { /^$ccat$/ } @$branchcats ) {
216 push( @addcats, $ccat );
219 foreach my $cat (@addcats) {
222 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
224 $sth->execute( $branchcode, $cat );
227 foreach my $cat (@removecats) {
230 "delete from branchrelations where branchcode=? and categorycode=?"
232 $sth->execute( $branchcode, $cat );
237 =head2 GetBranchCategory
239 $results = GetBranchCategory($categorycode);
241 C<$results> is an ref to an array.
245 sub GetBranchCategory {
247 # returns a reference to an array of hashes containing branches,
249 my $dbh = C4::Context->dbh;
252 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
256 "select * from branchcategories where categorycode = ?");
257 $sth->execute($catcode);
260 $sth = $dbh->prepare("Select * from branchcategories");
264 while ( my $data = $sth->fetchrow_hashref ) {
265 push( @results, $data );
269 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
275 $branch = GetBranch( $query, $branches );
280 my ( $query, $branches ) = @_; # get branch for this query from branches
281 my $branch = $query->param('branch');
282 my %cookie = $query->cookie('userenv');
283 ($branch) || ($branch = $cookie{'branchname'});
284 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
288 =head2 GetBranchDetail
290 $branchname = &GetBranchDetail($branchcode);
292 Given the branch code, the function returns the corresponding
293 branch name for a comprehensive information display
297 sub GetBranchDetail {
298 my ($branchcode) = @_;
299 my $dbh = C4::Context->dbh;
300 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
301 $sth->execute($branchcode);
302 my $branchname = $sth->fetchrow_hashref();
308 =head2 get_branchinfos_of
310 my $branchinfos_of = get_branchinfos_of(@branchcodes);
312 Associates a list of branchcodes to the information of the branch, taken in
315 Returns a href where keys are branchcodes and values are href where keys are
316 branch information key.
318 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
322 sub get_branchinfos_of {
323 my @branchcodes = @_;
329 WHERE branchcode IN ('
330 . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
332 return C4::Koha::get_infos_of( $query, 'branchcode' );
337 $results = GetBranchInfo($branchcode);
339 returns C<$results>, a reference to an array of hashes containing branches.
344 my ($branchcode) = @_;
345 my $dbh = C4::Context->dbh;
350 "Select * from branches where branchcode = ? order by branchcode");
351 $sth->execute($branchcode);
354 $sth = $dbh->prepare("Select * from branches order by branchcode");
358 while ( my $data = $sth->fetchrow_hashref ) {
361 "select categorycode from branchrelations where branchcode = ?");
362 $nsth->execute( $data->{'branchcode'} );
364 while ( my ($cat) = $nsth->fetchrow_array ) {
368 $data->{'categories'} = \@cats;
369 push( @results, $data );
377 &DelBranch($branchcode);
382 my ($branchcode) = @_;
383 my $dbh = C4::Context->dbh;
384 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
385 $sth->execute($branchcode);
389 =head2 ModBranchCategoryInfo
391 &ModBranchCategoryInfo($data);
392 sets the data from the editbranch form, and writes to the database...
396 sub ModBranchCategoryInfo {
399 my $dbh = C4::Context->dbh;
400 my $sth = $dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
401 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'} );
405 =head2 DeleteBranchCategory
407 DeleteBranchCategory($categorycode);
411 sub DelBranchCategory {
412 my ($categorycode) = @_;
413 my $dbh = C4::Context->dbh;
414 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
415 $sth->execute($categorycode);
419 =head2 CheckBranchCategorycode
421 $number_rows_affected = CheckBranchCategorycode($categorycode);
425 sub CheckBranchCategorycode {
427 # check to see if the branchcode is being used in the database somewhere....
428 my ($categorycode) = @_;
429 my $dbh = C4::Context->dbh;
432 "select count(*) from branchrelations where categorycode=?");
433 $sth->execute($categorycode);
434 my ($total) = $sth->fetchrow_array;
442 Koha Developement team <info@koha.org>