followup da49fc77df9 enh 3736
[koha.git] / admin / clone-rules.pl
blob7daa36905687fa4f687e8a930cddfccb21eb8dde
1 #!/usr/bin/perl
2 # vim: et ts=4 sw=4
3 # Copyright BibLibre
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
10 # version.
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
21 # This script clones issuing rules from a library to another
22 # parameters :
23 # - frombranch : the branch we want to clone issuing rules from
24 # - tobranch : the branch we want to clone issuing rules to
26 # The script can be called with one of the parameters, both or none
28 use strict;
29 use CGI;
30 use C4::Context;
31 use C4::Output;
32 use C4::Auth;
33 use C4::Koha;
34 use C4::Debug;
35 use C4::Branch; # GetBranches
37 my $input = new CGI;
38 my $dbh = C4::Context->dbh;
40 my ($template, $loggedinuser, $cookie)
41 = get_template_and_user({template_name => "admin/clone-rules.tmpl",
42 query => $input,
43 type => "intranet",
44 authnotrequired => 0,
45 flagsrequired => {parameters => 1},
46 debug => 1,
47 });
49 my $frombranch = $input->param("frombranch");
50 my $tobranch = $input->param("tobranch");
51 my $branchloop = GetBranchesLoop;
53 $template->param(frombranch => $frombranch) if ($frombranch);
54 $template->param(frombranchname => GetBranchName($frombranch)) if ($frombranch);
55 $template->param(tobranch => $tobranch) if ($tobranch);
56 $template->param(tobranchname => GetBranchName($tobranch)) if ($tobranch);
58 $template->param(branchloop => $branchloop);
60 if ($frombranch && $tobranch) {
62 my $error;
64 # First, we create a temporary table with the rules we want to clone
65 my $query = "CREATE TEMPORARY TABLE tmpissuingrules ENGINE=memory SELECT * FROM issuingrules WHERE branchcode=?";
66 my $sth = $dbh->prepare($query);
67 my $res = $sth->execute($frombranch);
68 $error = 1 unless ($res);
70 if (!$error) {
71 # We modify these rules according to the new branchcode
72 $query = "UPDATE tmpissuingrules SET branchcode=? WHERE branchcode=?";
73 $sth = $dbh->prepare($query);
74 $res = $sth->execute($tobranch, $frombranch);
75 $error = 1 unless ($res);
78 if (!$error) {
79 # We delete the rules for the existing branchode
80 $query = "DELETE FROM issuingrules WHERE branchcode=?";
81 $sth = $dbh->prepare($query);
82 $res = $sth->execute($tobranch);
83 $error = 1 unless ($res);
87 if (!$error) {
88 # We insert the new rules from our temporary table
89 $query = "INSERT INTO issuingrules SELECT * FROM tmpissuingrules WHERE branchcode=?";
90 $sth = $dbh->prepare($query);
91 $res = $sth->execute($tobranch);
92 $error = 1 unless ($res);
95 # Finally, we delete our temporary table
96 $query = "DROP TABLE tmpissuingrules";
97 $sth = $dbh->prepare($query);
98 $res = $sth->execute();
100 $template->param(result => "1");
101 $template->param(error => $error);
106 output_html_with_http_headers $input, $cookie, $template->output;