Noted most likely correct tab size
[koha.git] / admin / branches.pl
1 #!/usr/bin/perl
2 # NOTE: Use standard 8-space tabs for this file (indents are 4 spaces)
3
4 #require '/u/acli/lib/cvs.pl';#DEBUG
5 open(DEBUG,'>/tmp/koha.debug');
6
7 # FIXME: individual fields in branch address need to be exported to templates,
8 #        in order to fix bug 180; need to notify translators
9 # FIXME: looped html (e.g., list of checkboxes) need to be properly
10 #        TMPL_LOOP'ized; doing this properly will fix bug 130; need to
11 #        notify translators
12 # FIXME: need to implement the branch categories stuff
13 # FIXME: heading() need to be moved to templates, need to notify translators
14 # FIXME: there are too many TMPL_IF's; the proper way to do it is to have
15 #        separate templates for each individual action; need to notify
16 #        translators
17
18 # Finlay working on this file from 26-03-2002
19 # Reorganising this branches admin page.....
20
21
22 # Copyright 2000-2002 Katipo Communications
23 #
24 # This file is part of Koha.
25 #
26 # Koha is free software; you can redistribute it and/or modify it under the
27 # terms of the GNU General Public License as published by the Free Software
28 # Foundation; either version 2 of the License, or (at your option) any later
29 # version.
30 #
31 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
32 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
33 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
34 #
35 # You should have received a copy of the GNU General Public License along with
36 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
37 # Suite 330, Boston, MA  02111-1307 USA
38
39 use strict;
40 use CGI;
41 use C4::Auth;
42 use C4::Context;
43 use C4::Output;
44 use C4::Interface::CGI::Output;
45 use HTML::Template;
46
47 # Fixed variables
48 my $linecolor1='#ffffcc';
49 my $linecolor2='white';
50 my $backgroundimage="/images/background-mem.gif";
51 my $script_name="/cgi-bin/koha/admin/branches.pl";
52 my $pagesize=20;
53
54
55 #######################################################################################
56 # Main loop....
57
58 my $input = new CGI;
59 my $branchcode=$input->param('branchcode');
60 my $op = $input->param('op');
61
62 my ($template, $borrowernumber, $cookie)
63     = get_template_and_user({template_name => "parameters/branches.tmpl",
64                              query => $input,
65                              type => "intranet",
66                              authnotrequired => 0,
67                              flagsrequired => {parameters => 1},
68                              debug => 1,
69                              });
70 if ($op) {
71     $template->param(script_name => $script_name,
72                      $op         => 1); # we show only the TMPL_VAR names $op
73 } else {
74     $template->param(script_name => $script_name,
75                      else        => 1); # we show only the TMPL_VAR names $op
76 }
77 $template->param(action => $script_name);
78
79 if ($op eq 'add') {
80 # If the user has pressed the "add new branch" button.
81     heading("Branches: Add Branch");
82     editbranchform();
83
84 } elsif ($op eq 'edit') {
85 # if the user has pressed the "edit branch settings" button.
86     heading("Branches: Edit Branch");
87     $template->param(add => 1);
88     editbranchform($branchcode);
89
90 } elsif ($op eq 'add_validate') {
91 # confirm settings change...
92     my $params = $input->Vars;
93     unless ($params->{'branchcode'} && $params->{'branchname'}) {
94         default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
95     } else {
96         setbranchinfo($params);
97         $template->param(else => 1);
98         default ("Branch record changed for branch: $params->{'branchname'}");
99     }
100
101 } elsif ($op eq 'delete') {
102 # if the user has pressed the "delete branch" button.
103     my $message = checkdatabasefor($branchcode);
104     if ($message) {
105         $template->param(else => 1);
106         default($message);
107     } else {
108         deleteconfirm($branchcode);
109         $template->param(delete_confirm => 1);
110         $template->param(branchcode => $branchcode);
111     }
112
113 } elsif ($op eq 'delete_confirmed') {
114 # actually delete branch and return to the main screen....
115     deletebranch($branchcode);
116     $template->param(else => 1);
117     default("The branch with code $branchcode has been deleted.");
118
119 } elsif ($op eq 'add_cat') {
120 # If the user has pressed the "add new category" button.
121     heading("Branches: Add Branch");
122     editcatform();
123
124 } else {
125 # if no operation has been set...
126     default();
127 }
128
129
130
131 ######################################################################################################
132 #
133 # html output functions....
134
135 sub default {
136     my ($message) = @_;
137     heading("Branches");
138     $template->param(message => $message);
139     $template->param(action => $script_name);
140     branchinfotable();
141     
142     
143 }
144
145 # FIXME: this function should not exist; otherwise headings are untranslatable
146 sub heading {
147     my ($head) = @_;
148     $template->param(head => $head);
149 }
150
151 sub editbranchform {
152 # prepares the edit form...
153     my ($branchcode) = @_;
154     my $data;
155     if ($branchcode) {
156         $data = getbranchinfo($branchcode);
157         $data = $data->[0];
158         $template->param(branchcode => $data->{'branchcode'});
159         $template->param(branchname => $data->{'branchname'});
160         $template->param(branchaddress1 => $data->{'branchaddress1'});
161         $template->param(branchaddress2 => $data->{'branchaddress2'});
162         $template->param(branchaddress3 => $data->{'branchaddress3'});
163         $template->param(branchphone => $data->{'branchphone'});
164         $template->param(branchfax => $data->{'branchfax'});
165         $template->param(branchemail => $data->{'branchemail'});
166     }
167 # make the checkboxs.....
168 # FIXME: this way of doing it is wrong (bug 130)
169     my $catinfo = getcategoryinfo();
170     my $catcheckbox;
171     print DEBUG "catinfo=".cvs($catinfo)."\n";
172     foreach my $cat (@$catinfo) {
173         my $checked = "";
174         my $tmp = $cat->{'categorycode'};
175         if (grep {/^$tmp$/} @{$data->{'categories'}}) {
176             $checked = "CHECKED";
177         }
178         $template->param(categoryname => $cat->{'categoryname'});
179         $template->param(categorycode => $cat->{'categorycode'});
180         $template->param(codedescription => $checked>$cat->{'codedescription'});
181     }
182    
183 }
184
185 sub deleteconfirm {
186 # message to print if the 
187     my ($branchcode) = @_;
188 }
189
190
191 sub branchinfotable {
192 # makes the html for a table of branch info from reference to an array of hashs.
193
194     my ($branchcode) = @_;
195     my $branchinfo;
196     if ($branchcode) {
197         $branchinfo = getbranchinfo($branchcode);
198     } else {
199         $branchinfo = getbranchinfo();
200     }
201     my $color;
202     my @loop_data =();
203     foreach my $branch (@$branchinfo) {
204         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
205         my $address = '';
206         $address .= $branch->{'branchaddress1'}          if ($branch->{'branchaddress1'});
207         $address .= '<br>'.$branch->{'branchaddress2'}   if ($branch->{'branchaddress2'});
208         $address .= '<br>'.$branch->{'branchaddress3'}   if ($branch->{'branchaddress3'});
209         $address .= '<br>ph: '.$branch->{'branchphone'}   if ($branch->{'branchphone'});
210         $address .= '<br>fax: '.$branch->{'branchfax'}    if ($branch->{'branchfax'});
211         $address .= '<br>email: '.$branch->{'branchemail'} if ($branch->{'branchemail'});
212         $address = '(nothing entered)' unless ($address);
213         my $categories = '';
214         foreach my $cat (@{$branch->{'categories'}}) {
215             my ($catinfo) = @{getcategoryinfo($cat)};
216             $categories .= $catinfo->{'categoryname'}."<br>";
217         }
218         $categories = '(no categories set)' unless ($categories);
219         my @colors = ();
220         my @branch_name = ();
221         my @branch_code = ();
222         my @address = ();
223         my @categories = ();
224         my @value = ();
225         my @action =();
226         push(@colors,$color);
227         push(@branch_name,$branch->{'branchname'});
228         push(@branch_code,$branch->{'branchcode'});
229         push(@address,$address);
230         push(@categories,$categories);
231         push(@value,$branch->{'branchcode'});
232         push(@action,"/cgi-bin/koha/admin/branches.pl");
233         while (@colors and @branch_name and @branch_code and @address and @categories and @value and @action) {
234             my %row_data;
235             $row_data{color} = shift @colors;
236             $row_data{branch_name} = shift @branch_name;
237             $row_data{branch_code} = shift @branch_code;
238             $row_data{address} = shift @address;
239             $row_data{categories} = shift @categories;
240             $row_data{value} = shift @value;
241             $row_data{action} = shift @action;
242             push(@loop_data, \%row_data);
243         }
244     
245     }
246     $template->param(branches => \@loop_data);
247
248 }
249
250 # FIXME logic seems wrong
251 sub branchcategoriestable {
252 #Needs to be implemented...
253
254     my $categoryinfo = getcategoryinfo();
255     my $color;
256     foreach my $cat (@$categoryinfo) {
257         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
258         $template->param(color => $color);
259         $template->param(categoryname => $cat->{'categoryname'});
260         $template->param(categorycode => $cat->{'categorycode'});
261         $template->param(codedescription => $cat->{'codedescription'});
262     }
263 }
264
265 ######################################################################################################
266 #
267 # Database functions....
268
269 sub getbranchinfo {
270 # returns a reference to an array of hashes containing branches,
271
272     my ($branchcode) = @_;
273     my $dbh = C4::Context->dbh;
274     my $query;
275     if ($branchcode) {
276         my $bc = $dbh->quote($branchcode);
277         $query = "Select * from branches where branchcode = $bc";
278     } else {
279         $query = "Select * from branches";
280     }
281     my $sth = $dbh->prepare($query);
282     $sth->execute;
283     my @results;
284     while (my $data = $sth->fetchrow_hashref) { 
285         my $tmp = $data->{'branchcode'}; my $brc = $dbh->quote($tmp);
286         $query = "select categorycode from branchrelations where branchcode = $brc";
287         my $nsth = $dbh->prepare($query);
288         $nsth->execute;
289         my @cats = ();
290         while (my ($cat) = $nsth->fetchrow_array) {
291             push(@cats, $cat);
292         }
293         $nsth->finish;
294         $data->{'categories'} = \@cats;
295         push(@results, $data);
296     }
297     $sth->finish;
298     return \@results;
299 }
300
301 # FIXME This doesn't belong here; it should be moved into a module
302 sub getcategoryinfo {
303 # returns a reference to an array of hashes containing branches,
304     my ($catcode) = @_;
305     my $dbh = C4::Context->dbh;
306     my $query;
307     print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
308     if ($catcode) {
309         my $cc = $dbh->quote($catcode);
310         $query = "select * from branchcategories where categorycode = $cc";
311     } else {
312         $query = "Select * from branchcategories";
313     }
314     print DEBUG "getcategoryinfo: query=".cvs($query)."\n";
315     my $sth = $dbh->prepare($query);
316     $sth->execute;
317     my @results;
318     while (my $data = $sth->fetchrow_hashref) { 
319         push(@results, $data);
320     }
321     $sth->finish;
322     print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
323     return \@results;
324 }
325
326 sub setbranchinfo {
327 # sets the data from the editbranch form, and writes to the database...
328     my ($data) = @_;
329     my $dbh = C4::Context->dbh;
330     my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
331     my $tmp;
332     $tmp = $data->{'branchcode'}; $query.= $dbh->quote($tmp).",";
333     $tmp = $data->{'branchname'}; $query.= $dbh->quote($tmp).",";
334     $tmp = $data->{'branchaddress1'}; $query.= $dbh->quote($tmp).",";
335     $tmp = $data->{'branchaddress2'}; $query.= $dbh->quote($tmp).",";
336     $tmp = $data->{'branchaddress3'}; $query.= $dbh->quote($tmp).",";
337     $tmp = $data->{'branchphone'}; $query.= $dbh->quote($tmp).",";
338     $tmp = $data->{'branchfax'}; $query.= $dbh->quote($tmp).",";
339     $tmp = $data->{'branchemail'}; $query.= $dbh->quote($tmp).")";
340     my $sth=$dbh->prepare($query);
341     $sth->execute;
342     $sth->finish;
343 # sort out the categories....
344     my @checkedcats;
345     my $cats = getcategoryinfo();
346     foreach my $cat (@$cats) {
347         my $code = $cat->{'categorycode'};
348         if ($data->{$code}) {
349             push(@checkedcats, $code);
350         }
351     }
352     my $branchcode = $data->{'branchcode'};
353     my $branch = getbranchinfo($branchcode);
354     $branch = $branch->[0];
355     my $branchcats = $branch->{'categories'};
356     my @addcats;
357     my @removecats;
358     foreach my $bcat (@$branchcats) {
359         unless (grep {/^$bcat$/} @checkedcats) {
360             push(@removecats, $bcat);
361         }
362     }
363     foreach my $ccat (@checkedcats){
364         unless (grep {/^$ccat$/} @$branchcats) {
365             push(@addcats, $ccat);
366         }
367     }   
368     # FIXME - There's already a $dbh in this scope.
369     my $dbh = C4::Context->dbh;
370     foreach my $cat (@addcats) {
371         my $query = "insert into branchrelations (branchcode, categorycode) values('$branchcode', '$cat')";
372         my $sth = $dbh->prepare($query);
373         $sth->execute;
374         $sth->finish;
375     }
376     foreach my $cat (@removecats) {
377         my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
378         my $sth = $dbh->prepare($query);
379         $sth->execute;
380         $sth->finish;
381     }
382 }
383
384 sub deletebranch {
385 # delete branch...
386     my ($branchcode) = @_;
387     my $query = "delete from branches where branchcode = '$branchcode'";
388     my $dbh = C4::Context->dbh;
389     my $sth=$dbh->prepare($query);
390     $sth->execute;
391     $sth->finish;
392 }
393
394 sub checkdatabasefor {
395 # check to see if the branchcode is being used in the database somewhere....
396     my ($branchcode) = @_;
397     my $dbh = C4::Context->dbh;
398     my $sth=$dbh->prepare("select count(*) from items where holdingbranch='$branchcode' or homebranch='$branchcode'");
399     $sth->execute;
400     my ($total) = $sth->fetchrow_array;
401     $sth->finish;
402     my $message;
403     if ($total) {
404         $message = "Branch cannot be deleted because there are $total items using that branch.";
405     } 
406     return $message;
407 }
408
409 output_html_with_http_headers $input, $cookie, $template->output;
410
411 # Local Variables:
412 # tab-width: 8
413 # End: