Bug 7367 - One "the" too many
[koha.git] / edithelp.pl
blob5cde3149d49a706f73fe6e41bfae0dc74c743593
1 #!/usr/bin/perl
3 # Copyright 2007 Liblime Ltd
5 # This file is part of Koha.
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.
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.
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.
20 use strict;
21 use C4::Output;
22 use C4::Templates;
23 use C4::Auth;
24 use CGI;
25 use warnings;
27 use vars qw($debug);
29 BEGIN {
30 $debug = $ENV{DEBUG} || 0;
33 our $input = new CGI;
35 my $type = $input->param('type') || '';
36 my $referer = $input->param('referer') || '';
37 my $oldreferer = $referer;
38 my $help = $input->param('help') || '';
39 # strip any DOS-newlines that TinyMCE may have sneaked in
40 $help =~ s/\r//g;
41 my $error;
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45 template_name => "help/edithelp.tt",
46 query => $input,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => {
50 catalogue => 1,
51 circulate => 1,
52 parameters => 1,
53 borrowers => 1,
54 permissions => 1,
55 reserveforothers => 1,
56 borrow => 1,
57 reserveforself => 1,
58 editcatalogue => 1,
59 updatecharges => 1,
61 debug => 1,
65 sub _get_filepath ($;$) {
66 my $referer = shift;
67 $referer =~ /koha\/(.*)\.pl/;
68 my $from = "help/$1.tt";
69 my $htdocs = C4::Context->config('intrahtdocs');
70 my ($theme, $lang) = C4::Templates::themelanguage( $htdocs, $from, "intranet", $input );
71 $debug and print STDERR "help filepath: $htdocs/$theme/$lang/modules/$from";
72 return "$htdocs/$theme/$lang/modules/$from";
75 $type = 'create' if $type eq 'addnew';
76 if ( $type eq 'create' || $type eq 'save' ) {
77 my $file = _get_filepath($referer);
78 open my $fh, ">", $file;
79 if ( $fh ) {
80 # file is open write to it
81 print $fh
82 " [% INCLUDE 'help-top.inc' %]\n",
83 $type eq 'create' ? "<div class=\"main\">\n$help\n</div>" : $help,
84 "\n[% INCLUDE 'help-bottom.inc' %]\n";
85 close $fh;
86 print $input->redirect("/cgi-bin/koha/help.pl?url=$oldreferer");
88 else {
89 $error = "Cannot write file: '$file'";
92 elsif ( $type eq 'modify' ) {
93 # open file load data, kill include calls, pass data to the template
94 my $file = _get_filepath($referer, 1); # 2nd argument triggers themelanguage call
95 if (! -r $file) {
96 $error = "Cannot read file: '$file'.";
97 } else {
98 (-w $file) or $error =
99 "WARNING: You will not be able save, because your webserver cannot write to '$file'. Contact your admin about help file permissions.";
100 open (my $fh, '<', $file) or die "Cannot read file '$file'"; # unlikely death, since we just checked
101 my $help = '';
102 while ( <$fh> ) {
103 $help .= /\[% INCLUDE .* %\](.*)$/ ? $1 : $_;
105 close $fh;
106 $template->param( 'help' => $help );
107 $type = 'save';
111 $template->param(
112 'referer' => $referer,
113 'type' => $type,
115 ($error) and $template->param('error' => $error);
116 output_html_with_http_headers $input, "", $template->output;