first pass RT4 merge, RT#13852
[freeside.git] / rt / share / html / Articles / Article / Edit.html
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6 %#                                          <sales@bestpractical.com>
7 %#
8 %# (Except where explicitly superseded by other copyright notices)
9 %#
10 %#
11 %# LICENSE:
12 %#
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %#
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %#
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %#
29 %#
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %#
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %#
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %#
47 %# END BPS TAGGED BLOCK }}}
48 <& /Elements/Header, Title => $title &>
49 <& /Elements/Tabs &>
50 <& /Elements/ListActions, actions => \@results &>
51
52 <form method="post" action="Edit.html" name="EditArticle" id="EditArticle" enctype="multipart/form-data">
53 <input type="hidden" name="next" value="<%$ARGS{next}||''%>" />
54 <input type="hidden" name="id" value="<%$id%>" />
55 <table width="100%">
56 <tr>
57 <td><h2><&|/l&>Basics</&></h2></td>
58 <td></td>
59 </tr>
60 <& Elements/EditBasics, ArticleObj => $ArticleObj,
61                         EditClass =>$EditClass,
62                         ClassObj => $ClassObj,  
63                         %ARGS ,
64                         id => $id
65                         &>
66
67 <tr>
68 <td><h2><&|/l&>Content</&></h2></td>
69 <td></td>
70 </tr>
71 <& Elements/EditCustomFields, ArticleObj => $ArticleObj, 
72                               CFContent => \%CFContent, 
73                               ClassObj => $ClassObj, 
74                               %ARGS,
75                               id =>$id,
76                               &>
77
78 <tr>
79 <td><h2><&|/l&>Links</&></h2></td>
80 <td></td>
81 </tr>
82 <& Elements/EditLinks, ArticleObj => $ArticleObj, 
83                        %ARGS,
84                         id => $id
85                        &>
86 <tr>
87 <td><h2><&|/l&>Topics</&></h2></td>
88 <td></td>
89 </tr>
90 <tr>
91 <td class="label"><&|/l&>Topics</&></td>
92 <td>
93 <& Elements/EditTopics, ArticleObj => $ArticleObj,
94                         Classes => [$ArticleObj->Id ? $ArticleObj->ClassObj : $ClassObj],
95                         OnlyThisClass => 1,
96                         %ARGS,
97                         id => $id
98                         &>
99 </td>
100 </tr>
101 </table>
102 <& /Elements/Submit, Label => ($id eq 'new' ? loc('Create') : loc('Save Changes')), color => "#993333" &>
103 </form>
104
105 <%INIT>
106
107 if ( exists $ARGS{'Topics'} ) {
108     $ARGS{'Topics'} =
109       ref( $ARGS{'Topics'} ) ? $ARGS{'Topics'} : [ $ARGS{'Topics'} ];
110 }
111
112 my @results;
113 my $title;
114
115 my $Entries    = {};
116 my $ArticleObj = RT::Article->new( $session{'CurrentUser'} );
117 my $ClassObj   = RT::Class->new( $session{'CurrentUser'} );
118 my %create_args;
119 my %CFContent;
120 my $EditClass = 1;
121 if ( !$id ) {
122     $title = loc('Create a new article');
123     foreach my $arg ( sort keys %ARGS ) {
124         if ( $arg =~ /^Transaction-(\d+)$/ ) {
125             my $trans = RT::Transaction->new( $session{'CurrentUser'} );
126             $trans->Load($1);
127             $CFContent{ $ARGS{$arg} } .= "\n\n" if $CFContent{ $ARGS{$arg} };
128             $CFContent{ $ARGS{$arg} } .= $trans->Content;
129         }
130     }
131
132     $ClassObj->Load($Class);
133     unless ( $ClassObj->Id ) {
134         $m->comp( "/Elements/Error",
135             Why => loc( "'[_1]' isn't a valid class identifier", $Class ) );
136     }
137     $EditClass = 0;
138     $id        = 'new';
139 }
140 elsif ( $id eq 'new' ) {
141
142     if ( $ARGS{'RefersTo-new'} ) {
143         @{ $create_args{'RefersTo-new'} } =
144           split( /\s+/, $ARGS{'RefersTo-new'} );
145     }
146
147     if ( $ARGS{'new-RefersTo'} ) {
148         @{ $create_args{'new-RefersTo'} } =
149           split( /\s+/, $ARGS{'new-RefersTo'} );
150     }
151
152
153     foreach my $arg (keys %ARGS) {
154         next if $arg =~ /-(?:Magic|Category)$/;
155         # Object-RT::Article--CustomField-3-Values
156         if ( $arg =~ /^Object-RT::Article--CustomField-(\d+)(.*?)$/ ) {
157             my $cfid = $1; 
158         
159             my $cf = RT::CustomField->new( $session{'CurrentUser'} );
160             $cf->Load( $cfid );
161             unless ( $cf->id ) {
162                 $RT::Logger->error( "Couldn't load custom field #". $cfid );
163                 next;
164             }
165
166             if ( $arg =~ /-Upload$/ ) {
167                 $create_args{"CustomField-$cfid"} = _UploadedFile( $arg );
168                 next;
169             }
170             
171             my $type = $cf->Type;
172         
173             my @values = ();
174             if ( ref $ARGS{ $arg } eq 'ARRAY' ) {
175                 @values = @{ $ARGS{ $arg } };
176             } elsif ( $type =~ /text/i ) {
177                 @values = ($ARGS{ $arg });
178             } else {
179                 @values = split /\r*\n/, $ARGS{ $arg } || '';
180             }
181             @values = grep $_ ne '',
182                 map {
183                     s/\r+\n/\n/g;
184                     s/^\s+//;
185                     s/\s+$//;
186                     $_;
187                 }
188                 grep defined, @values;
189
190             $create_args{"CustomField-$cfid"} = \@values;
191         }
192     }
193
194     my $msg;
195     ( $id, $msg ) = $ArticleObj->Create(
196         Summary => $ARGS{'Summary'},
197         Name    => $ARGS{'Name'},
198         Class   => $ARGS{'Class'},
199         Topics  => $ARGS{'Topics'},
200         %create_args
201     );
202     push( @results, $msg );
203     if ($id) {
204
205
206         $ArticleObj->Load($id);
207
208         $title = loc( 'Modify article #[_1]', $ArticleObj->Id );
209         delete $ARGS{id};
210
211         if ( $ARGS{next} ) {
212             $m->redirect($ARGS{next});
213         }        
214         else {
215             MaybeRedirectForResults(
216                 Actions   => \@results,
217                 Arguments => { id => $ArticleObj->id },
218             );
219         }
220     }
221    if (!$id) {
222         $ClassObj->Load($Class);
223         unless ( $ClassObj->Id ) {
224             $m->comp( "/Elements/Error",
225                 Why => loc( "'[_1]' isn't a valid class identifier", $Class ) );
226         }
227         $ArticleObj = RT::Article->new( $session{'CurrentUser'} );
228         $id        = 'new';
229         $EditClass = 0;
230         $title     = loc('Create a new article');
231     }
232 }
233 else {
234
235     $ArticleObj->Load($id);
236     unless ( $ArticleObj->id ) {
237         $m->comp( "/Elements/Error",
238             Why => loc("Unable to load article") );
239     }
240
241     my @attribs = qw(Name Summary Class);
242
243     @results = UpdateRecordObject(
244         AttributesRef => \@attribs,
245         Object        => $ArticleObj,
246         ARGSRef       => \%ARGS
247     );
248
249     my @cf_results = ProcessObjectCustomFieldUpdates(
250         Object  => $ArticleObj,
251         ARGSRef => \%ARGS
252     );
253     @results = ( @results, @cf_results );
254
255     # Delete links that are gone gone gone.
256     foreach my $arg ( keys %ARGS ) {
257         if ( $arg =~ /DeleteLink-(.*?)-(RefersTo|MemberOf|RefersTo)-(.*)$/ ) {
258             my $base   = $1;
259             my $type   = $2;
260             my $target = $3;
261
262             my ( $val, $msg ) = $ArticleObj->DeleteLink(
263                 Base   => $base,
264                 Type   => $type,
265                 Target => $target
266             );
267
268             push @results, $msg;
269
270         }
271
272     }
273
274     my @linktypes = qw(DependsOn MemberOf RefersTo );
275
276     foreach my $linktype (@linktypes) {
277
278     for my $luri ( split( / /, ( $ARGS{ $ArticleObj->Id . "-$linktype" } || '' )) ) {
279             $luri =~ s/\s*$//;    # Strip trailing whitespace
280             my ( $val, $msg ) =
281               $ArticleObj->AddLink( Target => $luri, Type => $linktype );
282             push @results, $msg;
283             delete $ARGS{ $ArticleObj->Id . "-$linktype" } if $val;
284         }
285
286         for my $luri ( split( / /, ( $ARGS{ "$linktype-" . $ArticleObj->Id } || '' )) ) {
287             my ( $val, $msg ) =
288               $ArticleObj->AddLink( Base => $luri, Type => $linktype );
289             push @results, $msg;
290             delete $ARGS{ "$linktype-" . $ArticleObj->Id } if $val;
291         }
292
293     }
294
295     my %topics;
296     if ( $ARGS{'EditTopics'} ) {
297         $topics{$_}++ for @{ $ARGS{'Topics'} };
298         my $objTopics =
299           RT::ObjectTopics->new( $session{'CurrentUser'} );
300         $objTopics->LimitToObject($ArticleObj);
301         while ( my $t = $objTopics->Next ) {
302             $topics{ $t->Topic }--;
303         }
304         for my $id ( keys %topics ) {
305             if ( $topics{$id} > 0 ) {
306                 my ( $val, $msg ) = $ArticleObj->AddTopic( Topic => $id );
307                 push @results, $msg;
308             }
309             elsif ( $topics{$id} < 0 ) {
310                 my ( $val, $msg ) = $ArticleObj->DeleteTopic( Topic => $id );
311                 push @results, $msg;
312             }
313         }
314     }
315
316     $title = loc( 'Modify article #[_1]', $ArticleObj->Id );
317 }
318
319 # if they're working on an existing article
320 if ( $ArticleObj->id ) {
321     unless ( $ArticleObj->CurrentUserHasRight('ShowArticle') ) {
322         $m->comp( "/Elements/Error",
323             Why => loc("No permission to view Article") );
324     }
325 }
326
327 </%INIT>
328
329
330 <%ARGS>
331 $id => undef
332 $Class => undef
333 </%ARGS>