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
29 use C4::Interface::CGI::Output;
33 my $linecolor1='#ffffcc';
34 my $linecolor2='white';
35 my $backgroundimage="/images/background-mem.gif";
36 my $script_name="/cgi-bin/koha/admin/branches.pl";
40 #######################################################################################
44 my $branchcode=$input->param('branchcode');
45 my $op = $input->param('op');
47 my ($template, $borrowernumber, $cookie)
48 = get_template_and_user({template_name => "parameters/branches.tmpl",
52 flagsrequired => {parameters => 1},
56 $template->param(script_name => $script_name,
57 $op => 1); # we show only the TMPL_VAR names $op
59 $template->param(script_name => $script_name,
60 else => 1); # we show only the TMPL_VAR names $op
62 $template->param(action => $script_name);
65 # If the user has pressed the "add new branch" button.
66 heading("Branches: Add Branch");
69 } elsif ($op eq 'edit') {
70 # if the user has pressed the "edit branch settings" button.
71 heading("Branches: Edit Branch");
72 $template->param(add => 1);
73 editbranchform($branchcode);
75 } elsif ($op eq 'add_validate') {
76 # confirm settings change...
77 my $params = $input->Vars;
78 unless ($params->{'branchcode'} && $params->{'branchname'}) {
79 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
81 setbranchinfo($params);
82 $template->param(else => 1);
83 default ("Branch record changed for branch: $params->{'branchname'}");
86 } elsif ($op eq 'delete') {
87 # if the user has pressed the "delete branch" button.
88 my $message = checkdatabasefor($branchcode);
90 $template->param(else => 1);
93 deleteconfirm($branchcode);
94 $template->param(delete_confirm => 1);
95 $template->param(branchcode => $branchcode);
98 } elsif ($op eq 'delete_confirmed') {
99 # actually delete branch and return to the main screen....
100 deletebranch($branchcode);
101 $template->param(else => 1);
102 default("The branch with code $branchcode has been deleted.");
105 # if no operation has been set...
111 ######################################################################################################
113 # html output functions....
118 $template->param(message => $message);
119 $template->param(action => $script_name);
127 $template->param(head => $head);
131 # prepares the edit form...
132 my ($branchcode) = @_;
135 $data = getbranchinfo($branchcode);
137 $template->param(branchcode => $data->{'branchcode'});
138 $template->param(branchname => $data->{'branchname'});
139 $template->param(branchaddress1 => $data->{'branchaddress1'});
140 $template->param(branchaddress2 => $data->{'branchaddress2'});
141 $template->param(branchaddress3 => $data->{'branchaddress3'});
142 $template->param(branchphone => $data->{'branchphone'});
143 $template->param(branchfax => $data->{'branchfax'});
144 $template->param(branchemail => $data->{'branchemail'});
146 # make the checkboxs.....
147 my $catinfo = getcategoryinfo();
149 foreach my $cat (@$catinfo) {
151 my $tmp = $cat->{'categorycode'};
152 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
153 $checked = "CHECKED";
155 $template->param(categoryname => $cat->{'categoryname'});
156 $template->param(categorycode => $cat->{'categorycode'});
157 $template->param(codedescription => $checked>$cat->{'codedescription'});
163 # message to print if the
164 my ($branchcode) = @_;
168 sub branchinfotable {
169 # makes the html for a table of branch info from reference to an array of hashs.
171 my ($branchcode) = @_;
174 $branchinfo = getbranchinfo($branchcode);
176 $branchinfo = getbranchinfo();
180 foreach my $branch (@$branchinfo) {
181 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
183 $address .= $branch->{'branchaddress1'} if ($branch->{'branchaddress1'});
184 $address .= '<br>'.$branch->{'branchaddress2'} if ($branch->{'branchaddress2'});
185 $address .= '<br>'.$branch->{'branchaddress3'} if ($branch->{'branchaddress3'});
186 $address .= '<br>ph: '.$branch->{'branchphone'} if ($branch->{'branchphone'});
187 $address .= '<br>fax: '.$branch->{'branchfax'} if ($branch->{'branchfax'});
188 $address .= '<br>email: '.$branch->{'branchemail'} if ($branch->{'branchemail'});
189 $address = '(nothing entered)' unless ($address);
191 foreach my $cat (@{$branch->{'categories'}}) {
192 my ($catinfo) = @{getcategoryinfo($cat)};
193 $categories .= $catinfo->{'categoryname'}."<br>";
195 $categories = '(no categories set)' unless ($categories);
197 my @branch_name = ();
198 my @branch_code = ();
203 push(@colors,$color);
204 push(@branch_name,$branch->{'branchname'});
205 push(@branch_code,$branch->{'branchcode'});
206 push(@address,$address);
207 push(@categories,$categories);
208 push(@value,$branch->{'branchcode'});
209 push(@action,"/cgi-bin/koha/admin/branches.pl");
210 while (@colors and @branch_name and @branch_code and @address and @categories and @value and @action) {
212 $row_data{color} = shift @colors;
213 $row_data{branch_name} = shift @branch_name;
214 $row_data{branch_code} = shift @branch_code;
215 $row_data{address} = shift @address;
216 $row_data{categories} = shift @categories;
217 $row_data{value} = shift @value;
218 $row_data{action} = shift @action;
219 push(@loop_data, \%row_data);
223 $template->param(branches => \@loop_data);
227 sub branchcategoriestable {
228 #Needs to be implemented...
230 my $categoryinfo = getcategoryinfo();
232 foreach my $cat (@$categoryinfo) {
233 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
234 $template->param(color => $color);
235 $template->param(categoryname => $cat->{'categoryname'});
236 $template->param(categorycode => $cat->{'categorycode'});
237 $template->param(codedescription => $cat->{'codedescription'});
241 ######################################################################################################
243 # Database functions....
246 # returns a reference to an array of hashes containing branches,
248 my ($branchcode) = @_;
249 my $dbh = C4::Context->dbh;
252 my $bc = $dbh->quote($branchcode);
253 $query = "Select * from branches where branchcode = $bc";
255 else {$query = "Select * from branches";}
256 my $sth = $dbh->prepare($query);
259 while (my $data = $sth->fetchrow_hashref) {
260 my $tmp = $data->{'branchcode'}; my $brc = $dbh->quote($tmp);
261 $query = "select categorycode from branchrelations where branchcode = $brc";
262 my $nsth = $dbh->prepare($query);
265 while (my ($cat) = $nsth->fetchrow_array) {
269 $data->{'categories'} = \@cats;
270 push(@results, $data);
276 sub getcategoryinfo {
277 # returns a reference to an array of hashes containing branches,
279 my $dbh = C4::Context->dbh;
282 my $cc = $dbh->quote($catcode);
283 $query = "select * from branchcategories where categorycode = $cc";
285 $query = "Select * from branchcategories";
287 my $sth = $dbh->prepare($query);
290 while (my $data = $sth->fetchrow_hashref) {
291 push(@results, $data);
298 # sets the data from the editbranch form, and writes to the database...
300 my $dbh = C4::Context->dbh;
301 my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
303 $tmp = $data->{'branchcode'}; $query.= $dbh->quote($tmp).",";
304 $tmp = $data->{'branchname'}; $query.= $dbh->quote($tmp).",";
305 $tmp = $data->{'branchaddress1'}; $query.= $dbh->quote($tmp).",";
306 $tmp = $data->{'branchaddress2'}; $query.= $dbh->quote($tmp).",";
307 $tmp = $data->{'branchaddress3'}; $query.= $dbh->quote($tmp).",";
308 $tmp = $data->{'branchphone'}; $query.= $dbh->quote($tmp).",";
309 $tmp = $data->{'branchfax'}; $query.= $dbh->quote($tmp).",";
310 $tmp = $data->{'branchemail'}; $query.= $dbh->quote($tmp).")";
311 my $sth=$dbh->prepare($query);
314 # sort out the categories....
316 my $cats = getcategoryinfo();
317 foreach my $cat (@$cats) {
318 my $code = $cat->{'categorycode'};
319 if ($data->{$code}) {
320 push(@checkedcats, $code);
323 my $branchcode = $data->{'branchcode'};
324 my $branch = getbranchinfo($branchcode);
325 $branch = $branch->[0];
326 my $branchcats = $branch->{'categories'};
329 foreach my $bcat (@$branchcats) {
330 unless (grep {/^$bcat$/} @checkedcats) {
331 push(@removecats, $bcat);
334 foreach my $ccat (@checkedcats){
335 unless (grep {/^$ccat$/} @$branchcats) {
336 push(@addcats, $ccat);
339 # FIXME - There's already a $dbh in this scope.
340 my $dbh = C4::Context->dbh;
341 foreach my $cat (@addcats) {
342 my $query = "insert into branchrelations (branchcode, categorycode) values('$branchcode', '$cat')";
343 my $sth = $dbh->prepare($query);
347 foreach my $cat (@removecats) {
348 my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
349 my $sth = $dbh->prepare($query);
357 my ($branchcode) = @_;
358 my $query = "delete from branches where branchcode = '$branchcode'";
359 my $dbh = C4::Context->dbh;
360 my $sth=$dbh->prepare($query);
365 sub checkdatabasefor {
366 # check to see if the branchcode is being used in the database somewhere....
367 my ($branchcode) = @_;
368 my $dbh = C4::Context->dbh;
369 my $sth=$dbh->prepare("select count(*) from items where holdingbranch='$branchcode' or homebranch='$branchcode'");
371 my ($total) = $sth->fetchrow_array;
375 $message = "Branch cannot be deleted because there are $total items using that branch.";
380 output_html_with_http_headers $input, $cookie, $template->output;