Bug 7683: Relabel "acquired date" and "removed date"
[koha.git] / C4 / Branch.pm
blobae2216a8c74ff7cc46b513b205a9970a66324eb1
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 &ModBranch
38 &GetBranchInfo
39 &GetBranchesInCategory
40 &mybranch
42 @EXPORT_OK = qw( &onlymine &mybranch );
45 =head1 NAME
47 C4::Branch - Koha branch module
49 =head1 SYNOPSIS
51 use C4::Branch;
53 =head1 DESCRIPTION
55 The functions in this module deal with branches.
57 =head1 FUNCTIONS
59 =head2 GetBranches
61 $branches = &GetBranches();
63 Returns informations about ALL branches, IndependentBranches Insensitive.
64 GetBranchInfo() returns the same information.
66 Create a branch selector with the following code.
68 =head3 in PERL SCRIPT
70 my $branches = GetBranches;
71 my @branchloop;
72 foreach my $thisbranch (sort 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;
81 =head3 in TEMPLATE
83 <select name="branch" id="branch">
84 <option value=""></option>
85 [% FOREACH branchloo IN branchloop %]
86 [% IF ( branchloo.selected ) %]
87 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
88 [% ELSE %]
89 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
90 [% END %]
91 [% END %]
92 </select>
94 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
96 =cut
98 sub GetBranches {
99 my ($onlymine) = @_;
101 # returns a reference to a hash of references to ALL branches...
102 my %branches;
103 my $dbh = C4::Context->dbh;
104 my $sth;
105 my $query = "SELECT * FROM branches";
106 my @bind_parameters;
107 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
108 $query .= ' WHERE branchcode = ? ';
109 push @bind_parameters, C4::Context->userenv->{branch};
111 $query .= " ORDER BY branchname";
112 $sth = $dbh->prepare($query);
113 $sth->execute(@bind_parameters);
115 my $relations_sth =
116 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
117 $relations_sth->execute();
118 my %relations;
119 while ( my $rel = $relations_sth->fetchrow_hashref ) {
120 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
123 while ( my $branch = $sth->fetchrow_hashref ) {
124 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
125 $branch->{category}{$cat} = 1;
127 $branches{ $branch->{'branchcode'} } = $branch;
129 return ( \%branches );
132 sub onlymine {
133 return
134 C4::Context->preference('IndependentBranches')
135 && C4::Context->userenv
136 && !C4::Context->IsSuperLibrarian()
137 && C4::Context->userenv->{branch};
140 # always returns a string for OK comparison via "eq" or "ne"
141 sub mybranch {
142 C4::Context->userenv or return '';
143 return C4::Context->userenv->{branch} || '';
146 sub GetBranchesLoop { # since this is what most pages want anyway
147 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
148 my $onlymine = @_ ? shift : onlymine();
149 my $branches = GetBranches($onlymine);
150 my @loop;
151 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
152 push @loop, {
153 value => $branchcode,
154 branchcode => $branchcode,
155 selected => ($branchcode eq $branch) ? 1 : 0,
156 branchname => $branches->{$branchcode}->{branchname},
159 return \@loop;
162 =head2 GetBranchName
164 =cut
166 sub GetBranchName {
167 my ($branchcode) = @_;
168 my $dbh = C4::Context->dbh;
169 my $sth;
170 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
171 $sth->execute($branchcode);
172 my $branchname = $sth->fetchrow_array;
173 return ($branchname);
176 =head2 ModBranch
178 $error = &ModBranch($newvalue);
180 This function modifies an existing branch
182 C<$newvalue> is a ref to an array which contains all the columns from branches table.
184 =cut
186 sub ModBranch {
187 my ($data) = @_;
189 my $dbh = C4::Context->dbh;
190 if ($data->{add}) {
191 my $query = "
192 INSERT INTO branches
193 (branchcode,branchname,branchaddress1,
194 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
195 branchcountry,branchphone,branchfax,branchemail,
196 branchurl,branchip,branchprinter,branchnotes,opac_info,
197 branchreplyto, branchreturnpath)
198 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
200 my $sth = $dbh->prepare($query);
201 $sth->execute(
202 $data->{'branchcode'}, $data->{'branchname'},
203 $data->{'branchaddress1'}, $data->{'branchaddress2'},
204 $data->{'branchaddress3'}, $data->{'branchzip'},
205 $data->{'branchcity'}, $data->{'branchstate'},
206 $data->{'branchcountry'},
207 $data->{'branchphone'}, $data->{'branchfax'},
208 $data->{'branchemail'}, $data->{'branchurl'},
209 $data->{'branchip'}, $data->{'branchprinter'},
210 $data->{'branchnotes'}, $data->{opac_info},
211 $data->{'branchreplyto'}, $data->{'branchreturnpath'}
213 return 1 if $dbh->err;
214 } else {
215 my $query = "
216 UPDATE branches
217 SET branchname=?,branchaddress1=?,
218 branchaddress2=?,branchaddress3=?,branchzip=?,
219 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
220 branchfax=?,branchemail=?,branchurl=?,branchip=?,
221 branchprinter=?,branchnotes=?,opac_info=?,
222 branchreplyto=?, branchreturnpath=?
223 WHERE branchcode=?
225 my $sth = $dbh->prepare($query);
226 $sth->execute(
227 $data->{'branchname'},
228 $data->{'branchaddress1'}, $data->{'branchaddress2'},
229 $data->{'branchaddress3'}, $data->{'branchzip'},
230 $data->{'branchcity'}, $data->{'branchstate'},
231 $data->{'branchcountry'},
232 $data->{'branchphone'}, $data->{'branchfax'},
233 $data->{'branchemail'}, $data->{'branchurl'},
234 $data->{'branchip'}, $data->{'branchprinter'},
235 $data->{'branchnotes'}, $data->{opac_info},
236 $data->{'branchreplyto'}, $data->{'branchreturnpath'},
237 $data->{'branchcode'},
240 # sort out the categories....
241 my @checkedcats;
242 my @cats = Koha::LibraryCategories->search;
243 foreach my $cat (@cats) {
244 my $code = $cat->categorycode;
245 if ( $data->{$code} ) {
246 push( @checkedcats, $code );
249 my $branchcode = uc( $data->{'branchcode'} );
250 my $branch = GetBranchInfo($branchcode);
251 $branch = $branch->[0];
252 my $branchcats = $branch->{'categories'};
253 my @addcats;
254 my @removecats;
255 foreach my $bcat (@$branchcats) {
257 unless ( grep { /^$bcat$/ } @checkedcats ) {
258 push( @removecats, $bcat );
261 foreach my $ccat (@checkedcats) {
262 unless ( grep { /^$ccat$/ } @$branchcats ) {
263 push( @addcats, $ccat );
266 foreach my $cat (@addcats) {
267 my $sth =
268 $dbh->prepare(
269 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
271 $sth->execute( $branchcode, $cat );
273 foreach my $cat (@removecats) {
274 my $sth =
275 $dbh->prepare(
276 "delete from branchrelations where branchcode=? and categorycode=?"
278 $sth->execute( $branchcode, $cat );
282 =head2 GetBranch
284 $branch = GetBranch( $query, $branches );
286 =cut
288 sub GetBranch {
289 my ( $query, $branches ) = @_; # get branch for this query from branches
290 my $branch = $query->param('branch');
291 my %cookie = $query->cookie('userenv');
292 ($branch) || ($branch = $cookie{'branchname'});
293 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
294 return $branch;
297 =head2 GetBranchDetail
299 $branch = &GetBranchDetail($branchcode);
301 Given the branch code, the function returns a
302 hashref for the corresponding row in the branches table.
304 =cut
306 sub GetBranchDetail {
307 my ($branchcode) = shift or return;
308 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
309 $sth->execute($branchcode);
310 return $sth->fetchrow_hashref();
313 =head2 GetBranchesInCategory
315 my $branches = GetBranchesInCategory($categorycode);
317 Returns a href: keys %$branches eq (branchcode,branchname) .
319 =cut
321 sub GetBranchesInCategory {
322 my ($categorycode) = @_;
323 my @branches;
324 my $dbh = C4::Context->dbh();
325 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
326 where r.branchcode=b.branchcode and r.categorycode=?");
327 $sth->execute($categorycode);
328 while (my $branch = $sth->fetchrow) {
329 push @branches, $branch;
331 return( \@branches );
334 =head2 GetBranchInfo
336 $results = GetBranchInfo($branchcode);
338 returns C<$results>, a reference to an array of hashes containing branches.
339 if $branchcode, just this branch, with associated categories.
340 if ! $branchcode && $categorytype, all branches in the category.
342 =cut
344 sub GetBranchInfo {
345 my ($branchcode,$categorytype) = @_;
346 my $dbh = C4::Context->dbh;
347 my $sth;
350 if ($branchcode) {
351 $sth =
352 $dbh->prepare(
353 "Select * from branches where branchcode = ? order by branchcode");
354 $sth->execute($branchcode);
356 else {
357 $sth = $dbh->prepare("Select * from branches order by branchcode");
358 $sth->execute();
360 my @results;
361 while ( my $data = $sth->fetchrow_hashref ) {
362 my @bind = ($data->{'branchcode'});
363 my $query= "select r.categorycode from branchrelations r";
364 $query .= ", branchcategories c " if($categorytype);
365 $query .= " where branchcode=? ";
366 if($categorytype) {
367 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
368 push @bind, $categorytype;
370 my $nsth=$dbh->prepare($query);
371 $nsth->execute( @bind );
372 my @cats = ();
373 while ( my ($cat) = $nsth->fetchrow_array ) {
374 push( @cats, $cat );
376 $data->{'categories'} = \@cats;
377 push( @results, $data );
379 return \@results;
383 __END__
385 =head1 AUTHOR
387 Koha Development Team <http://koha-community.org/>
389 =cut