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
24 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26 # set the version for version checking
31 C4::Branch - Koha branch module
39 The functions in this module deal with branches.
54 &CheckBranchCategorycode
56 &ModBranchCategoryInfo
63 $branches = &GetBranches();
64 returns informations about ALL branches.
65 Create a branch selector with the following code
66 IndependantBranches Insensitive...
70 my $branches = GetBranches;
72 foreach my $thisbranch (keys %$branches) {
73 my $selected = 1 if $thisbranch eq $branch;
74 my %row =(value => $thisbranch,
75 selected => $selected,
76 branchname => $branches->{$thisbranch}->{'branchname'},
78 push @branchloop, \%row;
83 <select name="branch">
84 <option value="">Default</option>
85 <!-- TMPL_LOOP name="branchloop" -->
86 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
94 # returns a reference to a hash of references to ALL branches...
96 my $dbh = C4::Context->dbh;
98 my $query="SELECT * from branches";
99 if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
100 $query .= " WHERE branchcode =".$dbh->quote(C4::Context->userenv->{branch});
102 $query.=" order by branchname";
103 $sth = $dbh->prepare($query);
105 while ( my $branch = $sth->fetchrow_hashref ) {
108 "select categorycode from branchrelations where branchcode = ?");
109 $nsth->execute( $branch->{'branchcode'} );
110 while ( my ($cat) = $nsth->fetchrow_array ) {
112 # FIXME - This seems wrong. It ought to be
113 # $branch->{categorycodes}{$cat} = 1;
114 # otherwise, there's a namespace collision if there's a
115 # category with the same name as a field in the 'branches'
116 # table (i.e., don't create a category called "issuing").
117 # In addition, the current structure doesn't really allow
118 # you to list the categories that a branch belongs to:
119 # you'd have to list keys %$branch, and remove those keys
120 # that aren't fields in the "branches" table.
123 $branches{ $branch->{'branchcode'} } = $branch;
125 return ( \%branches );
133 my ($branchcode) = @_;
134 my $dbh = C4::Context->dbh;
136 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
137 $sth->execute($branchcode);
138 my $branchname = $sth->fetchrow_array;
140 return ($branchname);
145 &ModBranch($newvalue);
147 This function modify an existing branches.
149 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
156 my $dbh = C4::Context->dbh;
160 (branchcode,branchname,branchaddress1,
161 branchaddress2,branchaddress3,branchphone,
162 branchfax,branchemail,branchip,branchprinter)
163 VALUES (?,?,?,?,?,?,?,?,?,?)
165 my $sth = $dbh->prepare($query);
167 $data->{'branchcode'}, $data->{'branchname'},
168 $data->{'branchaddress1'}, $data->{'branchaddress2'},
169 $data->{'branchaddress3'}, $data->{'branchphone'},
170 $data->{'branchfax'}, $data->{'branchemail'},
171 $data->{'branchip'}, $data->{'branchprinter'},
176 SET branchname=?,branchaddress1=?,
177 branchaddress2=?,branchaddress3=?,branchphone=?,
178 branchfax=?,branchemail=?,branchip=?,branchprinter=?
181 my $sth = $dbh->prepare($query);
183 $data->{'branchname'},
184 $data->{'branchaddress1'}, $data->{'branchaddress2'},
185 $data->{'branchaddress3'}, $data->{'branchphone'},
186 $data->{'branchfax'}, $data->{'branchemail'},
187 $data->{'branchip'}, $data->{'branchprinter'},
188 $data->{'branchcode'},
191 # sort out the categories....
193 my $cats = GetBranchCategory();
194 foreach my $cat (@$cats) {
195 my $code = $cat->{'categorycode'};
196 if ( $data->{$code} ) {
197 push( @checkedcats, $code );
200 my $branchcode = uc( $data->{'branchcode'} );
201 my $branch = GetBranchInfo($branchcode);
202 $branch = $branch->[0];
203 my $branchcats = $branch->{'categories'};
206 foreach my $bcat (@$branchcats) {
208 unless ( grep { /^$bcat$/ } @checkedcats ) {
209 push( @removecats, $bcat );
212 foreach my $ccat (@checkedcats) {
213 unless ( grep { /^$ccat$/ } @$branchcats ) {
214 push( @addcats, $ccat );
217 foreach my $cat (@addcats) {
220 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
222 $sth->execute( $branchcode, $cat );
225 foreach my $cat (@removecats) {
228 "delete from branchrelations where branchcode=? and categorycode=?"
230 $sth->execute( $branchcode, $cat );
235 =head2 GetBranchCategory
237 $results = GetBranchCategory($categorycode);
239 C<$results> is an ref to an array.
243 sub GetBranchCategory {
245 # returns a reference to an array of hashes containing branches,
247 my $dbh = C4::Context->dbh;
250 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
254 "select * from branchcategories where categorycode = ?");
255 $sth->execute($catcode);
258 $sth = $dbh->prepare("Select * from branchcategories");
262 while ( my $data = $sth->fetchrow_hashref ) {
263 push( @results, $data );
267 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
273 $branch = GetBranch( $query, $branches );
278 my ( $query, $branches ) = @_; # get branch for this query from branches
279 my $branch = $query->param('branch');
280 my %cookie = $query->cookie('userenv');
281 ($branch) || ($branch = $cookie{'branchname'});
282 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
286 =head2 GetBranchDetail
288 $branchname = &GetBranchDetail($branchcode);
290 Given the branch code, the function returns the corresponding
291 branch name for a comprehensive information display
295 sub GetBranchDetail {
296 my ($branchcode) = @_;
297 my $dbh = C4::Context->dbh;
298 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
299 $sth->execute($branchcode);
300 my $branchname = $sth->fetchrow_hashref();
306 =head2 get_branchinfos_of
308 my $branchinfos_of = get_branchinfos_of(@branchcodes);
310 Associates a list of branchcodes to the information of the branch, taken in
313 Returns a href where keys are branchcodes and values are href where keys are
314 branch information key.
316 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
320 sub get_branchinfos_of {
321 my @branchcodes = @_;
327 WHERE branchcode IN ('
328 . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
330 return C4::Koha::get_infos_of( $query, 'branchcode' );
335 $results = GetBranchInfo($branchcode);
337 returns C<$results>, a reference to an array of hashes containing branches.
342 my ($branchcode) = @_;
343 my $dbh = C4::Context->dbh;
348 "Select * from branches where branchcode = ? order by branchcode");
349 $sth->execute($branchcode);
352 $sth = $dbh->prepare("Select * from branches order by branchcode");
356 while ( my $data = $sth->fetchrow_hashref ) {
359 "select categorycode from branchrelations where branchcode = ?");
360 $nsth->execute( $data->{'branchcode'} );
362 while ( my ($cat) = $nsth->fetchrow_array ) {
366 $data->{'categories'} = \@cats;
367 push( @results, $data );
375 &DelBranch($branchcode);
380 my ($branchcode) = @_;
381 my $dbh = C4::Context->dbh;
382 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
383 $sth->execute($branchcode);
387 =head2 ModBranchCategoryInfo
389 &ModBranchCategoryInfo($data);
390 sets the data from the editbranch form, and writes to the database...
394 sub ModBranchCategoryInfo {
397 my $dbh = C4::Context->dbh;
398 my $sth = $dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
399 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'} );
403 =head2 DeleteBranchCategory
405 DeleteBranchCategory($categorycode);
409 sub DelBranchCategory {
410 my ($categorycode) = @_;
411 my $dbh = C4::Context->dbh;
412 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
413 $sth->execute($categorycode);
417 =head2 CheckBranchCategorycode
419 $number_rows_affected = CheckBranchCategorycode($categorycode);
423 sub CheckBranchCategorycode {
425 # check to see if the branchcode is being used in the database somewhere....
426 my ($categorycode) = @_;
427 my $dbh = C4::Context->dbh;
430 "select count(*) from branchrelations where categorycode=?");
431 $sth->execute($categorycode);
432 my ($total) = $sth->fetchrow_array;
440 Koha Developement team <info@koha.org>