3 # Finlay working on this file from 26-03-2002
4 # Reorganising this branches admin page.....
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
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
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.
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
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";
38 #######################################################################################
42 my $branchcode=$input->param('branchcode');
43 my $op = $input->param('op');
45 my $template = gettemplate("parameters/branches.tmpl",0);
47 $template->param(script_name => $script_name,
48 $op => 1); # we show only the TMPL_VAR names $op
50 $template->param(script_name => $script_name,
51 else => 1); # we show only the TMPL_VAR names $op
53 $template->param(action => $script_name);
56 # If the user has pressed the "add new branch" button.
57 heading("Branches: Add Branch");
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);
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");
71 setbranchinfo($params);
72 $template->param(else => 1);
73 default ("Branch record changed for branch: $params->{'branchname'}");
76 } elsif ($op eq 'delete') {
77 # if the user has pressed the "delete branch" button.
78 my $message = checkdatabasefor($branchcode);
80 $template->param(else => 1);
83 deleteconfirm($branchcode);
84 $template->param(delete_confirm => 1);
85 $template->param(branchcode => $branchcode);
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.");
95 # if no operation has been set...
101 ######################################################################################################
103 # html output functions....
108 $template->param(message => $message);
109 $template->param(action => $script_name);
117 $template->param(head => $head);
121 # prepares the edit form...
122 my ($branchcode) = @_;
125 $data = getbranchinfo($branchcode);
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'});
136 # make the checkboxs.....
137 my $catinfo = getcategoryinfo();
139 foreach my $cat (@$catinfo) {
141 my $tmp = $cat->{'categorycode'};
142 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
143 $checked = "CHECKED";
146 $template->param(categoryname => $cat->{'categoryname'});
147 $template->param(categorycode => $cat->{'categorycode'});
148 $template->param(codedescription => $checked>$cat->{'codedescription'});
153 $catcheckbox .= <<EOF;
154 <tr><td>$cat->{'categoryname'}</td>
155 <td><INPUT TYPE="checkbox" NAME="$cat->{'categorycode'}" VALUE="1" $checked>$cat->{'codedescription'}</td></tr>
159 <form action='$script_name' name=Aform method=post>
160 <input type=hidden name=op value='add_validate'>
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'}'> </td></tr>
165 <tr><td>Address</td><td><input type=text name=branchaddress1 value='$data->{'branchaddress1'}'></td></tr>
166 <tr><td> </td><td><input type=text name=branchaddress2 value='$data->{'branchaddress2'}'></td></tr>
167 <tr><td> </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> </td><td><input type=submit value='Submit'></td></tr>
180 # message to print if the
181 my ($branchcode) = @_;
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>
197 sub branchinfotable {
198 # makes the html for a table of branch info from reference to an array of hashs.
200 my ($branchcode) = @_;
203 $branchinfo = getbranchinfo($branchcode);
205 $branchinfo = getbranchinfo();
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> </b></td>
224 foreach my $branch (@$branchinfo) {
225 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
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);
235 foreach my $cat (@{$branch->{'categories'}}) {
236 my ($catinfo) = @{getcategoryinfo($cat)};
237 $categories .= $catinfo->{'categoryname'}."<br>";
239 $categories = '(no categories set)' unless ($categories);
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'});
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>
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>
271 sub branchcategoriestable {
272 #Needs to be implemented...
274 my $categoryinfo = getcategoryinfo();
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>
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'});
298 ######################################################################################################
300 # Database functions....
303 # returns a reference to an array of hashes containing branches,
305 my ($branchcode) = @_;
306 my $dbh = C4::Context->dbh;
309 my $bc = $dbh->quote($branchcode);
310 $query = "Select * from branches where branchcode = $bc";
312 else {$query = "Select * from branches";}
313 my $sth = $dbh->prepare($query);
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);
322 while (my ($cat) = $nsth->fetchrow_array) {
326 $data->{'categories'} = \@cats;
327 push(@results, $data);
333 sub getcategoryinfo {
334 # returns a reference to an array of hashes containing branches,
336 my $dbh = C4::Context->dbh;
339 my $cc = $dbh->quote($catcode);
340 $query = "select * from branchcategories where categorycode = $cc";
342 $query = "Select * from branchcategories";
344 my $sth = $dbh->prepare($query);
347 while (my $data = $sth->fetchrow_hashref) {
348 push(@results, $data);
355 # sets the data from the editbranch form, and writes to the database...
357 my $dbh = C4::Context->dbh;
358 my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
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);
371 # sort out the categories....
373 my $cats = getcategoryinfo();
374 foreach my $cat (@$cats) {
375 my $code = $cat->{'categorycode'};
376 if ($data->{$code}) {
377 push(@checkedcats, $code);
380 my $branchcode = $data->{'branchcode'};
381 my $branch = getbranchinfo($branchcode);
382 $branch = $branch->[0];
383 my $branchcats = $branch->{'categories'};
386 foreach my $bcat (@$branchcats) {
387 unless (grep {/^$bcat$/} @checkedcats) {
388 push(@removecats, $bcat);
391 foreach my $ccat (@checkedcats){
392 unless (grep {/^$ccat$/} @$branchcats) {
393 push(@addcats, $ccat);
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);
404 foreach my $cat (@removecats) {
405 my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
406 my $sth = $dbh->prepare($query);
414 my ($branchcode) = @_;
415 my $query = "delete from branches where branchcode = '$branchcode'";
416 my $dbh = C4::Context->dbh;
417 my $sth=$dbh->prepare($query);
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'");
428 my ($total) = $sth->fetchrow_array;
432 $message = "Branch cannot be deleted because there are $total items using that branch.";
437 print "Content-Type: text/html\n\n", $template->output;