Bug 5940: Catalog and Cataloging Help Files for 3.4
[koha.git] / edithelp.pl
1 #!/usr/bin/perl
2
3 # Copyright 2007 Liblime Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use C4::Output;
22 use C4::Auth;
23 use CGI;
24 use warnings;
25
26 use vars qw($debug);
27
28 BEGIN {
29         $debug = $ENV{DEBUG} || 0;
30 }
31
32 our $input = new CGI;
33
34 my $type    = $input->param('type') || '';
35 my $referer = $input->param('referer') || '';
36 my $oldreferer = $referer;
37 my $help    = $input->param('help') || '';
38 # strip any DOS-newlines that TinyMCE may have sneaked in
39 $help =~ s/\r//g;
40 my $error;
41
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {
44         template_name   => "help/edithelp.tmpl",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => {
49             catalogue        => 1,
50             circulate        => 1,
51             parameters       => 1,
52             borrowers        => 1,
53             permissions      => 1,
54             reserveforothers => 1,
55             borrow           => 1,
56             reserveforself   => 1,
57             editcatalogue    => 1,
58             updatecharges    => 1,
59         },
60         debug => 1,
61     }
62 );
63
64 sub _get_filepath ($;$) {
65     my $referer = shift;
66     $referer =~ /.*koha\/(.+)\.pl.*/;
67     my $from   = "help/$1.tmpl";
68     my $htdocs = C4::Context->config('intrahtdocs');
69     my ($theme, $lang) = themelanguage( $htdocs, $from, "intranet", $input );
70         $debug and print STDERR "help filepath: $htdocs/$theme/$lang/modules/$from";
71         return "$htdocs/$theme/$lang/modules/$from";
72 }
73
74 if ( $type eq 'addnew' ) {
75     $type = 'create';
76 }
77 elsif ( $type eq 'create' || $type eq 'save' ) {
78         my $file = _get_filepath($referer);
79         unless (open (OUTFILE, ">$file")) {$error = "Cannot write file: '$file'";} else {
80         #open (OUTFILE, ">$file") or die "Cannot write file: '$file'";  # unlikely death, since we just checked
81         # file is open write to it
82         print OUTFILE "<!-- TMPL_INCLUDE NAME=\"help-top.inc\" -->\n";
83                 print OUTFILE ($type eq 'create') ? "<div class=\"main\">\n$help\n</div>" : $help;
84         print OUTFILE "\n<!-- TMPL_INCLUDE NAME=\"help-bottom.inc\" -->\n";
85         close OUTFILE;
86                 print $input->redirect("/cgi-bin/koha/help.pl?url=$oldreferer");
87     }
88     
89 }
90 elsif ( $type eq 'modify' ) {
91     # open file load data, kill include calls, pass data to the template
92         my $file = _get_filepath($referer, 1);  # 2nd argument triggers themelanguage call
93         if (! -r $file) {
94                 $error = "Cannot read file: '$file'.";
95         } else {
96                 (-w $file) or $error = 
97                         "WARNING: You will not be able save, because your webserver cannot write to '$file'. Contact your admin about help file permissions.";
98         open (INFILE, $file) or die "Cannot read file '$file'";         # unlikely death, since we just checked
99                 my $help = '';
100                 while ( my $inp = <INFILE> ) {
101                         unless ( $inp =~ /TMPL\_INCLUDE/ ) {
102                                 $help .= $inp;
103                         }
104                 }
105                 close INFILE;
106         $template->param( 'help' => $help );
107                 $type = 'save';
108         }
109 }
110
111 $template->param(
112     'referer' => $referer,
113     'type'    => $type,
114 );
115 ($error) and $template->param('error' => $error);
116 output_html_with_http_headers $input, "", $template->output;