File: editPart.pm

package info (click to toggle)
faqomatic 2.721-9.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,996 kB
  • ctags: 548
  • sloc: perl: 13,356; sh: 69; makefile: 47
file content (184 lines) | stat: -rw-r--r-- 7,002 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
##############################################################################
# The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved.      #
#                                                                            #
# This program is free software; you can redistribute it and/or              #
# modify it under the terms of the GNU General Public License                #
# as published by the Free Software Foundation; either version 2             #
# of the License, or (at your option) any later version.                     #
#                                                                            #
# This program is distributed in the hope that it will be useful,            #
# but WITHOUT ANY WARRANTY; without even the implied warranty of             #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
# GNU General Public License for more details.                               #
#                                                                            #
# You should have received a copy of the GNU General Public License          #
# along with this program; if not, write to the Free Software                #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.#
#                                                                            #
# Jon Howell can be contacted at:                                            #
# 6211 Sudikoff Lab, Dartmouth College                                       #
# Hanover, NH  03755-3510                                                    #
# jonh@cs.dartmouth.edu                                                      #
#                                                                            #
# An electronic copy of the GPL is available at:                             #
# http://www.gnu.org/copyleft/gpl.html                                       #
#                                                                            #
##############################################################################

use strict;

package FAQ::OMatic::editPart;

use CGI;
use FAQ::OMatic::Item;
use FAQ::OMatic;
use FAQ::OMatic::Auth;
use FAQ::OMatic::I18N;
use FAQ::OMatic::HelpMod;

sub main {
	my $cgi = FAQ::OMatic::dispatch::cgi();
	my $rt='';

	my $params = FAQ::OMatic::getParams($cgi);

	FAQ::OMatic::mirrorsCantEdit($cgi, $params);

	$rt .= FAQ::OMatic::pageHeader($params, ['help', 'faq']);
	
	my $item = new FAQ::OMatic::Item($params->{'file'});
	if ($item->isBroken()) {
		FAQ::OMatic::gripe('error', "The file (".$params->{'file'}
			.") doesn't exist.");
	}

	# check sequence number to prevent later confusion -- the user's
	# insert or change request came from an out-of-date page.
	$item->checkSequence($params);

	my $insertpart = $params->{'_insertpart'};

	my $partnum = $params->{'partnum'};
	if ($partnum =~ m/afterLast/) {
		# it's possible to come from a mirror, who might be enough
		# out of date that naming a specific part number would cause
		# the [Append to This Answer] link to sometimes cause people to
		# unintentionally insert text before existing text.
		# The pattern match lets us send something like '9999afterLast',
		# which will cause older versions (which a newer mirror may
		# point to) to generate a nice error message, instead of
		# letting the user edit the wrong part.
		$partnum = scalar(@{$item->{'Parts'}}) - 1;
	}
	my $part = undef;
	$partnum = FAQ::OMatic::stripInt($partnum);
	if ($partnum >= 0) {
		$part = $item->getPart($partnum);
		if (not $part) {
			FAQ::OMatic::gripe('error', "Part number $partnum in "
				.$params->{'file'}." doesn't exist.");
		}
	} else {
		$partnum = -1;
	}
	if (($partnum < 0) and (not $insertpart)) {
		FAQ::OMatic::gripe('error', "Part number \"$partnum\" in \""
			.$params->{'file'}."\" doesn't exist.");
	}

	# for inserts, create the part in our in-memory copy of the item
	# just like it would be created in submitItem, so that the
	# form-generator below can't tell the difference.

	# duplicates are exactly the same as inserts, but you copy an existing
	# part, rather than start with an empty one.
	if ($insertpart) {
		my $newpart;
		if ($cgi->param('_duplicate')) {
			# duplicate part -- the one above the insert, I guess.
			$newpart = $item->getPart($partnum)->clone();
		} else {
			# new part
			$newpart = new FAQ::OMatic::Part();
		}
		# squeeze the new part into the item's part list.
		splice @{$item->{'Parts'}}, $partnum+1, 0, $newpart;
		# inheret properties from parent part
		if ($part) {
			if ($part->{'Type'} ne 'directory') {
				$newpart->{'Type'} = $part->{'Type'};
			}
			$newpart->{'HideAttributions'} = $part->{'HideAttributions'};
		}
		$part = $newpart;
	}

	if ($part->{'Text'} =~ m/^\s*$/s) {
		# if the part starts out empty, we're as good as adding, not
		# editing existing content.
		# We let the item author add any new parts he wants.
		FAQ::OMatic::Auth::ensurePerm('-item'=>$item,
			'-operation'=>'PermAddPart',
			'-restart'=>FAQ::OMatic::commandName(),
			'-cgi'=>$cgi,
			'-failexit'=>1);
	} else {
		FAQ::OMatic::Auth::ensurePerm('-item'=>$item,
			'-operation'=>'PermEditPart',
			'-restart'=>FAQ::OMatic::commandName(),
			'-cgi'=>$cgi,
			'-failexit'=>1);

		if ($part->{'Type'} eq 'html') {
			# discourage unauthorized users from editing HTML parts which
			# they won't later be able to submit.
			FAQ::OMatic::Auth::ensurePerm('-item'=>$item,
				'-operation'=>'PermUseHTML',
				'-restart'=>FAQ::OMatic::commandName(),
				'-cgi'=>$cgi,
				'-xreason'=>'useHTML',
				'-failexit'=>1);
		}
	}
	
	if ($params->{'_insertpart'}) {
		my $insertHint = $params->{'_insert'} || '';
		if ($insertHint eq 'answer') {
			$rt .= gettexta("Enter the answer to <b>%0</b>", $item->getTitle())."\n";
		} elsif ($insertHint eq 'category') {
			$rt .= gettexta("Enter a description for <b>%0</b>", $item->getTitle())."\n";
		} elsif ($params->{'_duplicate'}) {
			$rt .= gettexta("Edit duplicated text for <b>%0</b>", $item->getTitle())."\n";
		} else {
			$rt .= gettexta("Enter new text for <b>%0</b>", $item->getTitle())."\n";
		}
	} else {
		# little white lie -- user sees 1-based indices, but parts
		# are stored 0-based. Is this bad?
		$rt .= gettexta("Editing the %0 text part in <b>%1</b>.", 
				FAQ::OMatic::cardinal($partnum+1), $item->getTitle())
			."\n";
	}
	$rt .= $part->displayPartEditor($item, $partnum, $params);

	$rt .= FAQ::OMatic::HelpMod::helpFor($params, 'editPart', "<br>\n");
	$rt .= FAQ::OMatic::HelpMod::helpFor($params, 'makingLinks', "<br>\n");
	$rt .= FAQ::OMatic::HelpMod::helpFor($params, 'seeAlso', "<br>\n");

	# TODO: this will probably be unnecessary once there is a help system.
	if (FAQ::OMatic::getParam($params, 'editCmds') eq 'hide') {
		$rt .=
		    "<p>" .
		    gettexta("If you later need to edit or delete this text, use the [Appearance] page to turn on the expert editing commands.") .
		    "\n";
	}

	$rt .= FAQ::OMatic::pageFooter($params, ['help','faq']);

	print $rt;
}

1;