Modified File
[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::Context;
27 use C4::Output;
28 use HTML::Template;
29
30 # Fixed variables
31 my $linecolor1='#ffffcc';
32 my $linecolor2='white';
33 my $backgroundimage="/images/background-mem.gif";
34 my $script_name="/cgi-bin/koha/admin/newbranches.pl";
35 my $pagesize=20;
36
37
38 #######################################################################################
39 # Main loop....
40
41 my $input = new CGI;
42 my $branchcode=$input->param('branchcode');
43 my $op = $input->param('op');
44
45 my $template = gettemplate("parameters/branches.tmpl",0);
46 if ($op) {
47 $template->param(script_name => $script_name,
48                                                 $op              => 1); # we show only the TMPL_VAR names $op
49 } else {
50 $template->param(script_name => $script_name,
51                                                 else              => 1); # we show only the TMPL_VAR names $op
52 }
53 $template->param(action => $script_name);
54
55 if ($op eq 'add') {
56 # If the user has pressed the "add new branch" button. 
57     heading("Branches: Add Branch");
58     editbranchform();
59 } elsif ($op eq 'edit') {
60 # if the user has pressed the "edit branch settings" button.
61     heading("Branches: Edit Branch");
62     $template->param(add => 1);
63     editbranchform($branchcode);
64
65 } elsif ($op eq 'add_validate') {
66 # confirm settings change...
67     my $params = $input->Vars;
68     unless ($params->{'branchcode'} && $params->{'branchname'}) {
69         default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
70     } else {
71         setbranchinfo($params);
72         $template->param(else => 1);
73         default ("Branch record changed for branch: $params->{'branchname'}");
74     }
75
76 } elsif ($op eq 'delete') {
77 # if the user has pressed the "delete branch" button.
78     my $message = checkdatabasefor($branchcode);
79     if ($message) {
80         $template->param(else => 1);
81         default($message);
82     } else {
83         deleteconfirm($branchcode);
84         $template->param(delete_confirm => 1);
85         $template->param(branchcode => $branchcode);
86     }
87
88 } elsif ($op eq 'delete_confirmed') {
89 # actually delete branch and return to the main screen....
90     deletebranch($branchcode);
91     $template->param(else => 1);
92     default("The branch with code $branchcode has been deleted.");
93
94 } else {
95 # if no operation has been set...
96     default();
97 }
98
99
100
101 ######################################################################################################
102 #
103 # html output functions....
104
105 sub default {
106     my ($message) = @_;
107     heading("Branches");
108     $template->param(message => $message);
109     $template->param(action => $script_name);
110     branchinfotable();
111     
112     
113 }
114
115 sub heading {
116     my ($head) = @_;
117     $template->param(head => $head);
118 }
119
120 sub editbranchform {
121 # prepares the edit form...
122     my ($branchcode) = @_;
123     my $data;
124     if ($branchcode) {
125         $data = getbranchinfo($branchcode);
126         $data = $data->[0];
127         $template->param(branchcode => $data->{'branchcode'});
128         $template->param(branchname => $data->{'branchname'});
129         $template->param(branchaddress1 => $data->{'branchaddress1'});
130         $template->param(branchaddress2 => $data->{'branchaddress2'});
131         $template->param(branchaddress3 => $data->{'branchaddress3'});
132         $template->param(branchphone => $data->{'branchphone'});
133         $template->param(branchfax => $data->{'branchfax'});
134         $template->param(branchemail => $data->{'branchemail'});
135     }
136 # make the checkboxs.....
137     my $catinfo = getcategoryinfo();
138     my $catcheckbox;
139     foreach my $cat (@$catinfo) {
140         my $checked = "";
141         my $tmp = $cat->{'categorycode'};
142         if (grep {/^$tmp$/} @{$data->{'categories'}}) {
143             $checked = "CHECKED";
144 <<<<<<< branches.pl
145                 }
146     $template->param(categoryname => $cat->{'categoryname'});
147     $template->param(categorycode => $cat->{'categorycode'});
148     $template->param(codedescription => $checked>$cat->{'codedescription'});    
149     }
150    
151 =======
152         }
153         $catcheckbox .= <<EOF;
154 <tr><td>$cat->{'categoryname'}</td>
155 <td><INPUT TYPE="checkbox" NAME="$cat->{'categorycode'}" VALUE="1" $checked>$cat->{'codedescription'}</td></tr>
156 EOF
157     }
158     my $form = <<EOF;
159 <form action='$script_name' name=Aform method=post>
160 <input type=hidden name=op value='add_validate'>
161 <table>
162 <tr><td>Branch code</td><td><input type=text name=branchcode size=5 maxlength=5 value='$data->{'branchcode'}'></td></tr>
163 <tr><td>Name</td><td><input type=text name=branchname size=40 maxlength=80 value='$data->{'branchname'}'>&nbsp;</td></tr>
164 $catcheckbox
165 <tr><td>Address</td><td><input type=text name=branchaddress1 value='$data->{'branchaddress1'}'></td></tr>
166 <tr><td>&nbsp;</td><td><input type=text name=branchaddress2 value='$data->{'branchaddress2'}'></td></tr>
167 <tr><td>&nbsp;</td><td><input type=text name=branchaddress3 value='$data->{'branchaddress3'}'></td></tr>
168 <tr><td>Phone</td><td><input type=text name=branchphone value='$data->{'branchphone'}'></td></tr>
169 <tr><td>Fax</td><td><input type=text name=branchfax value='$data->{'branchfax'}'></td></tr>
170 <tr><td>E-mail</td><td><input type=text name=branchemail value='$data->{'branchemail'}'></td></tr>
171 <tr><td>&nbsp;</td><td><input type=submit value='Submit'></td></tr>
172 </table>
173 </form>
174 EOF
175     return $form;
176 >>>>>>> 1.5
177 }
178
179 sub deleteconfirm {
180 # message to print if the
181     my ($branchcode) = @_;
182 <<<<<<< branches.pl
183 =======
184     my $output = <<EOF;
185 Confirm delete:
186 <form action='$script_name' method=post><input type='hidden' name='op' value='delete_confirmed'>
187 <input type='hidden' name='branchcode' value=$branchcode>
188 <input type=submit value=YES></form>
189 <form action='$script_name' method=post><input type='hidden' name='op' value=''>
190 <input type=submit value=NO></form>
191 EOF
192     return $output;
193 >>>>>>> 1.5
194 }
195
196
197 sub branchinfotable {
198 # makes the html for a table of branch info from reference to an array of hashs.
199
200     my ($branchcode) = @_;
201     my $branchinfo;
202     if ($branchcode) {
203         $branchinfo = getbranchinfo($branchcode);
204     } else {
205         $branchinfo = getbranchinfo();
206     }
207 <<<<<<< branches.pl
208 =======
209     my $table = <<EOF;
210 <table border='1' cellpadding='5' cellspacing='0' width='550'>
211 <tr> <th colspan='5' align='left' bgcolor='#99cc33' background=$backgroundimage>
212 <font size='5'><b>Branches</b></font></th> </tr>
213 <tr bgcolor='#889999'>
214 <td width='175'><b>Name</b></td>
215 <td width='25'><b>Code</b></td>
216 <td width='175'><b>Address</b></td>
217 <td width='175'><b>Categories</b></td>
218 <td width='50'><b>&nbsp;</b></td>
219 </tr>
220 EOF
221
222 >>>>>>> 1.5
223     my $color;
224     foreach my $branch (@$branchinfo) {
225         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
226         my $address = '';
227         $address .= $branch->{'branchaddress1'}          if ($branch->{'branchaddress1'});
228         $address .= '<br>'.$branch->{'branchaddress2'}   if ($branch->{'branchaddress2'});
229         $address .= '<br>'.$branch->{'branchaddress3'}   if ($branch->{'branchaddress3'});
230         $address .= '<br>ph: '.$branch->{'branchphone'}   if ($branch->{'branchphone'});
231         $address .= '<br>fax: '.$branch->{'branchfax'}    if ($branch->{'branchfax'});
232         $address .= '<br>email: '.$branch->{'branchemail'} if ($branch->{'branchemail'});
233         $address = '(nothing entered)' unless ($address);
234         my $categories = '';
235         foreach my $cat (@{$branch->{'categories'}}) {
236             my ($catinfo) = @{getcategoryinfo($cat)};
237             $categories .= $catinfo->{'categoryname'}."<br>";
238         }
239         $categories = '(no categories set)' unless ($categories);
240 <<<<<<< branches.pl
241 $template->param(color => $color);
242 $template->param(branch_name => $branch->{'branchname'});
243 $template->param(adress => $address);
244 $template->param(categories => $categories);
245 $template->param(branch_code => $branch->{'branchcode'});
246 $template->param(value => $branch->{'branchcode'});
247 =======
248         $table .= <<EOF;
249 <tr bgcolor='$color'>
250     <td align='left' valign='top'>$branch->{'branchname'}</td>
251     <td align='left' valign='top'>$branch->{'branchcode'}</td>
252     <td align='left' valign='top'>$address</td>
253     <td align='left' valign='top'>$categories</td>
254     <td align='left' valign='top'>
255 <form action='$script_name' method=post>
256 <input type='hidden' name='op' value='edit'>
257 <input type='hidden' name='branchcode' value='$branch->{'branchcode'}'>
258 <input type=submit value=Edit>
259 </form>
260 <form action='$script_name' method=post>
261 <input type='hidden' name='branchcode' value='$branch->{'branchcode'}'>
262 <input type='hidden' name='op' value='delete'><input type=submit value=Delete>
263 </form></td>
264 </tr>
265 EOF
266 >>>>>>> 1.5
267     }
268
269 }
270
271 sub branchcategoriestable {
272 #Needs to be implemented...
273
274     my $categoryinfo = getcategoryinfo();
275 <<<<<<< branches.pl
276 =======
277     my $table = <<EOF;
278 <table border='1' cellpadding='5' cellspacing='0'>
279 <tr> <th colspan='5' align='left' bgcolor='#99cc33' background=$backgroundimage>
280 <font size='5'><b>Branches Categories</b></font></th> </tr>
281 <tr bgcolor='#889999'>
282 <td width='175'><b>Name</b></td>
283 <td width='25'><b>Code</b></td>
284 <td width='200'><b>Description</b></td>
285 </tr>
286 EOF
287 >>>>>>> 1.5
288 my $color;
289     foreach my $cat (@$categoryinfo) {
290         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
291 $template->param(color => $color);
292 $template->param(categoryname => $cat->{'categoryname'});
293 $template->param(categorycode => $cat->{'categorycode'});
294 $template->param(codedescription => $cat->{'codedescription'});
295     }
296 }
297
298 ######################################################################################################
299 #
300 # Database functions....
301
302 sub getbranchinfo {
303 # returns a reference to an array of hashes containing branches,
304
305     my ($branchcode) = @_;
306     my $dbh = C4::Context->dbh;
307     my $query;
308     if ($branchcode) {
309         my $bc = $dbh->quote($branchcode);
310         $query = "Select * from branches where branchcode = $bc";
311     }
312     else {$query = "Select * from branches";}
313     my $sth = $dbh->prepare($query);
314     $sth->execute;
315     my @results;
316     while (my $data = $sth->fetchrow_hashref) {
317         my $tmp = $data->{'branchcode'}; my $brc = $dbh->quote($tmp);
318         $query = "select categorycode from branchrelations where branchcode = $brc";
319         my $nsth = $dbh->prepare($query);
320         $nsth->execute;
321         my @cats = ();
322         while (my ($cat) = $nsth->fetchrow_array) {
323             push(@cats, $cat);
324         }
325         $nsth->finish;
326         $data->{'categories'} = \@cats;
327         push(@results, $data);
328     }
329     $sth->finish;
330     return \@results;
331 }
332
333 sub getcategoryinfo {
334 # returns a reference to an array of hashes containing branches,
335     my ($catcode) = @_;
336     my $dbh = C4::Context->dbh;
337     my $query;
338     if ($catcode) {
339         my $cc = $dbh->quote($catcode);
340         $query = "select * from branchcategories where categorycode = $cc";
341     } else {
342         $query = "Select * from branchcategories";
343     }
344     my $sth = $dbh->prepare($query);
345     $sth->execute;
346     my @results;
347     while (my $data = $sth->fetchrow_hashref) {
348         push(@results, $data);
349     }
350     $sth->finish;
351     return \@results;
352 }
353
354 sub setbranchinfo {
355 # sets the data from the editbranch form, and writes to the database...
356     my ($data) = @_;
357     my $dbh = C4::Context->dbh;
358     my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
359     my $tmp;
360     $tmp = $data->{'branchcode'}; $query.= $dbh->quote($tmp).",";
361     $tmp = $data->{'branchname'}; $query.= $dbh->quote($tmp).",";
362     $tmp = $data->{'branchaddress1'}; $query.= $dbh->quote($tmp).",";
363     $tmp = $data->{'branchaddress2'}; $query.= $dbh->quote($tmp).",";
364     $tmp = $data->{'branchaddress3'}; $query.= $dbh->quote($tmp).",";
365     $tmp = $data->{'branchphone'}; $query.= $dbh->quote($tmp).",";
366     $tmp = $data->{'branchfax'}; $query.= $dbh->quote($tmp).",";
367     $tmp = $data->{'branchemail'}; $query.= $dbh->quote($tmp).")";
368     my $sth=$dbh->prepare($query);
369     $sth->execute;
370     $sth->finish;
371 # sort out the categories....
372     my @checkedcats;
373     my $cats = getcategoryinfo();
374     foreach my $cat (@$cats) {
375         my $code = $cat->{'categorycode'};
376         if ($data->{$code}) {
377             push(@checkedcats, $code);
378         }
379     }
380     my $branchcode = $data->{'branchcode'};
381     my $branch = getbranchinfo($branchcode);
382     $branch = $branch->[0];
383     my $branchcats = $branch->{'categories'};
384     my @addcats;
385     my @removecats;
386     foreach my $bcat (@$branchcats) {
387         unless (grep {/^$bcat$/} @checkedcats) {
388             push(@removecats, $bcat);
389         }
390     }
391     foreach my $ccat (@checkedcats){
392         unless (grep {/^$ccat$/} @$branchcats) {
393             push(@addcats, $ccat);
394         }
395     }
396     # FIXME - There's already a $dbh in this scope.
397     my $dbh = C4::Context->dbh;
398     foreach my $cat (@addcats) {
399         my $query = "insert into branchrelations (branchcode, categorycode) values('$branchcode', '$cat')";
400         my $sth = $dbh->prepare($query);
401         $sth->execute;
402         $sth->finish;
403     }
404     foreach my $cat (@removecats) {
405         my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
406         my $sth = $dbh->prepare($query);
407         $sth->execute;
408         $sth->finish;
409     }
410 }
411
412 sub deletebranch {
413 # delete branch...
414     my ($branchcode) = @_;
415     my $query = "delete from branches where branchcode = '$branchcode'";
416     my $dbh = C4::Context->dbh;
417     my $sth=$dbh->prepare($query);
418     $sth->execute;
419     $sth->finish;
420 }
421
422 sub checkdatabasefor {
423 # check to see if the branchcode is being used in the database somewhere....
424     my ($branchcode) = @_;
425     my $dbh = C4::Context->dbh;
426     my $sth=$dbh->prepare("select count(*) from items where holdingbranch='$branchcode' or homebranch='$branchcode'");
427     $sth->execute;
428     my ($total) = $sth->fetchrow_array;
429     $sth->finish;
430     my $message;
431     if ($total) {
432         $message = "Branch cannot be deleted because there are $total items using that branch.";
433     }
434     return $message;
435 }
436
437 print "Content-Type: text/html\n\n", $template->output;