3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 FIXME: individual fields in branch address need to be exported to templates,
23 in order to fix bug 180; need to notify translators
24 FIXME: looped html (e.g., list of checkboxes) need to be properly
25 TMPL_LOOP'ized; doing this properly will fix bug 130; need to
27 FIXME: need to implement the branch categories stuff
28 FIXME: there are too many TMPL_IF's; the proper way to do it is to have
29 separate templates for each individual action; need to notify
31 FIXME: there are lots of error messages exported to the template; a lot
32 of these should be converted into exported booleans / counters etc
33 so that the error messages can be localized; need to notify translators
35 Finlay working on this file from 26-03-2002
36 Reorganising this branches admin page.....
50 my $script_name = "/cgi-bin/koha/admin/branches.pl";
52 ################################################################################
55 my $branchcode = $input->param('branchcode');
56 my $branchname = $input->param('branchname');
57 my $categorycode = $input->param('categorycode');
58 my $op = $input->param('op') || '';
60 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
62 template_name
=> "admin/branches.tmpl",
66 flagsrequired
=> { parameters
=> 1},
71 script_name
=> $script_name,
72 action
=> $script_name,
74 $template->param( ($op || 'else') => 1 );
78 # If the user has pressed the "add new branch" button.
79 $template->param( 'heading_branches_add_branch_p' => 1 );
80 editbranchform
($branchcode,$template);
83 elsif ( $op eq 'edit' ) {
85 # if the user has pressed the "edit branch settings" button.
86 $template->param( 'heading_branches_add_branch_p' => 0,
88 editbranchform
($branchcode,$template);
90 elsif ( $op eq 'add_validate' ) {
92 # confirm settings change...
93 my $params = $input->Vars;
94 unless ( $params->{'branchcode'} && $params->{'branchname'} ) {
95 $template->param( else => 1 );
96 default("MESSAGE1",$template);
100 if ($params->{add
}) {
102 C4
::Context
->dbh->selectrow_array("SELECT count(*) FROM branches WHERE branchcode = ?", {}, $branchcode);
105 _branch_to_template
($params, $template); # preserve most (FIXME) of user's input
106 $template->param( 'heading_branches_add_branch_p' => 1, 'add' => 1, 'ERROR1' => 1 );
110 my $error = ModBranch
($params); # FIXME: causes warnings to log on duplicate branchcode
111 # if error saving, stay on edit and rise error
113 # copy input parameters back to form
114 # FIXME - doing this doesn't preserve any branch group selections, but good enough for now
115 editbranchform
($branchcode,$template);
116 $template->param( 'heading_branches_add_branch_p' => 1, 'add' => 1, "ERROR$error" => 1 );
118 $template->param( else => 1);
119 default("MESSAGE2",$template);
124 elsif ( $op eq 'delete' ) {
125 # if the user has pressed the "delete branch" button.
127 # check to see if the branchcode is being used in the database somewhere....
128 my $dbh = C4
::Context
->dbh;
129 my $sthitems = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
130 my $sthborrowers = $dbh->prepare("select count(*) from borrowers where branchcode=?");
131 $sthitems->execute( $branchcode, $branchcode );
132 $sthborrowers->execute( $branchcode );
133 my ($totalitems) = $sthitems->fetchrow_array;
134 my ($totalborrowers) = $sthborrowers->fetchrow_array;
135 if ($totalitems && !$totalborrowers) {
136 $template->param( else => 1 );
137 default("MESSAGE10", $template);
139 elsif (!$totalitems && $totalborrowers){
140 $template->param( else => 1 );
141 default("MESSAGE11", $template);
143 elsif ($totalitems && $totalborrowers){
144 $template->param( else => 1 );
145 default("MESSAGE7", $template);
148 $template->param( delete_confirm
=> 1 );
149 $template->param( branchname
=> $branchname );
150 $template->param( branchcode
=> $branchcode );
153 elsif ( $op eq 'delete_confirmed' ) {
155 # actually delete branch and return to the main screen....
156 DelBranch
($branchcode);
157 $template->param( else => 1 );
158 default("MESSAGE3",$template);
160 elsif ( $op eq 'editcategory' ) {
162 # If the user has pressed the "add new category" or "modify" buttons.
163 $template->param( 'heading_branches_edit_category_p' => 1 );
164 editcatform
($categorycode,$template);
166 elsif ( $op eq 'addcategory_validate' ) {
168 $template->param( else => 1 );
169 # confirm settings change...
170 my $params = $input->Vars;
171 unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
172 default("MESSAGE4",$template);
174 elsif ($input->param('add')){
175 # doing an add must check the code is unique
176 if (CheckCategoryUnique
($input->param('categorycode'))){
177 ModBranchCategoryInfo
($params);
178 default("MESSAGE5",$template);
181 default("MESSAGE9",$template);
185 ModBranchCategoryInfo
($params);
186 default("MESSAGE5",$template);
189 elsif ( $op eq 'delete_category' ) {
191 # if the user has pressed the "delete branch" button.
192 if ( CheckBranchCategorycode
($categorycode) ) {
193 $template->param( else => 1 );
194 default( 'MESSAGE8', $template );
196 $template->param( delete_category
=> 1 );
197 $template->param( categorycode
=> $categorycode );
200 elsif ( $op eq 'categorydelete_confirmed' ) {
202 # actually delete branch and return to the main screen....
203 DelBranchCategory
($categorycode);
204 $template->param( else => 1 );
205 default("MESSAGE6",$template);
209 # if no operation has been set...
210 default("",$template);
213 ################################################################################
215 # html output functions....
218 my $message = shift || '';
219 my $innertemplate = shift or return;
220 $innertemplate->param($message => 1) if $message;
221 $innertemplate->param(
222 'heading_branches_p' => 1,
224 branchinfotable
("",$innertemplate);
228 my ($branchcode,$innertemplate) = @_;
229 # initiate the scrolling-list to select the printers
230 my $printers = GetPrinters
();
236 $data = GetBranchInfo
($branchcode);
239 # get the old printer of the branch
240 $oldprinter = $data->{'branchprinter'} || '';
241 _branch_to_template
($data, $innertemplate);
244 foreach my $thisprinter ( keys %$printers ) {
246 value
=> $thisprinter,
247 selected
=> ( $oldprinter eq $printers->{$thisprinter} ),
248 branchprinter
=> $printers->{$thisprinter}->{'printqueue'},
252 $innertemplate->param( printerloop
=> \
@printerloop );
253 # make the checkboxes.....
255 # We export a "categoryloop" array to the template, each element of which
256 # contains separate 'categoryname', 'categorycode', 'codedescription', and
257 # 'checked' fields. The $checked field is either empty or 1'
259 my $catinfo = GetBranchCategory
();
260 my @categoryloop = ();
261 foreach my $cat (@
$catinfo) {
263 my $tmp = quotemeta( $cat->{'categorycode'} );
264 if ( grep { /^$tmp$/ } @
{ $data->{'categories'} } ) {
267 push @categoryloop, {
268 categoryname
=> $cat->{'categoryname'},
269 categorycode
=> $cat->{'categorycode'},
270 categorytype
=> $cat->{'categorytype'},
271 codedescription
=> $cat->{'codedescription'},
275 $innertemplate->param( categoryloop
=> \
@categoryloop );
277 for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
278 $innertemplate->param(
279 $obsolete => 'Your template is out of date (bug 130)' );
285 # prepares the edit form...
286 my ($categorycode,$innertemplate) = @_;
287 # warn "cat : $categorycode";
291 my $data = GetBranchCategory
($categorycode);
293 $innertemplate->param(
294 categorycode
=> $data->{'categorycode'},
295 categoryname
=> $data->{'categoryname'},
296 codedescription
=> $data->{'codedescription'},
299 for my $ctype (GetCategoryTypes
()) {
300 push @cats , { type
=> $ctype , selected
=> ($data->{'categorytype'} and $data->{'categorytype'} eq $ctype) };
302 $innertemplate->param(categorytype
=> \
@cats);
305 sub branchinfotable
{
307 # makes the html for a table of branch info from reference to an array of hashs.
309 my ($branchcode,$innertemplate) = @_;
310 my $branchinfo = $branchcode ? GetBranchInfo
($branchcode) : GetBranchInfo
();
312 foreach my $branch (@
$branchinfo) {
314 # We export the following fields to the template. These are not
315 # pre-composed as a single "address" field because the template
316 # might (and should) escape what is exported here. (See bug 180)
318 # - branch_name (Note: not "branchname")
319 # - branch_code (Note: not "branchcode")
320 # - address (containing a static error message)
323 # - branchaddress3 | comprising the old "address" field
331 # - address-empty-p (1 if no address information, 0 otherwise)
332 # - categories (containing a static error message)
333 # - category_list (loop containing "categoryname")
334 # - no-categories-p (1 if no categories set, 0 otherwise)
339 # Handle address fields separately
340 my $address_empty_p = 1;
342 'branchaddress1', 'branchaddress2',
343 'branchaddress3', 'branchzip',
344 'branchcity', 'branchstate', 'branchcountry',
345 'branchphone', 'branchfax',
346 'branchemail', 'branchurl',
347 'branchip', 'branchprinter', 'branchnotes'
350 $row{$field} = $branch->{$field};
351 $address_empty_p = 0 if ( $branch->{$field} );
353 $row{'address-empty-p'} = $address_empty_p;
356 my $no_categories_p = 1;
358 foreach my $cat ( @
{ $branch->{'categories'} } ) {
359 my ($catinfo) = @
{ GetBranchCategory
($cat) };
360 push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
361 $no_categories_p = 0;
364 $row{'category_list'} = \
@categories;
365 $row{'no-categories-p'} = $no_categories_p;
366 $row{'branch_name'} = $branch->{'branchname'};
367 $row{'branch_code'} = $branch->{'branchcode'};
368 $row{'value'} = $branch->{'branchcode'};
370 push @loop_data, \
%row;
372 my @branchcategories = ();
373 for my $ctype ( GetCategoryTypes
() ) {
374 my $catinfo = GetBranchCategories
(undef,$ctype);
376 foreach my $cat (@
$catinfo) {
378 categoryname
=> $cat->{'categoryname'},
379 categorycode
=> $cat->{'categorycode'},
380 codedescription
=> $cat->{'codedescription'},
381 categorytype
=> $cat->{'categorytype'},
384 push @branchcategories, { categorytype
=> $ctype , $ctype => 1 , catloop
=> ( @categories ? \
@categories : undef) };
386 $innertemplate->param(
387 branches
=> \
@loop_data,
388 branchcategories
=> \
@branchcategories
393 sub _branch_to_template
{
394 my ($data, $template) = @_;
396 branchcode
=> $data->{'branchcode'},
397 branch_name
=> $data->{'branchname'},
398 branchaddress1
=> $data->{'branchaddress1'},
399 branchaddress2
=> $data->{'branchaddress2'},
400 branchaddress3
=> $data->{'branchaddress3'},
401 branchzip
=> $data->{'branchzip'},
402 branchcity
=> $data->{'branchcity'},
403 branchstate
=> $data->{'branchstate'},
404 branchcountry
=> $data->{'branchcountry'},
405 branchphone
=> $data->{'branchphone'},
406 branchfax
=> $data->{'branchfax'},
407 branchemail
=> $data->{'branchemail'},
408 branchurl
=> $data->{'branchurl'},
409 branchip
=> $data->{'branchip'},
410 branchnotes
=> $data->{'branchnotes'},
414 output_html_with_http_headers
$input, $cookie, $template->output;