root/trunk/CommunityEdit/lib/MT/App/CommunityEdit.pm @ 816

Revision 816, 9.6 kB (checked in by arvind, 17 months ago)

Capturing error on post_save for style directory

Line 
1package MT::App::CommunityEdit;
2
3use strict;
4use base qw( MT::App::Community );
5use MT::Util qw( encode_url );
6use CustomFields::Util qw( field_loop );
7
8sub edit_entry {
9        my $app      = shift;
10    my $entry_id = $app->param('id');
11    my $blog_id  = $app->param('blog_id') || 0;
12    my $user     = $app->_login_user_commenter();
13   
14    if(!$user) { # Redirect them to log in so that we don't have to use the JS
15        my $cfg = $app->config;
16        my $signin_link = $cfg->CGIPath;
17        $signin_link .= '/' unless $signin_link =~ m!/$!;
18        $signin_link .= $cfg->CommunityScript . '?__mode=login&blog_id='.$blog_id;
19        $signin_link .= '&return_to=' . encode_url($app->uri( mode => 'edit_entry', 
20                                                        args => { id => $entry_id, blog_id => $blog_id}));
21                                                       
22        return $app->redirect($signin_link);
23    }
24   
25    my $perms = $user->permissions($blog_id) if $blog_id && $user;
26   
27    require MT::Entry;
28    my $entry;
29   
30    ## Can Create Entry?
31    if ( !$entry_id ) {
32        return $app->errtrans("Permission denied.")
33          unless ( $perms && $perms->can_create_post );
34         
35        $entry = MT::Entry->new();
36    } else {
37        $entry = MT::Entry->load($entry_id); 
38    }
39       
40        ## Can Edit Entry?
41        if($entry_id && $entry) {
42            return $app->error( $app->translate("Permission denied.") )
43          unless $perms && $perms->can_edit_entry( $entry, $user );
44        }
45
46        my $tmpl = $app->load_global_tmpl('edit_entry')
47      or return $app->error("No edit entry template defined");
48       
49    my %param = ();
50    $param{blog_id}  = $blog_id;
51    $param{field_loop} =
52      field_loop( object_type => 'entry', ($entry) ? (object_id => $entry_id) : () );
53        $param{entry_id} = $entry_id;
54        $param{entry_status} = lc $entry->status_text;
55        $param{magic_token} = $app->current_magic;
56    $param{error} = $app->param('error') || $app->errstr;
57   
58    my $ctx = $tmpl->context;
59    $ctx->stash('author', $user);
60    $ctx->stash('blog_id', $blog_id) if $blog_id;
61    $ctx->stash('entry', $entry);
62    $tmpl->param( \%param );
63    $tmpl;
64}
65
66sub save_entry {
67    my $app = shift;
68    return
69        unless $app->request_method eq 'POST';
70
71    my $q   = $app->param;
72    my $blog_id = $q->param('blog_id');
73    return $app->errtrans("Invalid request") unless $blog_id;
74
75    my $blog = $app->model('blog.community')->load($blog_id);
76    return $app->errtrans("Invalid request") unless $blog;
77    $app->blog($blog);
78
79    my $user = $app->_login_user_commenter()
80      or return $app->errtrans("Login required");
81    $app->validate_magic;
82   
83    my $perms = $user->permissions($blog_id);
84    my $entry_id = $q->param('id');
85   
86    require MT::Entry;
87    my $entry;
88   
89    if ( !$entry_id ) {
90        return $app->errtrans("Permission denied.")
91          unless ( $perms && $perms->can_create_post );
92    } else {
93        $entry = MT::Entry->load($entry_id);
94    }
95   
96    if($entry) {
97        return $app->error( $app->translate("Permission denied.") )
98              unless $perms->can_edit_entry( $entry, $user );
99    } else {
100        $entry = MT::Entry->new;
101    }
102
103    require MT::Sanitize;
104    my $sanitize_spec = $blog->sanitize_spec
105      || $app->config->GlobalSanitizeSpec;
106    my $title = $q->param('title');
107    $title = $title ? MT::Sanitize->sanitize( $title, $sanitize_spec ) : $title;
108    my $text = $q->param('text');
109    $text = $text ? MT::Sanitize->sanitize( $text, $sanitize_spec ) : $text;
110    if ( $title && $text ) {
111        # for custom fields
112        $q->param('customfield_beacon', 1);
113        $q->param('_type', 'entry');
114
115        {
116            require CustomFields::App::CMS;
117            local $app->{component} = 'Commercial';
118            CustomFields::App::CMS::CMSSaveFilter_customfield_objs( $app, $app )
119                or return $app->error( $app->errstr() );
120        }
121
122        my $orig  = $entry->clone();
123        $entry->title($title);
124        $entry->text($text);
125        $entry->blog_id($blog_id);
126        $entry->author_id( $user->id );
127        my $cb = $user->text_format || $blog->convert_paras || '__default__';
128
129        # richtext is specifically disabled since we don't support
130        # rich text editing blog-side yet.
131        $cb = '__default__' if $cb eq 'richtext';
132
133        $entry->convert_breaks($cb);
134        $entry->allow_comments($blog->allow_comments_default);
135        $entry->allow_pings($blog->allow_pings_default);
136       
137        # If it's published, updates remain published = trust!
138        $entry->status(($entry->status == MT::Entry::RELEASE()) ? 
139                                                    MT::Entry::RELEASE() : MT::Entry::REVIEW());
140
141        # FIXME: This should be in MT::Util, not CMS.
142        require MT::App::CMS;
143        MT::App::CMS::_translate_naughty_words($app, $entry);
144
145        $app->run_callbacks( 'api_pre_save.entry', $app, $entry, $orig )
146          || return $app->error(
147            $app->translate(
148                "Saving [_1] failed: [_2]",
149                $entry->class_label, $app->errstr
150            )
151          );
152
153        $entry->save
154          or return $app->error(
155            $app->translate(
156                "Saving [_1] failed: [_2]", $entry->class_label,
157                $entry->errstr
158            )
159          );
160         
161        my $tags = $app->param('tags');
162        if (defined $tags) {
163            require MT::Tag;
164            my $tag_delim = chr( $app->user->entry_prefs->{tag_delim} );
165            my @tags = MT::Tag->split( $tag_delim, $tags );
166            $entry->set_tags(@tags);
167            $entry->save_tags;
168        } 
169
170        my $save_result = $app->run_callbacks( 'api_post_save.entry', $app, $entry, $orig );
171        if(!$save_result) {
172            my %param = ();
173            $param{error}       = $app->errstr;
174            $param{return_args} = $app->param('return_args');
175            return $app->forward("edit_entry", \%param );
176        }
177
178        $app->log(
179            {
180                message => $app->translate(
181                    "[_1] '[_2]' (ID:[_3]) added by user '[_4]'",
182                    $entry->class_label, $entry->title,
183                    $entry->id,          $user->name
184                ),
185                level    => MT::Log::INFO(),
186                class    => 'entry',
187                category => 'new',
188                metadata => $entry->id
189            }
190        );
191
192        # for custom field asset
193        foreach my $p ( $q->param() ) {
194            next if !$q->param($p);
195            if ( $p =~ /^file_customfield_(.*?)$/ ) {
196                if ( my $file_type = $q->param("type_customfield_$1") ) {
197                    my $asset =
198                      $app->_handle_upload( $p, require_type => $file_type );
199                    if ( !defined($asset) && $app->errstr ) {
200                        return $app->error( $app->errstr );
201                    }
202                    if ($asset) {
203                        $q->param( "customfield_$1",
204                            $asset->as_html( { include => 1 } ) );
205                    }
206                }
207            }
208        }
209
210        my $cid = $q->param('category') || $q->param('category_id');
211        my @add_cat;
212            my @param = $q->param();
213            foreach (@param) {
214                if (m/^add_category_id_(\d+)$/) {
215                    push @add_cat, $1;
216                }
217            }       
218       
219        require MT::Placement;
220            my $place = MT::Placement->load({ entry_id => $entry->id, is_primary => 1 });
221            if ($cid) {
222                unless ($place) {
223                    $place = MT::Placement->new;
224                    $place->entry_id($entry->id);
225                    $place->blog_id($entry->blog_id);
226                    $place->is_primary(1);
227                }
228                $place->category_id($cid);
229                $place->save;
230            } else {
231                if ((defined $cid) && ($place)) {
232                    $place->remove;
233                }
234            }
235            my @place = MT::Placement->load({ entry_id => $entry->id, is_primary => 0 });
236            for my $place (@place) {
237                $place->remove;
238            }       
239           
240            for my $cid (@add_cat) {
241                my $place = MT::Placement->new;
242                $place->entry_id($entry->id);
243                $place->blog_id($entry->blog_id);
244                $place->is_primary(0);
245                $place->category_id($cid);
246                $place->save
247                    or return $app->error($app->translate(
248                        "Saving placement failed: [_1]", $place->errstr));
249            }
250
251        {
252            local $app->{component} = 'Commercial';
253            CustomFields::App::CMS::CMSPostSave_customfield_objs( $app, $app, $entry )
254              or return $app->error( $app->errstr() );
255        }
256
257        if ( MT::Entry::RELEASE() == $entry->status ) {
258            $app->rebuild_entry( Entry => $entry->id, BuildDependencies => 1, BuildIndexes => 0 )
259              or return $app->error(
260                $app->translate( "Publish failed: [_1]", $app->errstr ) );
261
262            MT::Util::start_background_task(
263                sub {
264                    $app->rebuild_indexes( Blog => $blog )
265                      or return $app->errtrans( "Publish failed: [_1]", $app->errstr );
266                }
267            );
268        }
269
270        ## Send notification email in the background.
271        if ( my $registration = $app->config->CommenterRegistration ) {
272            if ( my $ids = $registration->{Notify} ) {
273                MT::Util::start_background_task(
274                    sub {
275                        $app->_send_entry_notification( $entry, $blog, $ids );
276                    }
277                );
278            }
279        }
280
281
282        return $app->redirect($app->uri( mode => 'edit_entry', 
283                                                        args => { id => $entry->id, blog_id => $blog_id}));
284    }
285
286    return $app->errtrans('Posting a new entry failed.');
287}
288
2891;
Note: See TracBrowser for help on using the browser.