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
32 my $linecolor1='#ffffcc';
33 my $linecolor2='white';
34 my $backgroundimage="/images/background-mem.gif";
35 my $script_name="/cgi-bin/koha/admin/branches.pl";
39 #######################################################################################
43 my $branchcode=$input->param('branchcode');
44 my $op = $input->param('op');
46 my ($template, $borrowernumber, $cookie)
47 = get_template_and_user({template_name => "parameters/branches.tmpl",
51 flagsrequired => {parameters => 1},
55 $template->param(script_name => $script_name,
56 $op => 1); # we show only the TMPL_VAR names $op
58 $template->param(script_name => $script_name,
59 else => 1); # we show only the TMPL_VAR names $op
61 $template->param(action => $script_name);
64 # If the user has pressed the "add new branch" button.
65 heading("Branches: Add Branch");
68 } elsif ($op eq 'edit') {
69 # if the user has pressed the "edit branch settings" button.
70 heading("Branches: Edit Branch");
71 $template->param(add => 1);
72 editbranchform($branchcode);
74 } elsif ($op eq 'add_validate') {
75 # confirm settings change...
76 my $params = $input->Vars;
77 unless ($params->{'branchcode'} && $params->{'branchname'}) {
78 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
80 setbranchinfo($params);
81 $template->param(else => 1);
82 default ("Branch record changed for branch: $params->{'branchname'}");
85 } elsif ($op eq 'delete') {
86 # if the user has pressed the "delete branch" button.
87 my $message = checkdatabasefor($branchcode);
89 $template->param(else => 1);
92 deleteconfirm($branchcode);
93 $template->param(delete_confirm => 1);
94 $template->param(branchcode => $branchcode);
97 } elsif ($op eq 'delete_confirmed') {
98 # actually delete branch and return to the main screen....
99 deletebranch($branchcode);
100 $template->param(else => 1);
101 default("The branch with code $branchcode has been deleted.");
104 # if no operation has been set...
110 ######################################################################################################
112 # html output functions....
117 $template->param(message => $message);
118 $template->param(action => $script_name);
126 $template->param(head => $head);
130 # prepares the edit form...
131 my ($branchcode) = @_;
134 $data = getbranchinfo($branchcode);
136 $template->param(branchcode => $data->{'branchcode'});
137 $template->param(branchname => $data->{'branchname'});
138 $template->param(branchaddress1 => $data->{'branchaddress1'});
139 $template->param(branchaddress2 => $data->{'branchaddress2'});
140 $template->param(branchaddress3 => $data->{'branchaddress3'});
141 $template->param(branchphone => $data->{'branchphone'});
142 $template->param(branchfax => $data->{'branchfax'});
143 $template->param(branchemail => $data->{'branchemail'});
145 # make the checkboxs.....
146 my $catinfo = getcategoryinfo();
148 foreach my $cat (@$catinfo) {
150 my $tmp = $cat->{'categorycode'};
151 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
152 $checked = "CHECKED";
154 $template->param(categoryname => $cat->{'categoryname'});
155 $template->param(categorycode => $cat->{'categorycode'});
156 $template->param(codedescription => $checked>$cat->{'codedescription'});
162 # message to print if the
163 my ($branchcode) = @_;
167 sub branchinfotable {
168 # makes the html for a table of branch info from reference to an array of hashs.
170 my ($branchcode) = @_;
173 $branchinfo = getbranchinfo($branchcode);
175 $branchinfo = getbranchinfo();
179 foreach my $branch (@$branchinfo) {
180 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
182 $address .= $branch->{'branchaddress1'} if ($branch->{'branchaddress1'});
183 $address .= '<br>'.$branch->{'branchaddress2'} if ($branch->{'branchaddress2'});
184 $address .= '<br>'.$branch->{'branchaddress3'} if ($branch->{'branchaddress3'});
185 $address .= '<br>ph: '.$branch->{'branchphone'} if ($branch->{'branchphone'});
186 $address .= '<br>fax: '.$branch->{'branchfax'} if ($branch->{'branchfax'});
187 $address .= '<br>email: '.$branch->{'branchemail'} if ($branch->{'branchemail'});
188 $address = '(nothing entered)' unless ($address);
190 foreach my $cat (@{$branch->{'categories'}}) {
191 my ($catinfo) = @{getcategoryinfo($cat)};
192 $categories .= $catinfo->{'categoryname'}."<br>";
194 $categories = '(no categories set)' unless ($categories);
196 my @branch_name = ();
197 my @branch_code = ();
202 push(@colors,$color);
203 push(@branch_name,$branch->{'branchname'});
204 push(@branch_code,$branch->{'branchcode'});
205 push(@address,$address);
206 push(@categories,$categories);
207 push(@value,$branch->{'branchcode'});
208 push(@action,"/cgi-bin/koha/admin/branches.pl");
209 while (@colors and @branch_name and @branch_code and @address and @categories and @value and @action) {
211 $row_data{color} = shift @colors;
212 $row_data{branch_name} = shift @branch_name;
213 $row_data{branch_code} = shift @branch_code;
214 $row_data{address} = shift @address;
215 $row_data{categories} = shift @categories;
216 $row_data{value} = shift @value;
217 $row_data{action} = shift @action;
218 push(@loop_data, \%row_data);
222 $template->param(branches => \@loop_data);
226 sub branchcategoriestable {
227 #Needs to be implemented...
229 my $categoryinfo = getcategoryinfo();
231 foreach my $cat (@$categoryinfo) {
232 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
233 $template->param(color => $color);
234 $template->param(categoryname => $cat->{'categoryname'});
235 $template->param(categorycode => $cat->{'categorycode'});
236 $template->param(codedescription => $cat->{'codedescription'});
240 ######################################################################################################
242 # Database functions....
245 # returns a reference to an array of hashes containing branches,
247 my ($branchcode) = @_;
248 my $dbh = C4::Context->dbh;
251 my $bc = $dbh->quote($branchcode);
252 $query = "Select * from branches where branchcode = $bc";
254 else {$query = "Select * from branches";}
255 my $sth = $dbh->prepare($query);
258 while (my $data = $sth->fetchrow_hashref) {
259 my $tmp = $data->{'branchcode'}; my $brc = $dbh->quote($tmp);
260 $query = "select categorycode from branchrelations where branchcode = $brc";
261 my $nsth = $dbh->prepare($query);
264 while (my ($cat) = $nsth->fetchrow_array) {
268 $data->{'categories'} = \@cats;
269 push(@results, $data);
275 sub getcategoryinfo {
276 # returns a reference to an array of hashes containing branches,
278 my $dbh = C4::Context->dbh;
281 my $cc = $dbh->quote($catcode);
282 $query = "select * from branchcategories where categorycode = $cc";
284 $query = "Select * from branchcategories";
286 my $sth = $dbh->prepare($query);
289 while (my $data = $sth->fetchrow_hashref) {
290 push(@results, $data);
297 # sets the data from the editbranch form, and writes to the database...
299 my $dbh = C4::Context->dbh;
300 my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
302 $tmp = $data->{'branchcode'}; $query.= $dbh->quote($tmp).",";
303 $tmp = $data->{'branchname'}; $query.= $dbh->quote($tmp).",";
304 $tmp = $data->{'branchaddress1'}; $query.= $dbh->quote($tmp).",";
305 $tmp = $data->{'branchaddress2'}; $query.= $dbh->quote($tmp).",";
306 $tmp = $data->{'branchaddress3'}; $query.= $dbh->quote($tmp).",";
307 $tmp = $data->{'branchphone'}; $query.= $dbh->quote($tmp).",";
308 $tmp = $data->{'branchfax'}; $query.= $dbh->quote($tmp).",";
309 $tmp = $data->{'branchemail'}; $query.= $dbh->quote($tmp).")";
310 my $sth=$dbh->prepare($query);
313 # sort out the categories....
315 my $cats = getcategoryinfo();
316 foreach my $cat (@$cats) {
317 my $code = $cat->{'categorycode'};
318 if ($data->{$code}) {
319 push(@checkedcats, $code);
322 my $branchcode = $data->{'branchcode'};
323 my $branch = getbranchinfo($branchcode);
324 $branch = $branch->[0];
325 my $branchcats = $branch->{'categories'};
328 foreach my $bcat (@$branchcats) {
329 unless (grep {/^$bcat$/} @checkedcats) {
330 push(@removecats, $bcat);
333 foreach my $ccat (@checkedcats){
334 unless (grep {/^$ccat$/} @$branchcats) {
335 push(@addcats, $ccat);
338 # FIXME - There's already a $dbh in this scope.
339 my $dbh = C4::Context->dbh;
340 foreach my $cat (@addcats) {
341 my $query = "insert into branchrelations (branchcode, categorycode) values('$branchcode', '$cat')";
342 my $sth = $dbh->prepare($query);
346 foreach my $cat (@removecats) {
347 my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
348 my $sth = $dbh->prepare($query);
356 my ($branchcode) = @_;
357 my $query = "delete from branches where branchcode = '$branchcode'";
358 my $dbh = C4::Context->dbh;
359 my $sth=$dbh->prepare($query);
364 sub checkdatabasefor {
365 # check to see if the branchcode is being used in the database somewhere....
366 my ($branchcode) = @_;
367 my $dbh = C4::Context->dbh;
368 my $sth=$dbh->prepare("select count(*) from items where holdingbranch='$branchcode' or homebranch='$branchcode'");
370 my ($total) = $sth->fetchrow_array;
374 $message = "Branch cannot be deleted because there are $total items using that branch.";
379 print $input->header(-cookie => $cookie), $template->output;