Finalized XML version for intranet
[koha.git] / admin / holdingstagstructure.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 my $frameworkcode = $input->param('frameworkcode'); # set to select framework
34 $frameworkcode="" unless $frameworkcode;
35 my $existingframeworkcode = $input->param('existingframeworkcode'); # set when we have to create a new framework (in frameworkcode) by copying an old one (in existingframeworkcode)
36 $existingframeworkcode = "" unless $existingframeworkcode;
37 my $frameworkinfo = getframeworkinfo($frameworkcode);
38 my $searchfield=$input->param('searchfield');
39 $searchfield=0 unless $searchfield;
40 $searchfield=~ s/\,//g;
41
42 my $offset=$input->param('offset');
43 my $op = $input->param('op');
44 my $dspchoice = $input->param('select_display');
45 my $pagesize=20;
46
47 my $script_name="/cgi-bin/koha/admin/holdingstagstructure.pl";
48
49 my $dbh = C4::Context->dbh;
50
51 # open template
52 my ($template, $loggedinuser, $cookie)
53     = get_template_and_user({template_name => "admin/holdingstagstructure.tmpl",
54                              query => $input,
55                              type => "intranet",
56                              authnotrequired => 0,
57                              flagsrequired => {parameters => 1},
58                              debug => 1,
59                              });
60
61 # get framework list
62 my $frameworks = getframeworks();
63 my @frameworkloop;
64 foreach my $thisframeworkcode (keys %$frameworks) {
65         my $selected = 1 if $thisframeworkcode eq $frameworkcode;
66         my %row =(value => $thisframeworkcode,
67                                 selected => $selected,
68                                 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
69                         );
70         push @frameworkloop, \%row;
71 }
72
73 # check that framework is defined in holdings_tag_structure
74 my $sth=$dbh->prepare("select count(*) from holdings_tag_structure where frameworkcode=?");
75 $sth->execute($frameworkcode);
76 my ($frameworkexist) = $sth->fetchrow;
77 if ($frameworkexist) {
78 } else {
79         # if frameworkcode does not exists, then OP must be changed to "create framework" if we are not on the way to create it
80         # (op = itemtyp_create_confirm)
81         if ($op eq "framework_create_confirm") {
82                 duplicate_framework($frameworkcode, $existingframeworkcode);
83                 $op=""; # unset $op to go back to framework list
84         } else {
85                 $op = "framework_create";
86         }
87 }
88 $template->param(frameworkloop => \@frameworkloop,
89                                 frameworkcode => $frameworkcode,
90                                 frameworktext => $frameworkinfo->{frameworktext});
91 if ($op) {
92 $template->param(script_name => $script_name,
93                                                 $op              => 1); # we show only the TMPL_VAR names $op
94 } else {
95 $template->param(script_name => $script_name,
96                                                 else              => 1); # we show only the TMPL_VAR names $op
97 }
98
99
100 ################## ADD_FORM ##################################
101 # called by default. Used to create form to add or  modify a record
102 if ($op eq 'add_form') {
103         #---- if primkey exists, it's a modify action, so read values to modify...
104         my $data;
105         if ($searchfield) {
106                 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from holdings_tag_structure where tagfield=? and frameworkcode=?");
107                 $sth->execute($searchfield,$frameworkcode);
108                 $data=$sth->fetchrow_hashref;
109                 $sth->finish;
110         }
111         my $sth = $dbh->prepare("select distinct category from authorised_values");
112         $sth->execute;
113         my @authorised_values;
114         push @authorised_values,"";
115         while ((my $category) = $sth->fetchrow_array) {
116                 push @authorised_values, $category;
117         }
118         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
119                         -values=> \@authorised_values,
120                         -size=>1,
121                         -id=>"authorised_value",
122                         -multiple=>0,
123                         -default => $data->{'authorised_value'},
124                         );
125
126         if ($searchfield) {
127                 $template->param(action => "Modify tag",
128                                                                 searchfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$searchfield\" />$searchfield");
129                 $template->param('heading-modify-tag-p' => 1);
130         } else {
131                 $template->param(action => "Add tag",
132                                                                 searchfield => "<input type=\"text\" name=\"tagfield\" size=\"5\" maxlength=\"3\" />");
133                 $template->param('heading-add-tag-p' => 1);
134         }
135         $template->param('use-heading-flags-p' => 1);
136         $template->param(liblibrarian => $data->{'liblibrarian'},
137                         libopac => $data->{'libopac'},
138                         repeatable => CGI::checkbox(-name=>'repeatable',
139                                                 -checked=> $data->{'repeatable'}?'checked':'',
140                                                 -value=> 1,
141                                                 -label => '',
142                                                 -id=> 'repeatable'),
143                         mandatory => CGI::checkbox(-name => 'mandatory',
144                                                 -checked => $data->{'mandatory'}?'checked':'',
145                                                 -value => 1,
146                                                 -label => '',
147                                                 -id => 'mandatory'),
148                         authorised_value => $authorised_value,
149                         frameworkcode => $frameworkcode,
150                         );
151                                                                                                         # END $OP eq ADD_FORM
152 ################## ADD_VALIDATE ##################################
153 # called by add_form, used to insert/modify data in DB
154 } elsif ($op eq 'add_validate') {
155         $sth=$dbh->prepare("replace holdings_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)");
156         my $tagfield       =$input->param('tagfield');
157         my $liblibrarian  = $input->param('liblibrarian');
158         my $libopac       =$input->param('libopac');
159         my $repeatable =$input->param('repeatable');
160         my $mandatory =$input->param('mandatory');
161         my $authorised_value =$input->param('authorised_value');
162         unless (C4::Context->config('demo') eq 1) {
163                 $sth->execute($tagfield,
164                                                         $liblibrarian,
165                                                         $libopac,
166                                                         $repeatable?1:0,
167                                                         $mandatory?1:0,
168                                                         $authorised_value,
169                                                         $frameworkcode
170                                                         );
171         }
172         $sth->finish;
173         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=holdingstagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
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 tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from holdings_tag_structure where tagfield=? and frameworkcode=?");
180         $sth->execute($searchfield,$frameworkcode);
181         my $data=$sth->fetchrow_hashref;
182         $sth->finish;
183         $template->param(liblibrarian => $data->{'liblibrarian'},
184                                                         searchfield => $searchfield,
185                                                         frameworkcode => $frameworkcode,
186                                                         );
187                                                                                                         # END $OP eq DELETE_CONFIRM
188 ################## DELETE_CONFIRMED ##################################
189 # called by delete_confirm, used to effectively confirm deletion of data in DB
190 } elsif ($op eq 'delete_confirmed') {
191         unless (C4::Context->config('demo') eq 1) {
192                 $dbh->do("delete from holdings_tag_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
193                 $dbh->do("delete from holdings_subfield_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
194
195         }
196                                                                                                         # END $OP eq DELETE_CONFIRMED
197 ################## ITEMTYPE_CREATE ##################################
198 # called automatically if an unexisting  frameworkis selected
199 } elsif ($op eq 'framework_create') {
200         $sth = $dbh->prepare("select count(*),holdings_tag_structure.frameworkcode,frameworktext from holdings_tag_structure,biblio_framework where biblio_framework.frameworkcode=holdings_tag_structure.frameworkcode group by holdings_tag_structure.frameworkcode");
201         $sth->execute;
202         my @existingframeworkloop;
203         while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
204                 if ($tot>0) {
205                         my %line = ( value => $thisframeworkcode,
206                                                 frameworktext => $frameworktext,
207                                         );
208                         push @existingframeworkloop,\%line;
209                 }
210         }
211         $template->param(existingframeworkloop => \@existingframeworkloop,
212                                         frameworkcode => $frameworkcode,
213 #                                       FRtext => $frameworkinfo->{frameworktext},
214                                         );
215 ################## DEFAULT ##################################
216 } else { # DEFAULT
217         # here, $op can be unset or set to "framework_create_confirm".
218         if  ($searchfield ne '') {
219                  $template->param(searchfield => $searchfield);
220         }
221         my $cnt=0;
222         if ($dspchoice) {
223                 #here, user only wants used tags/subfields displayed
224                 my $env;
225                 $searchfield=~ s/\'/\\\'/g;
226                 my @data=split(' ',$searchfield);
227                 my $sth=$dbh->prepare("Select holdings_tag_structure.tagfield as mts_tagfield,holdings_tag_structure.liblibrarian as mts_liblibrarian,holdings_tag_structure.libopac as mts_libopac,holdings_tag_structure.repeatable as mts_repeatable,holdings_tag_structure.mandatory as mts_mandatory,holdings_tag_structure.authorised_value as mts_authorized_value,holdings_subfield_structure.* from holdings_tag_structure LEFT JOIN holdings_subfield_structure ON (holdings_tag_structure.tagfield=holdings_subfield_structure.tagfield AND holdings_tag_structure.frameworkcode=holdings_subfield_structure.frameworkcode) where (holdings_tag_structure.tagfield >= ? and holdings_tag_structure.frameworkcode=?) AND holdings_subfield_structure.tab>=0 order by holdings_tag_structure.tagfield,holdings_subfield_structure.tagsubfield");
228                 #could be ordoned by tab
229                 $sth->execute($data[0], $frameworkcode);
230                 my @results = ();
231                 while (my $data=$sth->fetchrow_hashref){
232                         push(@results,$data);
233                         $cnt++;
234                 }
235                 $sth->finish;
236                 
237                 my $toggle=0;
238                 my @loop_data = ();
239                 my $j=1;
240                 my $i=$offset;
241                 while ($i < ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt)) {
242                         if ($toggle eq 0){
243                                 $toggle=1;
244                         } else {
245                                 $toggle=0;
246                         }
247                         my %row_data;  # get a fresh hash for the row data
248                         $row_data{tagfield} = $results[$i]->{'mts_tagfield'};
249                         $row_data{liblibrarian} = $results[$i]->{'mts_liblibrarian'};
250                         $row_data{repeatable} = $results[$i]->{'mts_repeatable'};
251                         $row_data{mandatory} = $results[$i]->{'mts_mandatory'};
252                         $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
253                         $row_data{subfield_link} ="holdings_subfields_structure.pl?op=add_form&tagfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
254                         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
255                         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results[$i]->{'mts_tagfield'}."&frameworkcode=".$frameworkcode;
256                         $row_data{toggle} = $toggle;
257                         $j=$i;
258                         my @internal_loop = ();
259                         while (($results[$i]->{'tagfield'}==$results[$j]->{'tagfield'}) and ($j< ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt))) {
260                                 if ($toggle eq 0) {
261                                         $toggle=1;
262                                 } else {
263                                         $toggle=0;
264                                 }
265                                 my %subfield_data;
266                                 $subfield_data{tagsubfield} = $results[$j]->{'tagsubfield'};
267                                 $subfield_data{liblibrarian} = $results[$j]->{'liblibrarian'};
268                                 
269                                 $subfield_data{repeatable} = $results[$j]->{'repeatable'};
270                                 $subfield_data{mandatory} = $results[$j]->{'mandatory'};
271                                 $subfield_data{tab} = $results[$j]->{'tab'};
272                                 $subfield_data{seealso} = $results[$j]->{'seealso'};
273                                 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
274                                 $subfield_data{authtypecode}= $results[$j]->{'authtypecode'};
275                                 $subfield_data{value_builder}= $results[$j]->{'value_builder'};
276                                 $subfield_data{toggle}  = $toggle;
277 #                               warn "tagfield :  ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
278                                 push @internal_loop,\%subfield_data;
279                                 $j++;
280                         }
281                         $row_data{'subfields'}=\@internal_loop;
282                         push(@loop_data, \%row_data);
283 #                       undef @internal_loop;
284                         $i=$j;
285                 }
286                 $template->param(select_display => "True",
287                                                 loop => \@loop_data);
288                 #  $sth->execute;
289                 $sth->finish;
290         } else {
291                 #here, normal old style : display every tags
292                 my $env;
293                 my ($count,$results)=StringSearch($env,$searchfield,$frameworkcode);
294                 $cnt = $count;
295                 my $toggle=0;
296                 my @loop_data = ();
297                 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
298                         if ($toggle eq 0){
299                                 $toggle=1;
300                         } else {
301                                 $toggle=0;
302                         }
303                         my %row_data;  # get a fresh hash for the row data
304                         $row_data{tagfield} = $results->[$i]{'tagfield'};
305                         $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
306                         $row_data{repeatable} = $results->[$i]{'repeatable'};
307                         $row_data{mandatory} = $results->[$i]{'mandatory'};
308                         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
309                         $row_data{subfield_link} ="holdings_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
310                         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
311                         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'tagfield'}."&frameworkcode=".$frameworkcode;
312                         $row_data{toggle} = $toggle;
313                         push(@loop_data, \%row_data);
314                 }
315                 $template->param(loop => \@loop_data);
316         }
317         if ($offset>0) {
318                 my $prevpage = $offset-$pagesize;
319                 $template->param(isprevpage => $offset,
320                                                 prevpage=> $prevpage,
321                                                 searchfield => $searchfield,
322                                                 script_name => $script_name,
323                                                 frameworkcode => $frameworkcode,
324                 );
325         }
326         if ($offset+$pagesize<$cnt) {
327                 my $nextpage =$offset+$pagesize;
328                 $template->param(nextpage =>$nextpage,
329                                                 searchfield => $searchfield,
330                                                 script_name => $script_name,
331                                                 frameworkcode => $frameworkcode,
332                 );
333         }
334 } #---- END $OP eq DEFAULT
335
336 $template->param(loggeninuser => $loggedinuser,
337                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
338                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
339                 IntranetNav => C4::Context->preference("IntranetNav"),
340                 );
341 output_html_with_http_headers $input, $cookie, $template->output;
342
343
344 #
345 # the sub used for searches
346 #
347 sub StringSearch  {
348         my ($env,$searchstring,$frameworkcode)=@_;
349         my $dbh = C4::Context->dbh;
350         $searchstring=~ s/\'/\\\'/g;
351         my @data=split(' ',$searchstring);
352         my $count=@data;
353         my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from holdings_tag_structure where (tagfield >= ? and frameworkcode=?) order by tagfield");
354         $sth->execute($data[0], $frameworkcode);
355         my @results;
356         while (my $data=$sth->fetchrow_hashref){
357         push(@results,$data);
358         }
359         #  $sth->execute;
360         $sth->finish;
361         return (scalar(@results),\@results);
362 }
363
364 #
365
366