Merge branch 'master' of https://github.com/jgoodman/Freeside
[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-2014 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->SetContextObject( $ArticleObj );
161             $cf->Load( $cfid );
162             unless ( $cf->id ) {
163                 $RT::Logger->error( "Couldn't load custom field #". $cfid );
164                 next;
165             }
166
167             if ( $arg =~ /-Upload$/ ) {
168                 $create_args{"CustomField-$cfid"} = _UploadedFile( $arg );
169                 next;
170             }
171             
172             my $type = $cf->Type;
173         
174             my @values = ();
175             if ( ref $ARGS{ $arg } eq 'ARRAY' ) {
176                 @values = @{ $ARGS{ $arg } };
177             } elsif ( $type =~ /text/i ) {
178                 @values = ($ARGS{ $arg });
179             } else {
180                 @values = split /\r*\n/, $ARGS{ $arg } || '';
181             }
182             @values = grep $_ ne '',
183                 map {
184                     s/\r+\n/\n/g;
185                     s/^\s+//;
186                     s/\s+$//;
187                     $_;
188                 }
189                 grep defined, @values;
190
191             $create_args{"CustomField-$cfid"} = \@values;
192         }
193     }
194
195     my $msg;
196     ( $id, $msg ) = $ArticleObj->Create(
197         Summary => $ARGS{'Summary'},
198         Name    => $ARGS{'Name'},
199         Class   => $ARGS{'Class'},
200         Topics  => $ARGS{'Topics'},
201         %create_args
202     );
203     push( @results, $msg );
204     if ($id) {
205
206
207         $ArticleObj->Load($id);
208
209         $title = loc( 'Modify article #[_1]', $ArticleObj->Id );
210         delete $ARGS{id};
211
212         if ( $ARGS{next} ) {
213             $m->redirect($ARGS{next});
214         }        
215         else {
216             MaybeRedirectForResults(
217                 Actions   => \@results,
218                 Arguments => { id => $ArticleObj->id },
219             );
220         }
221     }
222    if (!$id) {
223         $ClassObj->Load($Class);
224         unless ( $ClassObj->Id ) {
225             $m->comp( "/Elements/Error",
226                 Why => loc( "'[_1]' isn't a valid class identifier", $Class ) );
227         }
228         $ArticleObj = RT::Article->new( $session{'CurrentUser'} );
229         $id        = 'new';
230         $EditClass = 0;
231         $title     = loc('Create a new article');
232     }
233 }
234 else {
235
236     $ArticleObj->Load($id);
237     unless ( $ArticleObj->id ) {
238         $m->comp( "/Elements/Error",
239             Why => loc("Unable to load article") );
240     }
241
242     my @attribs = qw(Name Summary Class);
243
244     @results = UpdateRecordObject(
245         AttributesRef => \@attribs,
246         Object        => $ArticleObj,
247         ARGSRef       => \%ARGS
248     );
249
250     my @cf_results = ProcessObjectCustomFieldUpdates(
251         Object  => $ArticleObj,
252         ARGSRef => \%ARGS
253     );
254     @results = ( @results, @cf_results );
255
256     # Delete links that are gone gone gone.
257     foreach my $arg ( keys %ARGS ) {
258         if ( $arg =~ /DeleteLink-(.*?)-(RefersTo|MemberOf|RefersTo)-(.*)$/ ) {
259             my $base   = $1;
260             my $type   = $2;
261             my $target = $3;
262
263             my ( $val, $msg ) = $ArticleObj->DeleteLink(
264                 Base   => $base,
265                 Type   => $type,
266                 Target => $target
267             );
268
269             push @results, $msg;
270
271         }
272
273     }
274
275     my @linktypes = qw(DependsOn MemberOf RefersTo );
276
277     foreach my $linktype (@linktypes) {
278
279     for my $luri ( split( / /, ( $ARGS{ $ArticleObj->Id . "-$linktype" } || '' )) ) {
280             $luri =~ s/\s*$//;    # Strip trailing whitespace
281             my ( $val, $msg ) =
282               $ArticleObj->AddLink( Target => $luri, Type => $linktype );
283             push @results, $msg;
284             delete $ARGS{ $ArticleObj->Id . "-$linktype" } if $val;
285         }
286
287         for my $luri ( split( / /, ( $ARGS{ "$linktype-" . $ArticleObj->Id } || '' )) ) {
288             my ( $val, $msg ) =
289               $ArticleObj->AddLink( Base => $luri, Type => $linktype );
290             push @results, $msg;
291             delete $ARGS{ "$linktype-" . $ArticleObj->Id } if $val;
292         }
293
294     }
295
296     my %topics;
297     if ( $ARGS{'EditTopics'} ) {
298         $topics{$_}++ for @{ $ARGS{'Topics'} };
299         my $objTopics =
300           RT::ObjectTopics->new( $session{'CurrentUser'} );
301         $objTopics->LimitToObject($ArticleObj);
302         while ( my $t = $objTopics->Next ) {
303             $topics{ $t->Topic }--;
304         }
305         for my $id ( keys %topics ) {
306             if ( $topics{$id} > 0 ) {
307                 my ( $val, $msg ) = $ArticleObj->AddTopic( Topic => $id );
308                 push @results, $msg;
309             }
310             elsif ( $topics{$id} < 0 ) {
311                 my ( $val, $msg ) = $ArticleObj->DeleteTopic( Topic => $id );
312                 push @results, $msg;
313             }
314         }
315     }
316
317     $title = loc( 'Modify article #[_1]', $ArticleObj->Id );
318 }
319
320 # if they're working on an existing article
321 if ( $ArticleObj->id ) {
322     unless ( $ArticleObj->CurrentUserHasRight('ShowArticle') ) {
323         $m->comp( "/Elements/Error",
324             Why => loc("No permission to view Article") );
325     }
326 }
327
328 </%INIT>
329
330
331 <%ARGS>
332 $id => undef
333 $Class => undef
334 </%ARGS>