Putting this template in with reserves where it belongs. Adding alternating row...
[koha.git] / admin / systempreferences.pl
1 #!/usr/bin/perl
2
3 #script to administer the systempref table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # ALGO :
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 #       - the default screen is build (with all records, or filtered datas).
11 #       - the   user can clic on add, modify or delete record.
12 # if $op=add_form
13 #       - if primkey exists, this is a modification,so we read the $primkey record
14 #       - builds the add/modify form
15 # if $op=add_validate
16 #       - the user has just send datas, so we create/modify the record
17 # if $op=delete_form
18 #       - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 #       - we delete the record having primkey=$primkey
21
22
23 # Copyright 2000-2002 Katipo Communications
24 #
25 # This file is part of Koha.
26 #
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
30 # version.
31 #
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License along with
37 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 # Suite 330, Boston, MA  02111-1307 USA
39
40 use strict;
41 use CGI;
42 use C4::Auth;
43 use C4::Context;
44 use C4::Koha;
45 use C4::Output;
46 use C4::Interface::CGI::Output;
47 use C4::Search;
48 use HTML::Template;
49 use C4::Context;
50
51 my %tabsysprefs;
52 $tabsysprefs{acquisitions}="Acquisitions";
53 $tabsysprefs{gist}="Acquisitions";
54 $tabsysprefs{authoritysep}="Authorities";
55 $tabsysprefs{ISBD}="Catalogue";
56 $tabsysprefs{marc}="Catalogue";
57 $tabsysprefs{marcflavour}="Catalogue";
58 $tabsysprefs{SubscriptionHistory}="Catalogue";
59 $tabsysprefs{maxoutstanding}="Circulation";
60 $tabsysprefs{printcirculationslips}="Circulation";
61 $tabsysprefs{ReturnBeforeExpiry}="Circulation";
62 $tabsysprefs{suggestion}="Acquisitions";
63 $tabsysprefs{automembernum}="Members";
64 $tabsysprefs{noissuescharge}="Circulation";
65 $tabsysprefs{opacthemes}="OPAC";
66 $tabsysprefs{opaclanguages}="OPAC";
67 $tabsysprefs{LibraryName}="OPAC";
68 $tabsysprefs{opacstylesheet}="OPAC";
69 $tabsysprefs{BiblioDefaultView}="OPAC";
70 $tabsysprefs{hidelostitem}="OPAC";
71 $tabsysprefs{KohaAdmin}="Admin";
72 $tabsysprefs{checkdigit}="Admin";
73 $tabsysprefs{dateformat}="Admin";
74 $tabsysprefs{insecure}="Admin";
75 $tabsysprefs{ldapinfos}="Admin";
76 $tabsysprefs{ldapserver}="Admin";
77 $tabsysprefs{itemcallnumber}="Catalogue";
78 $tabsysprefs{maxreserves}="Circulation";
79 $tabsysprefs{virtualshelves}="OPAC";
80
81 my %tabsysprefs;
82 $tabsysprefs{acquisitions}="Acquisitions";
83 $tabsysprefs{gist}="Acquisitions";
84 $tabsysprefs{authoritysep}="Authorities";
85 $tabsysprefs{ISBD}="Catalogue";
86 $tabsysprefs{marc}="Catalogue";
87 $tabsysprefs{marcflavour}="Catalogue";
88 $tabsysprefs{SubscriptionHistory}="Catalogue";
89 $tabsysprefs{maxoutstanding}="Circulation";
90 $tabsysprefs{printcirculationslips}="Circulation";
91 $tabsysprefs{ReturnBeforeExpiry}="Circulation";
92 $tabsysprefs{suggestion}="Acquisitions";
93 $tabsysprefs{automembernum}="Members";
94 $tabsysprefs{noissuescharge}="Circulation";
95 $tabsysprefs{opacthemes}="OPAC";
96 $tabsysprefs{opaclanguages}="OPAC";
97 $tabsysprefs{LibraryName}="OPAC";
98 $tabsysprefs{opacstylesheet}="OPAC";
99 $tabsysprefs{BiblioDefaultView}="OPAC";
100 $tabsysprefs{hidelostitem}="OPAC";
101 $tabsysprefs{KohaAdmin}="Admin";
102 $tabsysprefs{checkdigit}="Admin";
103 $tabsysprefs{dateformat}="Admin";
104 $tabsysprefs{insecure}="Admin";
105 $tabsysprefs{ldapinfos}="Admin";
106 $tabsysprefs{ldapserver}="Admin";
107 $tabsysprefs{itemcallnumber}="Catalogue";
108 $tabsysprefs{maxreserves}="Circulation";
109 $tabsysprefs{virtualshelves}="OPAC";
110
111 sub StringSearch  {
112         my ($env,$searchstring,$type)=@_;
113         my $dbh = C4::Context->dbh;
114         $searchstring=~ s/\'/\\\'/g;
115         my @data=split(' ',$searchstring);
116         my $count=@data;
117         my @results;
118         my $cnt=0;
119         if ($type){
120                 foreach my $syspref (sort keys %tabsysprefs){
121                         if ($tabsysprefs{$syspref} eq $type){
122                                 my $sth=$dbh->prepare("Select variable,value,explanation,type,options from systempreferences where (variable like ?) order by variable");
123                                 $sth->execute($syspref);
124                                 while (my $data=$sth->fetchrow_hashref){
125                                         push(@results,$data);
126                                         $cnt++;
127                                 }
128                                 $sth->finish;
129                         }
130                 }
131         } else {
132                 my $strsth ="Select variable,value,explanation,type,options from systempreferences where variable not in (";  
133                 foreach my $syspref (keys %tabsysprefs){
134                         $strsth .= $dbh->quote($syspref).",";
135                 }
136                 $strsth =~ s/,$/) /;
137                 $strsth .= " order by variable";
138                 warn $strsth;
139                 my $sth=$dbh->prepare($strsth);
140                 $sth->execute();
141                 while (my $data=$sth->fetchrow_hashref){
142                         push(@results,$data);
143                         $cnt++;
144                 }
145                 $sth->finish;
146         }
147         return ($cnt,\@results);
148 }
149
150
151 my $input = new CGI;
152 my $searchfield=$input->param('searchfield');
153 my $offset=$input->param('offset');
154 my $script_name="/cgi-bin/koha/admin/systempreferences.pl";
155
156 my ($template, $borrowernumber, $cookie)
157     = get_template_and_user({template_name => "parameters/systempreferences.tmpl",
158                              query => $input,
159                              type => "intranet",
160                              authnotrequired => 0,
161                              flagsrequired => {parameters => 1},
162                              debug => 1,
163                              });
164 my $pagesize=100;
165 my $op = $input->param('op');
166 $searchfield=~ s/\,//g;
167
168 if ($op) {
169 $template->param(script_name => $script_name,
170                                                 $op              => 1,); # we show only the TMPL_VAR names $op
171 } else {
172 $template->param(script_name => $script_name,
173                                                 else              => 1,); # we show only the TMPL_VAR names $op
174 }
175
176 if ($op eq 'update_and_reedit') {
177         foreach ($input->param) {
178         }
179         my $value='';
180         if (my $currentorder=$input->param('currentorder')) {
181                 my @currentorder=split /\|/, $currentorder;
182                 my $orderchanged=0;
183                 foreach my $param ($input->param) {
184                         if ($param=~m#up-(\d+).x#) {
185                                 my $temp=$currentorder[$1];
186                                 $currentorder[$1]=$currentorder[$1-1];
187                                 $currentorder[$1-1]=$temp;
188                                 $orderchanged=1;
189                                 last;
190                         } elsif ($param=~m#down-(\d+).x#) {
191                                 my $temp=$currentorder[$1];
192                                 $currentorder[$1]=$currentorder[$1+1];
193                                 $currentorder[$1+1]=$temp;
194                                 $orderchanged=1;
195                                 last;
196                         }
197                 }
198                 $value=join ' ', @currentorder;
199                 if ($orderchanged) {
200                         $op='add_form';
201                         $template->param(script_name => $script_name,
202                                                         $op              => 1); # we show only the TMPL_VAR names $op
203                 } else {
204                         $op='';
205                         $searchfield='';
206                         $template->param(script_name => $script_name,
207                                                         else              => 1); # we show only the TMPL_VAR names $op
208                 }
209         }
210         my $dbh = C4::Context->dbh;
211         my $query="select * from systempreferences where variable=?";
212         my $sth=$dbh->prepare($query);
213         $sth->execute($input->param('variable'));
214         if ($sth->rows) {
215                 unless (C4::Context->config('demo') eq 1) {
216                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
217                         $sth->execute($value, $input->param('explanation'), $input->param('variable'), $input->param('preftype'), $input->param('prefoptions'));
218                         $sth->finish;
219                 }
220     } else {
221                 unless (C4::Context->config('demo') eq 1) {
222                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation) values (?,?,?,?,?)");
223                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
224                         $sth->finish;
225                 }
226         }
227         $sth->finish;
228
229 }
230
231 ################## ADD_FORM ##################################
232 # called by default. Used to create form to add or  modify a record
233
234 if ($op eq 'add_form') {
235         #---- if primkey exists, it's a modify action, so read values to modify...
236         my $data;
237         if ($searchfield) {
238                 my $dbh = C4::Context->dbh;
239                 my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
240                 $sth->execute($searchfield);
241                 $data=$sth->fetchrow_hashref;
242                 $sth->finish;
243                 $template->param(modify => 1);
244         }
245
246         my @options;
247         foreach my $option (split(/\|/, $data->{'options'})) {
248                 my $selected='0';
249                 $option eq $data->{'value'} and $selected=1;
250                 push @options, { option => $option, selected => $selected };
251         }
252         if ($data->{'type'} eq 'Choice') {
253                 $template->param('type-choice' => 1);
254         } elsif ($data->{'type'} eq 'YesNo') {
255                 $template->param('type-yesno' => 1);
256                 $data->{'value'}=C4::Context->boolean_preference($data->{'variable'});
257                 ($data->{'value'} eq '1') ? ($template->param('value-yes'=>1)) : ($template->param('value-no'=>1));
258         } elsif ($data->{'type'} eq 'Integer') {
259                 $template->param('type-free' => 1);
260                 $template->param('fieldlength' => $data->{'options'});
261         } elsif ($data->{'type'} eq 'Textarea') {
262                 $template->param('type-textarea' => 1);
263                 $data->{options} =~ /(.*)\|(.*)/;
264                 $template->param('cols' => $1, 'rows' => $2);;
265         } elsif ($data->{'type'} eq 'Float') {
266                 $template->param('type-free' => 1);
267                 $template->param('fieldlength' => $data->{'options'});
268         } elsif ($data->{'type'} eq 'Themes') {
269                 $template->param('type-choice' => 1);
270                 my $type='';
271                 ($data->{'variable'}=~m#opac#i) ? ($type='opac') : ($type='intranet');
272                 @options=();
273                 my $currently_selected_themes;
274                 my $counter=0;
275                 foreach my $theme (split /\s+/, $data->{'value'}) {
276                     push @options, { option => $theme, counter => $counter };
277                     $currently_selected_themes->{$theme}=1;
278                     $counter++;
279                 }
280                 foreach my $theme (getallthemes($type)) {
281                         my $selected='0';
282                         next if $currently_selected_themes->{$theme};
283                         push @options, { option => $theme, counter => $counter };
284                         $counter++;
285                 }
286         } elsif ($data->{'type'} eq 'Languages') {
287                 $template->param('type-choice' => 1);
288                 my $type='';
289                 @options=();
290                 my $currently_selected_languages;
291                 my $counter=0;
292                 foreach my $language (split /\s+/, $data->{'value'}) {
293                     next if $language eq 'images';
294                     push @options, { option => $language, counter => $counter };
295                     $currently_selected_languages->{$language}=1;
296                     $counter++;
297                 }
298                 foreach my $language (getalllanguages()) {
299                         next if $language eq 'images';
300                         my $selected='0';
301                         next if $currently_selected_languages->{$language};
302                         push @options, { option => $language, counter => $counter };
303                         $counter++;
304                 }
305         } else {
306                 $template->param('type-free' => 1);
307                 $template->param('fieldlength' => $data->{'options'}>0?$data->{'options'}:60);
308         }
309         $template->param(explanation => $data->{'explanation'},
310                          value => $data->{'value'},
311                          type => $data->{'type'},
312                          options => \@options,
313                          preftype => $data->{'type'},
314                          prefoptions => $data->{'options'},
315                          searchfield => $searchfield);
316
317 ################## ADD_VALIDATE ##################################
318 # called by add_form, used to insert/modify data in DB
319 } elsif ($op eq 'add_validate') {
320         my $dbh = C4::Context->dbh;
321         my $sth=$dbh->prepare("select * from systempreferences where variable=?");
322         $sth->execute($input->param('variable'));
323         if ($sth->rows) {
324                 unless (C4::Context->config('demo') eq 1) {
325                         my $sth=$dbh->prepare("update systempreferences set value=?,explanation=?,type=?,options=? where variable=?");
326                         $sth->execute($input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'), $input->param('variable'));
327                         $sth->finish;
328                 }
329         } else {
330                 unless (C4::Context->config('demo') eq 1) {
331                         my $sth=$dbh->prepare("insert into systempreferences (variable,value,explanation,type,options) values (?,?,?,?,?)");
332                         $sth->execute($input->param('variable'), $input->param('value'), $input->param('explanation'), $input->param('preftype'), $input->param('prefoptions'));
333                         $sth->finish;
334                 }
335         }
336         $sth->finish;
337         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=systempreferences.pl\"></html>";
338         exit;
339 ################## DELETE_CONFIRM ##################################
340 # called by default form, used to confirm deletion of data in DB
341 } elsif ($op eq 'delete_confirm') {
342         my $dbh = C4::Context->dbh;
343         my $sth=$dbh->prepare("select variable,value,explanation,type,options from systempreferences where variable=?");
344         $sth->execute($searchfield);
345         my $data=$sth->fetchrow_hashref;
346         $sth->finish;
347         $template->param(searchfield => $searchfield,
348                                                         Tvalue => $data->{'value'},
349                                                         );
350
351                                                                                                         # END $OP eq DELETE_CONFIRM
352 ################## DELETE_CONFIRMED ##################################
353 # called by delete_confirm, used to effectively confirm deletion of data in DB
354 } elsif ($op eq 'delete_confirmed') {
355         my $dbh = C4::Context->dbh;
356         my $sth=$dbh->prepare("delete from systempreferences where variable=?");
357         $sth->execute($searchfield);
358         $sth->finish;
359                                                                                                         # END $OP eq DELETE_CONFIRMED
360 ################## DEFAULT ##################################
361 } else { # DEFAULT
362         #Adding tab management for system preferences
363         my $tab=$input->param('tab');
364         
365         my $env;
366         my ($count,$results)=StringSearch($env,$searchfield,$tab);
367         my $toggle=0;
368         my @loop_data = ();
369         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
370                 if ($toggle eq 0){
371                         $toggle=1;
372                 } else {
373                         $toggle=0;
374                 }
375                 my %row_data;  # get a fresh hash for the row data
376                 $row_data{variable} = $results->[$i]{'variable'};
377                 $row_data{value} = $results->[$i]{'value'};
378                 $row_data{explanation} = $results->[$i]{'explanation'};
379                 $row_data{toggle} = $toggle;
380                 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'variable'};
381                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'variable'};
382                 push(@loop_data, \%row_data);
383         }
384         $tab=($tab?$tab:"Others");
385         $template->param(loop => \@loop_data, $tab => 1);
386         if ($offset>0) {
387                 my $prevpage = $offset-$pagesize;
388                 $template->param("<a href=$script_name?offset=".$prevpage.'&lt;&lt; Prev</a>');
389         }
390         if ($offset+$pagesize<$count) {
391                 my $nextpage =$offset+$pagesize;
392                 $template->param("a href=$script_name?offset=".$nextpage.'Next &gt;&gt;</a>');
393         }
394 } #---- END $OP eq DEFAULT
395
396 output_html_with_http_headers $input, $cookie, $template->output;