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/branches.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");
60 } elsif ($op eq 'edit') {
61 # if the user has pressed the "edit branch settings" button.
62 heading("Branches: Edit Branch");
63 $template->param(add => 1);
64 editbranchform($branchcode);
66 } elsif ($op eq 'add_validate') {
67 # confirm settings change...
68 my $params = $input->Vars;
69 unless ($params->{'branchcode'} && $params->{'branchname'}) {
70 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
72 setbranchinfo($params);
73 $template->param(else => 1);
74 default ("Branch record changed for branch: $params->{'branchname'}");
77 } elsif ($op eq 'delete') {
78 # if the user has pressed the "delete branch" button.
79 my $message = checkdatabasefor($branchcode);
81 $template->param(else => 1);
84 deleteconfirm($branchcode);
85 $template->param(delete_confirm => 1);
86 $template->param(branchcode => $branchcode);
89 } elsif ($op eq 'delete_confirmed') {
90 # actually delete branch and return to the main screen....
91 deletebranch($branchcode);
92 $template->param(else => 1);
93 default("The branch with code $branchcode has been deleted.");
96 # if no operation has been set...
102 ######################################################################################################
104 # html output functions....
109 $template->param(message => $message);
110 $template->param(action => $script_name);
118 $template->param(head => $head);
122 # prepares the edit form...
123 my ($branchcode) = @_;
126 $data = getbranchinfo($branchcode);
128 $template->param(branchcode => $data->{'branchcode'});
129 $template->param(branchname => $data->{'branchname'});
130 $template->param(branchaddress1 => $data->{'branchaddress1'});
131 $template->param(branchaddress2 => $data->{'branchaddress2'});
132 $template->param(branchaddress3 => $data->{'branchaddress3'});
133 $template->param(branchphone => $data->{'branchphone'});
134 $template->param(branchfax => $data->{'branchfax'});
135 $template->param(branchemail => $data->{'branchemail'});
137 # make the checkboxs.....
138 my $catinfo = getcategoryinfo();
140 foreach my $cat (@$catinfo) {
142 my $tmp = $cat->{'categorycode'};
143 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
144 $checked = "CHECKED";
146 $template->param(categoryname => $cat->{'categoryname'});
147 $template->param(categorycode => $cat->{'categorycode'});
148 $template->param(codedescription => $checked>$cat->{'codedescription'});
154 # message to print if the
155 my ($branchcode) = @_;
159 sub branchinfotable {
160 # makes the html for a table of branch info from reference to an array of hashs.
162 my ($branchcode) = @_;
165 $branchinfo = getbranchinfo($branchcode);
167 $branchinfo = getbranchinfo();
171 foreach my $branch (@$branchinfo) {
172 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
174 $address .= $branch->{'branchaddress1'} if ($branch->{'branchaddress1'});
175 $address .= '<br>'.$branch->{'branchaddress2'} if ($branch->{'branchaddress2'});
176 $address .= '<br>'.$branch->{'branchaddress3'} if ($branch->{'branchaddress3'});
177 $address .= '<br>ph: '.$branch->{'branchphone'} if ($branch->{'branchphone'});
178 $address .= '<br>fax: '.$branch->{'branchfax'} if ($branch->{'branchfax'});
179 $address .= '<br>email: '.$branch->{'branchemail'} if ($branch->{'branchemail'});
180 $address = '(nothing entered)' unless ($address);
182 foreach my $cat (@{$branch->{'categories'}}) {
183 my ($catinfo) = @{getcategoryinfo($cat)};
184 $categories .= $catinfo->{'categoryname'}."<br>";
186 $categories = '(no categories set)' unless ($categories);
188 my @branch_name = ();
189 my @branch_code = ();
194 push(@colors,$color);
195 push(@branch_name,$branch->{'branchname'});
196 push(@branch_code,$branch->{'branchcode'});
197 push(@address,$address);
198 push(@categories,$categories);
199 push(@value,$branch->{'branchcode'});
200 push(@action,"/cgi-bin/koha/admin/branches.pl");
201 while (@colors and @branch_name and @branch_code and @address and @categories and @value and @action) {
203 $row_data{color} = shift @colors;
204 $row_data{branch_name} = shift @branch_name;
205 $row_data{branch_code} = shift @branch_code;
206 $row_data{address} = shift @address;
207 $row_data{categories} = shift @categories;
208 $row_data{value} = shift @value;
209 $row_data{action} = shift @action;
210 push(@loop_data, \%row_data);
214 $template->param(branches => \@loop_data);
218 sub branchcategoriestable {
219 #Needs to be implemented...
221 my $categoryinfo = getcategoryinfo();
223 foreach my $cat (@$categoryinfo) {
224 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
225 $template->param(color => $color);
226 $template->param(categoryname => $cat->{'categoryname'});
227 $template->param(categorycode => $cat->{'categorycode'});
228 $template->param(codedescription => $cat->{'codedescription'});
232 ######################################################################################################
234 # Database functions....
237 # returns a reference to an array of hashes containing branches,
239 my ($branchcode) = @_;
240 my $dbh = C4::Context->dbh;
243 my $bc = $dbh->quote($branchcode);
244 $query = "Select * from branches where branchcode = $bc";
246 else {$query = "Select * from branches";}
247 my $sth = $dbh->prepare($query);
250 while (my $data = $sth->fetchrow_hashref) {
251 my $tmp = $data->{'branchcode'}; my $brc = $dbh->quote($tmp);
252 $query = "select categorycode from branchrelations where branchcode = $brc";
253 my $nsth = $dbh->prepare($query);
256 while (my ($cat) = $nsth->fetchrow_array) {
260 $data->{'categories'} = \@cats;
261 push(@results, $data);
267 sub getcategoryinfo {
268 # returns a reference to an array of hashes containing branches,
270 my $dbh = C4::Context->dbh;
273 my $cc = $dbh->quote($catcode);
274 $query = "select * from branchcategories where categorycode = $cc";
276 $query = "Select * from branchcategories";
278 my $sth = $dbh->prepare($query);
281 while (my $data = $sth->fetchrow_hashref) {
282 push(@results, $data);
289 # sets the data from the editbranch form, and writes to the database...
291 my $dbh = C4::Context->dbh;
292 my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
294 $tmp = $data->{'branchcode'}; $query.= $dbh->quote($tmp).",";
295 $tmp = $data->{'branchname'}; $query.= $dbh->quote($tmp).",";
296 $tmp = $data->{'branchaddress1'}; $query.= $dbh->quote($tmp).",";
297 $tmp = $data->{'branchaddress2'}; $query.= $dbh->quote($tmp).",";
298 $tmp = $data->{'branchaddress3'}; $query.= $dbh->quote($tmp).",";
299 $tmp = $data->{'branchphone'}; $query.= $dbh->quote($tmp).",";
300 $tmp = $data->{'branchfax'}; $query.= $dbh->quote($tmp).",";
301 $tmp = $data->{'branchemail'}; $query.= $dbh->quote($tmp).")";
302 my $sth=$dbh->prepare($query);
305 # sort out the categories....
307 my $cats = getcategoryinfo();
308 foreach my $cat (@$cats) {
309 my $code = $cat->{'categorycode'};
310 if ($data->{$code}) {
311 push(@checkedcats, $code);
314 my $branchcode = $data->{'branchcode'};
315 my $branch = getbranchinfo($branchcode);
316 $branch = $branch->[0];
317 my $branchcats = $branch->{'categories'};
320 foreach my $bcat (@$branchcats) {
321 unless (grep {/^$bcat$/} @checkedcats) {
322 push(@removecats, $bcat);
325 foreach my $ccat (@checkedcats){
326 unless (grep {/^$ccat$/} @$branchcats) {
327 push(@addcats, $ccat);
330 # FIXME - There's already a $dbh in this scope.
331 my $dbh = C4::Context->dbh;
332 foreach my $cat (@addcats) {
333 my $query = "insert into branchrelations (branchcode, categorycode) values('$branchcode', '$cat')";
334 my $sth = $dbh->prepare($query);
338 foreach my $cat (@removecats) {
339 my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
340 my $sth = $dbh->prepare($query);
348 my ($branchcode) = @_;
349 my $query = "delete from branches where branchcode = '$branchcode'";
350 my $dbh = C4::Context->dbh;
351 my $sth=$dbh->prepare($query);
356 sub checkdatabasefor {
357 # check to see if the branchcode is being used in the database somewhere....
358 my ($branchcode) = @_;
359 my $dbh = C4::Context->dbh;
360 my $sth=$dbh->prepare("select count(*) from items where holdingbranch='$branchcode' or homebranch='$branchcode'");
362 my ($total) = $sth->fetchrow_array;
366 $message = "Branch cannot be deleted because there are $total items using that branch.";
371 print "Content-Type: text/html\n\n", $template->output;