21826983b8f9766bdf192f71b9a73a6519cf9fa6
[koha.git] / admin / koha_attr.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::Auth;
24 use C4::Koha;
25 use C4::Context;
26 use C4::Output;
27 use C4::Interface::CGI::Output;
28 use C4::Search;
29
30
31 # retrieve parameters
32 my $input = new CGI;
33
34 my $searchfield=$input->param('searchfield');
35 $searchfield="" unless $searchfield;
36 my $id=$input->param('id');
37 my $offset=$input->param('offset');
38 $offset=0 unless $offset;
39 my $op = $input->param('op');
40 my $dspchoice = $input->param('select_display');
41 my $pagesize=20;
42 my @results = ();
43 my $script_name="/cgi-bin/koha/admin/koha_attr.pl";
44
45 my $dbh = C4::Context->dbh;
46 my $sth;
47 # open template
48 my ($template, $loggedinuser, $cookie)
49     = get_template_and_user({template_name => "admin/koha_attr.tmpl",
50                              query => $input,
51                              type => "intranet",
52                              authnotrequired => 0,
53                              flagsrequired => {parameters => 1},
54                              debug => 1,
55                              });
56
57
58 if ($op) {
59 $template->param(script_name => $script_name,
60                                                 $op              => 1); # we show only the TMPL_VAR names $op
61 } else {
62 $template->param(script_name => $script_name,
63                                                 else              => 1); # we show only the TMPL_VAR names $op
64 }
65
66
67 ################## ADD_FORM ##################################
68 # called by default. Used to create form to add or  modify a record
69 if ($op eq 'add_form') {
70         #---- if primkey exists, it's a modify action, so read values to modify...
71         my $data;
72         if ($id) {
73                 $sth=$dbh->prepare("select id,marctokoha,attr,liblibrarian,sorts,recordtype,tagfield,tagsubfield,opacshow,intrashow from koha_attr where id=? ");
74                 $sth->execute($id);
75                 $data=$sth->fetchrow_hashref;
76                 $sth->finish;
77         }
78         my $sth = $dbh->prepare("select authorised_value from authorised_values where category='recordtype'");
79         $sth->execute;
80         my @authorised_values;
81         #push @authorised_values,"";
82         while ((my $category) = $sth->fetchrow_array) {
83                 push @authorised_values, $category;
84         }
85         my $recordlist  = CGI::scrolling_list(-name=>'recordtype',
86                         -values=> \@authorised_values,
87                         -size=>1,
88                         -multiple=>0,
89                         -default => $data->{'recordtype'},
90                         );
91         my $sth = $dbh->prepare("select authorised_value from authorised_values where category='mfield' order by lib");
92         $sth->execute;
93         my @authorised_values;
94         #push @authorised_values,"";
95         while ((my $category) = $sth->fetchrow_array) {
96                 push @authorised_values, $category;
97         }
98         my $taglist  = CGI::scrolling_list(-name=>'tagfield',
99                         -values=> \@authorised_values,
100                         -size=>1,
101                         -multiple=>0,
102                         -default => $data->{'tagfield'},
103                         );
104         my $sth = $dbh->prepare("select authorised_value from authorised_values where category='subfield' order by lib ");
105         $sth->execute;
106         my @authorised_values;
107         #push @authorised_values,"";
108         while ((my $category) = $sth->fetchrow_array) {
109                 push @authorised_values, $category;
110         }
111         my $tagsublist  = CGI::scrolling_list(-name=>'tagsubfield',
112                         -values=> \@authorised_values,
113                         -size=>1,
114                         -multiple=>0,
115                         -default => $data->{'tagsubfield'},
116                         );
117         
118         if ($searchfield) {
119                 $template->param(action => "Modify tag",id=>$id ,searchfield => "<input type=\"hidden\" name=\"marctokoha\" value=\"$searchfield\" />$searchfield");
120                 $template->param('heading-modify-tag-p' => 1);
121         } else {
122                 $template->param(action => "Add tag",
123                                                                 searchfield => "<input type=\"text\" name=\"marctokoha\" size=\"40\" maxlength=\"80\" />");
124                 $template->param('heading-add-tag-p' => 1);
125         }
126         $template->param('use-heading-flags-p' => 1);
127         $template->param(liblibrarian => $data->{'liblibrarian'},
128                         attr=> $data->{'attr'},
129                         recordtype=>$recordlist,
130                         tagfield=>$taglist,
131                         tagsubfield=>$tagsublist,
132                         sorts => CGI::checkbox(-name=>'sorts',
133                                                 -checked=> $data->{'sorts'}?'checked':'',
134                                                 -value=> 1,
135                                                 -label => '',
136                                                 -id=> 'sorts'),
137                         opacshow => CGI::checkbox(-name=>'opacshow',
138                                                 -checked=> $data->{'opacshow'}?'checked':'',
139                                                 -value=> 1,
140                                                 -label => '',
141                                                 -id=> 'opacshow'),
142                         intrashow => CGI::checkbox(-name=>'intrashow',
143                                                 -checked=> $data->{'intrashow'}?'checked':'',
144                                                 -value=> 1,
145                                                 -label => '',
146                                                 -id=> 'intrashow'),
147
148
149                         );
150                                                                                                         # END $OP eq ADD_FORM
151 ################## ADD_VALIDATE ##################################
152 # called by add_form, used to insert/modify data in DB
153 } elsif ($op eq 'add_validate') {
154 my $id       =$input->param('id');
155
156         $sth=$dbh->prepare("replace koha_attr  set id=?,marctokoha=?,attr=?,liblibrarian=?,sorts=?,recordtype=?,tagfield=?,tagsubfield=? ,opacshow=?,intrashow=? ");
157
158         
159         my $marctokoha       =$input->param('marctokoha');
160         my $attr       =$input->param('attr');
161         my $liblibrarian  = $input->param('liblibrarian');
162         my $sorts =$input->param('sorts');
163         my $opacshow =$input->param('opacshow');
164         my $intrashow =$input->param('intrashow');
165         my $recordtype =$input->param('recordtype');
166         my $tagfield =$input->param('tagfield');
167         my $tagsubfield =$input->param('tagsubfield');
168         unless (C4::Context->config('demo') eq 1) {
169                 $sth->execute( $id,$marctokoha,$attr,$liblibrarian,$sorts?1:0,$recordtype,$tagfield,$tagsubfield,$opacshow?1:0,$intrashow?1:0);
170         }
171         $sth->finish;
172         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=koha_attr.pl?searchfield=$marctokoha\"></html>";
173
174         exit;
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         $sth=$dbh->prepare("select id,marctokoha,liblibrarian,recordtype from koha_attr where id=? ");
180                 $sth->execute($id);
181         my $data=$sth->fetchrow_hashref;
182         $sth->finish;
183         $template->param(liblibrarian => $data->{'liblibrarian'}."/". $data->{'recordtype'},id=>$data->{'id'},
184                                                         searchfield => $searchfield,
185                                                         );
186                                                                                                         # END $OP eq DELETE_CONFIRM
187 ################## DELETE_CONFIRMED ##################################
188 # called by delete_confirm, used to effectively confirm deletion of data in DB
189 } elsif ($op eq 'delete_confirmed') {
190
191         unless (C4::Context->config('demo') eq 1) {
192                 $dbh->do("delete from koha_attr where id=$id ");
193         }
194                                                                                                         # END $OP eq DELETE_CONFIRMED
195
196 ################## DEFAULT ##################################
197 } else { # DEFAULT
198         # here, $op can be unset or set 
199         if  ($searchfield ne '') {
200                  $template->param(searchfield => $searchfield);
201         }
202         my $cnt=0;
203         if ($dspchoice) {
204                 #here, user only wants used tags/subfields displayed
205                 my $sth=$dbh->prepare("Select * from koha_attr where tagfield <>'' and marctokoha >= ? ");
206                 #could be ordoned by tab
207                 $sth->execute($searchfield);
208
209                 while (my $data=$sth->fetchrow_hashref){
210                         push(@results,$data);
211                         $cnt++;
212                 }
213
214                 $sth->finish;
215                 
216                 my $toggle=0;
217                 my @loop_data = ();
218                 my $j=1;
219                 my $i=$offset;
220                 while ($i < ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt)) {
221                         if ($toggle eq 0){
222                                 $toggle=1;
223                         } else {
224                                 $toggle=0;
225                         }
226                         my %row_data;  # get a fresh hash for the row data
227                         $row_data{id} = $results[$i]->{'id'};
228                         $row_data{marctokoha} = $results[$i]->{'marctokoha'};
229                         $row_data{liblibrarian} = $results[$i]->{'liblibrarian'};
230                         $row_data{sorts} = $results[$i]->{'sorts'};
231                         $row_data{attr} = $results[$i]->{'attr'};
232                         $row_data{recordtype} = $results[$i]->{'recordtype'};
233                         $row_data{tagfield} = $results[$i]->{'tagfield'};
234                         $row_data{tagsubfield} = $results[$i]->{'tagsubfield'};
235                         $row_data{opacshow} = $results[$i]->{'opacshow'};
236                         $row_data{intrashow} = $results[$i]->{'intrashow'};
237                         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results[$i]->{'marctokoha'}."&amp;id=".$results[$i]->{'id'};
238                         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results[$i]->{'marctokoha'}."&amp;id=".$results[$i]->{'id'};
239                         $row_data{toggle} = $toggle;
240                         push(@loop_data, \%row_data);
241                         $i++;
242                 }
243                 $template->param(select_display => "True",
244                                                 loop => \@loop_data);
245                 $sth->finish;
246         } else {
247                 #here, normal old style : display every tags
248                 my ($count,@results)=StringSearch($dbh,$searchfield);
249                 $cnt = $count;
250                 my $toggle=0;
251                 my @loop_data = ();
252                 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
253                         if ($toggle eq 0){
254                                 $toggle=1;
255                         } else {
256                                 $toggle=0;
257                         }
258                         my %row_data;  # get a fresh hash for the row data
259                         $row_data{id} = $results[$i]->{'id'};
260                         $row_data{marctokoha} = $results[$i]->{marctokoha};
261                         $row_data{liblibrarian} = $results[$i]->{liblibrarian};
262                         $row_data{sorts} = $results[$i]->{sorts};
263                         $row_data{opacshow} = $results[$i]->{'opacshow'};
264                         $row_data{intrashow} = $results[$i]->{'intrashow'};
265                         $row_data{attr} = $results[$i]->{attr};
266                         $row_data{recordtype} = $results[$i]->{'recordtype'};
267                         $row_data{tagfield} = $results[$i]->{'tagfield'};
268                         $row_data{tagsubfield} = $results[$i]->{'tagsubfield'};
269                         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results[$i]->{marctokoha}."&amp;id=".$results[$i]->{'id'};
270                         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results[$i]->{marctokoha}."&amp;id=".$results[$i]->{'id'};
271                         $row_data{toggle} = $toggle;
272                         push(@loop_data, \%row_data);
273                 }
274                 $template->param(loop => \@loop_data);
275         }
276         if ($offset>0) {
277                 my $prevpage = $offset-$pagesize;
278                 $template->param(isprevpage => $offset,
279                                                 prevpage=> $prevpage,
280                                                 searchfield => $searchfield,
281                                                 script_name => $script_name,
282                                                 
283                 );
284         }
285         if ($offset+$pagesize<$cnt) {
286                 my $nextpage =$offset+$pagesize;
287                 $template->param(nextpage =>$nextpage,
288                                                 searchfield => $searchfield,
289                                                 script_name => $script_name,
290                                                 
291                 );
292         }
293 } #---- END $OP eq DEFAULT
294
295 $template->param(loggeninuser => $loggedinuser,
296                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
297                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
298                 IntranetNav => C4::Context->preference("IntranetNav"),
299                 );
300 output_html_with_http_headers $input, $cookie, $template->output;
301
302
303 #
304 # the sub used for searches
305 #
306 sub StringSearch  {
307         my ($dbh,$searchstring)=@_;
308         my $sth=$dbh->prepare("Select * from koha_attr  where marctokoha >=?  order by marctokoha");
309         $sth->execute($searchstring);
310         my @dataresults;
311         while (my $data=$sth->fetchrow_hashref){
312         push(@dataresults,$data);
313
314         }
315
316         $sth->finish;
317         return (scalar(@dataresults),@dataresults);
318 }
319
320
321