Bug 15295: Koha::Libraries - Remove GetBranchCategory
[koha.git] / C4 / Branch.pm
1 package C4::Branch;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18
19 use strict;
20 #use warnings; FIXME - Bug 2505
21 require Exporter;
22 use C4::Context;
23
24 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
25
26 BEGIN {
27         # set the version for version checking
28     $VERSION = 3.07.00.049;
29         @ISA    = qw(Exporter);
30         @EXPORT = qw(
31                 &GetBranchName
32                 &GetBranch
33                 &GetBranches
34                 &GetBranchesLoop
35                 &GetBranchDetail
36                 &get_branchinfos_of
37                 &ModBranch
38                 &GetBranchInfo
39                 &GetCategoryTypes
40                 &GetBranchCategories
41                 &GetBranchesInCategory
42                 &ModBranchCategoryInfo
43                 &mybranch
44                 &GetBranchesCount
45         );
46     @EXPORT_OK = qw( &onlymine &mybranch );
47 }
48
49 =head1 NAME
50
51 C4::Branch - Koha branch module
52
53 =head1 SYNOPSIS
54
55 use C4::Branch;
56
57 =head1 DESCRIPTION
58
59 The functions in this module deal with branches.
60
61 =head1 FUNCTIONS
62
63 =head2 GetBranches
64
65   $branches = &GetBranches();
66
67 Returns informations about ALL branches, IndependentBranches Insensitive.
68 GetBranchInfo() returns the same information.
69
70 Create a branch selector with the following code.
71
72 =head3 in PERL SCRIPT
73
74     my $branches = GetBranches;
75     my @branchloop;
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},
81                 );
82         push @branchloop, \%row;
83     }
84
85 =head3 in TEMPLATE
86
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>
92                 [% ELSE %]
93                     <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
94                 [% END %]
95             [% END %]
96     </select>
97
98 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
99
100 =cut
101
102 sub GetBranches {
103     my ($onlymine) = @_;
104
105     # returns a reference to a hash of references to ALL branches...
106     my %branches;
107     my $dbh = C4::Context->dbh;
108     my $sth;
109     my $query = "SELECT * FROM branches";
110     my @bind_parameters;
111     if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
112         $query .= ' WHERE branchcode = ? ';
113         push @bind_parameters, C4::Context->userenv->{branch};
114     }
115     $query .= " ORDER BY branchname";
116     $sth = $dbh->prepare($query);
117     $sth->execute(@bind_parameters);
118
119     my $relations_sth =
120       $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
121     $relations_sth->execute();
122     my %relations;
123     while ( my $rel = $relations_sth->fetchrow_hashref ) {
124         push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
125     }
126
127     while ( my $branch = $sth->fetchrow_hashref ) {
128         foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
129             $branch->{category}{$cat} = 1;
130         }
131         $branches{ $branch->{'branchcode'} } = $branch;
132     }
133     return ( \%branches );
134 }
135
136 sub onlymine {
137     return
138          C4::Context->preference('IndependentBranches')
139       && C4::Context->userenv
140       && !C4::Context->IsSuperLibrarian()
141       && C4::Context->userenv->{branch};
142 }
143
144 # always returns a string for OK comparison via "eq" or "ne"
145 sub mybranch {
146     C4::Context->userenv           or return '';
147     return C4::Context->userenv->{branch} || '';
148 }
149
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);
154     my @loop;
155     foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
156         push @loop, {
157             value      => $branchcode,
158             branchcode => $branchcode,
159             selected   => ($branchcode eq $branch) ? 1 : 0,
160             branchname => $branches->{$branchcode}->{branchname},
161         };
162     }
163     return \@loop;
164 }
165
166 =head2 GetBranchName
167
168 =cut
169
170 sub GetBranchName {
171     my ($branchcode) = @_;
172     my $dbh = C4::Context->dbh;
173     my $sth;
174     $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
175     $sth->execute($branchcode);
176     my $branchname = $sth->fetchrow_array;
177     return ($branchname);
178 }
179
180 =head2 ModBranch
181
182 $error = &ModBranch($newvalue);
183
184 This function modifies an existing branch
185
186 C<$newvalue> is a ref to an array which contains all the columns from branches table.
187
188 =cut
189
190 sub ModBranch {
191     my ($data) = @_;
192     
193     my $dbh    = C4::Context->dbh;
194     if ($data->{add}) {
195         my $query  = "
196             INSERT INTO branches
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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
203         ";
204         my $sth    = $dbh->prepare($query);
205         $sth->execute(
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'}
216         );
217         return 1 if $dbh->err;
218     } else {
219         my $query  = "
220             UPDATE branches
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=?
227             WHERE branchcode=?
228         ";
229         my $sth    = $dbh->prepare($query);
230         $sth->execute(
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'},
242         );
243     }
244     # sort out the categories....
245     my @checkedcats;
246     my $cats = GetBranchCategories();
247     foreach my $cat (@$cats) {
248         my $code = $cat->{'categorycode'};
249         if ( $data->{$code} ) {
250             push( @checkedcats, $code );
251         }
252     }
253     my $branchcode = uc( $data->{'branchcode'} );
254     my $branch     = GetBranchInfo($branchcode);
255     $branch = $branch->[0];
256     my $branchcats = $branch->{'categories'};
257     my @addcats;
258     my @removecats;
259     foreach my $bcat (@$branchcats) {
260
261         unless ( grep { /^$bcat$/ } @checkedcats ) {
262             push( @removecats, $bcat );
263         }
264     }
265     foreach my $ccat (@checkedcats) {
266         unless ( grep { /^$ccat$/ } @$branchcats ) {
267             push( @addcats, $ccat );
268         }
269     }
270     foreach my $cat (@addcats) {
271         my $sth =
272           $dbh->prepare(
273 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
274           );
275         $sth->execute( $branchcode, $cat );
276     }
277     foreach my $cat (@removecats) {
278         my $sth =
279           $dbh->prepare(
280             "delete from branchrelations where branchcode=? and categorycode=?"
281           );
282         $sth->execute( $branchcode, $cat );
283     }
284 }
285
286 =head2 GetBranchCategories
287
288   my $categories = GetBranchCategories($categorytype,$show_in_pulldown,$selected_in_pulldown);
289
290 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
291 i.e. categorydescription, categorytype, categoryname.
292
293 =cut
294
295 sub GetBranchCategories {
296     my ( $categorytype, $show_in_pulldown, $selected_in_pulldown ) = @_;
297     my $dbh = C4::Context->dbh();
298
299     my $query = "SELECT * FROM branchcategories ";
300
301     my ( @where, @bind );
302     if ( $categorytype ) {
303         push @where, " categorytype = ? ";
304         push @bind, $categorytype;
305     }
306
307     if ( defined( $show_in_pulldown ) ) {
308         push( @where, " show_in_pulldown = ? " );
309         push( @bind, $show_in_pulldown );
310     }
311
312     $query .= " WHERE " . join(" AND ", @where) if(@where);
313     $query .= " ORDER BY categorytype, categorycode";
314     my $sth=$dbh->prepare( $query);
315     $sth->execute(@bind);
316
317     my $branchcats = $sth->fetchall_arrayref({});
318
319     if ( $selected_in_pulldown ) {
320         foreach my $bc ( @$branchcats ) {
321             $bc->{selected} = 1 if $bc->{categorycode} eq $selected_in_pulldown;
322         }
323     }
324
325     return $branchcats;
326 }
327
328 =head2 GetCategoryTypes
329
330 $categorytypes = GetCategoryTypes;
331 returns a list of category types.
332 Currently these types are HARDCODED.
333 type: 'searchdomain' defines a group of agencies that the calling library may search in.
334 Other usage of agency categories falls under type: 'properties'.
335         to allow for other uses of categories.
336 The searchdomain bit may be better implemented as a separate module, but
337 the categories were already here, and minimally used.
338
339 =cut
340
341         #TODO  manage category types.  rename possibly to 'agency domains' ? as borrowergroups are called categories.
342 sub GetCategoryTypes {
343         return ( 'searchdomain','properties');
344 }
345
346 =head2 GetBranch
347
348 $branch = GetBranch( $query, $branches );
349
350 =cut
351
352 sub GetBranch {
353     my ( $query, $branches ) = @_;    # get branch for this query from branches
354     my $branch = $query->param('branch');
355     my %cookie = $query->cookie('userenv');
356     ($branch)                || ($branch = $cookie{'branchname'});
357     ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
358     return $branch;
359 }
360
361 =head2 GetBranchDetail
362
363     $branch = &GetBranchDetail($branchcode);
364
365 Given the branch code, the function returns a
366 hashref for the corresponding row in the branches table.
367
368 =cut
369
370 sub GetBranchDetail {
371     my ($branchcode) = shift or return;
372     my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
373     $sth->execute($branchcode);
374     return $sth->fetchrow_hashref();
375 }
376
377 =head2 GetBranchesInCategory
378
379   my $branches = GetBranchesInCategory($categorycode);
380
381 Returns a href:  keys %$branches eq (branchcode,branchname) .
382
383 =cut
384
385 sub GetBranchesInCategory {
386     my ($categorycode) = @_;
387         my @branches;
388         my $dbh = C4::Context->dbh();
389         my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b 
390                                                         where r.branchcode=b.branchcode and r.categorycode=?");
391     $sth->execute($categorycode);
392         while (my $branch = $sth->fetchrow) {
393                 push @branches, $branch;
394         }
395         return( \@branches );
396 }
397
398 =head2 GetBranchInfo
399
400 $results = GetBranchInfo($branchcode);
401
402 returns C<$results>, a reference to an array of hashes containing branches.
403 if $branchcode, just this branch, with associated categories.
404 if ! $branchcode && $categorytype, all branches in the category.
405
406 =cut
407
408 sub GetBranchInfo {
409     my ($branchcode,$categorytype) = @_;
410     my $dbh = C4::Context->dbh;
411     my $sth;
412
413
414         if ($branchcode) {
415         $sth =
416           $dbh->prepare(
417             "Select * from branches where branchcode = ? order by branchcode");
418         $sth->execute($branchcode);
419     }
420     else {
421         $sth = $dbh->prepare("Select * from branches order by branchcode");
422         $sth->execute();
423     }
424     my @results;
425     while ( my $data = $sth->fetchrow_hashref ) {
426                 my @bind = ($data->{'branchcode'});
427         my $query= "select r.categorycode from branchrelations r";
428                 $query .= ", branchcategories c " if($categorytype);
429                 $query .= " where  branchcode=? ";
430                 if($categorytype) { 
431                         $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
432                         push @bind, $categorytype;
433                 }
434         my $nsth=$dbh->prepare($query);
435                 $nsth->execute( @bind );
436         my @cats = ();
437         while ( my ($cat) = $nsth->fetchrow_array ) {
438             push( @cats, $cat );
439         }
440         $data->{'categories'} = \@cats;
441         push( @results, $data );
442     }
443     return \@results;
444 }
445
446 =head2 ModBranchCategoryInfo
447
448 &ModBranchCategoryInfo($data);
449 sets the data from the editbranch form, and writes to the database...
450
451 =cut
452
453 sub ModBranchCategoryInfo {
454     my ($data) = @_;
455     my $dbh    = C4::Context->dbh;
456     if ($data->{'add'}){
457         # we are doing an insert
458   my $sth   = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)");
459         $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} );
460     }
461     else {
462         # modifying
463         my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?");
464         $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) );
465     }
466 }
467 sub GetBranchesCount {
468     my $dbh = C4::Context->dbh();
469     my $query = "SELECT COUNT(*) AS branches_count FROM branches";
470     my $sth = $dbh->prepare( $query );
471     $sth->execute();
472     my $row = $sth->fetchrow_hashref();
473     return $row->{'branches_count'};
474 }
475
476 1;
477 __END__
478
479 =head1 AUTHOR
480
481 Koha Development Team <http://koha-community.org/>
482
483 =cut