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