Fix for bug 1308: sort by order is lost when browsing results on more than one page
[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 # $Id$
19
20 use strict;
21 require Exporter;
22 use C4::Context;
23 use C4::Koha;
24
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
27 # set the version for version checking
28 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v).".".join( "_", map { sprintf "%03d", $_ } @v ); };
29
30 =head1 NAME
31
32 C4::Branch - Koha branch module
33
34 =head1 SYNOPSIS
35
36 use C4::Branch;
37
38 =head1 DESCRIPTION
39
40 The functions in this module deal with branches.
41
42 =head1 FUNCTIONS
43
44 =cut
45
46 @ISA    = qw(Exporter);
47 @EXPORT = qw(
48    &GetBranchCategory
49    &GetBranchName
50    &GetBranch
51    &GetBranches
52    &GetBranchDetail
53    &get_branchinfos_of
54    &ModBranch
55    &CheckBranchCategorycode
56    &GetBranchInfo
57    &ModBranchCategoryInfo
58    &DelBranch
59 );
60
61 =head2 GetBranches
62
63   $branches = &GetBranches();
64   returns informations about ALL branches.
65   Create a branch selector with the following code
66   IndependantBranches Insensitive...
67   
68 =head3 in PERL SCRIPT
69
70 my $branches = GetBranches;
71 my @branchloop;
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'},
77             );
78     push @branchloop, \%row;
79 }
80
81
82 =head3 in TEMPLATE
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>
87             <!-- /TMPL_LOOP -->
88             </select>
89
90 =cut
91
92 sub GetBranches {
93
94     my $onlymine=@_;
95     # returns a reference to a hash of references to ALL branches...
96     my %branches;
97     my $dbh = C4::Context->dbh;
98     my $sth;
99     my $query="SELECT * from branches";
100     if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
101       $query .= " WHERE branchcode =".$dbh->quote(C4::Context->userenv->{branch});
102     }
103     $query.=" order by branchname";
104     $sth = $dbh->prepare($query);
105     $sth->execute;
106     while ( my $branch = $sth->fetchrow_hashref ) {
107         my $nsth =
108           $dbh->prepare(
109             "select categorycode from branchrelations where branchcode = ?");
110         $nsth->execute( $branch->{'branchcode'} );
111         while ( my ($cat) = $nsth->fetchrow_array ) {
112
113             # FIXME - This seems wrong. It ought to be
114             # $branch->{categorycodes}{$cat} = 1;
115             # otherwise, there's a namespace collision if there's a
116             # category with the same name as a field in the 'branches'
117             # table (i.e., don't create a category called "issuing").
118             # In addition, the current structure doesn't really allow
119             # you to list the categories that a branch belongs to:
120             # you'd have to list keys %$branch, and remove those keys
121             # that aren't fields in the "branches" table.
122             $branch->{$cat} = 1;
123         }
124         $branches{ $branch->{'branchcode'} } = $branch;
125     }
126     return ( \%branches );
127 }
128
129 =head2 GetBranchName
130
131 =cut
132
133 sub GetBranchName {
134     my ($branchcode) = @_;
135     my $dbh = C4::Context->dbh;
136     my $sth;
137     $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
138     $sth->execute($branchcode);
139     my $branchname = $sth->fetchrow_array;
140     $sth->finish;
141     return ($branchname);
142 }
143
144 =head2 ModBranch
145
146 &ModBranch($newvalue);
147
148 This function modify an existing branches.
149
150 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
151
152 =cut
153
154 sub ModBranch {
155     my ($data) = @_;
156     
157     my $dbh    = C4::Context->dbh;
158     if ($data->{add}) {
159         my $query  = "
160             INSERT INTO branches
161             (branchcode,branchname,branchaddress1,
162             branchaddress2,branchaddress3,branchphone,
163             branchfax,branchemail,branchip,branchprinter)
164             VALUES (?,?,?,?,?,?,?,?,?,?)
165         ";
166         my $sth    = $dbh->prepare($query);
167         $sth->execute(
168             $data->{'branchcode'},       $data->{'branchname'},
169             $data->{'branchaddress1'},   $data->{'branchaddress2'},
170             $data->{'branchaddress3'},   $data->{'branchphone'},
171             $data->{'branchfax'},        $data->{'branchemail'},
172             $data->{'branchip'},         $data->{'branchprinter'},
173         );
174     } else {
175         my $query  = "
176             UPDATE branches
177             SET branchname=?,branchaddress1=?,
178                 branchaddress2=?,branchaddress3=?,branchphone=?,
179                 branchfax=?,branchemail=?,branchip=?,branchprinter=?
180             WHERE branchcode=?
181         ";
182         my $sth    = $dbh->prepare($query);
183         $sth->execute(
184             $data->{'branchname'},
185             $data->{'branchaddress1'},   $data->{'branchaddress2'},
186             $data->{'branchaddress3'},   $data->{'branchphone'},
187             $data->{'branchfax'},        $data->{'branchemail'},
188             $data->{'branchip'},         $data->{'branchprinter'},
189             $data->{'branchcode'},
190         );
191     }
192     # sort out the categories....
193     my @checkedcats;
194     my $cats = GetBranchCategory();
195     foreach my $cat (@$cats) {
196         my $code = $cat->{'categorycode'};
197         if ( $data->{$code} ) {
198             push( @checkedcats, $code );
199         }
200     }
201     my $branchcode = uc( $data->{'branchcode'} );
202     my $branch     = GetBranchInfo($branchcode);
203     $branch = $branch->[0];
204     my $branchcats = $branch->{'categories'};
205     my @addcats;
206     my @removecats;
207     foreach my $bcat (@$branchcats) {
208
209         unless ( grep { /^$bcat$/ } @checkedcats ) {
210             push( @removecats, $bcat );
211         }
212     }
213     foreach my $ccat (@checkedcats) {
214         unless ( grep { /^$ccat$/ } @$branchcats ) {
215             push( @addcats, $ccat );
216         }
217     }
218     foreach my $cat (@addcats) {
219         my $sth =
220           $dbh->prepare(
221 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
222           );
223         $sth->execute( $branchcode, $cat );
224         $sth->finish;
225     }
226     foreach my $cat (@removecats) {
227         my $sth =
228           $dbh->prepare(
229             "delete from branchrelations where branchcode=? and categorycode=?"
230           );
231         $sth->execute( $branchcode, $cat );
232         $sth->finish;
233     }
234 }
235
236 =head2 GetBranchCategory
237
238 $results = GetBranchCategory($categorycode);
239
240 C<$results> is an ref to an array.
241
242 =cut
243
244 sub GetBranchCategory {
245
246     # returns a reference to an array of hashes containing branches,
247     my ($catcode) = @_;
248     my $dbh = C4::Context->dbh;
249     my $sth;
250
251     #    print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
252     if ($catcode) {
253         $sth =
254           $dbh->prepare(
255             "select * from branchcategories where categorycode = ?");
256         $sth->execute($catcode);
257     }
258     else {
259         $sth = $dbh->prepare("Select * from branchcategories");
260         $sth->execute();
261     }
262     my @results;
263     while ( my $data = $sth->fetchrow_hashref ) {
264         push( @results, $data );
265     }
266     $sth->finish;
267
268     #    print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
269     return \@results;
270 }
271
272 =head2 GetBranch
273
274 $branch = GetBranch( $query, $branches );
275
276 =cut
277
278 sub GetBranch ($$) {
279     my ( $query, $branches ) = @_;    # get branch for this query from branches
280     my $branch = $query->param('branch');
281     my %cookie = $query->cookie('userenv');
282     ($branch)                || ($branch = $cookie{'branchname'});
283     ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
284     return $branch;
285 }
286
287 =head2 GetBranchDetail
288
289   $branchname = &GetBranchDetail($branchcode);
290
291 Given the branch code, the function returns the corresponding
292 branch name for a comprehensive information display
293
294 =cut
295
296 sub GetBranchDetail {
297     my ($branchcode) = @_;
298     my $dbh = C4::Context->dbh;
299     my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
300     $sth->execute($branchcode);
301     my $branchname = $sth->fetchrow_hashref();
302     $sth->finish();
303     return $branchname;
304 }
305
306
307 =head2 get_branchinfos_of
308
309   my $branchinfos_of = get_branchinfos_of(@branchcodes);
310
311 Associates a list of branchcodes to the information of the branch, taken in
312 branches table.
313
314 Returns a href where keys are branchcodes and values are href where keys are
315 branch information key.
316
317   print 'branchname is ', $branchinfos_of->{$code}->{branchname};
318
319 =cut
320
321 sub get_branchinfos_of {
322     my @branchcodes = @_;
323
324     my $query = '
325     SELECT branchcode,
326        branchname
327     FROM branches
328     WHERE branchcode IN ('
329       . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
330 ';
331     return C4::Koha::get_infos_of( $query, 'branchcode' );
332 }
333
334 =head2 GetBranchInfo
335
336 $results = GetBranchInfo($branchcode);
337
338 returns C<$results>, a reference to an array of hashes containing branches.
339
340 =cut
341
342 sub GetBranchInfo {
343     my ($branchcode) = @_;
344     my $dbh = C4::Context->dbh;
345     my $sth;
346     if ($branchcode) {
347         $sth =
348           $dbh->prepare(
349             "Select * from branches where branchcode = ? order by branchcode");
350         $sth->execute($branchcode);
351     }
352     else {
353         $sth = $dbh->prepare("Select * from branches order by branchcode");
354         $sth->execute();
355     }
356     my @results;
357     while ( my $data = $sth->fetchrow_hashref ) {
358         my $nsth =
359           $dbh->prepare(
360             "select categorycode from branchrelations where branchcode = ?");
361         $nsth->execute( $data->{'branchcode'} );
362         my @cats = ();
363         while ( my ($cat) = $nsth->fetchrow_array ) {
364             push( @cats, $cat );
365         }
366         $nsth->finish;
367         $data->{'categories'} = \@cats;
368         push( @results, $data );
369     }
370     $sth->finish;
371     return \@results;
372 }
373
374 =head2 DelBranch
375
376 &DelBranch($branchcode);
377
378 =cut
379
380 sub DelBranch {
381     my ($branchcode) = @_;
382     my $dbh = C4::Context->dbh;
383     my $sth = $dbh->prepare("delete from branches where branchcode = ?");
384     $sth->execute($branchcode);
385     $sth->finish;
386 }
387
388 =head2 ModBranchCategoryInfo
389
390 &ModBranchCategoryInfo($data);
391 sets the data from the editbranch form, and writes to the database...
392
393 =cut
394
395 sub ModBranchCategoryInfo {
396
397     my ($data) = @_;
398     my $dbh    = C4::Context->dbh;
399     my $sth    = $dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
400     $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'} );
401     $sth->finish;
402 }
403
404 =head2 DeleteBranchCategory
405
406 DeleteBranchCategory($categorycode);
407
408 =cut
409
410 sub DeleteBranchCategory {
411     my ($categorycode) = @_;
412     my $dbh = C4::Context->dbh;
413     my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
414     $sth->execute($categorycode);
415     $sth->finish;
416 }
417
418 =head2 CheckBranchCategorycode
419
420 $number_rows_affected = CheckBranchCategorycode($categorycode);
421
422 =cut
423
424 sub CheckBranchCategorycode {
425
426     # check to see if the branchcode is being used in the database somewhere....
427     my ($categorycode) = @_;
428     my $dbh            = C4::Context->dbh;
429     my $sth            =
430       $dbh->prepare(
431         "select count(*) from branchrelations where categorycode=?");
432     $sth->execute($categorycode);
433     my ($total) = $sth->fetchrow_array;
434     return $total;
435 }
436
437
438
439 =head1 AUTHOR
440
441 Koha Developement team <info@koha.org>
442
443 =cut