2 # NOTE: Use standard 8-space tabs for this file (indents are 4 spaces)
4 #require '/u/acli/lib/cvs.pl';#DEBUG
5 #open(DEBUG,'>/tmp/koha.debug');
7 # FIXME: individual fields in branch address need to be exported to templates,
8 # in order to fix bug 180; need to notify translators
9 # FIXME: looped html (e.g., list of checkboxes) need to be properly
10 # TMPL_LOOP'ized; doing this properly will fix bug 130; need to
12 # FIXME: need to implement the branch categories stuff
13 # FIXME: heading() need to be moved to templates, need to notify translators
14 # FIXME: there are too many TMPL_IF's; the proper way to do it is to have
15 # separate templates for each individual action; need to notify
17 # FIXME: there are lots of error messages exported to the template; a lot
18 # of these should be converted into exported booleans / counters etc
19 # so that the error messages can be localized; need to notify translators
21 # Finlay working on this file from 26-03-2002
22 # Reorganising this branches admin page.....
25 # Copyright 2000-2002 Katipo Communications
27 # This file is part of Koha.
29 # Koha is free software; you can redistribute it and/or modify it under the
30 # terms of the GNU General Public License as published by the Free Software
31 # Foundation; either version 2 of the License, or (at your option) any later
34 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
35 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
36 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
38 # You should have received a copy of the GNU General Public License along with
39 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
40 # Suite 330, Boston, MA 02111-1307 USA
47 use C4::Interface::CGI::Output;
51 my $linecolor1='#ffffcc';
52 my $linecolor2='white';
53 my $backgroundimage="/images/background-mem.gif";
54 my $script_name="/cgi-bin/koha/admin/branches.pl";
58 #######################################################################################
61 my $branchcode=$input->param('branchcode');
62 my $op = $input->param('op');
64 my ($template, $borrowernumber, $cookie)
65 = get_template_and_user({template_name => "parameters/branches.tmpl",
69 flagsrequired => {parameters => 1},
73 $template->param(script_name => $script_name,
74 $op => 1); # we show only the TMPL_VAR names $op
76 $template->param(script_name => $script_name,
77 else => 1); # we show only the TMPL_VAR names $op
79 $template->param(action => $script_name);
82 # If the user has pressed the "add new branch" button.
83 heading("Branches: Add Branch");
86 } elsif ($op eq 'edit') {
87 # if the user has pressed the "edit branch settings" button.
88 heading("Branches: Edit Branch");
89 $template->param(add => 1);
90 editbranchform($branchcode);
92 } elsif ($op eq 'add_validate') {
93 # confirm settings change...
94 my $params = $input->Vars;
95 unless ($params->{'branchcode'} && $params->{'branchname'}) {
96 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
98 setbranchinfo($params);
99 $template->param(else => 1);
100 default ("Branch record changed for branch: $params->{'branchname'}");
103 } elsif ($op eq 'delete') {
104 # if the user has pressed the "delete branch" button.
105 my $message = checkdatabasefor($branchcode);
107 $template->param(else => 1);
110 deleteconfirm($branchcode);
111 $template->param(delete_confirm => 1);
112 $template->param(branchcode => $branchcode);
115 } elsif ($op eq 'delete_confirmed') {
116 # actually delete branch and return to the main screen....
117 deletebranch($branchcode);
118 $template->param(else => 1);
119 default("The branch with code $branchcode has been deleted.");
121 } elsif ($op eq 'add_cat') {
122 # If the user has pressed the "add new category" button.
123 heading("Branches: Add Branch");
127 # if no operation has been set...
133 ######################################################################################################
135 # html output functions....
140 $template->param(message => $message);
141 $template->param(action => $script_name);
147 # FIXME: this function should not exist; otherwise headings are untranslatable
150 $template->param(head => $head);
154 # prepares the edit form...
155 my ($branchcode) = @_;
158 $data = getbranchinfo($branchcode);
160 $template->param(branchcode => $data->{'branchcode'});
161 $template->param(branchname => $data->{'branchname'});
162 $template->param(branchaddress1 => $data->{'branchaddress1'});
163 $template->param(branchaddress2 => $data->{'branchaddress2'});
164 $template->param(branchaddress3 => $data->{'branchaddress3'});
165 $template->param(branchphone => $data->{'branchphone'});
166 $template->param(branchfax => $data->{'branchfax'});
167 $template->param(branchemail => $data->{'branchemail'});
170 # make the checkboxs.....
172 # We export a "categoryloop" array to the template, each element of which
173 # contains separate 'categoryname', 'categorycode', 'codedescription', and
174 # 'checked' fields. The $checked field is either '' or 'checked'
177 my $catinfo = getcategoryinfo();
179 # print DEBUG "catinfo=".cvs($catinfo)."\n";
180 my @categoryloop = ();
181 foreach my $cat (@$catinfo) {
183 my $tmp = quotemeta($cat->{'categorycode'});
184 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
185 $checked = "CHECKED";
187 push @categoryloop, {
188 categoryname => $cat->{'categoryname'},
189 categorycode => $cat->{'categorycode'},
190 codedescription => $cat->{'codedescription'},
194 $template->param(categoryloop => \@categoryloop);
196 # {{{ Leave this here until bug 130 is completely resolved in the templates
197 for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
198 $template->param($obsolete => 'Your template is out of date (bug 130)');
204 # message to print if the
205 my ($branchcode) = @_;
209 sub branchinfotable {
210 # makes the html for a table of branch info from reference to an array of hashs.
212 my ($branchcode) = @_;
215 $branchinfo = getbranchinfo($branchcode);
217 $branchinfo = getbranchinfo();
221 foreach my $branch (@$branchinfo) {
222 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
224 # We export the following fields to the template. These are not
225 # pre-composed as a single "address" field because the template
226 # might (and should) escape what is exported here. (See bug 180)
229 # - branch_name (Note: not "branchname")
230 # - branch_code (Note: not "branchcode")
231 # - address (containing a static error message)
234 # - branchaddress3 | comprising the old "address" field
238 # - address-empty-p (1 if no address information, 0 otherwise)
239 # - categories (containing a static error message)
240 # - category_list (loop containing "categoryname")
241 # - no-categories-p (1 if no categories set, 0 otherwise)
247 # Handle address fields separately
248 my $address_empty_p = 1;
249 for my $field ('branchaddress1', 'branchaddress2', 'branchaddress3',
250 'branchphone', 'branchfax', 'branchemail') {
252 $row{$field} = $branch->{$field};
253 $address_empty_p = 0;
255 $row{'address-empty-p'} = $address_empty_p;
256 # {{{ Leave this here until bug 180 is completely resolved in templates
257 $row{'address'} = 'Your template is out of date (see bug 180)';
261 my $no_categories_p = 1;
263 foreach my $cat (@{$branch->{'categories'}}) {
264 my ($catinfo) = @{getcategoryinfo($cat)};
265 push @categories, {'categoryname' => $catinfo->{'categoryname'}};
266 $no_categories_p = 0;
268 # {{{ Leave this here until bug 180 is completely resolved in templates
269 $row{'categories'} = 'Your template is out of date (see bug 180)';
271 $row{'category_list'} = \@categories;
272 $row{'no-categories-p'} = $no_categories_p;
274 # Handle all other fields
275 $row{'branch_name'} = $branch->{'branchname'};
276 $row{'branch_code'} = $branch->{'branchcode'};
277 $row{'color'} = $color;
278 $row{'value'} = $branch->{'branchcode'};
279 $row{'action'} = '/cgi-bin/koha/admin/branches.pl';
281 push @loop_data, { %row };
283 $template->param(branches => \@loop_data);
287 # FIXME logic seems wrong
288 sub branchcategoriestable {
289 #Needs to be implemented...
291 my $categoryinfo = getcategoryinfo();
293 foreach my $cat (@$categoryinfo) {
294 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
295 $template->param(color => $color);
296 $template->param(categoryname => $cat->{'categoryname'});
297 $template->param(categorycode => $cat->{'categorycode'});
298 $template->param(codedescription => $cat->{'codedescription'});
302 ######################################################################################################
304 # Database functions....
307 # returns a reference to an array of hashes containing branches,
309 my ($branchcode) = @_;
310 my $dbh = C4::Context->dbh;
311 my ($query, @query_args);
313 $query = "Select * from branches where branchcode = ?";
314 @query_args = ($branchcode);
316 $query = "Select * from branches";
318 my $sth = $dbh->prepare($query);
319 $sth->execute(@query_args);
321 while (my $data = $sth->fetchrow_hashref) {
322 $query = "select categorycode from branchrelations where branchcode = ?";
323 my $nsth = $dbh->prepare($query);
324 $nsth->execute($data->{'branchcode'});;
326 while (my ($cat) = $nsth->fetchrow_array) {
330 $data->{'categories'} = \@cats;
331 push(@results, $data);
337 # FIXME This doesn't belong here; it should be moved into a module
338 sub getcategoryinfo {
339 # returns a reference to an array of hashes containing branches,
341 my $dbh = C4::Context->dbh;
342 my ($query, @query_args);
343 # print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
345 $query = "select * from branchcategories where categorycode = ?";
346 @query_args = ($catcode);
348 $query = "Select * from branchcategories";
350 # print DEBUG "getcategoryinfo: query=".cvs($query)."\n";
351 my $sth = $dbh->prepare($query);
352 $sth->execute(@query_args);
354 while (my $data = $sth->fetchrow_hashref) {
355 push(@results, $data);
358 # print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
362 # FIXME This doesn't belong here; it should be moved into a module
364 # sets the data from the editbranch form, and writes to the database...
366 my $dbh = C4::Context->dbh;
367 my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (?,?,?,?,?,?,?,?)";
368 my $sth=$dbh->prepare($query);
369 $sth->execute(uc($data->{'branchcode'}), $data->{'branchname'},
370 $data->{'branchaddress1'}, $data->{'branchaddress2'},
371 $data->{'branchaddress3'}, $data->{'branchphone'},
372 $data->{'branchfax'}, $data->{'branchemail'});
375 # sort out the categories....
377 my $cats = getcategoryinfo();
378 foreach my $cat (@$cats) {
379 my $code = $cat->{'categorycode'};
380 if ($data->{$code}) {
381 push(@checkedcats, $code);
384 my $branchcode =uc($data->{'branchcode'});
385 my $branch = getbranchinfo($branchcode);
386 $branch = $branch->[0];
387 my $branchcats = $branch->{'categories'};
390 foreach my $bcat (@$branchcats) {
391 unless (grep {/^$bcat$/} @checkedcats) {
392 push(@removecats, $bcat);
395 foreach my $ccat (@checkedcats){
396 unless (grep {/^$ccat$/} @$branchcats) {
397 push(@addcats, $ccat);
400 # FIXME - There's already a $dbh in this scope.
401 my $dbh = C4::Context->dbh;
402 foreach my $cat (@addcats) {
403 my $query = "insert into branchrelations (branchcode, categorycode) values(?, ?)";
404 my $sth = $dbh->prepare($query);
405 $sth->execute($branchcode, $cat);
408 foreach my $cat (@removecats) {
409 my $query = "delete from branchrelations where branchcode=? and categorycode=?";
410 my $sth = $dbh->prepare($query);
411 $sth->execute($branchcode, $cat);
418 my ($branchcode) = @_;
419 my $query = "delete from branches where branchcode = ?";
420 my $dbh = C4::Context->dbh;
421 my $sth=$dbh->prepare($query);
422 $sth->execute($branchcode);
426 sub checkdatabasefor {
427 # check to see if the branchcode is being used in the database somewhere....
428 my ($branchcode) = @_;
429 my $dbh = C4::Context->dbh;
430 my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
431 $sth->execute($branchcode, $branchcode);
432 my ($total) = $sth->fetchrow_array;
436 # FIXME: need to be replaced by an exported boolean parameter
437 $message = "Branch cannot be deleted because there are $total items using that branch.";
442 output_html_with_http_headers $input, $cookie, $template->output;