Fix unimarc 010 field plugin
[koha.git] / admin / clone-rules.pl
1 #!/usr/bin/perl
2 # vim: et ts=4 sw=4
3 # Copyright BibLibre 
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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
19
20
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
25 #
26 # The script can be called with one of the parameters, both or none
27
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
36
37 my $input = new CGI;
38 my $dbh = C4::Context->dbh;
39
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                             });
48
49 my $frombranch = $input->param("frombranch");
50 my $tobranch   = $input->param("tobranch");
51 my $branchloop = GetBranchesLoop;
52
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);
57
58 $template->param(branchloop => $branchloop);
59
60 if ($frombranch && $tobranch) {
61
62     my $error;  
63
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);
69
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);
76     }
77
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);
84     }
85
86
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);
93     }
94
95     # Finally, we delete our temporary table
96     $query = "DROP TABLE tmpissuingrules";
97     $sth = $dbh->prepare($query);
98     $res = $sth->execute();
99
100     $template->param(result => "1");
101     $template->param(error  => $error);
102 }
103
104
105
106 output_html_with_http_headers $input, $cookie, $template->output;
107