Merged with arensb-context branch: use C4::Context->dbh instead of
[koha.git] / admin / marctagstructure.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use CGI;
23 use C4::Context;
24 use C4::Output;
25 use C4::Search;
26
27 sub StringSearch  {
28         my ($env,$searchstring,$type)=@_;
29         my $dbh = C4::Context->dbh;
30         $searchstring=~ s/\'/\\\'/g;
31         my @data=split(' ',$searchstring);
32         my $count=@data;
33         my $query="Select tagfield,liblibrarian,libopac,repeatable,mandatory from marc_tag_structure where (tagfield like \"$data[0]%\") order by tagfield";
34         my $sth=$dbh->prepare($query);
35         $sth->execute;
36         my @results;
37         my $cnt=0;
38         while (my $data=$sth->fetchrow_hashref){
39         push(@results,$data);
40         $cnt ++;
41         }
42         #  $sth->execute;
43         $sth->finish;
44         return ($cnt,\@results);
45 }
46
47 my $input = new CGI;
48 my $searchfield=$input->param('searchfield');
49 my $pkfield="tagfield";
50 my $reqsel="select tagfield,liblibrarian,libopac,repeatable,mandatory from marc_tag_structure where $pkfield='$searchfield'";
51 my $reqdel="delete from marc_tag_structure where $pkfield='$searchfield'";
52 my $offset=$input->param('offset');
53 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
54
55 my $pagesize=20;
56 my $op = $input->param('op');
57 $searchfield=~ s/\,//g;
58 print $input->header;
59
60 #start the page and read in includes
61 print startpage();
62 print startmenu('admin');
63
64 ################## ADD_FORM ##################################
65 # called by default. Used to create form to add or  modify a record
66 if ($op eq 'add_form') {
67         #---- if primkey exists, it's a modify action, so read values to modify...
68         my $data;
69         if ($searchfield) {
70                 my $dbh = C4::Context->dbh;
71                 my $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory from marc_tag_structure where $pkfield='$searchfield'");
72                 $sth->execute;
73                 $data=$sth->fetchrow_hashref;
74                 $sth->finish;
75         }
76         print <<printend
77         <script>
78         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
79         function isNotNull(f,noalert) {
80                 if (f.value.length ==0) {
81    return false;
82                 }
83                 return true;
84         }
85         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
86         function toUC(f) {
87                 var x=f.value.toUpperCase();
88                 f.value=x;
89                 return true;
90         }
91         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
92         function isNum(v,maybenull) {
93         var n = new Number(v.value);
94         if (isNaN(n)) {
95                 return false;
96                 }
97         if (maybenull==0 && v.value=='') {
98                 return false;
99         }
100         return true;
101         }
102         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103         function isDate(f) {
104                 var t = Date.parse(f.value);
105                 if (isNaN(t)) {
106                         return false;
107                 }
108         }
109         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
110         function Check(f) {
111                 var ok=1;
112                 var _alertString="";
113                 var alertString2;
114                 if (f.tagfield.value.length==0) {
115                         _alertString += "- tagfield missing\\n";
116                 }
117                 if (f.repeatable.value!=0 && f.repeatable.value!=1) {
118                         _alertString += "- repeatable must be 0 or 1\\n";
119                 }
120                 if (f.mandatory.value!=0 && f.mandatory.value!=1) {
121                         _alertString += "- mandatory must be 0 or 1\\n";
122                 }
123                 if (_alertString.length==0) {
124                         document.Aform.submit();
125                 } else {
126                         alertString2 = "Form not submitted because of the following problem(s)\\n";
127                         alertString2 += "------------------------------------------------------------------------------------\\n\\n";
128                         alertString2 += _alertString;
129                         alert(alertString2);
130                 }
131         }
132         </SCRIPT>
133 printend
134 ;#/
135         if ($searchfield) {
136                 print "<h1>Modify tag</h1>";
137         } else {
138                 print "<h1>Add tag</h1>";
139         }
140         print "<form action='$script_name' name=Aform method=post>";
141         print "<input type=hidden name=op value='add_validate'>";
142         print "<table>";
143         if ($searchfield) {
144                 print "<tr><td>Tag</td><td><input type=hidden name=tagfield value='$searchfield'>$searchfield</td></tr>";
145         } else {
146                 print "<tr><td>Tag</td><td><input type=text name=tagfield size=5 maxlength=3></td></tr>";
147         }
148         print "<tr><td>Value</td><td><input type=text name=liblibrarian size=80 maxlength=255 value='$data->{'liblibrarian'}'></td></tr>";
149         print "<tr><td>Value</td><td><input type=text name=libopac size=80 maxlength=255 value='$data->{'libopac'}'></td></tr>";
150         print "<tr><td>Value</td><td><input type=text name=repeatable value='$data->{'repeatable'}'></td></tr>";
151         print "<tr><td>Value</td><td><input type=text name=mandatory value='$data->{'mandatory'}'></td></tr>";
152         print "<tr><td>&nbsp;</td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
153         print "</table>";
154         print "</form>";
155 ;
156                                                                                                         # END $OP eq ADD_FORM
157 ################## ADD_VALIDATE ##################################
158 # called by add_form, used to insert/modify data in DB
159 } elsif ($op eq 'add_validate') {
160         my $dbh = C4::Context->dbh;
161         my $query = "replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory) values (";
162         $query.= $dbh->quote($input->param('tagfield')).",";
163         $query.= $dbh->quote($input->param('liblibrarian')).",";
164         $query.= $dbh->quote($input->param('libopac')).",";
165         $query.= $dbh->quote($input->param('repeatable')).",";
166         $query.= $dbh->quote($input->param('mandatory')).")";
167         my $sth=$dbh->prepare($query);
168         $sth->execute;
169         $sth->finish;
170         print "data recorded";
171         print "<form action='$script_name' method=post>";
172         print "<input type=submit value=OK>";
173         print "</form>";
174                                                                                                         # END $OP eq ADD_VALIDATE
175 ################## DELETE_CONFIRM ##################################
176 # called by default form, used to confirm deletion of data in DB
177 } elsif ($op eq 'delete_confirm') {
178         my $dbh = C4::Context->dbh;
179         my $sth=$dbh->prepare($reqsel);
180         $sth->execute;
181         my $data=$sth->fetchrow_hashref;
182         $sth->finish;
183         print mktablehdr;
184         print mktablerow(2,'#99cc33',bold('Tag'),bold("$searchfield"),'/images/background-mem.gif');
185         print "<tr><td>liblibrarian</td><td>$data->{'liblibrarian'}</td></tr>";
186         print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=searchfield value='$searchfield'>";
187         print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
188         print "<tr><td><INPUT type=submit value='YES'></form></td><td><form action='$script_name' method=post><input type=submit value=NO></form></td></tr>";
189                                                                                                         # END $OP eq DELETE_CONFIRM
190 ################## DELETE_CONFIRMED ##################################
191 # called by delete_confirm, used to effectively confirm deletion of data in DB
192 } elsif ($op eq 'delete_confirmed') {
193         my $dbh = C4::Context->dbh;
194 #       my $searchfield=$input->param('branchcode');
195         my $sth=$dbh->prepare($reqdel);
196         $sth->execute;
197         $sth->finish;
198         print "data deleted";
199         print "<form action='$script_name' method=post>";
200         print "<input type=submit value=OK>";
201         print "</form>";
202                                                                                                         # END $OP eq DELETE_CONFIRMED
203 ################## DEFAULT ##################################
204 } else { # DEFAULT
205         my @inputs=(["text","searchfield",$searchfield],
206                 ["reset","reset","clr"]);
207         print mkheadr(2,'System preferences admin');
208         print mkformnotable("$script_name",@inputs);
209         print <<printend
210 printend
211         ;
212         if  ($searchfield ne '') {
213                 print "You Searched for <b>$searchfield<b><p>";
214         }
215         print mktablehdr;
216         print mktablerow(5,'#99cc33',bold('Tag'),bold('Value'),
217         '&nbsp;','&nbsp;','&nbsp;','/images/background-mem.gif');
218         my $env;
219         my ($count,$results)=StringSearch($env,$searchfield,'web');
220         my $toggle="white";
221         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
222                 if ($toggle eq 'white'){
223                         $toggle="#ffffcc";
224                 } else {
225                         $toggle="white";
226                 }
227                 print mktablerow(5,$toggle,$results->[$i]{'tagfield'},$results->[$i]{'liblibrarian'},mklink('','subfields_not_done'),
228                 mklink("$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'},'Edit'),
229                 mklink("$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'},'Delete'));
230         }
231         print mktableft;
232         print "<form action='$script_name' method=post>";
233         print "<input type=hidden name=op value=add_form>";
234         if ($offset>0) {
235                 my $prevpage = $offset-$pagesize;
236                 print mklink("$script_name?offset=".$prevpage,'&lt;&lt; Prev');
237         }
238         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
239         if ($offset+$pagesize<$count) {
240                 my $nextpage =$offset+$pagesize;
241                 print mklink("$script_name?offset=".$nextpage,'Next &gt;&gt;');
242         }
243         print "<br><input type=image src=\"/images/button-add-variable.gif\"  WIDTH=188  HEIGHT=44  ALT=\"Add budget\" BORDER=0 ></a><br>";
244         print "</form>";
245 } #---- END $OP eq DEFAULT
246 print endmenu('admin');
247 print endpage();