Bug 15295: Koha::Libraries - Remove GetBranchCategories
[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 use Koha::LibraryCategories;
24
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
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                 &get_branchinfos_of
38                 &ModBranch
39                 &GetBranchInfo
40                 &GetCategoryTypes
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 = Koha::LibraryCategories->search;
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 GetCategoryTypes
287
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.
296
297 =cut
298
299         #TODO  manage category types.  rename possibly to 'agency domains' ? as borrowergroups are called categories.
300 sub GetCategoryTypes {
301         return ( 'searchdomain','properties');
302 }
303
304 =head2 GetBranch
305
306 $branch = GetBranch( $query, $branches );
307
308 =cut
309
310 sub GetBranch {
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] );
316     return $branch;
317 }
318
319 =head2 GetBranchDetail
320
321     $branch = &GetBranchDetail($branchcode);
322
323 Given the branch code, the function returns a
324 hashref for the corresponding row in the branches table.
325
326 =cut
327
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();
333 }
334
335 =head2 GetBranchesInCategory
336
337   my $branches = GetBranchesInCategory($categorycode);
338
339 Returns a href:  keys %$branches eq (branchcode,branchname) .
340
341 =cut
342
343 sub GetBranchesInCategory {
344     my ($categorycode) = @_;
345         my @branches;
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;
352         }
353         return( \@branches );
354 }
355
356 =head2 GetBranchInfo
357
358 $results = GetBranchInfo($branchcode);
359
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.
363
364 =cut
365
366 sub GetBranchInfo {
367     my ($branchcode,$categorytype) = @_;
368     my $dbh = C4::Context->dbh;
369     my $sth;
370
371
372         if ($branchcode) {
373         $sth =
374           $dbh->prepare(
375             "Select * from branches where branchcode = ? order by branchcode");
376         $sth->execute($branchcode);
377     }
378     else {
379         $sth = $dbh->prepare("Select * from branches order by branchcode");
380         $sth->execute();
381     }
382     my @results;
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=? ";
388                 if($categorytype) { 
389                         $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
390                         push @bind, $categorytype;
391                 }
392         my $nsth=$dbh->prepare($query);
393                 $nsth->execute( @bind );
394         my @cats = ();
395         while ( my ($cat) = $nsth->fetchrow_array ) {
396             push( @cats, $cat );
397         }
398         $data->{'categories'} = \@cats;
399         push( @results, $data );
400     }
401     return \@results;
402 }
403
404 =head2 ModBranchCategoryInfo
405
406 &ModBranchCategoryInfo($data);
407 sets the data from the editbranch form, and writes to the database...
408
409 =cut
410
411 sub ModBranchCategoryInfo {
412     my ($data) = @_;
413     my $dbh    = C4::Context->dbh;
414     if ($data->{'add'}){
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'} );
418     }
419     else {
420         # modifying
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'} ) );
423     }
424 }
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 );
429     $sth->execute();
430     my $row = $sth->fetchrow_hashref();
431     return $row->{'branches_count'};
432 }
433
434 1;
435 __END__
436
437 =head1 AUTHOR
438
439 Koha Development Team <http://koha-community.org/>
440
441 =cut