opac.css - button background fix for input.icon
[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 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
8 # version.
9 #
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.
13 #
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
17
18
19 use strict;
20 require Exporter;
21 use C4::Context;
22 use C4::Koha;
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.01;
29         @ISA    = qw(Exporter);
30         @EXPORT = qw(
31                 &GetBranchCategory
32                 &GetBranchName
33                 &GetBranch
34                 &GetBranches
35                 &GetBranchDetail
36                 &get_branchinfos_of
37                 &ModBranch
38                 &CheckBranchCategorycode
39                 &GetBranchInfo
40                 &GetCategoryTypes
41                 &GetBranchCategories
42                 &GetBranchesInCategory
43                 &ModBranchCategoryInfo
44                 &DelBranch
45                 &DelBranchCategory
46         );
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   returns informations about ALL branches.
67   Create a branch selector with the following code
68   IndependantBranches Insensitive...
69   GetBranchInfo() returns the same information without the problems of this function 
70   (namespace collision, mainly).  You should probably use that, and replace GetBranches()
71   with GetBranchInfo() where you see it in the code.
72   
73 =head3 in PERL SCRIPT
74
75 my $branches = GetBranches;
76 my @branchloop;
77 foreach my $thisbranch (keys %$branches) {
78     my $selected = 1 if $thisbranch eq $branch;
79     my %row =(value => $thisbranch,
80                 selected => $selected,
81                 branchname => $branches->{$thisbranch}->{'branchname'},
82             );
83     push @branchloop, \%row;
84 }
85
86 =head3 in TEMPLATE
87             <select name="branch">
88                 <option value="">Default</option>
89             <!-- TMPL_LOOP name="branchloop" -->
90                 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
91             <!-- /TMPL_LOOP -->
92             </select>
93
94 =cut
95
96 sub GetBranches {
97     my ($onlymine)=@_;
98     # returns a reference to a hash of references to ALL branches...
99     my %branches;
100     my $dbh = C4::Context->dbh;
101     my $sth;
102     my $query="SELECT * FROM branches";
103     if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
104       $query .= " WHERE branchcode =".$dbh->quote(C4::Context->userenv->{branch});
105     }
106     $query.=" ORDER BY branchname";
107     $sth = $dbh->prepare($query);
108     $sth->execute;
109     while ( my $branch = $sth->fetchrow_hashref ) {
110         my $nsth =
111           $dbh->prepare(
112             "SELECT categorycode FROM branchrelations WHERE branchcode = ?");
113         $nsth->execute( $branch->{'branchcode'} );
114         while ( my ($cat) = $nsth->fetchrow_array ) {
115
116             # FIXME - This seems wrong. It ought to be
117             # $branch->{categorycodes}{$cat} = 1;
118             # otherwise, there's a namespace collision if there's a
119             # category with the same name as a field in the 'branches'
120             # table (i.e., don't create a category called "issuing").
121             # In addition, the current structure doesn't really allow
122             # you to list the categories that a branch belongs to:
123             # you'd have to list keys %$branch, and remove those keys
124             # that aren't fields in the "branches" table.
125          #   $branch->{$cat} = 1;
126             $branch->{category}{$cat} = 1;
127         }
128         $branches{ $branch->{'branchcode'} } = $branch;
129     }
130     return ( \%branches );
131 }
132
133 =head2 GetBranchName
134
135 =cut
136
137 sub GetBranchName {
138     my ($branchcode) = @_;
139     my $dbh = C4::Context->dbh;
140     my $sth;
141     $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
142     $sth->execute($branchcode);
143     my $branchname = $sth->fetchrow_array;
144     $sth->finish;
145     return ($branchname);
146 }
147
148 =head2 ModBranch
149
150 &ModBranch($newvalue);
151
152 This function modify an existing branches.
153
154 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
155
156 =cut
157
158 sub ModBranch {
159     my ($data) = @_;
160     
161     my $dbh    = C4::Context->dbh;
162     if ($data->{add}) {
163         my $query  = "
164             INSERT INTO branches
165             (branchcode,branchname,branchaddress1,
166             branchaddress2,branchaddress3,branchphone,
167             branchfax,branchemail,branchip,branchprinter)
168             VALUES (?,?,?,?,?,?,?,?,?,?)
169         ";
170         my $sth    = $dbh->prepare($query);
171         $sth->execute(
172             $data->{'branchcode'},       $data->{'branchname'},
173             $data->{'branchaddress1'},   $data->{'branchaddress2'},
174             $data->{'branchaddress3'},   $data->{'branchphone'},
175             $data->{'branchfax'},        $data->{'branchemail'},
176             $data->{'branchip'},         $data->{'branchprinter'},
177         );
178     } else {
179         my $query  = "
180             UPDATE branches
181             SET branchname=?,branchaddress1=?,
182                 branchaddress2=?,branchaddress3=?,branchphone=?,
183                 branchfax=?,branchemail=?,branchip=?,branchprinter=?
184             WHERE branchcode=?
185         ";
186         my $sth    = $dbh->prepare($query);
187         $sth->execute(
188             $data->{'branchname'},
189             $data->{'branchaddress1'},   $data->{'branchaddress2'},
190             $data->{'branchaddress3'},   $data->{'branchphone'},
191             $data->{'branchfax'},        $data->{'branchemail'},
192             $data->{'branchip'},         $data->{'branchprinter'},
193             $data->{'branchcode'},
194         );
195     }
196     # sort out the categories....
197     my @checkedcats;
198     my $cats = GetBranchCategory();
199     foreach my $cat (@$cats) {
200         my $code = $cat->{'categorycode'};
201         if ( $data->{$code} ) {
202             push( @checkedcats, $code );
203         }
204     }
205     my $branchcode = uc( $data->{'branchcode'} );
206     my $branch     = GetBranchInfo($branchcode);
207     $branch = $branch->[0];
208     my $branchcats = $branch->{'categories'};
209     my @addcats;
210     my @removecats;
211     foreach my $bcat (@$branchcats) {
212
213         unless ( grep { /^$bcat$/ } @checkedcats ) {
214             push( @removecats, $bcat );
215         }
216     }
217     foreach my $ccat (@checkedcats) {
218         unless ( grep { /^$ccat$/ } @$branchcats ) {
219             push( @addcats, $ccat );
220         }
221     }
222     foreach my $cat (@addcats) {
223         my $sth =
224           $dbh->prepare(
225 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
226           );
227         $sth->execute( $branchcode, $cat );
228         $sth->finish;
229     }
230     foreach my $cat (@removecats) {
231         my $sth =
232           $dbh->prepare(
233             "delete from branchrelations where branchcode=? and categorycode=?"
234           );
235         $sth->execute( $branchcode, $cat );
236         $sth->finish;
237     }
238 }
239
240 =head2 GetBranchCategory
241
242 $results = GetBranchCategory($categorycode);
243
244 C<$results> is an ref to an array.
245
246 =cut
247
248 sub GetBranchCategory {
249
250     # returns a reference to an array of hashes containing branches,
251     my ($catcode) = @_;
252     my $dbh = C4::Context->dbh;
253     my $sth;
254
255     #    print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
256     if ($catcode) {
257         $sth =
258           $dbh->prepare(
259             "select * from branchcategories where categorycode = ?");
260         $sth->execute($catcode);
261     }
262     else {
263         $sth = $dbh->prepare("Select * from branchcategories");
264         $sth->execute();
265     }
266     my @results;
267     while ( my $data = $sth->fetchrow_hashref ) {
268         push( @results, $data );
269     }
270     $sth->finish;
271
272     #    print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
273     return \@results;
274 }
275
276 =head2 GetBranchCategories
277
278   my $categories = GetBranchCategories($branchcode,$categorytype);
279
280 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
281 i.e. categorycode, categorydescription, categorytype, categoryname.
282 if $branchcode and/or $categorytype are passed, limit set to categories that
283 $branchcode is a member of , and to $categorytype.
284
285 =cut
286
287 sub GetBranchCategories {
288     my ($branchcode,$categorytype) = @_;
289         my $dbh = C4::Context->dbh();
290         my $query = "SELECT c.* FROM branchcategories c";
291         my (@where, @bind);
292         if($branchcode) {
293                 $query .= ",branchrelations r, branches b ";
294                 push @where, "c.categorycode=r.categorycode and r.branchcode=? ";  
295                 push @bind , $branchcode;
296         }
297         if ($categorytype) {
298                 push @where, " c.categorytype=? ";
299                 push @bind, $categorytype;
300         }
301         $query .= " where " . join(" and ", @where) if(@where);
302         $query .= " order by categorytype,c.categorycode";
303         my $sth=$dbh->prepare( $query);
304         $sth->execute(@bind);
305         
306         my $branchcats = $sth->fetchall_arrayref({});
307         $sth->finish();
308         return( $branchcats );
309 }
310
311 =head2 GetCategoryTypes
312
313 $categorytypes = GetCategoryTypes;
314 returns a list of category types.
315 Currently these types are HARDCODED.
316 type: 'searchdomain' defines a group of agencies that the calling library may search in.
317 Other usage of agency categories falls under type: 'properties'.
318         to allow for other uses of categories.
319 The searchdomain bit may be better implemented as a separate module, but
320 the categories were already here, and minimally used.
321 =cut
322
323         #TODO  manage category types.  rename possibly to 'agency domains' ? as borrowergroups are called categories.
324 sub GetCategoryTypes() {
325         return ( 'searchdomain','properties');
326 }
327
328 =head2 GetBranch
329
330 $branch = GetBranch( $query, $branches );
331
332 =cut
333
334 sub GetBranch ($$) {
335     my ( $query, $branches ) = @_;    # get branch for this query from branches
336     my $branch = $query->param('branch');
337     my %cookie = $query->cookie('userenv');
338     ($branch)                || ($branch = $cookie{'branchname'});
339     ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
340     return $branch;
341 }
342
343 =head2 GetBranchDetail
344
345   $branchname = &GetBranchDetail($branchcode);
346
347 Given the branch code, the function returns the corresponding
348 branch name for a comprehensive information display
349
350 =cut
351
352 sub GetBranchDetail {
353     my ($branchcode) = @_;
354     my $dbh = C4::Context->dbh;
355     my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
356     $sth->execute($branchcode);
357     my $branchname = $sth->fetchrow_hashref();
358     $sth->finish();
359     return $branchname;
360 }
361
362 =head2 get_branchinfos_of
363
364   my $branchinfos_of = get_branchinfos_of(@branchcodes);
365
366 Associates a list of branchcodes to the information of the branch, taken in
367 branches table.
368
369 Returns a href where keys are branchcodes and values are href where keys are
370 branch information key.
371
372   print 'branchname is ', $branchinfos_of->{$code}->{branchname};
373
374 =cut
375
376 sub get_branchinfos_of {
377     my @branchcodes = @_;
378
379     my $query = '
380     SELECT branchcode,
381        branchname
382     FROM branches
383     WHERE branchcode IN ('
384       . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
385 ';
386     return C4::Koha::get_infos_of( $query, 'branchcode' );
387 }
388
389
390 =head2 GetBranchesInCategory
391
392   my $branches = GetBranchesInCategory($categorycode);
393
394 Returns a href:  keys %$branches eq (branchcode,branchname) .
395
396 =cut
397
398 sub GetBranchesInCategory($) {
399     my ($categorycode) = @_;
400         my @branches;
401         my $dbh = C4::Context->dbh();
402         my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b 
403                                                         where r.branchcode=b.branchcode and r.categorycode=?");
404     $sth->execute($categorycode);
405         while (my $branch = $sth->fetchrow) {
406                 push @branches, $branch;
407         }
408         $sth->finish();
409         return( \@branches );
410 }
411
412 =head2 GetBranchInfo
413
414 $results = GetBranchInfo($branchcode);
415
416 returns C<$results>, a reference to an array of hashes containing branches.
417 if $branchcode, just this branch, with associated categories.
418 if ! $branchcode && $categorytype, all branches in the category.
419 =cut
420
421 sub GetBranchInfo {
422     my ($branchcode,$categorytype) = @_;
423     my $dbh = C4::Context->dbh;
424     my $sth;
425
426
427         if ($branchcode) {
428         $sth =
429           $dbh->prepare(
430             "Select * from branches where branchcode = ? order by branchcode");
431         $sth->execute($branchcode);
432     }
433     else {
434         $sth = $dbh->prepare("Select * from branches order by branchcode");
435         $sth->execute();
436     }
437     my @results;
438     while ( my $data = $sth->fetchrow_hashref ) {
439                 my @bind = ($data->{'branchcode'});
440         my $query= "select r.categorycode from branchrelations r";
441                 $query .= ", branchcategories c " if($categorytype);
442                 $query .= " where  branchcode=? ";
443                 if($categorytype) { 
444                         $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
445                         push @bind, $categorytype;
446                 }
447         my $nsth=$dbh->prepare($query);
448                 $nsth->execute( @bind );
449         my @cats = ();
450         while ( my ($cat) = $nsth->fetchrow_array ) {
451             push( @cats, $cat );
452         }
453         $nsth->finish;
454         $data->{'categories'} = \@cats;
455         push( @results, $data );
456     }
457     $sth->finish;
458     return \@results;
459 }
460
461 =head2 DelBranch
462
463 &DelBranch($branchcode);
464
465 =cut
466
467 sub DelBranch {
468     my ($branchcode) = @_;
469     my $dbh = C4::Context->dbh;
470     my $sth = $dbh->prepare("delete from branches where branchcode = ?");
471     $sth->execute($branchcode);
472     $sth->finish;
473 }
474
475 =head2 ModBranchCategoryInfo
476
477 &ModBranchCategoryInfo($data);
478 sets the data from the editbranch form, and writes to the database...
479
480 =cut
481
482 sub ModBranchCategoryInfo {
483     my ($data) = @_;
484     my $dbh    = C4::Context->dbh;
485         if ($data->{'add'}){
486                 # we are doing an insert
487                 my $sth   = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
488                 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
489                 $sth->finish();         
490         }
491         else {
492                 # modifying
493                 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
494                 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
495                 $sth->finish();
496         }
497 }
498
499 =head2 DeleteBranchCategory
500
501 DeleteBranchCategory($categorycode);
502
503 =cut
504
505 sub DelBranchCategory {
506     my ($categorycode) = @_;
507     my $dbh = C4::Context->dbh;
508     my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
509     $sth->execute($categorycode);
510     $sth->finish;
511 }
512
513 =head2 CheckBranchCategorycode
514
515 $number_rows_affected = CheckBranchCategorycode($categorycode);
516
517 =cut
518
519 sub CheckBranchCategorycode {
520
521     # check to see if the branchcode is being used in the database somewhere....
522     my ($categorycode) = @_;
523     my $dbh            = C4::Context->dbh;
524     my $sth            =
525       $dbh->prepare(
526         "select count(*) from branchrelations where categorycode=?");
527     $sth->execute($categorycode);
528     my ($total) = $sth->fetchrow_array;
529     return $total;
530 }
531
532 1;
533 __END__
534
535 =head1 AUTHOR
536
537 Koha Developement team <info@koha.org>
538
539 =cut