OMatic/editPart.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 129 CC 17 SCAR 53.8
CC
17
Cov
0%
main
306.0
BEGIN
2.0
BEGIN
2.0
CRAP 217.3
1 ##############################################################################
2 # The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved. #
3 # #
4 # This program is free software; you can redistribute it and/or #
5 # modify it under the terms of the GNU General Public License #
6 # as published by the Free Software Foundation; either version 2 #
7 # of the License, or (at your option) any later version. #
8 # #
9 # This program is distributed in the hope that it will be useful, #
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
12 # GNU General Public License for more details. #
13 # #
14 # You should have received a copy of the GNU General Public License #
15 # along with this program; if not, write to the Free Software #
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.#
17 # #
18 # Jon Howell can be contacted at: #
19 # 6211 Sudikoff Lab, Dartmouth College #
20 # Hanover, NH 03755-3510 #
21 # jonh@cs.dartmouth.edu #
22 # #
23 # An electronic copy of the GPL is available at: #
24 # http://www.gnu.org/copyleft/gpl.html #
25 # #
26 ##############################################################################
27  
28 use strict;
29  
30 package FAQ::OMatic::editPart;
31  
32 use CGI;
33 use FAQ::OMatic::Item;
34 use FAQ::OMatic;
35 use FAQ::OMatic::Auth;
36 use FAQ::OMatic::I18N;
37 use FAQ::OMatic::HelpMod;
38  
39 sub main {
40 my $cgi = FAQ::OMatic::dispatch::cgi();
41 my $rt='';
42  
43 my $params = FAQ::OMatic::getParams($cgi);
44  
45 FAQ::OMatic::mirrorsCantEdit($cgi, $params);
46  
47 $rt .= FAQ::OMatic::pageHeader($params, ['help', 'faq']);
48
49 my $item = new FAQ::OMatic::Item($params->{'file'});
50 if ($item->isBroken()) {
51 FAQ::OMatic::gripe('error', "The file (".$params->{'file'}
52 .") doesn't exist.");
53 }
54  
55 # check sequence number to prevent later confusion -- the user's
56 # insert or change request came from an out-of-date page.
57 $item->checkSequence($params);
58  
59 my $insertpart = $params->{'_insertpart'};
60  
61 my $partnum = $params->{'partnum'};
62 if ($partnum =~ m/afterLast/) {
63 # it's possible to come from a mirror, who might be enough
64 # out of date that naming a specific part number would cause
65 # the [Append to This Answer] link to sometimes cause people to
66 # unintentionally insert text before existing text.
67 # The pattern match lets us send something like '9999afterLast',
68 # which will cause older versions (which a newer mirror may
69 # point to) to generate a nice error message, instead of
70 # letting the user edit the wrong part.
71 $partnum = scalar(@{$item->{'Parts'}}) - 1;
72 }
73 my $part = undef;
74 $partnum = FAQ::OMatic::stripInt($partnum);
75 if ($partnum >= 0) {
76 $part = $item->getPart($partnum);
77 if (not $part) {
78 FAQ::OMatic::gripe('error', "Part number $partnum in "
79 .$params->{'file'}." doesn't exist.");
80 }
81 } else {
82 $partnum = -1;
83 }
84 if (($partnum < 0) and (not $insertpart)) {
85 FAQ::OMatic::gripe('error', "Part number \"$partnum\" in \""
86 .$params->{'file'}."\" doesn't exist.");
87 }
88  
89 # for inserts, create the part in our in-memory copy of the item
90 # just like it would be created in submitItem, so that the
91 # form-generator below can't tell the difference.
92  
93 # duplicates are exactly the same as inserts, but you copy an existing
94 # part, rather than start with an empty one.
95 if ($insertpart) {
96 my $newpart;
97 if ($cgi->param('_duplicate')) {
98 # duplicate part -- the one above the insert, I guess.
99 $newpart = $item->getPart($partnum)->clone();
100 } else {
101 # new part
102 $newpart = new FAQ::OMatic::Part();
103 }
104 # squeeze the new part into the item's part list.
105 splice @{$item->{'Parts'}}, $partnum+1, 0, $newpart;
106 # inheret properties from parent part
107 if ($part) {
108 if ($part->{'Type'} ne 'directory') {
109 $newpart->{'Type'} = $part->{'Type'};
110 }
111 $newpart->{'HideAttributions'} = $part->{'HideAttributions'};
112 }
113 $part = $newpart;
114 }
115  
116 if ($part->{'Text'} =~ m/^\s*$/s) {
117 # if the part starts out empty, we're as good as adding, not
118 # editing existing content.
119 # We let the item author add any new parts he wants.
120 FAQ::OMatic::Auth::ensurePerm('-item'=>$item,
121 '-operation'=>'PermAddPart',
122 '-restart'=>FAQ::OMatic::commandName(),
123 '-cgi'=>$cgi,
124 '-failexit'=>1);
125 } else {
126 FAQ::OMatic::Auth::ensurePerm('-item'=>$item,
127 '-operation'=>'PermEditPart',
128 '-restart'=>FAQ::OMatic::commandName(),
129 '-cgi'=>$cgi,
130 '-failexit'=>1);
131  
132 if ($part->{'Type'} eq 'html') {
133 # discourage unauthorized users from editing HTML parts which
134 # they won't later be able to submit.
135 FAQ::OMatic::Auth::ensurePerm('-item'=>$item,
136 '-operation'=>'PermUseHTML',
137 '-restart'=>FAQ::OMatic::commandName(),
138 '-cgi'=>$cgi,
139 '-xreason'=>'useHTML',
140 '-failexit'=>1);
141 }
142 }
143
144 if ($params->{'_insertpart'}) {
145 my $insertHint = $params->{'_insert'} || '';
146 if ($insertHint eq 'answer') {
147 $rt .= gettexta("Enter the answer to <b>%0</b>", $item->getTitle())."\n";
148 } elsif ($insertHint eq 'category') {
149 $rt .= gettexta("Enter a description for <b>%0</b>", $item->getTitle())."\n";
150 } elsif ($params->{'_duplicate'}) {
151 $rt .= gettexta("Edit duplicated text for <b>%0</b>", $item->getTitle())."\n";
152 } else {
153 $rt .= gettexta("Enter new text for <b>%0</b>", $item->getTitle())."\n";
154 }
155 } else {
156 # little white lie -- user sees 1-based indices, but parts
157 # are stored 0-based. Is this bad?
158 $rt .= gettexta("Editing the %0 text part in <b>%1</b>.",
159 FAQ::OMatic::cardinal($partnum+1), $item->getTitle())
160 ."\n";
161 }
162 $rt .= $part->displayPartEditor($item, $partnum, $params);
163  
164 $rt .= FAQ::OMatic::HelpMod::helpFor($params, 'editPart', "<br>\n");
165 $rt .= FAQ::OMatic::HelpMod::helpFor($params, 'makingLinks', "<br>\n");
166 $rt .= FAQ::OMatic::HelpMod::helpFor($params, 'seeAlso', "<br>\n");
167  
168 # TODO: this will probably be unnecessary once there is a help system.
169 if (FAQ::OMatic::getParam($params, 'editCmds') eq 'hide') {
170 $rt .=
171 "<p>" .
172 gettexta("If you later need to edit or delete this text, use the [Appearance] page to turn on the expert editing commands.") .
173 "\n";
174 }
175  
176 $rt .= FAQ::OMatic::pageFooter($params, ['help','faq']);
177  
178 print $rt;
179 }
180  
181 1;
182  
183  
184