Added copyright statement to all .pl and .pm files
[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 C4::Output;
23 use CGI;
24 use C4::Search;
25 use C4::Database;
26
27 sub StringSearch  {
28         my ($env,$searchstring,$type)=@_;
29         my $dbh = &C4Connect;
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         $dbh->disconnect;
45         return ($cnt,\@results);
46 }
47
48 my $input = new CGI;
49 my $searchfield=$input->param('searchfield');
50 my $pkfield="tagfield";
51 my $reqsel="select tagfield,liblibrarian,libopac,repeatable,mandatory from marc_tag_structure where $pkfield='$searchfield'";
52 my $reqdel="delete from marc_tag_structure where $pkfield='$searchfield'";
53 my $offset=$input->param('offset');
54 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
55
56 my $pagesize=20;
57 my $op = $input->param('op');
58 $searchfield=~ s/\,//g;
59 print $input->header;
60
61 #start the page and read in includes
62 print startpage();
63 print startmenu('admin');
64
65 ################## ADD_FORM ##################################
66 # called by default. Used to create form to add or  modify a record
67 if ($op eq 'add_form') {
68         #---- if primkey exists, it's a modify action, so read values to modify...
69         my $data;
70         if ($searchfield) {
71                 my $dbh = &C4Connect;
72                 my $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory from marc_tag_structure where $pkfield='$searchfield'");
73                 $sth->execute;
74                 $data=$sth->fetchrow_hashref;
75                 $sth->finish;
76         }
77         print <<printend
78         <script>
79         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
80         function isNotNull(f,noalert) {
81                 if (f.value.length ==0) {
82    return false;
83                 }
84                 return true;
85         }
86         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
87         function toUC(f) {
88                 var x=f.value.toUpperCase();
89                 f.value=x;
90                 return true;
91         }
92         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
93         function isNum(v,maybenull) {
94         var n = new Number(v.value);
95         if (isNaN(n)) {
96                 return false;
97                 }
98         if (maybenull==0 && v.value=='') {
99                 return false;
100         }
101         return true;
102         }
103         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104         function isDate(f) {
105                 var t = Date.parse(f.value);
106                 if (isNaN(t)) {
107                         return false;
108                 }
109         }
110         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
111         function Check(f) {
112                 var ok=1;
113                 var _alertString="";
114                 var alertString2;
115                 if (f.tagfield.value.length==0) {
116                         _alertString += "- tagfield missing\\n";
117                 }
118                 if (f.repeatable.value!=0 && f.repeatable.value!=1) {
119                         _alertString += "- repeatable must be 0 or 1\\n";
120                 }
121                 if (f.mandatory.value!=0 && f.mandatory.value!=1) {
122                         _alertString += "- mandatory must be 0 or 1\\n";
123                 }
124                 if (_alertString.length==0) {
125                         document.Aform.submit();
126                 } else {
127                         alertString2 = "Form not submitted because of the following problem(s)\\n";
128                         alertString2 += "------------------------------------------------------------------------------------\\n\\n";
129                         alertString2 += _alertString;
130                         alert(alertString2);
131                 }
132         }
133         </SCRIPT>
134 printend
135 ;#/
136         if ($searchfield) {
137                 print "<h1>Modify tag</h1>";
138         } else {
139                 print "<h1>Add tag</h1>";
140         }
141         print "<form action='$script_name' name=Aform method=post>";
142         print "<input type=hidden name=op value='add_validate'>";
143         print "<table>";
144         if ($searchfield) {
145                 print "<tr><td>Tag</td><td><input type=hidden name=tagfield value='$searchfield'>$searchfield</td></tr>";
146         } else {
147                 print "<tr><td>Tag</td><td><input type=text name=tagfield size=5 maxlength=3></td></tr>";
148         }
149         print "<tr><td>Value</td><td><input type=text name=liblibrarian size=80 maxlength=255 value='$data->{'liblibrarian'}'></td></tr>";
150         print "<tr><td>Value</td><td><input type=text name=libopac size=80 maxlength=255 value='$data->{'libopac'}'></td></tr>";
151         print "<tr><td>Value</td><td><input type=text name=repeatable value='$data->{'repeatable'}'></td></tr>";
152         print "<tr><td>Value</td><td><input type=text name=mandatory value='$data->{'mandatory'}'></td></tr>";
153         print "<tr><td>&nbsp;</td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
154         print "</table>";
155         print "</form>";
156 ;
157                                                                                                         # END $OP eq ADD_FORM
158 ################## ADD_VALIDATE ##################################
159 # called by add_form, used to insert/modify data in DB
160 } elsif ($op eq 'add_validate') {
161         my $dbh=C4Connect;
162         my $query = "replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory) values (";
163         $query.= $dbh->quote($input->param('tagfield')).",";
164         $query.= $dbh->quote($input->param('liblibrarian')).",";
165         $query.= $dbh->quote($input->param('libopac')).",";
166         $query.= $dbh->quote($input->param('repeatable')).",";
167         $query.= $dbh->quote($input->param('mandatory')).")";
168         my $sth=$dbh->prepare($query);
169         $sth->execute;
170         $sth->finish;
171         print "data recorded";
172         print "<form action='$script_name' method=post>";
173         print "<input type=submit value=OK>";
174         print "</form>";
175                                                                                                         # END $OP eq ADD_VALIDATE
176 ################## DELETE_CONFIRM ##################################
177 # called by default form, used to confirm deletion of data in DB
178 } elsif ($op eq 'delete_confirm') {
179         my $dbh = &C4Connect;
180         my $sth=$dbh->prepare($reqsel);
181         $sth->execute;
182         my $data=$sth->fetchrow_hashref;
183         $sth->finish;
184         print mktablehdr;
185         print mktablerow(2,'#99cc33',bold('Tag'),bold("$searchfield"),'/images/background-mem.gif');
186         print "<tr><td>liblibrarian</td><td>$data->{'liblibrarian'}</td></tr>";
187         print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=searchfield value='$searchfield'>";
188         print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
189         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>";
190                                                                                                         # END $OP eq DELETE_CONFIRM
191 ################## DELETE_CONFIRMED ##################################
192 # called by delete_confirm, used to effectively confirm deletion of data in DB
193 } elsif ($op eq 'delete_confirmed') {
194         my $dbh=C4Connect;
195 #       my $searchfield=$input->param('branchcode');
196         my $sth=$dbh->prepare($reqdel);
197         $sth->execute;
198         $sth->finish;
199         print "data deleted";
200         print "<form action='$script_name' method=post>";
201         print "<input type=submit value=OK>";
202         print "</form>";
203                                                                                                         # END $OP eq DELETE_CONFIRMED
204 ################## DEFAULT ##################################
205 } else { # DEFAULT
206         my @inputs=(["text","searchfield",$searchfield],
207                 ["reset","reset","clr"]);
208         print mkheadr(2,'System preferences admin');
209         print mkformnotable("$script_name",@inputs);
210         print <<printend
211 printend
212         ;
213         if  ($searchfield ne '') {
214                 print "You Searched for <b>$searchfield<b><p>";
215         }
216         print mktablehdr;
217         print mktablerow(5,'#99cc33',bold('Tag'),bold('Value'),
218         '&nbsp;','&nbsp;','&nbsp;','/images/background-mem.gif');
219         my $env;
220         my ($count,$results)=StringSearch($env,$searchfield,'web');
221         my $toggle="white";
222         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
223                 if ($toggle eq 'white'){
224                         $toggle="#ffffcc";
225                 } else {
226                         $toggle="white";
227                 }
228                 print mktablerow(5,$toggle,$results->[$i]{'tagfield'},$results->[$i]{'liblibrarian'},mklink('','subfields_not_done'),
229                 mklink("$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'},'Edit'),
230                 mklink("$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'},'Delete'));
231         }
232         print mktableft;
233         print "<form action='$script_name' method=post>";
234         print "<input type=hidden name=op value=add_form>";
235         if ($offset>0) {
236                 my $prevpage = $offset-$pagesize;
237                 print mklink("$script_name?offset=".$prevpage,'&lt;&lt; Prev');
238         }
239         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
240         if ($offset+$pagesize<$count) {
241                 my $nextpage =$offset+$pagesize;
242                 print mklink("$script_name?offset=".$nextpage,'Next &gt;&gt;');
243         }
244         print "<br><input type=image src=\"/images/button-add-variable.gif\"  WIDTH=188  HEIGHT=44  ALT=\"Add budget\" BORDER=0 ></a><br>";
245         print "</form>";
246 } #---- END $OP eq DEFAULT
247 print endmenu('admin');
248 print endpage();