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