OMatic/addItem.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 92 CC 9 SCAR 40.0
CC
9
Cov
0%
main
90.0
BEGIN
2.0
BEGIN
2.0
CRAP 54.8
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::addItem;
31  
32 use CGI;
33 use FAQ::OMatic::Item;
34 use FAQ::OMatic;
35 use FAQ::OMatic::Auth;
36 use FAQ::OMatic::I18N;
37  
38 sub main {
39 my $cgi = FAQ::OMatic::dispatch::cgi();
40
41 my $params = FAQ::OMatic::getParams($cgi);
42  
43 FAQ::OMatic::mirrorsCantEdit($cgi, $params);
44  
45 my $file = $params->{'file'} || '';
46 my $item = new FAQ::OMatic::Item($file);
47 if ($item->isBroken()) {
48 FAQ::OMatic::gripe('error', gettexta("The file (%0) doesn't exist.", $file));
49 }
50  
51 FAQ::OMatic::Auth::ensurePerm('-item'=>$item,
52 '-operation'=>'PermAddItem',
53 '-restart'=>FAQ::OMatic::commandName(),
54 '-cgi'=>$cgi,
55 '-failexit'=>1);
56  
57 my $duplicateFrom = $cgi->param('_duplicate');
58 my $newitem;
59 if ($duplicateFrom) {
60 # duplicate an existing item
61 my $source = new FAQ::OMatic::Item($duplicateFrom);
62 if ($source->isBroken()) {
63 FAQ::OMatic::gripe('error', "The source of the duplicate (file=".
64 $duplicateFrom.") is broken.");
65 }
66 $newitem = $source->clone();
67 $newitem->setProperty("Title", gettext("Copy of")." ".$newitem->getTitle());
68 } else {
69 # create a new item in destination item file
70 $newitem = new FAQ::OMatic::Item();
71 # inherit parent's properties:
72 $newitem->setProperty('AttributionsTogether',
73 $item->{'AttributionsTogether'});
74 }
75 $newitem->setProperty("Parent", $item->{'filename'});
76 # tell the new kid who his parent is.
77 $newitem->setProperty('Moderator', '');
78 # regardless of what the parent did, we inherit moderator
79 # from parent rather than setting it explicitly.
80
81 # THANKS to <dgatwood@mklinux.org> for the following patch:
82 # BZZT. That sucks. Set it explicitly if relax is set.
83 if (($item->getProperty('RelaxChildPerms')||'') eq 'relax') {
84 my ($id,$aq) = FAQ::OMatic::Auth::getID();
85 if ($id) {
86 $newitem->setProperty('Moderator', $id);
87 } else {
88 print FAQ::OMatic::header($cgi, '-type'=>'text/plain');
89 FAQ::OMatic::gripe('error',
90 "You must be logged in to add to a category if "
91 ."RelaxChildPerms is set to relax.\n"
92 ."You should not ever see this message. "
93 ."Contact your site administrator.\n");
94 FAQ::OMatic::myExit(0);
95 }
96 }
97  
98 # if user was asking for a category, add a directory just to
99 # make him feel better
100 if ($params->{'_insert'} eq 'category') {
101 $newitem->makeDirectory()->
102 setText(gettext("Subcategories:")."\n\n\n".gettext("Answers in this category:")."\n");
103 }
104  
105 # passing $file as a name ensures that new child will have the
106 # same type of name as its parent. (such as a helpfile)
107 $newitem->saveToFile(FAQ::OMatic::unallocatedItemName($file));
108  
109 # add that item to the (proud) parent item's catalog
110 $item->addSubItem($newitem->{'filename'});
111 $item->saveToFile();
112  
113 $item->notifyModerator($cgi, 'added a sub-item #'.$newitem->{'filename'});
114  
115 if (FAQ::OMatic::getParam($params, 'isapi')) {
116 # caller is a program; doesn't want a redirect to an HTML file!
117 # provide textual results
118 print FAQ::OMatic::header($cgi, '-type'=>'text/plain')
119 ."isapi=1\n"
120 ."file=".$newitem->{'filename'}."\n"
121 ."checkSequenceNumber=".$newitem->{'SequenceNumber'}."\n";
122 FAQ::OMatic::myExit(0);
123 }
124  
125 # redirect user to the new page
126 my $url;
127 if ($duplicateFrom) {
128 $url = FAQ::OMatic::makeAref('editItem',
129 {'file'=>$newitem->{'filename'},
130 '_duplicate'=>$params->{'_duplicate'}},
131 'url');
132 } else {
133 # send the user to the edit item page, to supply the title
134 $url = FAQ::OMatic::makeAref('editItem',
135 {'file'=>$newitem->{'filename'},
136 '_insert'=>$params->{'_insert'}},
137 'url');
138 }
139  
140 FAQ::OMatic::redirect($cgi, $url);
141 }
142  
143 1;